Merge 77b244c51ef7352a7978a430ece357e63bca8b70 into 4cac6b6ce8fbdcaf6ef640b8c0d80a8afe2c9a37

This commit is contained in:
Mumble 2024-07-06 00:43:01 +00:00 committed by GitHub
commit 3952c35c3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 86 additions and 0 deletions

View File

@ -55,6 +55,10 @@ func Init(mux *http.ServeMux) error {
mux.HandleFunc("/savedata/session/{action}", handleSession)
mux.HandleFunc("/savedata/system/{action}", handleSystem)
//run history
mux.HandleFunc("GET /savedata/runHistory", handleGetRunHistory)
mux.HandleFunc("POST /savedata/runHistory", handleRunHistory)
// new session
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)

View File

@ -259,6 +259,51 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
}
}
//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 = db.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 defs.RunHistoryData
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 = db.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"`

View File

@ -97,6 +97,8 @@ func setupDb(tx *sql.Tx) error {
// MIGRATION 002
`DROP TABLE accountCompensations`,
`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 {

View File

@ -152,3 +152,36 @@ func RetrievePlaytime(uuid []byte) (int, error) {
return playtime, 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
}

View File

@ -153,3 +153,5 @@ type SessionHistoryData struct {
}
type SessionHistoryResult int
type RunHistoryData map[string]interface{}