Skip to content

Commit

Permalink
add maps support to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
robbles committed May 25, 2022
1 parent dc843e1 commit 08d557a
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 303 deletions.
563 changes: 302 additions & 261 deletions cli/commands/play.go

Large diffs are not rendered by default.

63 changes: 44 additions & 19 deletions cli/commands/play_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ import (
"github.com/stretchr/testify/require"
)

func buildDefaultGameState() *GameState {
gameState := &GameState{
Width: 11,
Height: 11,
Names: nil,
Timeout: 500,
Sequential: false,
GameType: "standard",
MapName: "standard",
ViewMap: false,
UseColor: false,
Seed: 1,
TurnDelay: 0,
TurnDuration: 0,
DebugRequests: false,
Output: "",
FoodSpawnChance: 15,
MinimumFood: 1,
HazardDamagePerTurn: 14,
ShrinkEveryNTurns: 25,
}

return gameState
}

func TestGetIndividualBoardStateForSnake(t *testing.T) {
s1 := rules.Snake{ID: "one", Body: []rules.Point{{X: 3, Y: 3}}}
s2 := rules.Snake{ID: "two", Body: []rules.Point{{X: 4, Y: 3}}}
Expand All @@ -34,12 +59,16 @@ func TestGetIndividualBoardStateForSnake(t *testing.T) {
Tail: "bolt",
Color: "#654321",
}
snakeStates := map[string]SnakeState{

gameState := buildDefaultGameState()
gameState.initialize()
gameState.gameID = "GAME_ID"
gameState.snakeStates = map[string]SnakeState{
s1State.ID: s1State,
s2State.ID: s2State,
}
initialiseGameConfig() // initialise default config
snakeRequest := getIndividualBoardStateForSnake(state, s1State, snakeStates, getRuleset(0, snakeStates))

snakeRequest := gameState.getRequestBodyForSnake(state, s1State)
requestBody := serialiseSnakeRequest(snakeRequest)

test.RequireJSONMatchesFixture(t, "testdata/snake_request_body.json", string(requestBody))
Expand Down Expand Up @@ -69,29 +98,25 @@ func TestSettingsRequestSerialization(t *testing.T) {
Tail: "bolt",
Color: "#654321",
}
snakeStates := map[string]SnakeState{s1State.ID: s1State, s2State.ID: s2State}

rsb := rules.NewRulesetBuilder().
WithParams(map[string]string{
// standard
rules.ParamFoodSpawnChance: "11",
rules.ParamMinimumFood: "7",
rules.ParamHazardDamagePerTurn: "19",
rules.ParamHazardMap: "hz_spiral",
rules.ParamHazardMapAuthor: "altersaddle",
// royale
rules.ParamShrinkEveryNTurns: "17",
})

for _, gt := range []string{
rules.GameTypeStandard, rules.GameTypeRoyale, rules.GameTypeSolo,
rules.GameTypeWrapped, rules.GameTypeConstrictor,
} {
t.Run(gt, func(t *testing.T) {
// apply game type
ruleset := rsb.WithParams(map[string]string{rules.ParamGameType: gt}).Ruleset()
gameState := buildDefaultGameState()

gameState.FoodSpawnChance = 11
gameState.MinimumFood = 7
gameState.HazardDamagePerTurn = 19
gameState.ShrinkEveryNTurns = 17
gameState.GameType = gt

gameState.initialize()
gameState.gameID = "GAME_ID"
gameState.snakeStates = map[string]SnakeState{s1State.ID: s1State, s2State.ID: s2State}

snakeRequest := getIndividualBoardStateForSnake(state, s1State, snakeStates, ruleset)
snakeRequest := gameState.getRequestBodyForSnake(state, s1State)
requestBody := serialiseSnakeRequest(snakeRequest)
t.Log(string(requestBody))

Expand Down
5 changes: 4 additions & 1 deletion cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package commands

import (
"fmt"
"github.com/spf13/cobra"
"os"

"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
Expand All @@ -18,6 +19,8 @@ var rootCmd = &cobra.Command{
}

func Execute() {
rootCmd.AddCommand(NewPlayCommand())

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions cli/commands/testdata/snake_request_body.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"game": {
"id": "",
"id": "GAME_ID",
"ruleset": {
"name": "standard",
"version": "cli",
Expand All @@ -21,7 +21,7 @@
}
}
},
"map": "",
"map": "standard",
"timeout": 500,
"source": ""
},
Expand Down
8 changes: 4 additions & 4 deletions cli/commands/testdata/snake_request_body_constrictor.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"game": {
"id": "",
"id": "GAME_ID",
"ruleset": {
"name": "constrictor",
"version": "cli",
"settings": {
"foodSpawnChance": 11,
"minimumFood": 7,
"hazardDamagePerTurn": 19,
"hazardMap": "hz_spiral",
"hazardMapAuthor": "altersaddle",
"hazardMap": "",
"hazardMapAuthor": "",
"royale": {
"shrinkEveryNTurns": 17
},
Expand All @@ -21,7 +21,7 @@
}
}
},
"map": "",
"map": "standard",
"timeout": 500,
"source": ""
},
Expand Down
8 changes: 4 additions & 4 deletions cli/commands/testdata/snake_request_body_royale.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"game": {
"id": "",
"id": "GAME_ID",
"ruleset": {
"name": "royale",
"version": "cli",
"settings": {
"foodSpawnChance": 11,
"minimumFood": 7,
"hazardDamagePerTurn": 19,
"hazardMap": "hz_spiral",
"hazardMapAuthor": "altersaddle",
"hazardMap": "",
"hazardMapAuthor": "",
"royale": {
"shrinkEveryNTurns": 17
},
Expand All @@ -21,7 +21,7 @@
}
}
},
"map": "",
"map": "standard",
"timeout": 500,
"source": ""
},
Expand Down
8 changes: 4 additions & 4 deletions cli/commands/testdata/snake_request_body_solo.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"game": {
"id": "",
"id": "GAME_ID",
"ruleset": {
"name": "solo",
"version": "cli",
"settings": {
"foodSpawnChance": 11,
"minimumFood": 7,
"hazardDamagePerTurn": 19,
"hazardMap": "hz_spiral",
"hazardMapAuthor": "altersaddle",
"hazardMap": "",
"hazardMapAuthor": "",
"royale": {
"shrinkEveryNTurns": 17
},
Expand All @@ -21,7 +21,7 @@
}
}
},
"map": "",
"map": "standard",
"timeout": 500,
"source": ""
},
Expand Down
8 changes: 4 additions & 4 deletions cli/commands/testdata/snake_request_body_standard.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"game": {
"id": "",
"id": "GAME_ID",
"ruleset": {
"name": "standard",
"version": "cli",
"settings": {
"foodSpawnChance": 11,
"minimumFood": 7,
"hazardDamagePerTurn": 19,
"hazardMap": "hz_spiral",
"hazardMapAuthor": "altersaddle",
"hazardMap": "",
"hazardMapAuthor": "",
"royale": {
"shrinkEveryNTurns": 17
},
Expand All @@ -21,7 +21,7 @@
}
}
},
"map": "",
"map": "standard",
"timeout": 500,
"source": ""
},
Expand Down
8 changes: 4 additions & 4 deletions cli/commands/testdata/snake_request_body_wrapped.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"game": {
"id": "",
"id": "GAME_ID",
"ruleset": {
"name": "wrapped",
"version": "cli",
"settings": {
"foodSpawnChance": 11,
"minimumFood": 7,
"hazardDamagePerTurn": 19,
"hazardMap": "hz_spiral",
"hazardMapAuthor": "altersaddle",
"hazardMap": "",
"hazardMapAuthor": "",
"royale": {
"shrinkEveryNTurns": 17
},
Expand All @@ -21,7 +21,7 @@
}
}
},
"map": "",
"map": "standard",
"timeout": 500,
"source": ""
},
Expand Down

0 comments on commit 08d557a

Please sign in to comment.