diff game/js/storage.js @ 17:fe680946164b

reorganised repo
author ark
date 2026-10-20 00:12 +0200
parents 2c4fed8baed9
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/js/storage.js	Tue Oct 20 00:12:36 2026 +0200
@@ -0,0 +1,38 @@
+// settings kept in the browser (architecture.md, persistence). one
+// localStorage entry holds all of them, with a format version. every access
+// is wrapped, so the game still runs when storage is unavailable, for
+// example in private browsing; settings are then not kept.
+
+window.MM = window.MM || {};
+
+MM.storage = (function () {
+    "use strict";
+
+    var KEY = "minor_monsters";
+    var VERSION = 1;
+    var settings = {};
+
+    // an entry of another version is ignored and replaced on the next set()
+    try {
+        var entry = JSON.parse(window.localStorage.getItem(KEY));
+        if (entry && entry.version === VERSION && entry.settings) settings = entry.settings;
+    } catch (e) {
+        settings = {};
+    }
+
+    // the stored value of a setting, or fallback when it was never set
+    function get(name, fallback) {
+        return Object.prototype.hasOwnProperty.call(settings, name) ? settings[name] : fallback;
+    }
+
+    function set(name, value) {
+        settings[name] = value;
+        try {
+            window.localStorage.setItem(KEY, JSON.stringify({ version: VERSION, settings: settings }));
+        } catch (e) {
+            // storage unavailable or full: the setting holds until a reload
+        }
+    }
+
+    return { get: get, set: set };
+})();