Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play: Add option to write NDJSON "replay file". #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions board/replay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package board

import (
"os"
"encoding/json"
"fmt"
"io"

log "github.com/spf13/jwalterweatherman"
)

type ReplayFile struct {
handle io.WriteCloser
}

func NewReplayFile(path string) *ReplayFile {
fd, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.ERROR.Fatalf("Failed to open replay file: %w", err)

Check failure on line 19 in board/replay.go

View workflow job for this annotation

GitHub Actions / Lint (golangci-lint)

printf: (*log.Logger).Fatalf does not support error-wrapping directive %w (govet)
}

return &ReplayFile{
handle: fd,
}
}

func (replay *ReplayFile) WriteGameInfo(game Game) {
// TODO(schoon): Provide a clear delimiter between game info and frames.
// Additionally, we probably want to ensure they're ordered. Take in the
// `Game` in `NewReplayFile` (much like `NewBoardServer` and write
// game info as front matter?
jsonStr, err := json.Marshal(struct {
Game Game
}{game})
if err != nil {
log.ERROR.Printf("Unable to serialize event for replay file: %v", err)
}

_, err = io.WriteString(replay.handle, fmt.Sprintf("%s\n", jsonStr))
if err != nil {
log.WARN.Printf("Unable to write to replay file: %v", err)
}
}

func (replay *ReplayFile) WriteEvent(event GameEvent) {
jsonStr, err := json.Marshal(event)
if err != nil {
log.ERROR.Printf("Unable to serialize event for replay file: %v", err)
}

_, err = io.WriteString(replay.handle, fmt.Sprintf("%s\n", jsonStr))
if err != nil {
log.WARN.Printf("Unable to write to replay file: %v", err)
}
}
23 changes: 23 additions & 0 deletions cli/commands/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type GameState struct {
Seed int64
TurnDelay int
OutputPath string
ReplayFilePath string
ViewInBrowser bool
BoardURL string
FoodSpawnChance int
Expand All @@ -74,6 +75,7 @@ type GameState struct {
ruleset rules.Ruleset
gameMap maps.GameMap
outputFile io.WriteCloser
replayFile *board.ReplayFile
idGenerator func(int) string
}

Expand Down Expand Up @@ -108,6 +110,7 @@ func NewPlayCommand() *cobra.Command {
playCmd.Flags().IntVarP(&gameState.TurnDelay, "delay", "d", 0, "Turn Delay in Milliseconds")
playCmd.Flags().IntVarP(&gameState.TurnDuration, "duration", "D", 0, "Minimum Turn Duration in Milliseconds")
playCmd.Flags().StringVarP(&gameState.OutputPath, "output", "o", "", "File path to output game state to. Existing files will be overwritten")
playCmd.Flags().StringVar(&gameState.ReplayFilePath, "replay", "", "File path to write game frames to for replaying later. Existing files will be overwritten")
playCmd.Flags().BoolVar(&gameState.ViewInBrowser, "browser", false, "View the game in the browser using the Battlesnake game board")
playCmd.Flags().StringVar(&gameState.BoardURL, "board-url", "https://board.battlesnake.com", "Base URL for the game board when using --browser")

Expand Down Expand Up @@ -170,6 +173,10 @@ func (gameState *GameState) Initialize() error {
gameState.outputFile = f
}

if gameState.ReplayFilePath != "" {
gameState.replayFile = board.NewReplayFile(gameState.ReplayFilePath);
}

return nil
}

Expand Down Expand Up @@ -235,6 +242,11 @@ func (gameState *GameState) Run() error {
boardServer.SendEvent(gameState.buildFrameEvent(boardState))
}

if gameState.replayFile != nil {
gameState.replayFile.WriteGameInfo(boardGame)
gameState.replayFile.WriteEvent(gameState.buildFrameEvent(boardState))
}

log.INFO.Printf("Ruleset: %v, Seed: %v", gameState.GameType, gameState.Seed)

if gameState.ViewMap {
Expand Down Expand Up @@ -293,6 +305,10 @@ func (gameState *GameState) Run() error {
boardServer.SendEvent(gameState.buildFrameEvent(boardState))
}

if gameState.replayFile != nil {
gameState.replayFile.WriteEvent(gameState.buildFrameEvent(boardState))
}

if exportGame {
for _, snakeState := range gameState.snakeStates {
snakeRequest := gameState.getRequestBodyForSnake(boardState, snakeState)
Expand Down Expand Up @@ -334,6 +350,13 @@ func (gameState *GameState) Run() error {
})
}

if gameState.replayFile != nil {
gameState.replayFile.WriteEvent(board.GameEvent{
EventType: board.EVENT_TYPE_GAME_END,
Data: boardGame,
})
}

if exportGame {
lines, err := gameExporter.FlushToFile(gameState.outputFile)
if err != nil {
Expand Down
Loading