Add concurrently

This commit is contained in:
Ralex91
2024-02-08 16:32:05 +01:00
parent 31122b9d2f
commit 8a9b8df7d2
14 changed files with 252 additions and 298 deletions

View File

@@ -0,0 +1,13 @@
const convertTimeToPoint = (startTime, secondes) => {
let points = 1000
const actualTime = Date.now()
const tempsPasseEnSecondes = (actualTime - startTime) / 1000
points -= (1000 / secondes) * tempsPasseEnSecondes
points = Math.max(0, points)
return points
}
export default convertTimeToPoint

26
socket/utils/cooldown.js Normal file
View File

@@ -0,0 +1,26 @@
let cooldownTimeout
let cooldownResolve
export const abortCooldown = () => {
clearInterval(cooldownTimeout)
cooldownResolve()
}
export const cooldown = (ms, io, room) => {
let count = ms - 1
return new Promise((resolve) => {
cooldownResolve = resolve
cooldownTimeout = setInterval(() => {
if (!count) {
clearInterval(cooldownTimeout)
resolve()
}
io.to(room).emit("game:cooldown", count)
count -= 1
}, 1000)
})
}
export const sleep = (sec) => new Promise((r) => setTimeout(r, sec * 1000))

20
socket/utils/deepClone.js Normal file
View File

@@ -0,0 +1,20 @@
const deepClone = (obj) => {
if (obj === null || typeof obj !== "object") {
return obj
}
if (Array.isArray(obj)) {
return obj.map(deepClone)
}
const clonedObj = {}
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
clonedObj[key] = deepClone(obj[key])
}
}
return clonedObj
}
export default deepClone

View File

@@ -0,0 +1,14 @@
const generateRoomId = (length = 6) => {
let result = ""
const characters = "0123456789"
const charactersLength = characters.length
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charactersLength)
result += characters.charAt(randomIndex)
}
return result
}
export default generateRoomId

104
socket/utils/round.js Normal file
View File

@@ -0,0 +1,104 @@
import { cooldown, sleep } from "./cooldown.js"
export const startRound = async (game, io, socket) => {
const question = game.questions[game.currentQuestion]
if (!game.started) return
io.to(game.room).emit("game:updateQuestion", {
current: game.currentQuestion + 1,
total: game.questions.length,
})
io.to(game.room).emit("game:status", {
name: "SHOW_PREPARED",
data: {
totalAnswers: game.questions[game.currentQuestion].answers.length,
questionNumber: game.currentQuestion + 1,
},
})
await sleep(2)
if (!game.started) return
io.to(game.room).emit("game:status", {
name: "SHOW_QUESTION",
data: {
question: question.question,
image: question.image,
cooldown: question.cooldown,
},
})
await sleep(question.cooldown)
if (!game.started) return
game.roundStartTime = Date.now()
io.to(game.room).emit("game:status", {
name: "SELECT_ANSWER",
data: {
question: question.question,
answers: question.answers,
image: question.image,
time: question.time,
totalPlayer: game.players.length,
},
})
await cooldown(question.time, io, game.room)
if (!game.started) return
game.players.map(async (player) => {
let playerAnswer = await game.playersAnswer.find((p) => p.id === player.id)
let isCorrect = playerAnswer
? playerAnswer.answer === question.solution
: false
let points =
(isCorrect && Math.round(playerAnswer && playerAnswer.points)) || 0
player.points += points
let sortPlayers = game.players.sort((a, b) => b.points - a.points)
let rank = sortPlayers.findIndex((p) => p.id === player.id) + 1
let aheadPlayer = sortPlayers[rank - 2]
io.to(player.id).emit("game:status", {
name: "SHOW_RESULT",
data: {
correct: isCorrect,
message: isCorrect ? "Nice !" : "Too bad",
points: points,
myPoints: player.points,
rank,
aheadOfMe: aheadPlayer ? aheadPlayer.username : null,
},
})
})
let totalType = {}
game.playersAnswer.forEach(({ answer }) => {
totalType[answer] = (totalType[answer] || 0) + 1
})
// Manager
io.to(game.manager).emit("game:status", {
name: "SHOW_RESPONSES",
data: {
question: game.questions[game.currentQuestion].question,
responses: totalType,
correct: game.questions[game.currentQuestion].solution,
answers: game.questions[game.currentQuestion].answers,
image: game.questions[game.currentQuestion].image,
},
})
game.playersAnswer = []
}