diff --git a/api/common.go b/api/common.go index 96119f3..1e1ef74 100644 --- a/api/common.go +++ b/api/common.go @@ -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) diff --git a/api/endpoints.go b/api/endpoints.go index 2eccdd9..9472efe 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -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"` diff --git a/db/db.go b/db/db.go index a91a94a..10af2b8 100644 --- a/db/db.go +++ b/db/db.go @@ -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 { diff --git a/db/savedata.go b/db/savedata.go index 10256a3..f8bd56e 100644 --- a/db/savedata.go +++ b/db/savedata.go @@ -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 +} diff --git a/defs/savedata.go b/defs/savedata.go index 1ef6b80..95554e3 100644 --- a/defs/savedata.go +++ b/defs/savedata.go @@ -153,3 +153,5 @@ type SessionHistoryData struct { } type SessionHistoryResult int + +type RunHistoryData map[string]interface{}