added TurnDuration option to cli (#62)

This commit is contained in:
BelowAverageDeveloper 2022-03-28 13:22:51 -04:00 committed by GitHub
parent 447cbba8c0
commit 762c94caf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View file

@ -33,6 +33,7 @@ Usage:
Flags: Flags:
-d, --delay int32 Turn Delay in Milliseconds (default 0) -d, --delay int32 Turn Delay in Milliseconds (default 0)
-D, --duration int32 Minimum Turn Duration in Milliseconds (default 0)
-g, --gametype string Type of Game Rules (default "standard") -g, --gametype string Type of Game Rules (default "standard")
-H, --height int32 Height of Board (default 11) -H, --height int32 Height of Board (default 11)
-h, --help help for play -h, --help help for play

View file

@ -43,6 +43,7 @@ var Names []string
var URLs []string var URLs []string
var Squads []string var Squads []string
var Timeout int32 var Timeout int32
var TurnDuration int32
var Sequential bool var Sequential bool
var GameType string var GameType string
var ViewMap bool var ViewMap bool
@ -90,6 +91,7 @@ func init() {
playCmd.Flags().BoolVarP(&UseColor, "color", "c", false, "Use color to draw the map") playCmd.Flags().BoolVarP(&UseColor, "color", "c", false, "Use color to draw the map")
playCmd.Flags().Int64VarP(&Seed, "seed", "r", time.Now().UTC().UnixNano(), "Random Seed") playCmd.Flags().Int64VarP(&Seed, "seed", "r", time.Now().UTC().UnixNano(), "Random Seed")
playCmd.Flags().Int32VarP(&TurnDelay, "delay", "d", 0, "Turn Delay in Milliseconds") playCmd.Flags().Int32VarP(&TurnDelay, "delay", "d", 0, "Turn Delay in Milliseconds")
playCmd.Flags().Int32VarP(&TurnDuration, "duration", "D", 0, "Minimum Turn Duration in Milliseconds")
playCmd.Flags().BoolVar(&DebugRequests, "debug-requests", false, "Log body of all requests sent") playCmd.Flags().BoolVar(&DebugRequests, "debug-requests", false, "Log body of all requests sent")
playCmd.Flags().StringVarP(&Output, "output", "o", "", "File path to output game state to. Existing files will be overwritten") playCmd.Flags().StringVarP(&Output, "output", "o", "", "File path to output game state to. Existing files will be overwritten")
@ -111,6 +113,7 @@ var run = func(cmd *cobra.Command, args []string) {
GameId = uuid.New().String() GameId = uuid.New().String()
Turn = 0 Turn = 0
var endTime time.Time
snakeStates := buildSnakesFromOptions() snakeStates := buildSnakesFromOptions()
ruleset := getRuleset(Seed, snakeStates) ruleset := getRuleset(Seed, snakeStates)
@ -125,6 +128,10 @@ var run = func(cmd *cobra.Command, args []string) {
} }
for v := false; !v; v, _ = ruleset.IsGameOver(state) { for v := false; !v; v, _ = ruleset.IsGameOver(state) {
if TurnDuration > 0 {
endTime = time.Now().Add(time.Duration(TurnDuration) * time.Millisecond)
}
Turn++ Turn++
state = createNextBoardState(ruleset, state, snakeStates, Turn) state = createNextBoardState(ruleset, state, snakeStates, Turn)
@ -138,6 +145,10 @@ var run = func(cmd *cobra.Command, args []string) {
time.Sleep(time.Duration(TurnDelay) * time.Millisecond) time.Sleep(time.Duration(TurnDelay) * time.Millisecond)
} }
if TurnDuration > 0 {
time.Sleep(time.Until(endTime))
}
if exportGame { if exportGame {
// The output file was designed in a way so that (nearly) every entry is equivalent to a valid API request. // The output file was designed in a way so that (nearly) every entry is equivalent to a valid API request.
// This is meant to help unlock further development of tools such as replaying a saved game by simply copying each line and sending it as a POST request. // This is meant to help unlock further development of tools such as replaying a saved game by simply copying each line and sending it as a POST request.