mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
adding multiple choice (not fully tested yet)
This commit is contained in:
@@ -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)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -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),
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user