Add cloud save data
parent
e05f6f752b
commit
a19280d02c
@ -0,0 +1,53 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/Flashfyre/pokerogue-server/db"
|
||||
)
|
||||
|
||||
func GetUsernameFromRequest(request *http.Request) (string, error) {
|
||||
if request.Header.Get("Authorization") == "" {
|
||||
return "", fmt.Errorf("missing token")
|
||||
}
|
||||
|
||||
token, err := base64.StdEncoding.DecodeString(request.Header.Get("Authorization"))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode token: %s", err)
|
||||
}
|
||||
|
||||
if len(token) != 32 {
|
||||
return "", fmt.Errorf("invalid token length: got %d, expected 32", len(token))
|
||||
}
|
||||
|
||||
username, err := db.GetUsernameFromToken(token)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to validate token: %s", err)
|
||||
}
|
||||
|
||||
return username, nil
|
||||
}
|
||||
|
||||
func GetUuidFromRequest(request *http.Request) ([]byte, error) {
|
||||
if request.Header.Get("Authorization") == "" {
|
||||
return nil, fmt.Errorf("missing token")
|
||||
}
|
||||
|
||||
token, err := base64.StdEncoding.DecodeString(request.Header.Get("Authorization"))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode token: %s", err)
|
||||
}
|
||||
|
||||
if len(token) != 32 {
|
||||
return nil, fmt.Errorf("invalid token length: got %d, expected 32", len(token))
|
||||
}
|
||||
|
||||
uuid, err := db.GetUuidFromToken(token)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to validate token: %s", err)
|
||||
}
|
||||
|
||||
return uuid, nil
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package api
|
||||
|
||||
type SystemSaveData struct {
|
||||
TrainerId int `json:"trainerId"`
|
||||
SecretId int `json:"secretId"`
|
||||
DexData DexData `json:"dexData"`
|
||||
Unlocks Unlocks `json:"unlocks"`
|
||||
AchvUnlocks AchvUnlocks `json:"achvUnlocks"`
|
||||
VoucherUnlocks VoucherUnlocks `json:"voucherUnlocks"`
|
||||
VoucherCounts VoucherCounts `json:"voucherCounts"`
|
||||
Eggs []EggData `json:"eggs"`
|
||||
GameVersion string `json:"gameVersion"`
|
||||
Timestamp int `json:"timestamp"`
|
||||
}
|
||||
|
||||
type DexData map[int]DexEntry
|
||||
|
||||
type DexEntry struct {
|
||||
SeenAttr interface{} `json:"seenAttr"` // integer or string
|
||||
CaughtAttr interface{} `json:"caughtAttr"` // integer or string
|
||||
SeenCount int `json:"seenCount"`
|
||||
CaughtCount int `json:"caughtCount"`
|
||||
HatchedCount int `json:"hatchedCount"`
|
||||
Ivs []int `json:"ivs"`
|
||||
}
|
||||
|
||||
type Unlocks map[int]bool
|
||||
|
||||
type AchvUnlocks map[string]int
|
||||
|
||||
type VoucherUnlocks map[string]int
|
||||
|
||||
type VoucherCounts map[string]int
|
||||
|
||||
type EggData struct {
|
||||
Id int `json:"id"`
|
||||
GachaType GachaType `json:"gachaType"`
|
||||
HatchWaves int `json:"hatchWaves"`
|
||||
Timestamp int `json:"timestamp"`
|
||||
}
|
||||
|
||||
type GachaType int
|
||||
|
||||
type SessionSaveData struct {
|
||||
Seed string `json:"seed"`
|
||||
GameMode GameMode `json:"gameMode"`
|
||||
Party []PokemonData `json:"party"`
|
||||
EnemyParty []PokemonData `json:"enemyParty"`
|
||||
EnemyField []PokemonData `json:"enemyField"`
|
||||
Modifiers []PersistentModifierData `json:"modifiers"`
|
||||
EnemyModifiers []PersistentModifierData `json:"enemyModifiers"`
|
||||
Arena ArenaData `json:"arena"`
|
||||
PokeballCounts PokeballCounts `json:"pokeballCounts"`
|
||||
Money int `json:"money"`
|
||||
WaveIndex int `json:"waveIndex"`
|
||||
BattleType BattleType `json:"battleType"`
|
||||
Trainer TrainerData `json:"trainer"`
|
||||
GameVersion string `json:"gameVersion"`
|
||||
Timestamp int `json:"timestamp"`
|
||||
}
|
||||
|
||||
type GameMode int
|
||||
|
||||
type PokemonData interface{}
|
||||
|
||||
type PersistentModifierData interface{}
|
||||
|
||||
type ArenaData interface{}
|
||||
|
||||
type PokeballCounts map[string]int
|
||||
|
||||
type BattleType int
|
||||
|
||||
type TrainerData interface{}
|
@ -1,30 +1,221 @@
|
||||
package api
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
// /savedata/get - get save data
|
||||
"github.com/klauspost/compress/zstd"
|
||||
)
|
||||
|
||||
type SavedataGetRequest struct{}
|
||||
type SavedataGetResponse struct{}
|
||||
// /savedata/get - get save data
|
||||
|
||||
func (s *Server) HandleSavedataGet(w http.ResponseWriter, r *http.Request) {
|
||||
uuid, err := GetUuidFromRequest(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
hexUuid := hex.EncodeToString(uuid)
|
||||
|
||||
switch r.URL.Query().Get("datatype") {
|
||||
case "0": // System
|
||||
save, err := os.ReadFile("userdata/" + hexUuid + "/system.pzs")
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to read save file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// /savedata/update - update save data
|
||||
zstdReader, err := zstd.NewReader(nil)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create zstd reader: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
decompressed, err := zstdReader.DecodeAll(save, nil)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to decompress save file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
gobDecoderBuf := bytes.NewBuffer(decompressed)
|
||||
|
||||
var system SystemSaveData
|
||||
err = gob.NewDecoder(gobDecoderBuf).Decode(&system)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to deserialize save: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
saveJson, err := json.Marshal(system)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(saveJson)
|
||||
case "1": // Session
|
||||
save, err := os.ReadFile("userdata/" + hexUuid + "/session.pzs")
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to read save file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
type SavedataUpdateRequest struct{}
|
||||
type SavedataUpdateResponse struct{}
|
||||
zstdReader, err := zstd.NewReader(nil)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create zstd reader: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
decompressed, err := zstdReader.DecodeAll(save, nil)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to decompress save file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
gobDecoderBuf := bytes.NewBuffer(decompressed)
|
||||
|
||||
var session SessionSaveData
|
||||
err = gob.NewDecoder(gobDecoderBuf).Decode(&session)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to deserialize save: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
saveJson, err := json.Marshal(session)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(saveJson)
|
||||
default:
|
||||
http.Error(w, "invalid data type", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// /savedata/update - update save data
|
||||
|
||||
func (s *Server) HandleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
uuid, err := GetUuidFromRequest(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
hexUuid := hex.EncodeToString(uuid)
|
||||
|
||||
switch r.URL.Query().Get("datatype") {
|
||||
case "0": // System
|
||||
var system SystemSaveData
|
||||
err = json.NewDecoder(r.Body).Decode(&system)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// /savedata/delete - delete save date
|
||||
var gobBuffer bytes.Buffer
|
||||
err = gob.NewEncoder(&gobBuffer).Encode(system)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
zstdWriter, err := zstd.NewWriter(nil)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
type SavedataDeleteRequest struct{}
|
||||
type SavedataDeleteResponse struct{}
|
||||
compressed := zstdWriter.EncodeAll(gobBuffer.Bytes(), nil)
|
||||
|
||||
err = os.MkdirAll("userdata/"+hexUuid, 0755)
|
||||
if !os.IsExist(err) {
|
||||
http.Error(w, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = os.WriteFile("userdata/"+hexUuid+"/system.pzs", compressed, 0644)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
case "1": // Session
|
||||
var session SessionSaveData
|
||||
err = json.NewDecoder(r.Body).Decode(&session)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var gobBuffer bytes.Buffer
|
||||
err = gob.NewEncoder(&gobBuffer).Encode(session)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
zstdWriter, err := zstd.NewWriter(nil)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
compressed := zstdWriter.EncodeAll(gobBuffer.Bytes(), nil)
|
||||
|
||||
err = os.MkdirAll("userdata/"+hexUuid, 0755)
|
||||
if !os.IsExist(err) {
|
||||
http.Error(w, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = os.WriteFile("userdata/"+hexUuid+"/session.pzs", compressed, 0644)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
default:
|
||||
http.Error(w, "invalid data type", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// /savedata/delete - delete save date
|
||||
|
||||
func (s *Server) HandleSavedataDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid, err := GetUuidFromRequest(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
hexUuid := hex.EncodeToString(uuid)
|
||||
|
||||
switch r.URL.Query().Get("datatype") {
|
||||
case "0": // System
|
||||
err := os.Remove("userdata/"+hexUuid+"/system.pzs")
|
||||
if !os.IsNotExist(err) {
|
||||
http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
case "1": // Session
|
||||
err := os.Remove("userdata/"+hexUuid+"/session.pzs")
|
||||
if !os.IsNotExist(err) {
|
||||
http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
default:
|
||||
http.Error(w, "invalid data type", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
Loading…
Reference in New Issue