knowyt/server/src/game/changeGameState.go

34 lines
838 B
Go

package game
import (
"fmt"
)
func (gm *Game) changeGameState(expectedState, newState, newPhase string) error {
gm.mu.Lock()
defer gm.mu.Unlock()
if expectedState != STATE_ANY && gm.state != expectedState {
return fmt.Errorf("%s game state \"%s\" != expected state \"%s\"", gm.id, 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()
if expectedState != STATE_ANY && gm.state != expectedState {
return fmt.Errorf("%s game state \"%s\" != expected state \"%s\"", gm.id, gm.state, expectedState)
}
if gm.phase != expectedPhase {
return fmt.Errorf("%s game phase \"%s\" != expected phase \"%s\"", gm.id, gm.phase, expectedPhase)
}
gm.phase = newPhase
return nil
}