reset game

This commit is contained in:
Settel 2021-08-14 13:21:53 +02:00
parent fbd9a55e46
commit ead73a9c2d
8 changed files with 55 additions and 3 deletions

View File

@ -1,6 +1,7 @@
<template> <template>
<nav v-if="isGamemaster" class="gamecontrols"> <nav v-if="isGamemaster" class="gamecontrols">
<button @click="startGame">Start</button> <button @click="startGame">Start</button>
<button @click="resetGame">Reset</button>
</nav> </nav>
</template> </template>
@ -19,6 +20,9 @@ export default {
startGame() { startGame() {
this.$engine.startGame() this.$engine.startGame()
}, },
resetGame() {
this.$engine.resetGame()
},
}, },
} }
</script> </script>

View File

@ -4,6 +4,7 @@ import stop from './stop'
import fetchUpdate from './fetchUpdate' import fetchUpdate from './fetchUpdate'
import fetchUserInfo from './fetchUserInfo' import fetchUserInfo from './fetchUserInfo'
import startGame from './startGame' import startGame from './startGame'
import resetGame from './resetGame'
import parseSyncData from './parseSyncData' import parseSyncData from './parseSyncData'
export default (context, inject) => { export default (context, inject) => {
@ -19,6 +20,7 @@ export default (context, inject) => {
fetchUpdate, fetchUpdate,
fetchUserInfo, fetchUserInfo,
startGame, startGame,
resetGame,
parseSyncData, parseSyncData,
} }

View File

@ -0,0 +1,7 @@
export default async function() {
const { store } = this.context
await this.callApi('/api/resetGame', {
g: store.state.engine.user?.game,
})
}

View File

@ -0,0 +1,25 @@
package application
import (
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
func (app *Application) ResetGame(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.IsGamemaster() {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
gm.ResetGame()
}

View File

@ -8,7 +8,7 @@ func (gm *Game) changeGameState(expectedState, newState, newPhase string) error
gm.mu.Lock() gm.mu.Lock()
defer gm.mu.Unlock() defer gm.mu.Unlock()
if gm.state != expectedState { if expectedState != STATE_ANY && gm.state != expectedState {
return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState) return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState)
} }
gm.state = newState gm.state = newState
@ -22,7 +22,7 @@ func (gm *Game) changeGamePhase(expectedState, expectedPhase, newPhase string) e
gm.mu.Lock() gm.mu.Lock()
defer gm.mu.Unlock() defer gm.mu.Unlock()
if gm.state != expectedState { if expectedState != STATE_ANY && gm.state != expectedState {
return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState) return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState)
} }
if gm.phase != expectedPhase { if gm.phase != expectedPhase {

View File

@ -33,7 +33,15 @@ func (gm *Game) startGameSub() {
} }
} }
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, "") err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE)
if err != nil {
fmt.Println(err)
return
}
}
func (gm *Game) ResetGame() {
err := gm.changeGameState(STATE_ANY, STATE_IDLE, PHASE_NONE)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View File

@ -7,12 +7,17 @@ import (
) )
const ( const (
STATE_ANY = "*"
STATE_IDLE = "idle" STATE_IDLE = "idle"
STATE_ENTER_STATEMENTS = "enter-quotes" STATE_ENTER_STATEMENTS = "enter-quotes"
STATE_READY_SET = "ready-set" STATE_READY_SET = "ready-set"
STATE_PLAY = "play" STATE_PLAY = "play"
) )
const (
PHASE_NONE = ""
)
type playerInfo struct { type playerInfo struct {
id string id string
name string name string

View File

@ -24,6 +24,7 @@ func main() {
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo) mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
mux.PrivateHandleFunc("/api/sync", app.SyncHandler) mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
mux.PrivateHandleFunc("/api/startGame", app.StartGame) mux.PrivateHandleFunc("/api/startGame", app.StartGame)
mux.PrivateHandleFunc("/api/resetGame", app.ResetGame)
// default handler // default handler
fsHandler := http.FileServer(http.Dir("../../client/dist/")) fsHandler := http.FileServer(http.Dir("../../client/dist/"))