Load/save previous input

This commit is contained in:
Arne Keller 2022-02-23 12:00:26 +01:00
parent 0fd07d7d30
commit 4d6a87c6ec

View File

@ -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" });
}
}