From a2c95abde9a125e95633694cf85801be4931b294 Mon Sep 17 00:00:00 2001 From: Settel Date: Sat, 10 Dec 2022 19:58:24 +0100 Subject: [PATCH] feat: add new game state 'disabled' --- client/src/components/GameControls.vue | 12 ++++--- client/src/components/admin/GameInfosTile.vue | 13 +++++-- server/src/application/disableGame.go | 36 +++++++++++++++++++ server/src/game/startGame.go | 10 ++++++ server/src/game/struct.go | 1 + server/src/knowyt.go | 1 + server/src/user/user.go | 17 +++++++++ 7 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 server/src/application/disableGame.go diff --git a/client/src/components/GameControls.vue b/client/src/components/GameControls.vue index ac13146..6304531 100644 --- a/client/src/components/GameControls.vue +++ b/client/src/components/GameControls.vue @@ -4,11 +4,13 @@ diff --git a/client/src/components/admin/GameInfosTile.vue b/client/src/components/admin/GameInfosTile.vue index 8e99161..6781fa9 100644 --- a/client/src/components/admin/GameInfosTile.vue +++ b/client/src/components/admin/GameInfosTile.vue @@ -7,15 +7,18 @@ {{ $t('state')}} {{ $t('num-players')}} {{ $t('gamemasters')}} -   - + {{ game.name }} {{ game.lang }} {{ game.state }} {{ game.players.length }} {{ getGamemastersFromGame(game).join(', ') }} - @@ -71,6 +74,10 @@ const selectGame = async (game: GameInfo): Promise => { &:hover { background-color: #606060; } + + &__disabled { + color: #808080; + } } &__cell { diff --git a/server/src/application/disableGame.go b/server/src/application/disableGame.go new file mode 100644 index 0000000..28ea696 --- /dev/null +++ b/server/src/application/disableGame.go @@ -0,0 +1,36 @@ +package application + +import ( + "fmt" + "net/http" + + "sirlab.de/go/knowyt/log" + "sirlab.de/go/knowyt/user" +) + +func (app *Application) DisableGame(usr *user.User, w http.ResponseWriter, r *http.Request) { + gameRef := r.URL.Query().Get("g") + gm, err := app.GetGameById(gameRef) + if err != nil { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "game not found") + return + } + + if usr.GetGameId() != gameRef || !usr.IsAdminOrCameo() { + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "forbidden") + return + } + + gm.DisableGame() + + if err := app.saveGameState(gm); err != nil { + log.ErrorLog(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "couldn't save game state") + return + } + + fmt.Fprintf(w, "ok") +} diff --git a/server/src/game/startGame.go b/server/src/game/startGame.go index 5a709f6..1961bcc 100644 --- a/server/src/game/startGame.go +++ b/server/src/game/startGame.go @@ -17,3 +17,13 @@ func (gm *Game) ResetGame() { gm.notifyClients() } + +func (gm *Game) DisableGame() { + err := gm.changeGameState(STATE_ANY, STATE_DISABLED, PHASE_NONE) + if err != nil { + log.ErrorLog(err) + return + } + + gm.notifyClients() +} diff --git a/server/src/game/struct.go b/server/src/game/struct.go index 9c11fb5..9422c06 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -14,6 +14,7 @@ const ( STATE_READY_SET = "ready-set" STATE_PLAY = "play" STATE_FINAL = "final" + STATE_DISABLED = "disabled" ) const ( diff --git a/server/src/knowyt.go b/server/src/knowyt.go index d6da4ad..f27fe34 100644 --- a/server/src/knowyt.go +++ b/server/src/knowyt.go @@ -36,6 +36,7 @@ func main() { mux.PrivateHandleFunc("/api/resetGame", app.ResetGame) mux.PrivateHandleFunc("/api/continueGame", app.ContinueGame) mux.PrivateHandleFunc("/api/finishGame", app.FinishGame) + mux.PrivateHandleFunc("/api/disableGame", app.DisableGame) mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection) mux.PrivateHandleFunc("/api/getQuotes", app.GetQuotes) mux.PrivateHandleFunc("/api/saveQuote", app.SaveQuote) diff --git a/server/src/user/user.go b/server/src/user/user.go index 258ec76..a376966 100644 --- a/server/src/user/user.go +++ b/server/src/user/user.go @@ -167,6 +167,23 @@ func (usr *User) IsAdmin() bool { return usr.role == ROLE_ADMIN } +func (usr *User) IsAdminOrCameo() bool { + usr.mu.Lock() + defer usr.mu.Unlock() + + if usr.role == ROLE_ADMIN { + return true + } + + if usr.cameo != nil { + if usr.cameo.role == ROLE_ADMIN { + return true + } + } + + return false +} + func (usr *User) SetCameo(usrCameo *User) { usr.mu.Lock() defer usr.mu.Unlock()