chore: Refactor saving session data to prevent saving older sessions

This commit is contained in:
Frederico Santos 2024-06-02 19:25:25 +01:00
parent 0526c7a0f1
commit 5b96cd6082

View File

@ -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"
) )
@ -120,8 +121,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
} }