all repos — emchess @ e3439f4fd1351a66738e364bb3c2a16038ca6e6a

src/util/storage.js (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
const DEFAULTS = {
    pieceSet: "kingdom",
    sound: true,
    reactions: false,
    coords: true,
    personality: "beginner",
    side: "w",
    clock: "none",
    clockBase: 300,
    clockInc: 0,
    threats: false,
    blunder: false,
};
const PREFS_KEY = "emchess.prefs";
const SAVE_KEY = "emchess.save";
export function loadPrefs() {
    try {
        const raw = localStorage.getItem(PREFS_KEY);
        if (!raw)
            return { ...DEFAULTS };
        return { ...DEFAULTS, ...JSON.parse(raw) };
    }
    catch {
        return { ...DEFAULTS };
    }
}
export function savePrefs(prefs) {
    try {
        localStorage.setItem(PREFS_KEY, JSON.stringify(prefs));
    }
    catch {
        /* private mode, no storage — the game still works, it just won't remember. */
    }
}
export function saveGame(game) {
    try {
        localStorage.setItem(SAVE_KEY, JSON.stringify(game));
    }
    catch {
        /* ignore */
    }
}
export function loadGame() {
    try {
        const raw = localStorage.getItem(SAVE_KEY);
        return raw ? JSON.parse(raw) : null;
    }
    catch {
        return null;
    }
}
export function clearGame() {
    try {
        localStorage.removeItem(SAVE_KEY);
    }
    catch {
        /* ignore */
    }
}