From cb071d9d9b6b1b16125d436a3ee86166e0f05f34 Mon Sep 17 00:00:00 2001 From: Up Date: Wed, 8 May 2024 01:02:57 +0200 Subject: [PATCH] fix continue --- api/account/info.go | 34 +++++++++++----------------------- db/savedata.go | 10 ++++++++++ 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/api/account/info.go b/api/account/info.go index fa491a8..f0415d6 100644 --- a/api/account/info.go +++ b/api/account/info.go @@ -18,11 +18,7 @@ package account import ( - "fmt" - "os" - "strconv" - "time" - + "github.com/pagefaultgames/rogueserver/db" "github.com/pagefaultgames/rogueserver/defs" ) @@ -33,24 +29,16 @@ type InfoResponse struct { // /account/info - get account info func Info(username string, uuid []byte) (InfoResponse, error) { - var latestSave time.Time - latestSaveID := -1 - for id := range defs.SessionSlotCount { - fileName := "session" - if id != 0 { - fileName += strconv.Itoa(id) - } - - stat, err := os.Stat(fmt.Sprintf("userdata/%x/%s.pzs", uuid, fileName)) - if err != nil { - continue - } - - if stat.ModTime().After(latestSave) { - latestSave = stat.ModTime() - latestSaveID = id - } + response := InfoResponse{Username: username, LastSessionSlot: -1} + + slot, err := db.GetLatestSessionSaveDataSlot(uuid) + if err != nil { + return response, err + } + + if slot >= defs.SessionSlotCount { + response.LastSessionSlot = -1 } - return InfoResponse{Username: username, LastSessionSlot: latestSaveID}, nil + return response, nil } diff --git a/db/savedata.go b/db/savedata.go index fb36c65..1f48953 100644 --- a/db/savedata.go +++ b/db/savedata.go @@ -79,6 +79,16 @@ func ReadSessionSaveData(uuid []byte, slot int) (defs.SessionSaveData, error) { return save, err } +func GetLatestSessionSaveDataSlot(uuid []byte) (int, error) { + var slot int + err := handle.QueryRow("SELECT slot FROM sessionSaveData WHERE uuid = ? ORDER BY timestamp DESC, slot ASC LIMIT 1", uuid).Scan(&slot) + if err != nil { + return -1, err + } + + return slot, nil +} + func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) error { var buf bytes.Buffer