diff --git a/client/src/components/Final.vue b/client/src/components/Final.vue index 4a44c75..cf51659 100644 --- a/client/src/components/Final.vue +++ b/client/src/components/Final.vue @@ -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; } } diff --git a/client/src/components/GameControls.vue b/client/src/components/GameControls.vue index 0998537..ea28a16 100644 --- a/client/src/components/GameControls.vue +++ b/client/src/components/GameControls.vue @@ -1,8 +1,8 @@ diff --git a/server/src/application/addUsersToGames.go b/server/src/application/addUsersToGames.go deleted file mode 100644 index 2dc71ef..0000000 --- a/server/src/application/addUsersToGames.go +++ /dev/null @@ -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) - } - } -} diff --git a/server/src/application/application.go b/server/src/application/application.go index bb4ebd8..157b463 100644 --- a/server/src/application/application.go +++ b/server/src/application/application.go @@ -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() diff --git a/server/src/application/continueGame.go b/server/src/application/continueGame.go index a62d9f5..0c480a0 100644 --- a/server/src/application/continueGame.go +++ b/server/src/application/continueGame.go @@ -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) } } diff --git a/server/src/application/expireInactivePlayers.go b/server/src/application/expireInactivePlayers.go index 875db48..90afad3 100644 --- a/server/src/application/expireInactivePlayers.go +++ b/server/src/application/expireInactivePlayers.go @@ -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) } diff --git a/server/src/application/finishGame.go b/server/src/application/finishGame.go index 151edad..b134ccb 100644 --- a/server/src/application/finishGame.go +++ b/server/src/application/finishGame.go @@ -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) } diff --git a/server/src/application/loadGameState.go b/server/src/application/loadGameState.go new file mode 100644 index 0000000..2ad92f1 --- /dev/null +++ b/server/src/application/loadGameState.go @@ -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 +} diff --git a/server/src/application/loadGames.go b/server/src/application/loadGames.go index a45637d..cd0eb97 100644 --- a/server/src/application/loadGames.go +++ b/server/src/application/loadGames.go @@ -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) } diff --git a/server/src/application/saveGame.go b/server/src/application/saveGameState.go similarity index 91% rename from server/src/application/saveGame.go rename to server/src/application/saveGameState.go index e1be5be..5c38155 100644 --- a/server/src/application/saveGame.go +++ b/server/src/application/saveGameState.go @@ -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 { diff --git a/server/src/game/game.go b/server/src/game/game.go index 6436b39..7260b2a 100644 --- a/server/src/game/game.go +++ b/server/src/game/game.go @@ -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() diff --git a/server/src/game/setGameState.go b/server/src/game/setGameState.go new file mode 100644 index 0000000..67a7a80 --- /dev/null +++ b/server/src/game/setGameState.go @@ -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 + } +}