From 42689499d3414298524e0dcd3525c16634ba44dd Mon Sep 17 00:00:00 2001 From: Wynd Date: Sun, 2 Nov 2025 23:26:28 +0200 Subject: [PATCH] Added a simpler way to store game sensitive data --- public/scripts/common/helper.js | 2 +- public/scripts/common/mat-kind-filter.js | 25 ++++++++++++------------ public/scripts/common/storage.js | 13 ++++++++++++ public/scripts/kh1/synth.js | 21 +++++++------------- public/scripts/kh3/food-sim.js | 11 +++++++---- public/scripts/kh3/pro-codes-sim.js | 25 ++++++++++++------------ 6 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 public/scripts/common/storage.js diff --git a/public/scripts/common/helper.js b/public/scripts/common/helper.js index 4390c6c..98b2caa 100644 --- a/public/scripts/common/helper.js +++ b/public/scripts/common/helper.js @@ -1,4 +1,4 @@ -export function getGame(url) { +export function getGame(url = window.location.href) { let last = url.lastIndexOf("/", url.length); let first = url.lastIndexOf("/", last - 1) + 1; return url.substring(first, last); diff --git a/public/scripts/common/mat-kind-filter.js b/public/scripts/common/mat-kind-filter.js index 0461995..c66ec37 100644 --- a/public/scripts/common/mat-kind-filter.js +++ b/public/scripts/common/mat-kind-filter.js @@ -1,15 +1,11 @@ -import { getGame } from "./helper.js"; +import * as storage from "./storage.js"; const TRACKED_STORAGE_NAME = "/drops/tracked/"; -let gameName; - export let showOnlyTracked = false; export let kindFilter = new Set(); export function init() { - gameName = getGame(window.location.href); - loadTracked(); const onlyTrackedFilter = document.querySelector( @@ -65,10 +61,12 @@ function loadTracked() { for (const category of categories) { let id = - category.dataset["matKind"] + "-" + category.dataset["matType"]; - id = gameName + TRACKED_STORAGE_NAME + id; + TRACKED_STORAGE_NAME + + category.dataset["matKind"] + + "-" + + category.dataset["matType"]; - let isTracked = localStorage.getItem(id) === "true"; + let isTracked = storage.get(id) === "true"; if (isTracked) { category.dataset["isTracked"] = true; let trackButton = category.querySelector(".category button"); @@ -84,16 +82,19 @@ function loadTracked() { export function track(element) { let parent = element.parentElement.parentElement; - let id = parent.dataset["matKind"] + "-" + parent.dataset["matType"]; - id = gameName + TRACKED_STORAGE_NAME + id; + let id = + TRACKED_STORAGE_NAME + + parent.dataset["matKind"] + + "-" + + parent.dataset["matType"]; let isTracked = parent.dataset["isTracked"] ?? false; isTracked = isTracked === "true" ? false : true; if (isTracked) { - localStorage.setItem(id, true); + storage.set(id, true); } else { - localStorage.removeItem(id); + storage.remove(id); } parent.dataset["isTracked"] = isTracked; diff --git a/public/scripts/common/storage.js b/public/scripts/common/storage.js new file mode 100644 index 0000000..c91bb53 --- /dev/null +++ b/public/scripts/common/storage.js @@ -0,0 +1,13 @@ +import { getGame } from "./helper.js"; + +export function remove(key) { + localStorage.removeItem(getGame() + key); +} + +export function get(key) { + return localStorage.getItem(getGame() + key); +} + +export function set(key, value) { + localStorage.setItem(getGame() + key, value); +} diff --git a/public/scripts/kh1/synth.js b/public/scripts/kh1/synth.js index 405cce2..32f8781 100644 --- a/public/scripts/kh1/synth.js +++ b/public/scripts/kh1/synth.js @@ -1,29 +1,22 @@ import "../common/prototypes.js"; -import { getGame } from "../common/helper.js"; +import * as storage from "../common/storage.js"; const RECIPE_STORAGE_NAME = "/synth/"; -const LIST_STORAGE_NAME = "/synth/needed-mats"; +const LIST_STORAGE_NAME = "/synth/finished-mats"; let markedNeededMaterials = []; -let gameName; document.addEventListener("DOMContentLoaded", (event) => { - gameName = getGame(window.location.href); - const recipes = document.querySelectorAll(".recipe"); for (const recipe of recipes) { recipe.checked = - localStorage.getItem(gameName + RECIPE_STORAGE_NAME + recipe.id) === - "true" ?? false; + storage.get(RECIPE_STORAGE_NAME + recipe.id) === "true" ?? false; updateSynthRecipeState(recipe); recipe.addEventListener("input", function () { // Change the recipe's state and update and needed materials list based on the remaining recipes - localStorage.setItem( - gameName + RECIPE_STORAGE_NAME + this.id, - this.checked, - ); + storage.set(RECIPE_STORAGE_NAME + this.id, this.checked); updateSynthRecipeState(this); calcNeededMats(); }); @@ -37,8 +30,8 @@ document.addEventListener("DOMContentLoaded", (event) => { } // Turn the single saved string into an array with each material - if (localStorage.getItem(gameName + LIST_STORAGE_NAME) != null) { - let saved = localStorage.getItem(gameName + LIST_STORAGE_NAME); + if (storage.get(LIST_STORAGE_NAME) != null) { + let saved = storage.get(LIST_STORAGE_NAME); saved = saved.split(","); markedNeededMaterials = saved; markedNeededMaterials = markedNeededMaterials.filter( @@ -139,7 +132,7 @@ function updateMarkedNeededMats(ingredient) { markedNeededMaterials.push(ingredient); } markedNeededMaterials = markedNeededMaterials.filter((n) => n && n !== ""); - localStorage.setItem(gameName + LIST_STORAGE_NAME, markedNeededMaterials); + storage.set(LIST_STORAGE_NAME, markedNeededMaterials); } function markNeededMat(mat) { diff --git a/public/scripts/kh3/food-sim.js b/public/scripts/kh3/food-sim.js index 37b6eec..30e7314 100644 --- a/public/scripts/kh3/food-sim.js +++ b/public/scripts/kh3/food-sim.js @@ -1,4 +1,6 @@ -const STORAGE_NAME = "kh3/food-sim/recipe."; +import * as storage from "../common/storage.js"; + +const STORAGE_NAME = "/food-sim/recipe-"; const types = ["starters", "soups", "fish", "meat", "deserts"]; let globalStats = { str: 0, mag: 0, def: 0, hp: 0, mp: 0 }; @@ -10,7 +12,7 @@ document.addEventListener("DOMContentLoaded", (event) => { updateStats(); types.forEach(function (type, typeIdx) { - let savedRecipe = localStorage.getItem(STORAGE_NAME + typeIdx); + let savedRecipe = storage.get(STORAGE_NAME + typeIdx); const recipes = document.querySelectorAll( "div.recipes." + type + " .recipe", @@ -19,6 +21,7 @@ document.addEventListener("DOMContentLoaded", (event) => { recipes.forEach(function (item, index) { let recipeName = item.querySelector(".title").innerText; if (savedRecipe != undefined && recipeName == savedRecipe) { + hasSelection[typeIdx] = true; selectRecipe(item, typeIdx); } @@ -89,7 +92,7 @@ function unselectRecipe(index) { ); } - localStorage.removeItem(STORAGE_NAME + index); + storage.remove(STORAGE_NAME + index); typeItem[index].style["box-shadow"] = "0px 0px 10px rgba(0, 0, 0, 0.4)"; let stats = JSON.parse(typeItem[index].dataset["stats"]); @@ -107,7 +110,7 @@ function selectRecipe(item, index) { } let recipeName = item.querySelector(".title").innerText; - localStorage.setItem(STORAGE_NAME + index, recipeName); + storage.set(STORAGE_NAME + index, recipeName); let stats = JSON.parse(item.dataset["stats"]); addStats(stats); diff --git a/public/scripts/kh3/pro-codes-sim.js b/public/scripts/kh3/pro-codes-sim.js index 38211bf..e8b6459 100644 --- a/public/scripts/kh3/pro-codes-sim.js +++ b/public/scripts/kh3/pro-codes-sim.js @@ -1,5 +1,7 @@ -const CODES_STORAGE_NAME = "kh3/pro-codes-sim/codes/"; -const MERIT_STORAGE_NAME = "kh3/pro-codes-sim/merit/"; +import * as storage from "../common/storage.js"; + +const CODES_STORAGE_NAME = "/pro-codes-sim/codes/"; +const MERIT_STORAGE_NAME = "/pro-codes-sim/merit/"; const STAR_MULTIPLIER = 1.25; let selectionAllState = false; @@ -14,9 +16,7 @@ document.addEventListener("DOMContentLoaded", (event) => { // Loading enabled codes for (let code of codes) { const hasCodeToggled = - localStorage.getItem(CODES_STORAGE_NAME + code.id) === "true" - ? true - : false; + storage.get(CODES_STORAGE_NAME + code.id) === "true" ? true : false; if (hasCodeToggled) { // Normally the toggleCode gets called after the checkbox is checked, // since we don't interact with it at this point we mark it as checked manually here @@ -28,8 +28,7 @@ document.addEventListener("DOMContentLoaded", (event) => { // Loading marked fights and their merit for (let fight of fights) { const source = fight.dataset["meritSource"]; - const merit = - Number(localStorage.getItem(MERIT_STORAGE_NAME + source)) ?? 0; + const merit = Number(storage.get(MERIT_STORAGE_NAME + source)) ?? 0; if (merit > 0) { markFight(fight, merit); } @@ -50,7 +49,7 @@ function markFight(data, loadedMerit = 0) { updateMeritCounter(); fightMerit[source] = 0; - localStorage.removeItem(MERIT_STORAGE_NAME + source); + storage.remove(MERIT_STORAGE_NAME + source); data.innerText = "Mark"; data.classList.remove("danger"); @@ -64,7 +63,7 @@ function markFight(data, loadedMerit = 0) { updateMeritCounter(); fightMerit[source] = merit; - localStorage.setItem(MERIT_STORAGE_NAME + source, merit); + storage.set(MERIT_STORAGE_NAME + source, merit); data.innerText = "Unmark"; data.classList.add("danger"); @@ -77,12 +76,12 @@ function toggleCode(code) { if (code.checked) { stars += codeStars; - localStorage.setItem(CODES_STORAGE_NAME + code.id, true); + storage.set(CODES_STORAGE_NAME + code.id, true); } else { stars -= codeStars; selectionAllState = false; updateAllButton(); - localStorage.removeItem(CODES_STORAGE_NAME + code.id); + storage.remove(CODES_STORAGE_NAME + code.id); } updateStarsCounter(); } @@ -99,7 +98,7 @@ function toggleAllCodes() { } code.checked = true; stars += Number(code.dataset["stars"]); - localStorage.setItem(CODES_STORAGE_NAME + code.id, true); + storage.set(CODES_STORAGE_NAME + code.id, true); } } else { selectionAllState = false; @@ -110,7 +109,7 @@ function toggleAllCodes() { } code.checked = false; stars -= Number(code.dataset["stars"]); - localStorage.removeItem(CODES_STORAGE_NAME + code.id); + storage.remove(CODES_STORAGE_NAME + code.id); } }