Add session save slots

pull/1/head
Flashfyre 8 months ago
parent 2938dbc19d
commit 12137bc3b7

@ -5,21 +5,22 @@ import (
"crypto/rand" "crypto/rand"
"database/sql" "database/sql"
"encoding/base64" "encoding/base64"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
"strconv"
"time"
"github.com/Flashfyre/pokerogue-server/db" "github.com/Flashfyre/pokerogue-server/db"
"golang.org/x/crypto/argon2" "golang.org/x/crypto/argon2"
) )
const ( const (
argonTime = 1 argonTime = 1
argonMemory = 256*1024 argonMemory = 256 * 1024
argonThreads = 4 argonThreads = 4
argonKeyLength = 32 argonKeyLength = 32
) )
@ -27,9 +28,9 @@ var isValidUsername = regexp.MustCompile(`^\w{1,16}$`).MatchString
// /account/info - get account info // /account/info - get account info
type AccountInfoResponse struct{ type AccountInfoResponse struct {
Username string `json:"username"` Username string `json:"username"`
HasGameSession bool `json:"hasGameSession"` LastSessionSlot int `json:"lastSessionSlot"`
} }
func (s *Server) HandleAccountInfo(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleAccountInfo(w http.ResponseWriter, r *http.Request) {
@ -45,9 +46,26 @@ func (s *Server) HandleAccountInfo(w http.ResponseWriter, r *http.Request) {
return return
} }
_, err = os.Stat("userdata/" + hex.EncodeToString(uuid) + "/session.pzs") var latestSaveTime time.Time
latestSaveId := -1
for id := range 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(latestSaveTime) {
latestSaveTime = stat.ModTime()
latestSaveId = id
}
}
response, err := json.Marshal(AccountInfoResponse{Username: username, HasGameSession: err == nil}) response, err := json.Marshal(AccountInfoResponse{Username: username, LastSessionSlot: latestSaveId})
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
return return

@ -8,10 +8,13 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"strconv"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
) )
const sessionSlotCount = 3
// /savedata/get - get save data // /savedata/get - get save data
func (s *Server) HandleSavedataGet(w http.ResponseWriter, r *http.Request) { func (s *Server) HandleSavedataGet(w http.ResponseWriter, r *http.Request) {
@ -60,7 +63,23 @@ func (s *Server) HandleSavedataGet(w http.ResponseWriter, r *http.Request) {
w.Write(saveJson) w.Write(saveJson)
case "1": // Session case "1": // Session
save, err := os.ReadFile("userdata/" + hexUuid + "/session.pzs") slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return
}
if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return
}
fileName := "session"
if slotId != 0 {
fileName += strconv.Itoa(slotId)
}
save, err := os.ReadFile(fmt.Sprintf("userdata/%s/%s.pzs", hexUuid, fileName))
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to read save file: %s", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("failed to read save file: %s", err), http.StatusInternalServerError)
return return
@ -152,6 +171,22 @@ func (s *Server) HandleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
return return
} }
case "1": // Session case "1": // Session
slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return
}
if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return
}
fileName := "session"
if slotId != 0 {
fileName += strconv.Itoa(slotId)
}
var session SessionSaveData var session SessionSaveData
err = json.NewDecoder(r.Body).Decode(&session) err = json.NewDecoder(r.Body).Decode(&session)
if err != nil { if err != nil {
@ -180,7 +215,7 @@ func (s *Server) HandleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
return return
} }
err = os.WriteFile("userdata/"+hexUuid+"/session.pzs", compressed, 0644) err = os.WriteFile(fmt.Sprintf("userdata/%s/session%s.pzs", hexUuid, fileName), compressed, 0644)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
return return
@ -212,7 +247,23 @@ func (s *Server) HandleSavedataDelete(w http.ResponseWriter, r *http.Request) {
return return
} }
case "1": // Session case "1": // Session
err := os.Remove("userdata/" + hexUuid + "/session.pzs") slotId, err := strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil {
http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest)
return
}
if slotId < 0 || slotId >= sessionSlotCount {
http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest)
return
}
fileName := "session"
if slotId != 0 {
fileName += strconv.Itoa(slotId)
}
err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hexUuid, fileName))
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
return return

Loading…
Cancel
Save