knowyt/server/src/game/getGameInfo.go
2022-04-12 14:27:49 +02:00

42 lines
958 B
Go

package game
func (gm *Game) GetGameInfo() *GameInfoJson {
gameInfo := gm.initGameInfoJson()
for i, _ := range gameInfo.Players {
quotes := gm.getQuotesInfoByUserId(gameInfo.Players[i].Id)
gameInfo.Players[i].NumberOfQuotes = len(quotes)
}
return gameInfo
}
func (gm *Game) initGameInfoJson() *GameInfoJson {
numQuotesLeft, numQuotesTotal := gm.countQuotes()
gm.mu.Lock()
defer gm.mu.Unlock()
gameInfo := GameInfoJson{
Id: gm.id,
Name: gm.name,
Created: gm.created,
State: gm.state,
Players: make([]PlayerInfoJson, 0),
NumQuotesLeft: numQuotesLeft,
NumQuotesTotal: numQuotesTotal,
}
for _, player := range gm.players {
gameInfo.Players = append(gameInfo.Players, PlayerInfoJson{
Id: player.id,
Name: player.name,
Score: player.score,
IsPlaying: player.isPlaying,
IsIdle: player.isIdle,
Role: player.role,
})
}
return &gameInfo
}