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() { 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) + } +}