wobblyhead hg > minor_monsters
diff game/js/storage.js @ 8:2c4fed8baed9
art update, persistent sound setting, tower selling as double action
| author | ark |
|---|---|
| date | 2026-10-19 13:37 +0200 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/game/js/storage.js Mon Oct 19 13:37:52 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 }; +})();
