diff --git a/server/src/game/changeGameState.go b/server/src/game/changeGameState.go new file mode 100644 index 0000000..b793f9b --- /dev/null +++ b/server/src/game/changeGameState.go @@ -0,0 +1,35 @@ +package game + +import ( + "fmt" +) + +func (gm *Game) changeGameState(expectedState, newState, newPhase string) error { + gm.mu.Lock() + defer gm.mu.Unlock() + + if gm.state != expectedState { + return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState) + } + gm.state = newState + gm.phase = newPhase + gm.eng.Update() + + return nil +} + +func (gm *Game) changeGamePhase(expectedState, expectedPhase, newPhase string) error { + gm.mu.Lock() + defer gm.mu.Unlock() + + if gm.state != expectedState { + return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState) + } + if gm.phase != expectedPhase { + return fmt.Errorf("game phase \"%s\" != expected phase \"%s\"", gm.phase, expectedPhase) + } + gm.phase = newPhase + gm.eng.Update() + + return nil +} diff --git a/server/src/game/populateSyncDataCb.go b/server/src/game/populateSyncDataCb.go index 044b632..1bcfda8 100644 --- a/server/src/game/populateSyncDataCb.go +++ b/server/src/game/populateSyncDataCb.go @@ -15,6 +15,7 @@ func (gm *Game) populateGameInfo() syncdata.GameInfo { return syncdata.GameInfo{ GameId: gm.id, State: gm.state, + Phase: gm.phase, Players: gm.getPlayers(), } } diff --git a/server/src/game/startGame.go b/server/src/game/startGame.go index 8a38e03..b71f56c 100644 --- a/server/src/game/startGame.go +++ b/server/src/game/startGame.go @@ -1,9 +1,41 @@ package game -func (gm *Game) StartGame() { - gm.mu.Lock() - defer gm.mu.Unlock() +import ( + "fmt" + "time" +) - gm.state = STATE_PLAY - gm.eng.Update() +func (gm *Game) StartGame() { + go gm.startGameSub() +} + +func (gm *Game) startGameSub() { + phases := []string{ + "ready", + "3", + "2", + "1", + "go", + } + + err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0]) + if err != nil { + fmt.Println(err) + return + } + + for i := 1; i < len(phases); i++ { + time.Sleep(2 * time.Second) + err = gm.changeGamePhase(STATE_READY_SET, phases[i-1], phases[i]) + if err != nil { + fmt.Println(err) + return + } + } + time.Sleep(3 * time.Second) + err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, "") + if err != nil { + fmt.Println(err) + return + } } diff --git a/server/src/game/struct.go b/server/src/game/struct.go index cdf1f00..e263c73 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -9,7 +9,7 @@ import ( const ( STATE_IDLE = "idle" STATE_ENTER_STATEMENTS = "enter-quotes" - STATE_READY_SET_PLAY = "ready-set-play" + STATE_READY_SET = "ready-set" STATE_PLAY = "play" ) diff --git a/server/src/syncdata/syncdata.go b/server/src/syncdata/syncdata.go index d19fbf8..72a36fe 100644 --- a/server/src/syncdata/syncdata.go +++ b/server/src/syncdata/syncdata.go @@ -9,6 +9,7 @@ type PlayerInfo struct { type GameInfo struct { GameId string `json:"id"` State string `json:"state"` + Phase string `json:"phase"` Players []PlayerInfo `json:"players"` }