1
0
mirror of https://github.com/danog/2048.git synced 2024-12-13 17:57:42 +01:00
2048/js/local_score_manager.js

50 lines
1022 B
JavaScript
Raw Normal View History

2014-03-11 16:52:02 +01:00
window.fakeStorage = {
_data: {},
setItem: function (id, val) {
2014-03-11 16:52:02 +01:00
return this._data[id] = String(val);
},
getItem: function (id) {
2014-03-11 16:52:02 +01:00
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
},
removeItem: function (id) {
return delete this._data[id];
},
clear: function () {
return this._data = {};
}
2014-03-11 16:52:02 +01:00
};
2014-03-14 12:33:32 +01:00
function LocalScoreManager() {
this.key = "bestScore";
var supported = this.localStorageSupported();
console.log(supported);
this.storage = supported ? window.localStorage : window.fakeStorage;
}
LocalScoreManager.prototype.localStorageSupported = function () {
var testKey = "test";
var storage = window.localStorage;
try {
2014-03-14 12:33:32 +01:00
storage.setItem(testKey, "1");
storage.removeItem(testKey);
return true;
2014-03-14 12:33:32 +01:00
} catch (error) {
return false;
}
2014-03-14 12:33:32 +01:00
};
LocalScoreManager.prototype.get = function () {
2014-03-12 13:30:50 +01:00
return this.storage.getItem(this.key) || 0;
};
LocalScoreManager.prototype.set = function (score) {
2014-03-11 16:52:02 +01:00
this.storage.setItem(this.key, score);
};