wobblyhead hg > minor_monsters
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 7:4fd2da999ba4 | 8:2c4fed8baed9 |
|---|---|
| 1 // settings kept in the browser (architecture.md, persistence). one | |
| 2 // localStorage entry holds all of them, with a format version. every access | |
| 3 // is wrapped, so the game still runs when storage is unavailable, for | |
| 4 // example in private browsing; settings are then not kept. | |
| 5 | |
| 6 window.MM = window.MM || {}; | |
| 7 | |
| 8 MM.storage = (function () { | |
| 9 "use strict"; | |
| 10 | |
| 11 var KEY = "minor_monsters"; | |
| 12 var VERSION = 1; | |
| 13 var settings = {}; | |
| 14 | |
| 15 // an entry of another version is ignored and replaced on the next set() | |
| 16 try { | |
| 17 var entry = JSON.parse(window.localStorage.getItem(KEY)); | |
| 18 if (entry && entry.version === VERSION && entry.settings) settings = entry.settings; | |
| 19 } catch (e) { | |
| 20 settings = {}; | |
| 21 } | |
| 22 | |
| 23 // the stored value of a setting, or fallback when it was never set | |
| 24 function get(name, fallback) { | |
| 25 return Object.prototype.hasOwnProperty.call(settings, name) ? settings[name] : fallback; | |
| 26 } | |
| 27 | |
| 28 function set(name, value) { | |
| 29 settings[name] = value; | |
| 30 try { | |
| 31 window.localStorage.setItem(KEY, JSON.stringify({ version: VERSION, settings: settings })); | |
| 32 } catch (e) { | |
| 33 // storage unavailable or full: the setting holds until a reload | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 return { get: get, set: set }; | |
| 38 })(); |
