Add user-specific quiz statistics

This commit is contained in:
2022-11-05 18:44:42 +01:00
parent f5d0734e84
commit cc20ed870c
5 changed files with 156 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
export class Game {
constructor (id, questions, created, player1id, player1name, player1answers, player2id, player2name, player2answers) {
this.id = id
this.questions = questions
this.questionIds = questions
this.created = created
this.player1id = player1id
this.player1name = player1name
@@ -10,13 +10,38 @@ export class Game {
this.player2name = player2name
this.player2answers = player2answers
}
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)
return {
winner: correctAnswersPl1 > correctAnswersPl2 ? 1 : 2,
loser: correctAnswersPl1 > correctAnswersPl2 ? 2 : 1,
tie: correctAnswersPl1 === correctAnswersPl2,
correctAnswersPl1,
correctAnswersPl2
}
}
}
// Firestore data converter
export const GameConverter = {
toFirestore: (game) => {
return {
fragen: game.questions,
fragen: game.questionIds,
erstellt: game.created,
spieler1id: game.player1id,
spieler1name: game.player1name,