feat: getGames returns role and authcode of all players

This commit is contained in:
Settel 2022-03-17 08:24:13 +01:00
parent abb86fc451
commit 0fd022a7b3
4 changed files with 21 additions and 3 deletions

View File

@ -19,15 +19,28 @@ func (app *Application) GetGames(usr *user.User, w http.ResponseWriter, r *http.
return return
} }
app.mu.Lock()
defer app.mu.Unlock()
games := Games{ games := Games{
Games: make(map[string]*game.GameInfoJson), Games: make(map[string]*game.GameInfoJson),
} }
app.mu.Lock()
for k, gm := range app.games { for k, gm := range app.games {
games.Games[k] = gm.GetGameInfo() games.Games[k] = gm.GetGameInfo()
} }
app.mu.Unlock()
for k, gameInfo := range games.Games {
for i, _ := range gameInfo.Players {
if playerUser, err := app.GetUserById(gameInfo.Players[i].Id); err != nil {
fmt.Printf("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "internal server error")
return
} else {
gameInfo.Players[i].AuthCode = playerUser.GetAuthCode()
}
}
games.Games[k] = gameInfo
}
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(games) jsonString, _ := json.Marshal(games)

View File

@ -26,6 +26,7 @@ func (gm *Game) initGameInfoJson() *GameInfoJson {
Score: player.score, Score: player.score,
IsPlaying: player.isPlaying, IsPlaying: player.isPlaying,
IsIdle: player.isIdle, IsIdle: player.isIdle,
Role: player.role,
}) })
} }

View File

@ -19,6 +19,7 @@ func (gm *Game) EventPlayerJoins(usr *user.User) {
isPlaying: true, isPlaying: true,
isIdle: false, isIdle: false,
score: prevPlayer.score, score: prevPlayer.score,
role: usr.GetRole(),
} }
gm.players[usrId] = player gm.players[usrId] = player
@ -66,6 +67,7 @@ func (gm *Game) AddPlayer(usr *user.User) {
isPlaying: false, isPlaying: false,
isIdle: true, isIdle: true,
score: 0, score: 0,
role: usr.GetRole(),
} }
} }

View File

@ -35,6 +35,7 @@ type playerInfo struct {
isPlaying bool isPlaying bool
isIdle bool isIdle bool
score int score int
role string
} }
type Source struct { type Source struct {
@ -88,6 +89,7 @@ type PlayerInfoJson struct {
IsIdle bool `json:"isIdle"` IsIdle bool `json:"isIdle"`
NumberOfQuotes int `json:"numQuotes"` NumberOfQuotes int `json:"numQuotes"`
AuthCode string `json:"authcode"` AuthCode string `json:"authcode"`
Role string `json:"role"`
} }
type GameInfoJson struct { type GameInfoJson struct {