From 4fca93167325a9ac8fb34c9c8c725e364870518b Mon Sep 17 00:00:00 2001 From: Settel Date: Sun, 3 Apr 2022 21:20:32 +0200 Subject: [PATCH] feat: save and load game state --- .../users/ead6bcdb-347d-4613-96cc-661d435da986.json | 1 + server/src/application/collectQuotes.go | 8 ++++++++ server/src/application/finishGame.go | 12 ++++++++++++ server/src/application/resetGame.go | 9 +++++++++ server/src/application/startGame.go | 9 +++++++++ server/src/game/getGameState.go | 1 + server/src/game/setGameState.go | 7 +++++++ server/src/game/startGame.go | 1 - server/src/game/struct.go | 1 + 9 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 server/data/users/ead6bcdb-347d-4613-96cc-661d435da986.json diff --git a/server/data/users/ead6bcdb-347d-4613-96cc-661d435da986.json b/server/data/users/ead6bcdb-347d-4613-96cc-661d435da986.json new file mode 100644 index 0000000..8fdff87 --- /dev/null +++ b/server/data/users/ead6bcdb-347d-4613-96cc-661d435da986.json @@ -0,0 +1 @@ +{"authcode":"","name":"Crabbe","role":"player","game":"e24444aa-8a18-48aa-a36d-8f84620726f8"} \ No newline at end of file diff --git a/server/src/application/collectQuotes.go b/server/src/application/collectQuotes.go index 7fdfa45..73ce38d 100644 --- a/server/src/application/collectQuotes.go +++ b/server/src/application/collectQuotes.go @@ -22,4 +22,12 @@ func (app *Application) CollectQuotes(usr *user.User, w http.ResponseWriter, r * } gm.CollectQuotes() + if err := app.saveGameState(gm); err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "couldn't save game state") + return + } + + fmt.Fprintf(w, "ok") } diff --git a/server/src/application/finishGame.go b/server/src/application/finishGame.go index 62e12c1..69a8081 100644 --- a/server/src/application/finishGame.go +++ b/server/src/application/finishGame.go @@ -23,7 +23,19 @@ func (app *Application) FinishGame(usr *user.User, w http.ResponseWriter, r *htt if err := app.saveGameState(gm); err != nil { fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "couldn't save game state") + return } gm.FinishGame() + + if err := app.saveGameState(gm); err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "couldn't save game state") + return + } + + fmt.Fprintf(w, "ok") } diff --git a/server/src/application/resetGame.go b/server/src/application/resetGame.go index 50d60d7..a230a6a 100644 --- a/server/src/application/resetGame.go +++ b/server/src/application/resetGame.go @@ -22,4 +22,13 @@ func (app *Application) ResetGame(usr *user.User, w http.ResponseWriter, r *http } gm.ResetGame() + + if err := app.saveGameState(gm); err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "couldn't save game state") + return + } + + fmt.Fprintf(w, "ok") } diff --git a/server/src/application/startGame.go b/server/src/application/startGame.go index 3ae73e8..67180d6 100644 --- a/server/src/application/startGame.go +++ b/server/src/application/startGame.go @@ -22,4 +22,13 @@ func (app *Application) StartGame(usr *user.User, w http.ResponseWriter, r *http } gm.StartGame() + + if err := app.saveGameState(gm); err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "couldn't save game state") + return + } + + fmt.Fprintf(w, "ok") } diff --git a/server/src/game/getGameState.go b/server/src/game/getGameState.go index a37a3b4..3ded7c9 100644 --- a/server/src/game/getGameState.go +++ b/server/src/game/getGameState.go @@ -5,6 +5,7 @@ func (gm *Game) GetGameState() GameStateJson { defer gm.mu.Unlock() stateJson := GameStateJson{ + State: gm.state, Scores: make(map[string]int, 0), QuotesPlayed: make([]string, 0), } diff --git a/server/src/game/setGameState.go b/server/src/game/setGameState.go index 67a7a80..2216450 100644 --- a/server/src/game/setGameState.go +++ b/server/src/game/setGameState.go @@ -1,6 +1,13 @@ package game func (gm *Game) SetGameState(stateJson *GameStateJson) { + if stateJson.State == "idle" || + stateJson.State == "collect" || + stateJson.State == "final" { + + gm.state = stateJson.State + gm.phase = "" + } for id, score := range stateJson.Scores { gm.setScore(id, score) } diff --git a/server/src/game/startGame.go b/server/src/game/startGame.go index 3803ca2..18f0d99 100644 --- a/server/src/game/startGame.go +++ b/server/src/game/startGame.go @@ -6,7 +6,6 @@ import ( func (gm *Game) StartGame() { go gm.runCountdown() - // go gm.runRound() } func (gm *Game) ResetGame() { diff --git a/server/src/game/struct.go b/server/src/game/struct.go index a4a8af2..a8d1549 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -101,6 +101,7 @@ type GameInfoJson struct { } type GameStateJson struct { + State string `json:"state"` Scores map[string]int `json:"scores"` QuotesPlayed []string `json:"quotesPlayed"` }