Refactor saving session and system data to prevent saving older saves on previous waves (#26)

* chore: Refactor saving session data to prevent saving older sessions

* chore: Refactor saving system data to prevent saving older sessions

* Refactor saving session and system data to prevent saving older versions
pull/27/head
Frederico Santos 5 months ago committed by GitHub
parent 0526c7a0f1
commit 87e8f00cd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -20,6 +20,7 @@ package db
import ( import (
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"errors"
"github.com/pagefaultgames/rogueserver/defs" "github.com/pagefaultgames/rogueserver/defs"
) )
@ -69,8 +70,13 @@ func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
} }
func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error { func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
systemData, err := ReadSystemSaveData(uuid)
if err == nil && systemData.Timestamp > data.Timestamp {
return errors.New("attempted to save an older system save")
}
var buf bytes.Buffer var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(data) err = gob.NewEncoder(&buf).Encode(data)
if err != nil { if err != nil {
return err return err
} }
@ -120,8 +126,13 @@ func GetLatestSessionSaveDataSlot(uuid []byte) (int, error) {
} }
func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) error { func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) error {
session, err := ReadSessionSaveData(uuid, slot)
if err == nil && session.Seed == data.Seed && session.WaveIndex > data.WaveIndex {
return errors.New("attempted to save an older session")
}
var buf bytes.Buffer var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(data) err = gob.NewEncoder(&buf).Encode(data)
if err != nil { if err != nil {
return err return err
} }

Loading…
Cancel
Save