Added a simpler way to store game sensitive data

master
Wynd 2025-11-02 23:26:28 +02:00
parent 66e7635119
commit 42689499d3
6 changed files with 53 additions and 44 deletions

View File

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

View File

@ -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;

View File

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

View File

@ -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) {

View File

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

View File

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