feat: change game name

This commit is contained in:
Settel 2022-03-01 09:41:30 +01:00
parent 64265d97e3
commit e2ad380c6c
10 changed files with 117 additions and 22 deletions

View File

@ -1,13 +1,14 @@
<template> <template>
<AdminTile title="Game"> <AdminTile class="admin-tile-gameinfo" title="Game">
<table> <table>
<tr> <tr>
<td>Name:</td> <td>Name:</td>
<td>{{ gameinfo.name }}</td> <td>{{ gameinfo.name }}</td>
<td><div class="admin-tile-gameinfo__edit-game-name" @click="editName()"></div></td>
</tr> </tr>
<tr> <tr>
<td># Players:</td> <td># Players:</td>
<td>{{ players.length }}</td> <td colspan="2">{{ players.length }}</td>
</tr> </tr>
</table> </table>
</AdminTile> </AdminTile>
@ -16,5 +17,29 @@
<script> <script>
export default { export default {
props: ['gameinfo', 'players'], props: ['gameinfo', 'players'],
methods: {
async editName() {
const name = window.prompt('new game name: ')
if (name) {
const g = this.$store.state.engine.user.game
await this.$engine.setGameName({ g, name })
await this.$engine.fetchGameInfo({ g })
}
},
},
} }
</script> </script>
<style lang="scss">
.admin-tile-gameinfo {
&__edit-game-name {
cursor: pointer;
&:hover {
color: #a0a0a0;
}
&::after {
content: '✎';
}
}
}
</style>

View File

@ -2,7 +2,6 @@
<div class="page-admin"> <div class="page-admin">
<template v-if="!isGamemasterOrAdmin"> <template v-if="!isGamemasterOrAdmin">
<p>You are not a game master.</p> <p>You are not a game master.</p>
<button @click="login">Go to login page</button>
</template> </template>
<template v-if="isGamemasterOrAdmin"> <template v-if="isGamemasterOrAdmin">
<GameControls /> <GameControls />
@ -49,9 +48,5 @@ export default {
display: flex; display: flex;
margin: 24px; margin: 24px;
} }
&__back-button {
margin: 16px;
}
} }
</style> </style>

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 fetchGameInfo from './fetchGameInfo' import fetchGameInfo from './fetchGameInfo'
import setGameName from './setGameName'
import collectQuotes from './collectQuotes' import collectQuotes from './collectQuotes'
import startGame from './startGame' import startGame from './startGame'
import resetGame from './resetGame' import resetGame from './resetGame'
@ -29,6 +30,7 @@ export default (context, inject) => {
fetchUpdate, fetchUpdate,
fetchUserInfo, fetchUserInfo,
fetchGameInfo, fetchGameInfo,
setGameName,
collectQuotes, collectQuotes,
getMyQuotes, getMyQuotes,
saveQuote, saveQuote,

View File

@ -0,0 +1,8 @@
export default async function({ g, name }) {
const { store } = this.context
await this.callApi('/api/setGameName', {
g,
name,
})
}

View File

@ -23,4 +23,6 @@ func (app *Application) SaveSelection(usr *user.User, w http.ResponseWriter, r *
selection := r.URL.Query().Get("selection") selection := r.URL.Query().Get("selection")
gm.SaveSelection(usr, selection) gm.SaveSelection(usr, selection)
fmt.Fprintf(w, "ok")
} }

View File

@ -0,0 +1,32 @@
package application
import (
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
func (app *Application) SetGameName(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
}
name := r.URL.Query().Get("name")
if err := gm.SetGameName(name); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v", err)
fmt.Fprintf(w, "server error")
return
}
fmt.Fprintf(w, "ok")
}

View File

@ -20,6 +20,7 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
} else { } else {
gm := Game{ gm := Game{
id: id, id: id,
filename: fileName,
name: gmJson.Name, name: gmJson.Name,
eng: engine.NewEngine(), eng: engine.NewEngine(),
players: make(map[string]playerInfo, 0), players: make(map[string]playerInfo, 0),
@ -31,6 +32,23 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
} }
} }
func (gm *Game) SaveGame() error {
gm.mu.Lock()
defer gm.mu.Unlock()
gmJson := GameJson{
Name: gm.name,
}
if jsonBytes, err := json.Marshal(gmJson); err != nil {
return err
} else {
if err := os.WriteFile(gm.filename, jsonBytes, 0666); err != nil {
return err
}
}
return nil
}
func (gm *Game) StartEngine() { func (gm *Game) StartEngine() {
gm.eng.Run(gm.populateSyncDataCb) gm.eng.Run(gm.populateSyncDataCb)
} }

View File

@ -0,0 +1,11 @@
package game
import ()
func (gm *Game) SetGameName(name string) error {
gm.mu.Lock()
gm.name = name
gm.mu.Unlock()
return gm.SaveGame()
}

View File

@ -57,6 +57,7 @@ type Round struct {
type Game struct { type Game struct {
mu sync.Mutex mu sync.Mutex
id string id string
filename string
name string name string
players map[string]playerInfo players map[string]playerInfo
eng *engine.Engine eng *engine.Engine

View File

@ -23,6 +23,7 @@ func main() {
mux.PublicHandleFunc("/api/logout", mux.Logout) mux.PublicHandleFunc("/api/logout", mux.Logout)
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo) mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo) mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
mux.PrivateHandleFunc("/api/setGameName", app.SetGameName)
mux.PrivateHandleFunc("/api/sync", app.SyncHandler) mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
mux.PrivateHandleFunc("/api/collectQuotes", app.CollectQuotes) mux.PrivateHandleFunc("/api/collectQuotes", app.CollectQuotes)
mux.PrivateHandleFunc("/api/startGame", app.StartGame) mux.PrivateHandleFunc("/api/startGame", app.StartGame)