From 7e30d640c35430dfb4a5f94ff37950ae13e07464 Mon Sep 17 00:00:00 2001
From: Frutescens <info@laptop>
Date: Tue, 4 Jun 2024 23:06:23 -0700
Subject: [PATCH 1/3] Server Side code for Run History

---
 api/endpoints.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 db/db.go         |  5 +++++
 db/savedata.go   | 33 +++++++++++++++++++++++++++++++++
 defs/savedata.go |  2 ++
 4 files changed, 86 insertions(+)

diff --git a/api/endpoints.go b/api/endpoints.go
index 92d4570..aa37d74 100644
--- a/api/endpoints.go
+++ b/api/endpoints.go
@@ -565,6 +565,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 = 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 0e21c9c..4c589e5 100644
--- a/db/db.go
+++ b/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 {
diff --git a/db/savedata.go b/db/savedata.go
index bb593b8..8df77f2 100644
--- a/db/savedata.go
+++ b/db/savedata.go
@@ -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
+}
\ No newline at end of file
diff --git a/defs/savedata.go b/defs/savedata.go
index 1d63603..f434e1b 100644
--- a/defs/savedata.go
+++ b/defs/savedata.go
@@ -139,3 +139,5 @@ type SessionHistoryData struct {
 }
 
 type SessionHistoryResult int
+
+type RunHistoryData map[string]interface{}

From c0e302db93aecbb0c75a12ab65bfd36293322e8b Mon Sep 17 00:00:00 2001
From: Frutescens <info@laptop>
Date: Tue, 4 Jun 2024 23:17:32 -0700
Subject: [PATCH 2/3] Forgot a file

---
 api/common.go | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/api/common.go b/api/common.go
index e1666f5..325ab6a 100644
--- a/api/common.go
+++ b/api/common.go
@@ -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)

From d399c735976c3c7be954ef8f286dbb390f64fe32 Mon Sep 17 00:00:00 2001
From: Mumble <kimjoanne@protonmail.com>
Date: Fri, 7 Jun 2024 14:39:57 -0700
Subject: [PATCH 3/3] Update endpoints.go

Removed empty newline @ 578
---
 api/endpoints.go | 1 -
 1 file changed, 1 deletion(-)

diff --git a/api/endpoints.go b/api/endpoints.go
index aa37d74..1381265 100644
--- a/api/endpoints.go
+++ b/api/endpoints.go
@@ -575,7 +575,6 @@ func handleGetRunHistory(w http.ResponseWriter, r *http.Request) {
 
 	var runHistory any
 	runHistory, err = db.GetRunHistoryData(uuid);
-
 	if errors.Is(err, sql.ErrNoRows) {
 		http.Error(w, err.Error(), http.StatusNotFound)
 		return