feat: save and load game state

This commit is contained in:
Settel 2022-04-03 21:20:32 +02:00
parent 9bd56b9232
commit 4fca931673
9 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1 @@
{"authcode":"","name":"Crabbe","role":"player","game":"e24444aa-8a18-48aa-a36d-8f84620726f8"}

View File

@ -22,4 +22,12 @@ func (app *Application) CollectQuotes(usr *user.User, w http.ResponseWriter, r *
} }
gm.CollectQuotes() 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")
} }

View File

@ -23,7 +23,19 @@ func (app *Application) FinishGame(usr *user.User, w http.ResponseWriter, r *htt
if err := app.saveGameState(gm); err != nil { if err := app.saveGameState(gm); err != nil {
fmt.Println(err) fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "couldn't save game state")
return
} }
gm.FinishGame() 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")
} }

View File

@ -22,4 +22,13 @@ func (app *Application) ResetGame(usr *user.User, w http.ResponseWriter, r *http
} }
gm.ResetGame() 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")
} }

View File

@ -22,4 +22,13 @@ func (app *Application) StartGame(usr *user.User, w http.ResponseWriter, r *http
} }
gm.StartGame() 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")
} }

View File

@ -5,6 +5,7 @@ func (gm *Game) GetGameState() GameStateJson {
defer gm.mu.Unlock() defer gm.mu.Unlock()
stateJson := GameStateJson{ stateJson := GameStateJson{
State: gm.state,
Scores: make(map[string]int, 0), Scores: make(map[string]int, 0),
QuotesPlayed: make([]string, 0), QuotesPlayed: make([]string, 0),
} }

View File

@ -1,6 +1,13 @@
package game package game
func (gm *Game) SetGameState(stateJson *GameStateJson) { 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 { for id, score := range stateJson.Scores {
gm.setScore(id, score) gm.setScore(id, score)
} }

View File

@ -6,7 +6,6 @@ import (
func (gm *Game) StartGame() { func (gm *Game) StartGame() {
go gm.runCountdown() go gm.runCountdown()
// go gm.runRound()
} }
func (gm *Game) ResetGame() { func (gm *Game) ResetGame() {

View File

@ -101,6 +101,7 @@ type GameInfoJson struct {
} }
type GameStateJson struct { type GameStateJson struct {
State string `json:"state"`
Scores map[string]int `json:"scores"` Scores map[string]int `json:"scores"`
QuotesPlayed []string `json:"quotesPlayed"` QuotesPlayed []string `json:"quotesPlayed"`
} }