diff --git a/components/AddClosedEndedQuestion.vue b/components/AddClosedEndedQuestion.vue index 01752b6..f386879 100644 --- a/components/AddClosedEndedQuestion.vue +++ b/components/AddClosedEndedQuestion.vue @@ -2,54 +2,44 @@ - - Eigene Quizfrage einreichen - - {{ showBody ? 'mdi-chevron-up' : 'mdi-chevron-down' }} - - - - - - - - - - - - - - - - - - - - - - - - - mdi-information - {{ message }} - - - - - - - +10 Fragen - - Reset - Einreichen - - - + + + + + + + + + + + + + + + + + + + + + mdi-information + {{ message }} + + + + + + + +10 Fragen + + Reset + Einreichen + @@ -76,7 +66,6 @@ export default { }, data () { return { - showBody: false, loading: false, question: '', answers: [], diff --git a/components/Challenge.vue b/components/Challenge.vue deleted file mode 100644 index c2e2bc5..0000000 --- a/components/Challenge.vue +++ /dev/null @@ -1,64 +0,0 @@ - - - - Spiele {{ games.won + games.lost + games.tie }} - - - Siege: {{ games.won }} - Unentschieden: {{ games.lost }} - Niederlagen: {{ games.tie }} - Fragen: {{ games.questionsCorrect + games.questionsIncorrect }} - Richtig: {{ questionsCorrectPercent }}% - - - {{ text }} - - - - mdi-play - Spielen - - - - - - - - \ No newline at end of file diff --git a/components/CompletedGameCard.vue b/components/CompletedGameCard.vue new file mode 100644 index 0000000..c6434bd --- /dev/null +++ b/components/CompletedGameCard.vue @@ -0,0 +1,48 @@ + + + + + {{ icon }} + + + {{ title }} + gegen {{ opponentName }} + + + + + + \ No newline at end of file diff --git a/components/OpenGameCard.vue b/components/OpenGameCard.vue new file mode 100644 index 0000000..9f9356a --- /dev/null +++ b/components/OpenGameCard.vue @@ -0,0 +1,48 @@ + + + + + + {{ game.player1name }} + mdi-circle + + vs. + + {{ game.player2name || '?' }} + mdi-circle + + + + + + + \ No newline at end of file diff --git a/components/QuizComponent.vue b/components/QuizComponent.vue new file mode 100644 index 0000000..e428685 --- /dev/null +++ b/components/QuizComponent.vue @@ -0,0 +1,153 @@ + + + Challenge + + + mdi-play + Spielen + + + + + + Offene Spiele ({{ openGames.length }}) + + + + + + + + + + + + Beendete Spiele ({{ completedGames.length }}) + + + + + + + + + + + + Persönliche Statistik + + + + Spiele {{ games.won + games.lost + games.tie }} + + + Siege: {{ games.won }} + Unentschieden: {{ games.lost }} + Niederlagen: {{ games.tie }} + Fragen: {{ games.questionsCorrect + games.questionsIncorrect }} + Richtig: {{ questionsCorrectPercent }}% + + + {{ text }} + + + + + + Neue Quizfrage einreichen + + + + + + + + + + \ No newline at end of file diff --git a/pages/courses/_course/index.vue b/pages/courses/_course/index.vue index cbd9961..0b04822 100644 --- a/pages/courses/_course/index.vue +++ b/pages/courses/_course/index.vue @@ -42,7 +42,7 @@ - + diff --git a/pages/courses/_course/play.vue b/pages/courses/_course/play.vue index 589e4e7..904a7d9 100644 --- a/pages/courses/_course/play.vue +++ b/pages/courses/_course/play.vue @@ -181,6 +181,7 @@ export default { this.game.player2id = this.$auth.currentUser.uid this.game.player2name = this.$store.state.user.displayName this.game.player2answers = [] + this.game.playerIds.push(this.game.player2id) // Get a new write batch const batch = writeBatch(this.$db) @@ -214,6 +215,7 @@ export default { null, rndQuestions.map(q => q.id), Date.now() / 1000, // Current UNIX timestamp in seconds + false, this.$auth.currentUser.uid, this.$store.state.user.displayName, [], @@ -252,7 +254,11 @@ export default { // Update the user's given answers for the current game const gameRef = doc(this.$db, `kurse/${this.courseID}/spiele/${this.game.id}`) batch.update(gameRef, { - [`spieler${this.playerNumber}antworten`]: arrayUnion({ frage: this.selectedQuestion.id, antwort: this.submittedAnswer }) + [`spieler${this.playerNumber}antworten`]: arrayUnion({ + frage: this.selectedQuestion.id, + antwort: this.submittedAnswer, + richtig: this.answerCorrect + }) }) // Increment the number of correct/incorrect answers given by the user by 1 @@ -265,7 +271,11 @@ export default { // Commit the batch batch.commit().then((empty) => { // Batch execution was successful - this.game[`player${this.playerNumber}answers`].push({ frage: this.selectedQuestion.id, antwort: this.submittedAnswer }) + this.game[`player${this.playerNumber}answers`].push({ + frage: this.selectedQuestion.id, + antwort: this.submittedAnswer, + richtig: this.answerCorrect + }) }).catch((error) => { // Batch execution failed; display error message this.$toast({content: error, color: 'error'}) @@ -319,6 +329,7 @@ export default { }) }, completeGame () { + this.game.completed = true const result = this.game.getResult() // Figure out which player won and which player lost, or if the game was a tie @@ -345,6 +356,10 @@ export default { const refPl2 = doc(this.$db, `benutzer/${this.game.player2id}`) batch.update(refPl2, { [`spiele.${this.courseID}.${updateFieldPl2}`]: increment(1) }) + // Set the game's "completed" field to true + const refGame = doc(this.$db, `kurse/${this.courseID}/spiele/${this.game.id}`) + batch.update(refGame, { abgeschlossen: true }) + // Commit the batch batch.commit().catch((error) => { // Batch execution failed; display error message diff --git a/plugins/game.js b/plugins/game.js index ddf4432..5108ea2 100644 --- a/plugins/game.js +++ b/plugins/game.js @@ -1,31 +1,24 @@ export class Game { - constructor (id, questions, created, player1id, player1name, player1answers, player2id, player2name, player2answers) { + constructor (id, questions, created, completed, player1id, player1name, player1answers, player2id, player2name, player2answers) { this.id = id this.questionIds = questions this.created = created + this.completed = completed this.player1id = player1id this.player1name = player1name this.player1answers = player1answers this.player2id = player2id this.player2name = player2name this.player2answers = player2answers + this.playerIds = [] + + if (this.player1id) this.playerIds.push(this.player1id) + if (this.player2id) this.playerIds.push(this.player2id) } getResult () { - if (!this.questions) return {} - const qs = this.questions - - function countCorrectAnswers(answersGiven) { - let i = 0 - answersGiven.forEach(a => { - const q = qs.find(e => e.id === a.frage) - if (q && a.antwort === q.correctAnswer) i++ - }) - return i - } - - const correctAnswersPl1 = countCorrectAnswers(this.player1answers) - const correctAnswersPl2 = countCorrectAnswers(this.player2answers) + const correctAnswersPl1 = this.player1answers.filter(a => a.richtig).length + const correctAnswersPl2 = this.player2answers.filter(a => a.richtig).length return { winner: correctAnswersPl1 > correctAnswersPl2 ? 1 : 2, @@ -43,18 +36,20 @@ export const GameConverter = { return { fragen: game.questionIds, erstellt: game.created, + abgeschlossen: game.completed, spieler1id: game.player1id, spieler1name: game.player1name, spieler1antworten: game.player1answers, spieler2id: game.player2id, spieler2name: game.player2name, - spieler2antworten: game.player2answers + spieler2antworten: game.player2answers, + spielerIds: game.playerIds } }, fromFirestore: (snapshot, options) => { const data = snapshot.data(options) return new Game( - snapshot.id, data.fragen, data.erstellt, + snapshot.id, data.fragen, data.erstellt, data.abgeschlossen, data.spieler1id, data.spieler1name, data.spieler1antworten, data.spieler2id, data.spieler2name, data.spieler2antworten )