From 4812f10026d37ea8b16880f8f3e46ec6f4188f98 Mon Sep 17 00:00:00 2001
From: Brendan F <EpicWolverine@users.noreply.github.com>
Date: Mon, 19 Feb 2024 22:21:42 -0800
Subject: Add stats converter

---
 src/app.tsx | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

(limited to 'src/app.tsx')

diff --git a/src/app.tsx b/src/app.tsx
index 7cc348a..7c4c81d 100644
--- a/src/app.tsx
+++ b/src/app.tsx
@@ -56,6 +56,7 @@ function App() {
   }
 
   let stats = JSON.parse(localStorage.getItem("stats") || "{}");
+  let statsVersion = JSON.parse(localStorage.getItem("version") || "1");
 
   React.useEffect(() => {
     if (Array.isArray(stats)) {
@@ -84,6 +85,36 @@ function App() {
         solution: todaysSolution,
       });
     }
+    const currentVersion = 2;
+    if (firstRun) {
+      statsVersion = currentVersion
+    }
+    else if (statsVersion < currentVersion) {
+      statsVersion = currentVersion;
+      if (Array.isArray(stats)) {
+        for (let index = 0; index < stats.length; index++) {
+          const newGuesses: GuessType[] = [];
+          for (let guessIndex = 0; guessIndex < stats[index].guesses.length; guessIndex++) {
+            const guess = stats[index].guesses[guessIndex];
+            if (guess.skipped !== undefined) {
+                let state = undefined;
+                if (guess.skipped) {
+                  state = GuessState.Skipped;
+                } else if (guess.isCorrect){
+                  state = GuessState.Correct;
+                } else if (guess.isCorrect === false){
+                  state = GuessState.Incorrect;
+                }
+                newGuesses.push({
+                  song: guess.song,
+                  state: state,
+                } as GuessType);
+              }
+          }
+          stats[index].guesses = newGuesses;
+        }
+      }
+    }
   }, []);
 
   React.useEffect(() => {
@@ -99,6 +130,10 @@ function App() {
     localStorage.setItem("stats", JSON.stringify(stats));
   }, [stats]);
 
+  React.useEffect(() => {
+    localStorage.setItem("version", JSON.stringify(statsVersion));
+  }, [statsVersion]);
+
   const [isInfoPopUpOpen, setIsInfoPopUpOpen] =
     React.useState<boolean>(firstRun);
 
-- 
cgit v1.2.3

