From 5b96cd6082cbd7cefb957a11994c2184a7e0a158 Mon Sep 17 00:00:00 2001 From: Frederico Santos Date: Sun, 2 Jun 2024 19:25:25 +0100 Subject: [PATCH] chore: Refactor saving session data to prevent saving older sessions --- db/savedata.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/db/savedata.go b/db/savedata.go index bb792c5..021602f 100644 --- a/db/savedata.go +++ b/db/savedata.go @@ -20,6 +20,7 @@ package db import ( "bytes" "encoding/gob" + "errors" "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 { + 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 - err := gob.NewEncoder(&buf).Encode(data) + err = gob.NewEncoder(&buf).Encode(data) if err != nil { return err }