From 5f5033e84683dab0c0015e80ae33bf13f87e0a1b Mon Sep 17 00:00:00 2001 From: Settel Date: Sun, 15 Aug 2021 17:43:00 +0200 Subject: [PATCH] send quote and sources to clients --- client/src/components/EngineDebug.vue | 2 ++ server/src/game/populateSyncDataCb.go | 21 +++++++++++++++ server/src/game/runRound.go | 37 +++++++++++++++++++++++++++ server/src/game/startGame.go | 16 ------------ server/src/game/struct.go | 18 ++++++++----- server/src/syncdata/syncdata.go | 6 +++++ 6 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 server/src/game/runRound.go diff --git a/client/src/components/EngineDebug.vue b/client/src/components/EngineDebug.vue index b3463f4..feefd1f 100644 --- a/client/src/components/EngineDebug.vue +++ b/client/src/components/EngineDebug.vue @@ -24,6 +24,8 @@ export default { right: 0; bottom: 0; background-color: #002040; + max-width: 600px; + overflow: auto; &__json { margin: 1em 2em; diff --git a/server/src/game/populateSyncDataCb.go b/server/src/game/populateSyncDataCb.go index 1bcfda8..e272b8d 100644 --- a/server/src/game/populateSyncDataCb.go +++ b/server/src/game/populateSyncDataCb.go @@ -1,6 +1,7 @@ package game import ( + "fmt" "sirlab.de/go/knyt/syncdata" ) @@ -17,6 +18,7 @@ func (gm *Game) populateGameInfo() syncdata.GameInfo { State: gm.state, Phase: gm.phase, Players: gm.getPlayers(), + Round: gm.getRoundInfo(), } } @@ -34,3 +36,22 @@ func (gm *Game) getPlayers() []syncdata.PlayerInfo { return players } + +func (gm *Game) getRoundInfo() *syncdata.RoundInfo { + if gm.state != STATE_PLAY { + return nil + } + + quote := gm.quotes[gm.round.quoteId] + if quote == nil { + fmt.Printf("fatal error: quote id \"%s\" not found\n", gm.round.quoteId) + return nil + } + + roundInfo := syncdata.RoundInfo{ + Quote: quote.GetQuote(), + Sources: gm.round.sources, + } + + return &roundInfo +} diff --git a/server/src/game/runRound.go b/server/src/game/runRound.go new file mode 100644 index 0000000..277f574 --- /dev/null +++ b/server/src/game/runRound.go @@ -0,0 +1,37 @@ +package game + +import ( + "fmt" +) + +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_ANY, STATE_PLAY, PHASE_SELECT_QUOTE) + if err != nil { + fmt.Println(err) + return + } + + gm.selectQuote() + gm.notifyClients() +} + +func (gm *Game) selectQuote() { + gm.mu.Lock() + defer gm.mu.Unlock() + + gm.round.quoteId = "455df6bc-070d-4728-83ab-481ceafa8590" + gm.round.sources = []string{ + "Herbert Grönemeyer", + "Frank Sinatra", + "Gaius Julius Cäsar", + "Thomas Alva Edison", + "Konfuzius", + "George W. Bush jun.", + } +} diff --git a/server/src/game/startGame.go b/server/src/game/startGame.go index 04f8d02..b886642 100644 --- a/server/src/game/startGame.go +++ b/server/src/game/startGame.go @@ -9,22 +9,6 @@ func (gm *Game) StartGame() { go gm.runRound() } -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_ANY, STATE_PLAY, PHASE_NONE) - if err != nil { - fmt.Println(err) - return - } - - gm.notifyClients() -} - func (gm *Game) ResetGame() { err := gm.changeGameState(STATE_ANY, STATE_IDLE, PHASE_NONE) if err != nil { diff --git a/server/src/game/struct.go b/server/src/game/struct.go index 9cb4462..c118a95 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -7,15 +7,15 @@ import ( ) const ( - STATE_ANY = "*" - STATE_IDLE = "idle" - STATE_ENTER_STATEMENTS = "enter-quotes" - STATE_READY_SET = "ready-set" - STATE_PLAY = "play" + STATE_ANY = "*" + STATE_IDLE = "idle" + STATE_READY_SET = "ready-set" + STATE_PLAY = "play" ) const ( - PHASE_NONE = "" + PHASE_NONE = "" + PHASE_SELECT_QUOTE = "select-quote" ) type playerInfo struct { @@ -25,6 +25,11 @@ type playerInfo struct { isIdle bool } +type Round struct { + quoteId string + sources []string +} + type Game struct { mu sync.Mutex id string @@ -34,6 +39,7 @@ type Game struct { state string phase string quotes map[string]*quote.Quote + round Round } type GameJson struct { diff --git a/server/src/syncdata/syncdata.go b/server/src/syncdata/syncdata.go index 72a36fe..5d496f6 100644 --- a/server/src/syncdata/syncdata.go +++ b/server/src/syncdata/syncdata.go @@ -6,11 +6,17 @@ type PlayerInfo struct { IsIdle bool `json:"isIdle"` } +type RoundInfo struct { + Quote string `json:"quote"` + Sources []string `json:"sources"` +} + type GameInfo struct { GameId string `json:"id"` State string `json:"state"` Phase string `json:"phase"` Players []PlayerInfo `json:"players"` + Round *RoundInfo `json:"round"` } type SyncData struct {