send game state and phase to client
This commit is contained in:
parent
0eb9bf3602
commit
281baa5bd2
35
server/src/game/changeGameState.go
Normal file
35
server/src/game/changeGameState.go
Normal 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
|
||||||
|
}
|
@ -15,6 +15,7 @@ func (gm *Game) populateGameInfo() syncdata.GameInfo {
|
|||||||
return syncdata.GameInfo{
|
return syncdata.GameInfo{
|
||||||
GameId: gm.id,
|
GameId: gm.id,
|
||||||
State: gm.state,
|
State: gm.state,
|
||||||
|
Phase: gm.phase,
|
||||||
Players: gm.getPlayers(),
|
Players: gm.getPlayers(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,41 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
func (gm *Game) StartGame() {
|
import (
|
||||||
gm.mu.Lock()
|
"fmt"
|
||||||
defer gm.mu.Unlock()
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
gm.state = STATE_PLAY
|
func (gm *Game) StartGame() {
|
||||||
gm.eng.Update()
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
STATE_IDLE = "idle"
|
STATE_IDLE = "idle"
|
||||||
STATE_ENTER_STATEMENTS = "enter-quotes"
|
STATE_ENTER_STATEMENTS = "enter-quotes"
|
||||||
STATE_READY_SET_PLAY = "ready-set-play"
|
STATE_READY_SET = "ready-set"
|
||||||
STATE_PLAY = "play"
|
STATE_PLAY = "play"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ type PlayerInfo struct {
|
|||||||
type GameInfo struct {
|
type GameInfo struct {
|
||||||
GameId string `json:"id"`
|
GameId string `json:"id"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
|
Phase string `json:"phase"`
|
||||||
Players []PlayerInfo `json:"players"`
|
Players []PlayerInfo `json:"players"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user