feat: add new game state 'disabled'

This commit is contained in:
Settel 2022-12-10 19:58:24 +01:00
parent 5550341359
commit a2c95abde9
7 changed files with 82 additions and 8 deletions

View File

@ -4,11 +4,13 @@
<Button class="game-controls__control game-controls__control__end" :border="false" :disabled="false" @click="backToPlay">{{ $t('back') }}</Button>
</template>
<template v-else>
<template v-if="game.state !== 'disabled'">
<Button class="game-controls__control" :border="!disabled.collect" :disabled="disabled.collect" @click="collect">{{ $t('collect-quotes') }}</Button>
<Button class="game-controls__control" :border="!disabled.start" :disabled="disabled.start" @click="start">{{ $t('start') }}</Button>
<Button class="game-controls__control" :border="!disabled.cont" :disabled="disabled.cont" @click="cont">{{ $t('continue') }}</Button>
<Button class="game-controls__control" :border="!disabled.idle" :disabled="disabled.idle" @click="idle">{{ $t('idle') }}</Button>
<Button class="game-controls__control" :border="!disabled.finish" :disabled="disabled.finish" @click="finish">{{ $t('finish') }}</Button>
</template>
<Button class="game-controls__control game-controls__control__end" :border="false" :disabled="false" @click="goToAdmin">{{ $t('admin') }}</Button>
</template>
</div>

View File

@ -7,15 +7,18 @@
<th class="gameinfos-tile__table-head">{{ $t('state')}}</th>
<th class="gameinfos-tile__table-head">{{ $t('num-players')}}</th>
<th class="gameinfos-tile__table-head">{{ $t('gamemasters')}}</th>
<th class="gameinfos-tile__table-head">&nbsp;</th>
</tr>
<tr class="gameinfos-tile__row" v-for="game in games" :key="game.id" @click="selectGame(game)">
<tr
:class="{ 'gameinfos-tile__row': true, 'gameinfos-tile__row__disabled': game.state == 'disabled' }"
v-for="game in games"
:key="game.id"
@click="selectGame(game)"
>
<td class="gameinfos-tile__cell">{{ game.name }}</td>
<td class="gameinfos-tile__cell">{{ game.lang }}</td>
<td class="gameinfos-tile__cell">{{ game.state }}</td>
<td class="gameinfos-tile__cell">{{ game.players.length }}</td>
<td class="gameinfos-tile__cell">{{ getGamemastersFromGame(game).join(', ') }}</td>
<td class="gameinfos-tile__cell"><nuxt-icon name="toggle" /></td>
</tr>
</table>
</AdminInfoTile>
@ -71,6 +74,10 @@ const selectGame = async (game: GameInfo): Promise<void> => {
&:hover {
background-color: #606060;
}
&__disabled {
color: #808080;
}
}
&__cell {

View File

@ -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")
}

View File

@ -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()
}

View File

@ -14,6 +14,7 @@ const (
STATE_READY_SET = "ready-set"
STATE_PLAY = "play"
STATE_FINAL = "final"
STATE_DISABLED = "disabled"
)
const (

View File

@ -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)

View File

@ -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()