knowyt/server/src/game/changeGameState.go

34 lines
808 B
Go
Raw Normal View History

2021-08-13 20:39:37 +00:00
package game
import (
"fmt"
)
func (gm *Game) changeGameState(expectedState, newState, newPhase string) error {
gm.mu.Lock()
defer gm.mu.Unlock()
2021-08-14 11:21:53 +00:00
if expectedState != STATE_ANY && gm.state != expectedState {
2021-08-13 20:39:37 +00:00
return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState)
}
gm.state = newState
gm.phase = newPhase
return nil
}
func (gm *Game) changeGamePhase(expectedState, expectedPhase, newPhase string) error {
gm.mu.Lock()
defer gm.mu.Unlock()
2021-08-14 11:21:53 +00:00
if expectedState != STATE_ANY && gm.state != expectedState {
2021-08-13 20:39:37 +00:00
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
return nil
}