adding multiple choice (not fully tested yet)

This commit is contained in:
RandyJC
2025-12-09 08:52:07 +01:00
parent 82be8dee93
commit 55349e01f6
10 changed files with 170 additions and 48 deletions

View File

@@ -218,7 +218,7 @@ io.on("connection", (socket) => {
socket.on("player:selectedAnswer", ({ gameId, data }) =>
withGame(gameId, socket, (game) =>
game.selectAnswer(socket, data.answerKey)
game.selectAnswer(socket, data.answerKeys)
)
)

View File

@@ -104,7 +104,7 @@ class Config {
type: "audio",
url: "https://upload.wikimedia.org/wikipedia/commons/transcoded/4/4c/Beethoven_Moonlight_1st_movement.ogg/Beethoven_Moonlight_1st_movement.ogg.mp3",
},
solution: 1,
solution: [1],
cooldown: 5,
time: 25,
},
@@ -120,7 +120,7 @@ class Config {
type: "video",
url: "https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4",
},
solution: 2,
solution: [2],
cooldown: 5,
time: 40,
},

View File

@@ -125,11 +125,25 @@ class Game {
id: "",
connected: false,
}))
game.round = snapshot.round || {
const round = snapshot.round || {
playersAnswers: [],
currentQuestion: 0,
startTime: 0,
}
const migratedAnswers = Array.isArray(round.playersAnswers)
? round.playersAnswers.map((a: any) => ({
...a,
answerIds: Array.isArray(a.answerIds)
? a.answerIds
: typeof a.answerId === "number"
? [a.answerId]
: [],
}))
: []
game.round = {
...round,
playersAnswers: migratedAnswers,
}
game.cooldown = {
active: snapshot.cooldown?.active || false,
paused: snapshot.cooldown?.paused || false,
@@ -538,6 +552,8 @@ class Game {
media: question.media,
time: question.time,
totalPlayer: this.players.length,
allowsMultiple:
Array.isArray(question.solution) && question.solution.length > 1,
})
await this.startCooldown(question.time)
@@ -557,9 +573,10 @@ class Game {
: this.leaderboard.map((p) => ({ ...p }))
const totalType = this.round.playersAnswers.reduce(
(acc: Record<number, number>, { answerId }) => {
acc[answerId] = (acc[answerId] || 0) + 1
(acc: Record<number, number>, { answerIds }) => {
answerIds.forEach((id) => {
acc[id] = (acc[id] || 0) + 1
})
return acc
},
{}
@@ -571,8 +588,16 @@ class Game {
(a) => a.playerId === player.id
)
const correctAnswers = Array.isArray(question.solution)
? Array.from(new Set(question.solution))
: [question.solution]
const isCorrect = playerAnswer
? playerAnswer.answerId === question.solution
? (() => {
const chosen = Array.from(new Set(playerAnswer.answerIds))
if (chosen.length !== correctAnswers.length) return false
return correctAnswers.every((id: number) => chosen.includes(id))
})()
: false
const points =
@@ -615,7 +640,7 @@ class Game {
this.round.playersAnswers = []
this.persist()
}
selectAnswer(socket: Socket, answerId: number) {
selectAnswer(socket: Socket, answerIds: number[]) {
const player = this.players.find((player) => player.id === socket.id)
const question = this.quizz.questions[this.round.currentQuestion]
@@ -627,9 +652,17 @@ class Game {
return
}
const uniqueAnswers = Array.from(new Set(answerIds)).filter(
(id) => !Number.isNaN(id)
)
if (uniqueAnswers.length === 0) {
return
}
this.round.playersAnswers.push({
playerId: player.id,
answerId,
answerIds: uniqueAnswers,
points: timeToPoint(this.round.startTime, question.time),
})