read game state on startup

This commit is contained in:
Settel 2021-09-26 23:36:47 +02:00
parent ff87243628
commit d783ceba93
12 changed files with 83 additions and 36 deletions

View File

@ -109,21 +109,21 @@ export default {
&-row {
opacity: 0;
animation: finals-text 1s ease forwards;
&:nth-child(1) { animation-delay: 4s; }
&:nth-child(2) { animation-delay: 3.8s; }
&:nth-child(3) { animation-delay: 3.6s; }
&:nth-child(4) { animation-delay: 3.4s; }
&:nth-child(5) { animation-delay: 3.2s; }
&:nth-child(6) { animation-delay: 3s; }
&:nth-child(7) { animation-delay: 2.8s; }
&:nth-child(8) { animation-delay: 2.6s; }
&:nth-child(9) { animation-delay: 2.4s; }
&:nth-child(10) { animation-delay: 2.2s; }
&:nth-child(11) { animation-delay: 2s; }
&:nth-child(12) { animation-delay: 1.8s; }
&:nth-child(13) { animation-delay: 1.6s; }
&:nth-child(14) { animation-delay: 1.4s; }
&:nth-child(15) { animation-delay: 1.2s; }
&:nth-child(1) { animation-delay: 5.5s; }
&:nth-child(2) { animation-delay: 4.5s; }
&:nth-child(3) { animation-delay: 3.5s; }
&:nth-child(4) { animation-delay: 2.4s; }
&:nth-child(5) { animation-delay: 2.2s; }
&:nth-child(6) { animation-delay: 2s; }
&:nth-child(7) { animation-delay: 1.9s; }
&:nth-child(8) { animation-delay: 1.8s; }
&:nth-child(9) { animation-delay: 1.7s; }
&:nth-child(10) { animation-delay: 1.6s; }
&:nth-child(11) { animation-delay: 1.5s; }
&:nth-child(12) { animation-delay: 1.4s; }
&:nth-child(13) { animation-delay: 1.3s; }
&:nth-child(14) { animation-delay: 1.2s; }
&:nth-child(15) { animation-delay: 1.1s; }
&:nth-child(16) { animation-delay: 1s; }
}

View File

@ -1,8 +1,8 @@
<template>
<nav v-if="isGamemaster" class="gamecontrols">
<button @click="startGame">Start</button>
<button @click="resetGame">Reset Round</button>
<button @click="continueGame">Continue</button>
<button @click="resetGame">Reset Round</button>
<button @click="finishGame">Finish Game</button>
</nav>
</template>

View File

@ -1,11 +0,0 @@
package application
func (app *Application) addUsersToGames() {
for _, usr := range app.users {
if gm, err := app.GetGameById(usr.GetGameId()); err != nil {
continue
} else {
gm.AddPlayer(usr)
}
}
}

View File

@ -21,7 +21,6 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e
if err := app.loadGames(); err != nil {
return nil, err
}
app.addUsersToGames()
go app.expireInactivePlayersThread()

View File

@ -23,7 +23,7 @@ func (app *Application) ContinueGame(usr *user.User, w http.ResponseWriter, r *h
gm.ContinueGame()
if err := app.saveGame(gm); err != nil {
if err := app.saveGameState(gm); err != nil {
fmt.Println(err)
}
}

View File

@ -34,7 +34,7 @@ func (app *Application) expireInactivePlayers() []string {
for usrId, atime := range app.playerATime {
elapsed := now.Sub(atime)
if elapsed.Seconds() > 10 {
if elapsed.Seconds() > 5 {
delete(app.playerATime, usrId)
removedIds = append(removedIds, usrId)
}

View File

@ -21,7 +21,7 @@ func (app *Application) FinishGame(usr *user.User, w http.ResponseWriter, r *htt
return
}
if err := app.saveGame(gm); err != nil {
if err := app.saveGameState(gm); err != nil {
fmt.Println(err)
}

View File

@ -0,0 +1,23 @@
package application
import (
"encoding/json"
"fmt"
"os"
"sirlab.de/go/knyt/game"
)
func (app *Application) loadGameState(gm *game.Game, fileName string) error {
jsonBytes, err := os.ReadFile(fileName)
if err != nil {
return err
}
var stateJson game.GameStateJson
if err := json.Unmarshal(jsonBytes, &stateJson); err != nil {
return fmt.Errorf("%s: %v\n", fileName, err)
} else {
gm.SetGameState(&stateJson)
}
return nil
}

View File

@ -16,8 +16,8 @@ func (app Application) loadGames() error {
for _, file := range files {
id := file.Name()
gameDirName := path.Join(dirName, id)
fileName := path.Join(gameDirName, "game.json")
gm, errGame := game.NewGameFromFile(id, fileName)
gameFileName := path.Join(gameDirName, "game.json")
gm, errGame := game.NewGameFromFile(id, gameFileName)
if errGame != nil {
return errGame
}
@ -26,6 +26,14 @@ func (app Application) loadGames() error {
if errStatement != nil {
return errStatement
}
for _, usr := range app.users {
if usr.GetGameId() == id {
gm.AddPlayer(usr)
}
}
app.loadGameState(gm, path.Join(gameDirName, "state.json"))
go gm.StartEngine()
app.addGame(gm)
}

View File

@ -7,7 +7,7 @@ import (
"sirlab.de/go/knyt/game"
)
func (app *Application) saveGame(gm *game.Game) error {
func (app *Application) saveGameState(gm *game.Game) error {
gameJson := gm.GetGameState()
jsonString, errMarshal := json.MarshalIndent(gameJson, "", " ")
if errMarshal != nil {

View File

@ -27,12 +27,14 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
quotes: make(map[string]*quote.Quote, 0),
}
go gm.eng.Run(gm.populateSyncDataCb)
return &gm, nil
}
}
func (gm *Game) StartEngine() {
gm.eng.Run(gm.populateSyncDataCb)
}
func (gm *Game) GetId() string {
gm.mu.Lock()
defer gm.mu.Unlock()

View File

@ -0,0 +1,26 @@
package game
func (gm *Game) SetGameState(stateJson *GameStateJson) {
for id, score := range stateJson.Scores {
gm.setScore(id, score)
}
for _, id := range stateJson.QuotesPlayed {
if quote, err := gm.getQuoteById(id); err != nil {
continue
} else {
quote.SetIsPlayed()
}
}
}
func (gm *Game) setScore(id string, score int) {
gm.mu.Lock()
defer gm.mu.Unlock()
pi := gm.players[id]
if pi.id == id {
pi.score = score
gm.players[id] = pi
}
}