51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package application
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"sirlab.de/go/knowyt/game"
|
|
"sirlab.de/go/knowyt/log"
|
|
"sirlab.de/go/knowyt/user"
|
|
)
|
|
|
|
type Games struct {
|
|
Games map[string]*game.GameInfoJson `json:"games"`
|
|
}
|
|
|
|
func (app *Application) GetGames(usr *user.User, w http.ResponseWriter, r *http.Request) {
|
|
if !usr.IsAdmin() {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
fmt.Fprintf(w, "forbidden")
|
|
return
|
|
}
|
|
|
|
games := Games{
|
|
Games: make(map[string]*game.GameInfoJson),
|
|
}
|
|
app.mu.Lock()
|
|
for k, gm := range app.games {
|
|
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 {
|
|
log.Warn("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")
|
|
jsonString, _ := json.Marshal(games)
|
|
fmt.Fprintf(w, "%s", string(jsonString))
|
|
}
|