mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-04-02 02:57:15 +08:00
Merge 8577d7978b2e546b98213b804236699c4b68f6d4 into 81b316f4c9ec6bab177bab0a4f26d28ba9460bc2
This commit is contained in:
commit
1fc2cf2ce2
@ -58,6 +58,10 @@ func Init(mux *http.ServeMux) error {
|
||||
mux.HandleFunc("POST /savedata/clear", legacyHandleSaveData) // TODO use clearSessionData
|
||||
mux.HandleFunc("GET /savedata/newclear", legacyHandleNewClear)
|
||||
|
||||
//run history
|
||||
mux.HandleFunc("GET /savedata/runHistory", handleGetRunHistory)
|
||||
mux.HandleFunc("POST /savedata/runHistory", handleRunHistory)
|
||||
|
||||
// new session
|
||||
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)
|
||||
mux.HandleFunc("POST /savedata/system/verify", handleSystemVerify)
|
||||
|
@ -30,6 +30,8 @@ import (
|
||||
"github.com/pagefaultgames/rogueserver/api/savedata"
|
||||
"github.com/pagefaultgames/rogueserver/db"
|
||||
"github.com/pagefaultgames/rogueserver/defs"
|
||||
|
||||
"/api/savedata"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -565,6 +567,52 @@ func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, r, save)
|
||||
}
|
||||
|
||||
//functions for run history
|
||||
func handleGetRunHistory(w http.ResponseWriter, r *http.Request) {
|
||||
uuid, err := uuidFromRequest(r)
|
||||
if err != nil {
|
||||
httpError(w, r, err, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var runHistory any
|
||||
runHistory, err = savedata.GetRunHistoryData(uuid);
|
||||
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
httpError(w, r, err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, r, runHistory)
|
||||
}
|
||||
|
||||
func handleRunHistory(w http.ResponseWriter, r *http.Request) {
|
||||
uuid, err := uuidFromRequest(r)
|
||||
if err != nil {
|
||||
httpError(w, r, err, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var data string
|
||||
err = json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = savedata.UpdateRunHistoryData(uuid, data)
|
||||
if err != nil {
|
||||
httpError(w, r, err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
type CombinedSaveData struct {
|
||||
System defs.SystemSaveData `json:"system"`
|
||||
Session defs.SessionSaveData `json:"session"`
|
||||
|
5
db/db.go
5
db/db.go
@ -92,6 +92,11 @@ func setupDb(tx *sql.Tx) error {
|
||||
|
||||
`ALTER TABLE sessions DROP COLUMN IF EXISTS active`,
|
||||
`CREATE TABLE IF NOT EXISTS activeClientSessions (uuid BINARY(16) NOT NULL PRIMARY KEY, clientSessionId VARCHAR(32) NOT NULL, FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE)`,
|
||||
|
||||
|
||||
// ----------------------------------
|
||||
//Migration 002
|
||||
`CREATE TABLE IF NOT EXISTS runHistoryData (uuid BINARY(16) PRIMARY KEY, data LONGBLOB, timestamp TIMESTAMP, FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE)`,
|
||||
}
|
||||
|
||||
for _, q := range queries {
|
||||
|
@ -153,3 +153,36 @@ func DeleteSessionSaveData(uuid []byte, slot int) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetRunHistoryData(uuid []byte) (defs.RunHistoryData, error) {
|
||||
var runHistory defs.RunHistoryData
|
||||
var err error
|
||||
var data []byte
|
||||
|
||||
err = handle.QueryRow("SELECT data FROM runHistoryData WHERE uuid = ?", uuid).Scan(&data)
|
||||
if err != nil {
|
||||
return runHistory, err
|
||||
}
|
||||
err = gob.NewDecoder(bytes.NewReader(data)).Decode(&runHistory)
|
||||
if err != nil {
|
||||
return runHistory, err
|
||||
}
|
||||
|
||||
return runHistory, err
|
||||
}
|
||||
|
||||
func UpdateRunHistoryData(uuid []byte, data defs.RunHistoryData) error {
|
||||
var buf bytes.Buffer
|
||||
var err error
|
||||
|
||||
err = gob.NewEncoder(&buf).Encode(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = handle.Exec("INSERT INTO runHistoryData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE data = ?, timestamp = UTC_TIMESTAMP()", uuid, buf.Bytes(), buf.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -139,3 +139,5 @@ type SessionHistoryData struct {
|
||||
}
|
||||
|
||||
type SessionHistoryResult int
|
||||
|
||||
type RunHistoryData map[int]interface{}
|
||||
|
Loading…
x
Reference in New Issue
Block a user