From 4d6a87c6ec4b637f7357b90959e1bec2f7c1bff6 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Wed, 23 Feb 2022 12:00:26 +0100 Subject: [PATCH] Load/save previous input --- index.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d4bea3c..dd4d1fe 100644 --- a/index.js +++ b/index.js @@ -28,9 +28,13 @@ function mulberry32(a) { const LOCALSTORAGE_KEY = "qwörtle.data"; let saveData = JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY) || "{}"); +function saveLocalStorage() { + localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(saveData)); +} -const daysSinceEpoch = Math.floor(new Date()/8.64e7); +const daysSinceEpoch = Math.floor(new Date()/8.64e7) + 1270; const startDay = 19046; +const daysSinceStart = daysSinceEpoch - startDay + 1; const rng = mulberry32(daysSinceEpoch); const chosenWords = []; for (let i = 0; i < 4; i++) { @@ -105,6 +109,12 @@ function processKey(e) { } } guessed.push(input); + if (daysSinceEpoch in saveData) { + saveData[daysSinceEpoch].guesses.push(input); + } else { + saveData[daysSinceEpoch] = { guesses: [input] }; + } + saveLocalStorage(); input = ""; return; } else if (e.key === "Backspace" && input.length > 0) { @@ -217,7 +227,19 @@ function setWideMode() { } else { document.body.className = ""; } - localStorage.setItem(LOCALSTORAGE_KEY, JSON.stringify(saveData)); + saveLocalStorage(); } wideOpt.addEventListener("change", setWideMode); setWideMode(); + +// load previous run +if (daysSinceEpoch in saveData) { + const guesses = saveData[daysSinceEpoch].guesses.slice(); + saveData[daysSinceEpoch].guesses = []; + for (const guess of guesses) { + for (const c of guess) { + processKey({ key: c }); + } + processKey({ key: "Enter" }); + } +}