Skip to content

Commit

Permalink
added highscore list functionality with localStore demo
Browse files Browse the repository at this point in the history
  • Loading branch information
treibi committed May 5, 2017
1 parent 31356d6 commit a7c3937
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions js/coffeemeterGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,40 @@ function init(){

mainroad.writeVehiclesSimple();



//!!! test localStorage (a html5/js keyword) for highscores

if (typeof(Storage) !== "undefined") {
var scores =[];

// if coffeemeterGame_HighScores exists, get the stored scores

if(localStorage.coffeemeterGame_HighScores){ //check existence
scores = JSON.parse(localStorage.coffeemeterGame_HighScores);
}

// add new entry to scores array
// (scores empty if no coffeemeterGame_HighScores at the beginning)

scores.push({name:"Treibi",
score:42+scores.length,
date:new Date().valueOf()});
scores.sort(function(a,b){return a.score < b.score});
console.log("test localStorage:");
for(var i=0; i<scores.length; i++){
console.log("name:",scores[i].name,
" score:",scores[i].score,
" date:",scores[i].date);
}

// save scores on the client machine at
// localStorage.coffeemeterGame_HighScores

localStorage.coffeemeterGame_HighScores = JSON.stringify(scores);
}


}//init


Expand Down

0 comments on commit a7c3937

Please sign in to comment.