From b92385b98095002aae89ea1140390c8d14fe8558 Mon Sep 17 00:00:00 2001 From: Pablo Nazar <73798198+pablon@users.noreply.github.com> Date: Thu, 14 Nov 2024 09:42:23 +0100 Subject: [PATCH 1/3] fix(start-game): fix output dir permissions --- start-game.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/start-game.sh b/start-game.sh index 38cedb9..32224c1 100755 --- a/start-game.sh +++ b/start-game.sh @@ -138,8 +138,8 @@ function load_config() { # check if turns is defined if [[ ! "${turns}" ]]; then - _error "Game turns not defined" - fi + _error "Game turns not defined" + fi export THIS_GAME_TURNS="${turns}" _info "📝 Loaded configfile: ${YELLOW}${1}" @@ -156,6 +156,8 @@ function load_config() { for dir in log output; do [ -d "${REPO_DIR}/${dir}" ] || mkdir -p "${REPO_DIR}/${dir}" done + # fix output dir permissions *required* + chmod 0777 "${OUTPUT_DIR}" export GAME_CONFIG_FILE="${1}" } @@ -263,7 +265,7 @@ function create_game_log() { # ::: # ::: Game Round: ${GAME_TIMESTAMP} # ::: Config file: ${GAME_CONFIG_FILE} -# ::: Players: $(echo "${bots[@]}" | xargs) +# ::: Players: $(echo "${BOT_LIST[*]}" | xargs) # ::: Map: $(echo "${map}") # ::: From 35166e826c1fe9d3d5d55ab3524ee2fb6d7a6e97 Mon Sep 17 00:00:00 2001 From: dovixman Date: Thu, 14 Nov 2024 10:13:44 +0100 Subject: [PATCH 2/3] remove config file and add default values in code --- cfg.yaml | 7 ------- cmd/bootstrap/bootstrap.go | 16 +++++++--------- 2 files changed, 7 insertions(+), 16 deletions(-) delete mode 100644 cfg.yaml diff --git a/cfg.yaml b/cfg.yaml deleted file mode 100644 index 6a0ea75..0000000 --- a/cfg.yaml +++ /dev/null @@ -1,7 +0,0 @@ -listen_address: ":50051" -join_timeout: 5s -turn_request_timeout: 100ms -turns: 500 -board_path: "./maps/island_simple.txt" -verbosity: true -time_between_rounds: 0s diff --git a/cmd/bootstrap/bootstrap.go b/cmd/bootstrap/bootstrap.go index 838c3c1..6c1e1a3 100644 --- a/cmd/bootstrap/bootstrap.go +++ b/cmd/bootstrap/bootstrap.go @@ -3,7 +3,6 @@ package bootstrap import ( "fmt" "net" - "os" "time" "github.com/jonasdacruz/lighthouses_aicontest/internal/engine/game" @@ -69,14 +68,13 @@ func (b *Bootstrap) Run() { func (b *Bootstrap) initializeConfiguration() { viper.AutomaticEnv() - viper.AddConfigPath("./") - viper.SetConfigName("cfg") - viper.SetConfigType("yaml") - - err := viper.ReadInConfig() - if err != nil { - os.Exit(1) - } + viper.SetDefault("listen_address", ":50051") + viper.SetDefault("board_path", "./maps/island.txt") + viper.SetDefault("turns", 100) + viper.SetDefault("join_timeout", 5*time.Second) + viper.SetDefault("turn_request_timeout", 10*time.Millisecond) + viper.SetDefault("verbosity", true) + viper.SetDefault("time_between_rounds", 0*time.Second) fmt.Println("Loaded configuration:") for _, key := range viper.AllKeys() { From cd570012d6d174207b311e8fa74c98a181427afa Mon Sep 17 00:00:00 2001 From: dovixman Date: Thu, 14 Nov 2024 10:19:33 +0100 Subject: [PATCH 3/3] add method to recover from timeout panics --- internal/engine/game/startgame.go | 61 +++++++++++++++++++------------ 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/internal/engine/game/startgame.go b/internal/engine/game/startgame.go index 69a780a..92a0e06 100644 --- a/internal/engine/game/startgame.go +++ b/internal/engine/game/startgame.go @@ -46,30 +46,7 @@ func (e *Game) StartGame() { e.state.SetNewRound(roundId, round) for _, p := range e.players { - // send message to each Player with the info - na, err := p.RequestNewTurn(player.Turn{ - Position: p.Position, - Score: p.Score, - Energy: p.Energy, - View: e.gameMap.GetPlayerView(p), - Lighthouses: e.gameMap.GetLightHouses(), - }) - if err != nil { - // handle error - fmt.Printf("Requesting Turn to Player %d has error %v\n", p.ID, err) - // if by any reason the player does not respond, we skip the turn - continue - } - - err = e.execPlayerAction(p, na) - if err != nil { - fmt.Printf("Executing Player Action %d has error %v\n", p.ID, err) - } - fmt.Println("*************************************************") - - // generate turn state and set into game state - turn := state.NewTurn(p, e.gameMap.GetLightHouses()) - e.state.AddPlayerTurn(roundId, turn) + e.executeTurn(p, roundId) } e.CalcPlayersScores() @@ -88,3 +65,39 @@ func (e *Game) StartGame() { fmt.Printf("State to json could not be generated: %v\n", err) } } + +func (e *Game) executeTurn(p *player.Player, roundId int) { + // Control panics from timeouts + defer recoverFromPanic() + + // send message to each Player with the info + na, err := p.RequestNewTurn(player.Turn{ + Position: p.Position, + Score: p.Score, + Energy: p.Energy, + View: e.gameMap.GetPlayerView(p), + Lighthouses: e.gameMap.GetLightHouses(), + }) + if err != nil { + // handle error + fmt.Printf("Requesting Turn to Player %d has error %v\n", p.ID, err) + // if by any reason the player does not respond, we skip the turn + return + } + + err = e.execPlayerAction(p, na) + if err != nil { + fmt.Printf("Executing Player Action %d has error %v\n", p.ID, err) + } + fmt.Println("*************************************************") + + // generate turn state and set into game state + turn := state.NewTurn(p, e.gameMap.GetLightHouses()) + e.state.AddPlayerTurn(roundId, turn) +} + +func recoverFromPanic() { + if r := recover(); r != nil { + fmt.Println("Recovered from panic:", r) + } +}