2021-11-25 14:07:56 -08:00
|
|
|
package client
|
|
|
|
|
|
|
|
|
|
import "github.com/BattlesnakeOfficial/rules"
|
|
|
|
|
|
|
|
|
|
// The top-level message sent in /start, /move, and /end requests
|
|
|
|
|
type SnakeRequest struct {
|
|
|
|
|
Game Game `json:"game"`
|
2022-05-25 11:17:41 -07:00
|
|
|
Turn int `json:"turn"`
|
2021-11-25 14:07:56 -08:00
|
|
|
Board Board `json:"board"`
|
|
|
|
|
You Snake `json:"you"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Game represents the current game state
|
|
|
|
|
type Game struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
Ruleset Ruleset `json:"ruleset"`
|
2022-05-19 12:43:03 -07:00
|
|
|
Map string `json:"map"`
|
2022-05-25 11:17:41 -07:00
|
|
|
Timeout int `json:"timeout"`
|
2021-11-25 14:07:56 -08:00
|
|
|
Source string `json:"source"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Board provides information about the game board
|
|
|
|
|
type Board struct {
|
2022-05-25 11:17:41 -07:00
|
|
|
Height int `json:"height"`
|
|
|
|
|
Width int `json:"width"`
|
2021-11-25 14:07:56 -08:00
|
|
|
Snakes []Snake `json:"snakes"`
|
|
|
|
|
Food []Coord `json:"food"`
|
|
|
|
|
Hazards []Coord `json:"hazards"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Snake represents information about a snake in the game
|
|
|
|
|
type Snake struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Latency string `json:"latency"`
|
2022-05-25 11:17:41 -07:00
|
|
|
Health int `json:"health"`
|
2021-11-25 14:07:56 -08:00
|
|
|
Body []Coord `json:"body"`
|
|
|
|
|
Head Coord `json:"head"`
|
2022-05-25 11:17:41 -07:00
|
|
|
Length int `json:"length"`
|
2021-11-25 14:07:56 -08:00
|
|
|
Shout string `json:"shout"`
|
|
|
|
|
Squad string `json:"squad"`
|
|
|
|
|
Customizations Customizations `json:"customizations"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Customizations struct {
|
|
|
|
|
Color string `json:"color"`
|
|
|
|
|
Head string `json:"head"`
|
|
|
|
|
Tail string `json:"tail"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Ruleset struct {
|
2022-10-28 16:49:49 -07:00
|
|
|
Name string `json:"name"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
Settings RulesetSettings `json:"settings"`
|
2021-11-25 14:07:56 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-28 16:49:49 -07:00
|
|
|
// 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 contains settings that are specific to the "royale" game mode
|
|
|
|
|
type RoyaleSettings struct {
|
|
|
|
|
ShrinkEveryNTurns int `json:"shrinkEveryNTurns"`
|
|
|
|
|
}
|
2021-11-25 14:07:56 -08:00
|
|
|
|
2022-10-28 16:49:49 -07:00
|
|
|
// 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"`
|
|
|
|
|
}
|
2021-11-25 14:07:56 -08:00
|
|
|
|
2022-10-28 16:49:49 -07:00
|
|
|
// 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{},
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-25 14:07:56 -08:00
|
|
|
|
|
|
|
|
// Coord represents a point on the board
|
|
|
|
|
type Coord struct {
|
2022-05-25 11:17:41 -07:00
|
|
|
X int `json:"x"`
|
|
|
|
|
Y int `json:"y"`
|
2021-11-25 14:07:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The expected format of the response body from a /move request
|
|
|
|
|
type MoveResponse struct {
|
|
|
|
|
Move string `json:"move"`
|
|
|
|
|
Shout string `json:"shout"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The expected format of the response body from a GET request to a Battlesnake's index URL
|
|
|
|
|
type SnakeMetadataResponse struct {
|
|
|
|
|
APIVersion string `json:"apiversion,omitempty"`
|
|
|
|
|
Author string `json:"author,omitempty"`
|
|
|
|
|
Color string `json:"color,omitempty"`
|
|
|
|
|
Head string `json:"head,omitempty"`
|
|
|
|
|
Tail string `json:"tail,omitempty"`
|
|
|
|
|
Version string `json:"version,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CoordFromPoint(pt rules.Point) Coord {
|
|
|
|
|
return Coord{X: pt.X, Y: pt.Y}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CoordFromPointArray(ptArray []rules.Point) []Coord {
|
|
|
|
|
a := make([]Coord, 0)
|
|
|
|
|
for _, pt := range ptArray {
|
|
|
|
|
a = append(a, CoordFromPoint(pt))
|
|
|
|
|
}
|
|
|
|
|
return a
|
|
|
|
|
}
|