Path: blob/main/projects/cupcake2048/js/local_storage_manager.js
4626 views
window.fakeStorage = {1_data: {},23setItem: function (id, val) {4return this._data[id] = String(val);5},67getItem: function (id) {8return this._data.hasOwnProperty(id) ? this._data[id] : undefined;9},1011removeItem: function (id) {12return delete this._data[id];13},1415clear: function () {16return this._data = {};17}18};1920function LocalStorageManager() {21this.bestScoreKey = "bestScoreCupcakes";22this.bestPointsKey = "bestPointsCupcakes";23this.gameStateKey = "gameStateCupcakes";2425var supported = this.localStorageSupported();26this.storage = supported ? window.localStorage : window.fakeStorage;27}2829LocalStorageManager.prototype.localStorageSupported = function () {30var testKey = "test";31var storage = window.localStorage;3233try {34storage.setItem(testKey, "1");35storage.removeItem(testKey);36return true;37} catch (error) {38return false;39}40};4142// Best score getters/setters43LocalStorageManager.prototype.getBestScore = function () {44return this.storage.getItem(this.bestScoreKey) || 0;45};4647LocalStorageManager.prototype.setBestScore = function (score) {48this.storage.setItem(this.bestScoreKey, score);49};5051// Best points getters/setters52LocalStorageManager.prototype.getBestPoints = function () {53return this.storage.getItem(this.bestPointsKey) || 0;54};5556LocalStorageManager.prototype.setBestPoints = function (points) {57this.storage.setItem(this.bestPointsKey, points);58};5960// Game state getters/setters and clearing61LocalStorageManager.prototype.getGameState = function () {62var stateJSON = this.storage.getItem(this.gameStateKey);63return stateJSON ? JSON.parse(stateJSON) : null;64};6566LocalStorageManager.prototype.setGameState = function (gameState) {67this.storage.setItem(this.gameStateKey, JSON.stringify(gameState));68};6970LocalStorageManager.prototype.clearGameState = function () {71this.storage.removeItem(this.gameStateKey);72};737475