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

@ -43,6 +43,7 @@ var Names []string
var URLs []string
var Squads []string
var Timeout int32
var TurnDuration int32
var Sequential bool
var GameType string
var ViewMap bool
@ -90,6 +91,7 @@ func init() {
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().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().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()
Turn = 0
var endTime time.Time
snakeStates := buildSnakesFromOptions()
ruleset := getRuleset(Seed, snakeStates)
@ -125,6 +128,10 @@ var run = func(cmd *cobra.Command, args []string) {
}
for v := false; !v; v, _ = ruleset.IsGameOver(state) {
if TurnDuration > 0 {
endTime = time.Now().Add(time.Duration(TurnDuration) * time.Millisecond)
}
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)
}
if TurnDuration > 0 {
time.Sleep(time.Until(endTime))
}
if exportGame {
// 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.