DEV-1761: New rules API (#118)

* DEV-1761: Clean up Ruleset interface (#115)

* remove legacy ruleset types and simplify ruleset interface

* remove unnecessary settings argument from Ruleset interface

* decouple rules.Settings from client API and store settings as strings

* DEV 1761: Add new BoardState and Point fields (#117)

* add Point.TTL, Point.Value, GameState and PointState to BoardState

* allow maps to access BoardState.GameState,PointState

* add PreUpdateBoard and refactor snail_mode with it

* fix bug where an extra turn was printed to the console

* fix formatting

* fix lint errors

Co-authored-by: JonathanArns <jonathan.arns@googlemail.com>
This commit is contained in:
Rob O'Dwyer 2022-10-28 16:49:49 -07:00 committed by GitHub
parent 639362ef46
commit 82e1999126
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 1349 additions and 1610 deletions

View file

@ -9,7 +9,7 @@ func exampleSnakeRequest() SnakeRequest {
Ruleset: Ruleset{
Name: "test-ruleset-name",
Version: "cli",
Settings: exampleRulesetSettings,
Settings: ConvertRulesetSettings(exampleRulesetSettings),
},
Timeout: 33,
Source: "league",
@ -75,21 +75,9 @@ func exampleSnakeRequest() SnakeRequest {
}
}
var exampleRulesetSettings = rules.Settings{
FoodSpawnChance: 10,
MinimumFood: 20,
HazardDamagePerTurn: 30,
HazardMap: "hz_spiral",
HazardMapAuthor: "altersaddle",
RoyaleSettings: rules.RoyaleSettings{
ShrinkEveryNTurns: 40,
},
SquadSettings: rules.SquadSettings{
AllowBodyCollisions: true,
SharedElimination: true,
SharedHealth: true,
SharedLength: true,
},
}
var exampleRulesetSettings = rules.NewSettings(map[string]string{
rules.ParamFoodSpawnChance: "10",
rules.ParamMinimumFood: "20",
rules.ParamHazardDamagePerTurn: "30",
rules.ParamShrinkEveryNTurns: "40",
})

View file

@ -49,19 +49,47 @@ type Customizations struct {
}
type Ruleset struct {
Name string `json:"name"`
Version string `json:"version"`
Settings rules.Settings `json:"settings"`
Name string `json:"name"`
Version string `json:"version"`
Settings RulesetSettings `json:"settings"`
}
// RulesetSettings is deprecated: use rules.Settings instead
type RulesetSettings rules.Settings
// RulesetSettings contains a static collection of a few settings that are exposed through the API.
type RulesetSettings struct {
FoodSpawnChance int `json:"foodSpawnChance"`
MinimumFood int `json:"minimumFood"`
HazardDamagePerTurn int `json:"hazardDamagePerTurn"`
HazardMap string `json:"hazardMap"` // Deprecated, replaced by Game.Map
HazardMapAuthor string `json:"hazardMapAuthor"` // Deprecated, no planned replacement
RoyaleSettings RoyaleSettings `json:"royale"`
SquadSettings SquadSettings `json:"squad"` // Deprecated, provided with default fields for API compatibility
}
// RoyaleSettings is deprecated: use rules.RoyaleSettings instead
type RoyaleSettings rules.RoyaleSettings
// RoyaleSettings contains settings that are specific to the "royale" game mode
type RoyaleSettings struct {
ShrinkEveryNTurns int `json:"shrinkEveryNTurns"`
}
// SquadSettings is deprecated: use rules.SquadSettings instead
type SquadSettings rules.SquadSettings
// SquadSettings contains settings that are specific to the "squad" game mode
type SquadSettings struct {
AllowBodyCollisions bool `json:"allowBodyCollisions"`
SharedElimination bool `json:"sharedElimination"`
SharedHealth bool `json:"sharedHealth"`
SharedLength bool `json:"sharedLength"`
}
// Converts a rules.Settings (which can contain arbitrary settings) into the static RulesetSettings used in the client API.
func ConvertRulesetSettings(settings rules.Settings) RulesetSettings {
return RulesetSettings{
FoodSpawnChance: settings.Int(rules.ParamFoodSpawnChance, 0),
MinimumFood: settings.Int(rules.ParamMinimumFood, 0),
HazardDamagePerTurn: settings.Int(rules.ParamHazardDamagePerTurn, 0),
RoyaleSettings: RoyaleSettings{
ShrinkEveryNTurns: settings.Int(rules.ParamShrinkEveryNTurns, 0),
},
SquadSettings: SquadSettings{},
}
}
// Coord represents a point on the board
type Coord struct {

View file

@ -4,7 +4,6 @@ import (
"encoding/json"
"testing"
"github.com/BattlesnakeOfficial/rules"
"github.com/BattlesnakeOfficial/rules/test"
"github.com/stretchr/testify/require"
)
@ -19,7 +18,7 @@ func TestBuildSnakeRequestJSON(t *testing.T) {
func TestBuildSnakeRequestJSONEmptyRulesetSettings(t *testing.T) {
snakeRequest := exampleSnakeRequest()
snakeRequest.Game.Ruleset.Settings = rules.Settings{}
snakeRequest.Game.Ruleset.Settings = RulesetSettings{}
data, err := json.MarshalIndent(snakeRequest, "", " ")
require.NoError(t, err)

View file

@ -8,16 +8,16 @@
"foodSpawnChance": 10,
"minimumFood": 20,
"hazardDamagePerTurn": 30,
"hazardMap": "hz_spiral",
"hazardMapAuthor": "altersaddle",
"hazardMap": "",
"hazardMapAuthor": "",
"royale": {
"shrinkEveryNTurns": 40
},
"squad": {
"allowBodyCollisions": true,
"sharedElimination": true,
"sharedHealth": true,
"sharedLength": true
"allowBodyCollisions": false,
"sharedElimination": false,
"sharedHealth": false,
"sharedLength": false
}
}
},