Skip to content

Commit

Permalink
storage.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ntwcklng committed Feb 16, 2016
1 parent 0b20c58 commit 699c33c
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions web/js/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var Storage = (function Storage(){

var publicAPI, ls;

ls = localStorage;

publicAPI = {
updateBestScore: updateBestScore
};

return publicAPI;

function getDifficultyString(difficulty) {
switch (difficulty) {
case 0:

This comment has been minimized.

Copy link
@getify

getify Feb 22, 2016

these numeric values can be referenced via public properties on the Game api, as seen here.

Moreover, I don't see value in converting from the numeric values to these magic strings just so they can be stored at named properties in the JSON.

Let's just stick the scores in positions 0, 1, and 2 respectively in a single array.

return "easy";
break;
case 1:
return "medium";
break;
case 2:
return "hard";
break;
default:
return null;
}
}
function updateBestScore(difficulty, score) {
if(typeof ls === "undefined") {
return score;
}
var maxScore = score;
var getDifficulty = getDifficultyString(difficulty);
var storeData = {
version: Debug.BUILD_VERSION,
easy: {
score: 0
},
medium: {
score: 0
},
hard: {
score: 0
}
};

var getStorageScore = JSON.parse(ls.getItem("bestCloudScore"));

if(getStorageScore) {
// overwrite the template
storeData = getStorageScore;

var storedBuildVersion = storeData.version.split(".");
var actualBuildVersion = Debug.BUILD_VERSION.split(".");

if(storedBuildVersion[0] !== actualBuildVersion[0] || storedBuildVersion[1] !== actualBuildVersion[1]) {
// reset all scores, when there was a minor/major update
storeData.easy.score = 0;
storeData.medium.score = 0;
storeData.hard.score = 0;
}
maxScore = Math.max(
score,
storeData[getDifficulty].score
);
}
storeData[getDifficulty].score = maxScore;
ls.setItem("bestCloudScore", JSON.stringify(storeData));
return maxScore;
}

})();

0 comments on commit 699c33c

Please sign in to comment.