Functions for Database interactions

pull/31/head
Frutescens 6 months ago
parent 1f05a7bda5
commit 8577d7978b

@ -30,6 +30,8 @@ import (
"github.com/pagefaultgames/rogueserver/api/savedata" "github.com/pagefaultgames/rogueserver/api/savedata"
"github.com/pagefaultgames/rogueserver/db" "github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs" "github.com/pagefaultgames/rogueserver/defs"
"/api/savedata"
) )
/* /*
@ -574,7 +576,7 @@ func handleGetRunHistory(w http.ResponseWriter, r *http.Request) {
} }
var runHistory any var runHistory any
runHistory, err = savadata.Get(uuid,1); runHistory, err = savedata.GetRunHistoryData(uuid);
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound) http.Error(w, err.Error(), http.StatusNotFound)
@ -585,6 +587,7 @@ func handleGetRunHistory(w http.ResponseWriter, r *http.Request) {
httpError(w, r, err, http.StatusInternalServerError) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
writeJSON(w, r, runHistory) writeJSON(w, r, runHistory)
} }
@ -602,7 +605,7 @@ func handleRunHistory(w http.ResponseWriter, r *http.Request) {
return return
} }
err = db.UpdateRunHistory(uuid, data) err = savedata.UpdateRunHistoryData(uuid, data)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusInternalServerError) httpError(w, r, err, http.StatusInternalServerError)
return return

@ -153,3 +153,36 @@ func DeleteSessionSaveData(uuid []byte, slot int) error {
return nil 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 SessionHistoryResult int
type RunHistoryData map[int]interface{}

Loading…
Cancel
Save