chore: Update savedata API to handle session out of date errors

This commit is contained in:
Frederico Santos 2024-06-22 20:30:52 +01:00
parent 872c03d14b
commit 16cc0269f6
2 changed files with 43 additions and 1 deletions

View File

@ -197,7 +197,13 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
return
}
existingSave, err := savedata.GetSession(uuid, slot)
if err == nil {
if existingSave.Seed == session.Seed && existingSave.WaveIndex > session.WaveIndex {
httpError(w, r, fmt.Errorf("session out of date: existing wave index is greater"), http.StatusBadRequest)
return
}
}
err = savedata.UpdateSession(uuid, slot, session)
if err != nil {
httpError(w, r, fmt.Errorf("failed to put session data: %s", err), http.StatusInternalServerError)
@ -306,6 +312,23 @@ func handleUpdateAll(w http.ResponseWriter, r *http.Request) {
}
}
existingPlaytime, err := db.RetrievePlaytime(uuid)
playtime := data.System.GameStats.(map[string]interface{})["playTime"].(float64)
if err == nil {
if float64(existingPlaytime) > playtime {
httpError(w, r, fmt.Errorf("session out of date: existing playtime is greater"), http.StatusBadRequest)
return
}
}
existingSave, err := savedata.GetSession(uuid, data.SessionSlotId)
if err == nil {
if existingSave.Seed == data.Session.Seed && existingSave.WaveIndex > data.Session.WaveIndex {
httpError(w, r, fmt.Errorf("session out of date: existing wave index is greater"), http.StatusBadRequest)
return
}
}
err = savedata.Update(uuid, data.SessionSlotId, data.Session)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
@ -380,6 +403,15 @@ func handleSystem(w http.ResponseWriter, r *http.Request) {
return
}
existingPlaytime, err := db.RetrievePlaytime(uuid)
playtime := system.GameStats.(map[string]interface{})["playTime"].(float64)
if err == nil {
if float64(existingPlaytime) > playtime {
httpError(w, r, fmt.Errorf("session out of date: existing playtime is greater"), http.StatusBadRequest)
return
}
}
err = savedata.UpdateSystem(uuid, system)
if err != nil {
httpError(w, r, fmt.Errorf("failed to put system data: %s", err), http.StatusInternalServerError)

View File

@ -142,3 +142,13 @@ func DeleteSessionSaveData(uuid []byte, slot int) error {
return nil
}
func RetrievePlaytime(uuid []byte) (int, error) {
var playtime int
err := handle.QueryRow("SELECT playTime FROM accountStats WHERE uuid = ?", uuid).Scan(&playtime)
if err != nil {
return 0, err
}
return playtime, nil
}