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>
<AdminTile title="Game">
<AdminTile class="admin-tile-gameinfo" title="Game">
<table>
<tr>
<td>Name:</td>
<td>{{ gameinfo.name }}</td>
<td><div class="admin-tile-gameinfo__edit-game-name" @click="editName()"></div></td>
</tr>
<tr>
<td># Players:</td>
<td>{{ players.length }}</td>
<td colspan="2">{{ players.length }}</td>
</tr>
</table>
</AdminTile>
@ -16,5 +17,29 @@
<script>
export default {
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>
<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">
<template v-if="!isGamemasterOrAdmin">
<p>You are not a game master.</p>
<button @click="login">Go to login page</button>
</template>
<template v-if="isGamemasterOrAdmin">
<GameControls />
@ -49,9 +48,5 @@ export default {
display: flex;
margin: 24px;
}
&__back-button {
margin: 16px;
}
}
</style>

View File

@ -4,6 +4,7 @@ import stop from './stop'
import fetchUpdate from './fetchUpdate'
import fetchUserInfo from './fetchUserInfo'
import fetchGameInfo from './fetchGameInfo'
import setGameName from './setGameName'
import collectQuotes from './collectQuotes'
import startGame from './startGame'
import resetGame from './resetGame'
@ -29,6 +30,7 @@ export default (context, inject) => {
fetchUpdate,
fetchUserInfo,
fetchGameInfo,
setGameName,
collectQuotes,
getMyQuotes,
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")
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 {
gm := Game{
id: id,
filename: fileName,
name: gmJson.Name,
eng: engine.NewEngine(),
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() {
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 {
mu sync.Mutex
id string
filename string
name string
players map[string]playerInfo
eng *engine.Engine

View File

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