Skip to content

Commit

Permalink
Merge branch 'master' of github.com:intelygenz/codeconz-lighthouses-e…
Browse files Browse the repository at this point in the history
…ngine
  • Loading branch information
onizucraft committed Nov 14, 2024
2 parents f46e313 + cd57001 commit d31655b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
7 changes: 0 additions & 7 deletions cfg.yaml

This file was deleted.

16 changes: 7 additions & 9 deletions cmd/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bootstrap
import (
"fmt"
"net"
"os"
"time"

"github.com/jonasdacruz/lighthouses_aicontest/internal/engine/game"
Expand Down Expand Up @@ -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() {
Expand Down
61 changes: 37 additions & 24 deletions internal/engine/game/startgame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
}

0 comments on commit d31655b

Please sign in to comment.