Skip to content

Commit

Permalink
Properly handle highscores loading and saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Kovah committed Aug 5, 2018
1 parent 77ce6d2 commit 9d68b6b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
3 changes: 3 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
Menu,
Game,
Highscores
},
mounted() {
this.$store.commit('initHighscores');
}
};
</script>
Expand Down
26 changes: 1 addition & 25 deletions src/components/Highscores/Highscores.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,7 @@
return this.highscores.length > 0;
},
highscores: function () {
let highscores = this.$store.state.highscores;
if (highscores.length === 0) {
// Try to get the highscores from local storage
let localScores = JSON.parse(localStorage.getItem('highscores'));
if (localScores != null) {
console.log('Old highscores loaded');
highscores = localScores;
}
}
// Sort the highscores by total score descending
highscores = highscores.sort((a, b) => {
// Calculate a total score by substrating the fail from the success cards
let totalScoreA = a.score.success - a.score.fail;
let totalScoreB = b.score.success - b.score.fail;
if (totalScoreA < totalScoreB)
return 1;
if (totalScoreA > totalScoreB)
return -1;
return 0;
});
return highscores;
return this.$store.state.highscores;
}
}
};
Expand Down
47 changes: 38 additions & 9 deletions src/components/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,43 @@ const store = new Vuex.Store({
fail: 0
};
},

// Global actions
showMenu (state) {
state.gameStarted = false;
state.showGamePanel = false;
state.showHighscorePanel = false;

this.commit('resetGameState');
},

// Highscore actions
initHighscores(state) {
if (state.highscores.length === 0) {
// Try to get the highscores from local storage
let localScores = JSON.parse(localStorage.getItem('highscores'));
if (localScores != null) {
console.log('Old highscores loaded');
state.highscores = localScores;
}
}

this.commit('sortHighscores');
},
sortHighscores(state) {
// Sort the highscores by total score descending
state.highscores = state.highscores.sort((a, b) => {
// Calculate a total score by substrating the fail from the success cards
let totalScoreA = a.score.success - a.score.fail;
let totalScoreB = b.score.success - b.score.fail;

if (totalScoreA < totalScoreB)
return 1;
if (totalScoreA > totalScoreB)
return -1;
return 0;
});
},
saveHighscore (state) {
let highscore = {
name: state.playerName,
Expand All @@ -146,15 +183,8 @@ const store = new Vuex.Store({
// Save the highscore to the local storage is available
localStorage.setItem('highscores', JSON.stringify(state.highscores));
}
},

// Global actions
showMenu (state) {
state.gameStarted = false;
state.showGamePanel = false;
state.showHighscorePanel = false;

this.commit('resetGameState');
this.commit('sortHighscores');
},
showHighscores (state, afterGamePanel) {
state.gameStarted = false;
Expand All @@ -171,7 +201,6 @@ const store = new Vuex.Store({

this.commit('resetGameState');
},

deleteHighscores (state) {
if (!confirm('Highscores löschen?')) {
return;
Expand Down

0 comments on commit 9d68b6b

Please sign in to comment.