From 6bf0da711a10713f51e57dfc37ff208757beb5c4 Mon Sep 17 00:00:00 2001 From: Rakantor Date: Mon, 14 Nov 2022 14:55:36 +0100 Subject: [PATCH] Fix: user stats would only be updated after a hard refresh --- pages/courses/_course/play.vue | 8 +++++++- store/index.js | 35 +++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/pages/courses/_course/play.vue b/pages/courses/_course/play.vue index 9008f97..1a3a44e 100644 --- a/pages/courses/_course/play.vue +++ b/pages/courses/_course/play.vue @@ -275,6 +275,9 @@ export default { antwort: this.submittedAnswer, richtig: this.answerCorrect }) + + // Update user stats (correct/incorrect answers given) in store + this.$store.commit('addAnswerToStats', this.answerCorrect) }).catch((error) => { // Batch execution failed; display error message this.$toast({content: error, color: 'error'}) @@ -360,7 +363,10 @@ export default { batch.update(refGame, { abgeschlossen: true }) // Commit the batch - batch.commit().catch((error) => { + batch.commit().then((empty) => { + // Update user stats (wins/losses/ties) in store + this.$store.commit('addGameToStats', { won: result.winner === this.playerNumber, tie: result.tie }) + }).catch((error) => { // Batch execution failed; display error message this.$toast({ content: error, color: 'error' }) }) diff --git a/store/index.js b/store/index.js index eaaaed9..960333a 100644 --- a/store/index.js +++ b/store/index.js @@ -55,6 +55,28 @@ export const mutations = { setSelectedCourse (state, courseID) { state.selectedCourse = courseID }, + addGameInProgress (state, { courseID, gameID }) { + const index = state.user.gamesStarted.findIndex(e => e.course === courseID) + if (index !== -1) { + state.user.gamesStarted[index].course = courseID + state.user.gamesStarted[index].game = gameID + } else { + state.user.gamesStarted.push({ course: courseID, game: gameID }) + } + }, + removeGameInProgress (state, courseID) { + const index = state.user.gamesStarted.findIndex(e => e.course === courseID) + state.user.gamesStarted.splice(index, 1) + }, + addAnswerToStats (state, answerCorrect) { + if (answerCorrect) state.user.games[state.selectedCourse].questionsCorrect++ + else state.user.games[state.selectedCourse].questionsIncorrect++ + }, + addGameToStats (state, { won, tie }) { + if (tie) state.user.games[state.selectedCourse].tie++ + else if (won) state.user.games[state.selectedCourse].won++ + else state.user.games[state.selectedCourse].lost++ + }, setOpenEndedQuestions (state, questions) { state.openEndedQuestions = questions }, @@ -95,19 +117,6 @@ export const mutations = { state.openEndedQuestions[index].updateDifficultyLevel() } - }, - addGameInProgress (state, { courseID, gameID }) { - const index = state.user.gamesStarted.findIndex(e => e.course === courseID) - if (index !== -1) { - state.user.gamesStarted[index].course = courseID - state.user.gamesStarted[index].game = gameID - } else { - state.user.gamesStarted.push({ course: courseID, game: gameID }) - } - }, - removeGameInProgress (state, courseID) { - const index = state.user.gamesStarted.findIndex(e => e.course === courseID) - state.user.gamesStarted.splice(index, 1) } }