Merge pull request #29 from TimPetricola/fix-readme
[2048.git] / js / local_score_manager.js
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
window.fakeStorage = {
  _data: {},
 
  setItem: function (id, val) {
    return this._data[id] = String(val);
  },
 
  getItem: function (id) {
    return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
  },
 
  removeItem: function (id) {
    return delete this._data[id];
  },
 
  clear: function () {
    return this._data = {};
  }
};
 
function LocalScoreManager() {
  var localSupported = !!window.localStorage;
 
  this.key     = "bestScore";
  this.storage = localSupported ? window.localStorage : window.fakeStorage;
}
 
LocalScoreManager.prototype.get = function () {
  return this.storage.getItem(this.key) || 0;
};
 
LocalScoreManager.prototype.set = function (score) {
  this.storage.setItem(this.key, score);
};
 
 
comments