Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/doge2048/js/local_score_manager.js
4626 views
1
window.fakeStorage = {
2
_data: {},
3
4
setItem: function (id, val) {
5
return this._data[id] = String(val);
6
},
7
8
getItem: function (id) {
9
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
10
},
11
12
removeItem: function (id) {
13
return delete this._data[id];
14
},
15
16
clear: function () {
17
return this._data = {};
18
}
19
};
20
21
function LocalScoreManager() {
22
this.key = "bestScore";
23
24
var supported = this.localStorageSupported();
25
this.storage = supported ? window.localStorage : window.fakeStorage;
26
}
27
28
LocalScoreManager.prototype.localStorageSupported = function () {
29
var testKey = "test";
30
var storage = window.localStorage;
31
32
try {
33
storage.setItem(testKey, "1");
34
storage.removeItem(testKey);
35
return true;
36
} catch (error) {
37
return false;
38
}
39
};
40
41
LocalScoreManager.prototype.get = function () {
42
return this.storage.getItem(this.key) || 0;
43
};
44
45
LocalScoreManager.prototype.set = function (score) {
46
this.storage.setItem(this.key, score);
47
};
48
49