DEV 1404: Support streaming CLI games to the browser board (#88)

* add minimal support for serving a game to the board UI

* refactor into new board package

* support reporting errors and author names to board

* support passing an alternate board URL

* avoid using IPv6 for local URL

* use rules.Point instead of a custom Point type for board package

* use zero for generic communication error code in cli

* rename createGameEvent to buildFrameEvent

* tests for conversion from boardState/snakeState to game frame
This commit is contained in:
Rob O'Dwyer 2022-06-28 16:17:44 -07:00 committed by GitHub
parent f6c3ed0791
commit a451cda9c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 504 additions and 22 deletions

72
board/api.go Normal file
View file

@ -0,0 +1,72 @@
package board
import (
"github.com/BattlesnakeOfficial/rules"
)
// Types used to implement the JSON API expected by the board client.
// JSON structure returned by the game status endpoint.
type Game struct {
ID string `json:"ID"`
Status string `json:"Status"`
Width int `json:"Width"`
Height int `json:"Height"`
Ruleset map[string]string `json:"Ruleset"`
SnakeTimeout int `json:"SnakeTimeout"`
Source string `json:"Source"`
RulesetName string `json:"RulesetName"`
RulesStages []string `json:"RulesStages"`
Map string `json:"Map"`
}
// The websocket stream has support for returning different types of events, along with a "type" attribute.
type GameEventType string
const (
EVENT_TYPE_FRAME GameEventType = "frame"
EVENT_TYPE_GAME_END GameEventType = "game_end"
)
// Top-level JSON structure sent in each websocket frame.
type GameEvent struct {
EventType GameEventType `json:"Type"`
Data interface{} `json:"Data"`
}
// Represents a single turn in the game.
type GameFrame struct {
Turn int `json:"Turn"`
Snakes []Snake `json:"Snakes"`
Food []rules.Point `json:"Food"`
Hazards []rules.Point `json:"Hazards"`
}
type GameEnd struct {
Game Game `json:"game"`
}
type Snake struct {
ID string `json:"ID"`
Name string `json:"Name"`
Body []rules.Point `json:"Body"`
Health int `json:"Health"`
Death *Death `json:"Death"`
Color string `json:"Color"`
HeadType string `json:"HeadType"`
TailType string `json:"TailType"`
Latency string `json:"Latency"`
Shout string `json:"Shout"`
Squad string `json:"Squad"`
Author string `json:"Author"`
StatusCode int `json:"StatusCode"`
Error string `json:"Error"`
IsBot bool `json:"IsBot"`
IsEnvironment bool `json:"IsEnvironment"`
}
type Death struct {
Cause string `json:"Cause"`
Turn int `json:"Turn"`
EliminatedBy string `json:"EliminatedBy"`
}