calculate number of quotes left/total, send to client

This commit is contained in:
Settel 2021-12-03 18:36:19 +01:00
parent 32f9fa68b3
commit 237a0d0345
2 changed files with 29 additions and 10 deletions

View File

@ -9,14 +9,31 @@ func (gm *Game) populateSyncDataCb(syncData *syncdata.SyncData) {
}
func (gm *Game) populateGameInfo() syncdata.GameInfo {
numQuotesLeft, numQuotesTotal := gm.countQuotes()
gm.mu.Lock()
defer gm.mu.Unlock()
return syncdata.GameInfo{
GameId: gm.id,
State: gm.state,
Phase: gm.phase,
Players: gm.populateGetPlayers(),
Round: gm.populateGetRoundInfo(),
GameId: gm.id,
State: gm.state,
Phase: gm.phase,
Players: gm.populateGetPlayers(),
Round: gm.populateGetRoundInfo(),
NumQuotesLeft: numQuotesLeft,
NumQuotesTotal: numQuotesTotal,
}
}
func (gm *Game) countQuotes() (int, int) {
allQuotes := gm.getAllQuotes()
numQuotesLeft := 0
for _, q := range allQuotes {
if !q.IsPlayed() {
numQuotesLeft++
}
}
return numQuotesLeft, len(allQuotes)
}

View File

@ -31,11 +31,13 @@ type RoundInfo struct {
}
type GameInfo struct {
GameId string `json:"id"`
State string `json:"state"`
Phase string `json:"phase"`
Players []PlayerInfo `json:"players"`
Round *RoundInfo `json:"round"`
GameId string `json:"id"`
State string `json:"state"`
Phase string `json:"phase"`
Players []PlayerInfo `json:"players"`
Round *RoundInfo `json:"round"`
NumQuotesLeft int `json:"numQuotesLeft"`
NumQuotesTotal int `json:"numQuotesTotal"`
}
type SyncData struct {