send game state and phase to client

This commit is contained in:
Settel 2021-08-13 22:39:37 +02:00
parent 0eb9bf3602
commit 281baa5bd2
5 changed files with 75 additions and 6 deletions

View File

@ -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
}

View File

@ -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(),
}
}

View File

@ -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
}
}

View File

@ -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"
)

View File

@ -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"`
}