diff --git a/server/src/game/changeGameState.go b/server/src/game/changeGameState.go index 264ad90..4fb4341 100644 --- a/server/src/game/changeGameState.go +++ b/server/src/game/changeGameState.go @@ -13,7 +13,6 @@ func (gm *Game) changeGameState(expectedState, newState, newPhase string) error } gm.state = newState gm.phase = newPhase - gm.eng.Update() return nil } @@ -29,7 +28,6 @@ func (gm *Game) changeGamePhase(expectedState, expectedPhase, newPhase string) e 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/game.go b/server/src/game/game.go index 932f5f2..d2fdc5a 100644 --- a/server/src/game/game.go +++ b/server/src/game/game.go @@ -40,6 +40,13 @@ func (gm *Game) GetId() string { return gm.id } +func (gm *Game) GetState() string { + gm.mu.Lock() + defer gm.mu.Unlock() + + return gm.state +} + func (gm *Game) GetName() string { gm.mu.Lock() defer gm.mu.Unlock() diff --git a/server/src/game/notifyClients.go b/server/src/game/notifyClients.go new file mode 100644 index 0000000..dac2db1 --- /dev/null +++ b/server/src/game/notifyClients.go @@ -0,0 +1,8 @@ +package game + +func (gm *Game) notifyClients() { + gm.mu.Lock() + defer gm.mu.Unlock() + + gm.eng.Update() +} diff --git a/server/src/game/runCountdown.go b/server/src/game/runCountdown.go new file mode 100644 index 0000000..0857ca2 --- /dev/null +++ b/server/src/game/runCountdown.go @@ -0,0 +1,43 @@ +package game + +import ( + "fmt" + "time" +) + +func (gm *Game) runCountdown() { + phases := []string{ + "Ready?", + "3", + "2", + "1", + "Go!", + "", + } + + err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0]) + if err != nil { + fmt.Println(err) + return + } + gm.notifyClients() + time.Sleep(2000 * time.Millisecond) + + for i := 1; i < len(phases); i++ { + err = gm.changeGamePhase(STATE_READY_SET, phases[i-1], phases[i]) + if err != nil { + fmt.Println(err) + return + } + gm.notifyClients() + time.Sleep(1500 * time.Millisecond) + } + + err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE) + if err != nil { + fmt.Println(err) + return + } + + gm.runRound() +} diff --git a/server/src/game/startGame.go b/server/src/game/startGame.go index 2ba8e0a..04f8d02 100644 --- a/server/src/game/startGame.go +++ b/server/src/game/startGame.go @@ -2,43 +2,27 @@ package game import ( "fmt" - "time" ) func (gm *Game) StartGame() { - go gm.startGameSub() + // go gm.runCountdown() + go gm.runRound() } -func (gm *Game) startGameSub() { - phases := []string{ - "5", - "4", - "3", - "2", - "1", - "Go!", +func (gm *Game) runRound() { + state := gm.GetState() + if state != STATE_IDLE && state != STATE_PLAY { + fmt.Println(fmt.Errorf("expected state \"IDLE\" | \"PLAY\" != \"%s\"", state)) + return } - err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0]) + err := gm.changeGameState(STATE_ANY, STATE_PLAY, PHASE_NONE) if err != nil { fmt.Println(err) return } - for i := 1; i < len(phases); i++ { - time.Sleep(1500 * time.Millisecond) - 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, PHASE_NONE) - if err != nil { - fmt.Println(err) - return - } + gm.notifyClients() } func (gm *Game) ResetGame() {