feat: set game language

This commit is contained in:
Settel 2022-04-24 18:22:23 +02:00
parent 6f1d276523
commit c624f71b7f
12 changed files with 104 additions and 7 deletions

View File

@ -6,6 +6,17 @@
<td>{{ gameinfo.name }}</td>
<td><div class="admin-tile-gameinfo__edit-game-name" @click="editName()"></div></td>
</tr>
<tr>
<td>Sprache:</td>
<td v-if="!editLangShowDropdown">{{ gameinfo.lang }}</td>
<td v-if="editLangShowDropdown">
<select v-model="lang" @change="editLangChange">
<option value="de">de</option>
<option value="en">en</option>
</select>
</td>
<td><div class="admin-tile-gameinfo__edit-game-lang" @click="editLang()"></div></td>
</tr>
<tr>
<td>Erstellt:</td>
<td colspan="2">{{ $formatter.date(gameinfo.created) }}</td>
@ -36,6 +47,8 @@ export default {
data() {
return {
showId: false,
lang: '---',
editLangShowDropdown: false,
}
},
methods: {
@ -47,13 +60,23 @@ export default {
await this.$engine.fetchGameInfo({ g })
}
},
editLang() {
this.editLangShowDropdown = true
},
async editLangChange() {
this.editLangShowDropdown = false
const g = this.$store.state.engine.user.game
await this.$engine.setGameLang({ g, lang: this.lang })
await this.$engine.fetchGameInfo({ g })
},
},
}
</script>
<style lang="scss">
.admin-tile-gameinfo {
&__edit-game-name {
&__edit-game-name,
&__edit-game-lang {
cursor: pointer;
&:hover {
color: #a0a0a0;

View File

@ -3,6 +3,7 @@
<table class="admin-tile-games__table">
<tr>
<th class="admin-tile-games__table-head">Game name</th>
<th class="admin-tile-games__table-head">Lang</th>
<th class="admin-tile-games__table-head">Status</th>
<th class="admin-tile-games__table-head">#&nbsp;players</th>
<th class="admin-tile-games__table-head">Gamemaster(s)</th>
@ -14,6 +15,7 @@
:key="id"
>
<td>{{ games[id].name }}</td>
<td>{{ games[id].lang }}</td>
<td>{{ games[id].state }}</td>
<td>{{ games[id].players.length }}</td>
<td>{{ gamemasters[id] }}</td>

View File

@ -6,6 +6,7 @@ import fetchUserInfo from './fetchUserInfo'
import fetchGameInfo from './fetchGameInfo'
import fetchGames from './fetchGames'
import setGameName from './setGameName'
import setGameLang from './setGameLang'
import savePlayer from './savePlayer'
import deletePlayer from './deletePlayer'
import collectQuotes from './collectQuotes'
@ -36,6 +37,7 @@ export default (context, inject) => {
fetchGameInfo,
fetchGames,
setGameName,
setGameLang,
savePlayer,
deletePlayer,
collectQuotes,

View File

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

View File

@ -1 +1 @@
{"name":"Team Hogwards","created":1649841591}
{"name":"Team Hogwards","lang":"de","created":1649841591}

View File

@ -22,6 +22,10 @@ type PlayerInfo struct {
func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
teamname := r.URL.Query().Get("teamname")
lang := r.URL.Query().Get("lang")
if lang != "de" && lang != "en" {
lang = "de"
}
authcode := ""
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
@ -29,7 +33,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
authcode += strconv.Itoa(rnd.Intn(1000) / 10 % 10)
}
gm, err := app.createGame(teamname)
gm, err := app.createGame(teamname, lang)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err)
@ -56,7 +60,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(jsonString))
}
func (app *Application) createGame(gamename string) (*game.Game, error) {
func (app *Application) createGame(gamename, lang string) (*game.Game, error) {
gameId := uuid.NewString()
gameDirName := path.Join(app.config.DataDir, "games", gameId)
gameFileName := path.Join(gameDirName, "game.json")
@ -68,8 +72,8 @@ func (app *Application) createGame(gamename string) (*game.Game, error) {
if err := os.Mkdir(quotesDirName, 0777); err != nil {
return nil, err
}
fmt.Printf("createGame(\"%s\": \"%s\")\n", gameId, gamename)
gm, err := game.NewGame(gameId, gameFileName, gamename)
fmt.Printf("createGame(\"%s\": \"%s\" [%s])\n", gameId, gamename, lang)
gm, err := game.NewGame(gameId, gameFileName, gamename, lang)
if err != nil {
return nil, err
}

View File

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

View File

@ -20,10 +20,15 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
if err := json.Unmarshal(jsonBytes, &gmJson); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else {
lang := gmJson.Lang
if lang == "" {
lang = "de"
}
gm := Game{
id: id,
filename: fileName,
name: gmJson.Name,
lang: lang,
created: gmJson.Created,
eng: engine.NewEngine(),
players: make(map[string]playerInfo, 0),
@ -35,11 +40,12 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
}
}
func NewGame(id, fileName, name string) (*Game, error) {
func NewGame(id, fileName, name, lang string) (*Game, error) {
gm := Game{
id: id,
filename: fileName,
name: name,
lang: lang,
eng: engine.NewEngine(),
players: make(map[string]playerInfo, 0),
state: STATE_IDLE,
@ -56,6 +62,7 @@ func (gm *Game) SaveGame() error {
gmJson := GameJson{
Name: gm.name,
Lang: gm.lang,
Created: gm.created,
}
if jsonBytes, err := json.Marshal(gmJson); err != nil {

View File

@ -19,6 +19,7 @@ func (gm *Game) initGameInfoJson() *GameInfoJson {
gameInfo := GameInfoJson{
Id: gm.id,
Name: gm.name,
Lang: gm.lang,
Created: gm.created,
State: gm.state,
Players: make([]PlayerInfoJson, 0),

View File

@ -0,0 +1,14 @@
package game
import ()
func (gm *Game) SetGameLang(lang string) error {
gm.mu.Lock()
if lang != "de" && lang != "en" {
lang = "de"
}
gm.lang = lang
gm.mu.Unlock()
return gm.SaveGame()
}

View File

@ -60,6 +60,7 @@ type Game struct {
id string
filename string
name string
lang string
created int64
players map[string]playerInfo
eng *engine.Engine
@ -71,6 +72,7 @@ type Game struct {
type GameJson struct {
Name string `json:"name"`
Lang string `json:"lang"`
Created int64 `json:"created"`
}
@ -99,6 +101,7 @@ type PlayerInfoJson struct {
type GameInfoJson struct {
Id string `json:"id"`
Name string `json:"name"`
Lang string `json:"lang"`
Created int64 `json:"created"`
State string `json:"state"`
Players []PlayerInfoJson `json:"players"`

View File

@ -26,6 +26,7 @@ func main() {
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
mux.PrivateHandleFunc("/api/games", app.GetGames)
mux.PrivateHandleFunc("/api/setGameName", app.SetGameName)
mux.PrivateHandleFunc("/api/setGameLang", app.SetGameLang)
mux.PrivateHandleFunc("/api/savePlayer", app.SavePlayer)
mux.PrivateHandleFunc("/api/deletePlayer", app.DeletePlayer)
mux.PrivateHandleFunc("/api/sync", app.SyncHandler)