forked from getify/cloud-sweeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
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; | ||
} | ||
|
||
})(); |
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.