提交 d6da8d99 编写于 作者: T Tim Petricola

localStorage fallback

上级 fda0e840
...@@ -2,7 +2,7 @@ function GameManager(size, InputManager, Actuator, ScoreManager) { ...@@ -2,7 +2,7 @@ function GameManager(size, InputManager, Actuator, ScoreManager) {
this.size = size; // Size of the grid this.size = size; // Size of the grid
this.inputManager = new InputManager; this.inputManager = new InputManager;
this.scoreManager = new ScoreManager; this.scoreManager = new ScoreManager;
this.actuator = new Actuator(this.scoreManager.isSupported()); this.actuator = new Actuator;
this.startTiles = 2; this.startTiles = 2;
......
function HTMLActuator(bestScoreSupported) { function HTMLActuator() {
this.tileContainer = document.getElementsByClassName("tile-container")[0]; this.tileContainer = document.getElementsByClassName("tile-container")[0];
this.scoreContainer = document.getElementsByClassName("score-container")[0]; this.scoreContainer = document.getElementsByClassName("score-container")[0];
this.bestContainer = document.getElementsByClassName("best-container")[0]; this.bestContainer = document.getElementsByClassName("best-container")[0];
this.messageContainer = document.getElementsByClassName("game-message")[0]; this.messageContainer = document.getElementsByClassName("game-message")[0];
this.score = 0; this.score = 0;
this.bestScoreSupported = bestScoreSupported;
if (!this.bestScoreSupported) {
this.bestContainer.style.display = "none";
}
} }
HTMLActuator.prototype.actuate = function (grid, metadata) { HTMLActuator.prototype.actuate = function (grid, metadata) {
...@@ -111,9 +106,7 @@ HTMLActuator.prototype.updateScore = function (score) { ...@@ -111,9 +106,7 @@ HTMLActuator.prototype.updateScore = function (score) {
}; };
HTMLActuator.prototype.updateBestScore = function (bestScore) { HTMLActuator.prototype.updateBestScore = function (bestScore) {
if (this.bestScoreSupported) { this.bestContainer.textContent = bestScore;
this.bestContainer.textContent = bestScore;
}
}; };
HTMLActuator.prototype.message = function (won) { HTMLActuator.prototype.message = function (won) {
......
window.fakeStorage = {
_data : {},
setItem : function (id, val) {
console.log('set');
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() { function LocalScoreManager() {
var localSupported = !!window.localStorage;
this.key = 'bestScore'; this.key = 'bestScore';
this.storage = localSupported ? window.localStorage : window.fakeStorage;
} }
LocalScoreManager.prototype.get = function () { LocalScoreManager.prototype.get = function () {
if (!this.isSupported()) { var score = this.storage.getItem(this.key);
return 0; if (typeof score === "undefined" || score === null) {
score = 0;
} }
return score;
return localStorage.getItem(this.key);
}; };
LocalScoreManager.prototype.set = function (score) { LocalScoreManager.prototype.set = function (score) {
if (!this.isSupported()) { this.storage.setItem(this.key, score);
return false;
}
localStorage.setItem(this.key, score);
};
LocalScoreManager.prototype.isSupported = function () {
return !!window.localStorage;
}; };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册