mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
Fix EsLint config & update packages
This commit is contained in:
@@ -52,6 +52,7 @@ io.on("connection", (socket) => {
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
console.log(`user disconnected ${socket.id}`)
|
||||
|
||||
if (gameState.manager === socket.id) {
|
||||
console.log("Reset game")
|
||||
io.to(gameState.room).emit("game:reset")
|
||||
|
||||
@@ -8,22 +8,24 @@ const Manager = {
|
||||
createRoom: (game, io, socket, password) => {
|
||||
if (game.password !== password) {
|
||||
io.to(socket.id).emit("game:errorMessage", "Bad Password")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (game.manager || game.room) {
|
||||
io.to(socket.id).emit("game:errorMessage", "Already manager")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
let roomInvite = generateRoomId()
|
||||
const roomInvite = generateRoomId()
|
||||
game.room = roomInvite
|
||||
game.manager = socket.id
|
||||
|
||||
socket.join(roomInvite)
|
||||
io.to(socket.id).emit("manager:inviteCode", roomInvite)
|
||||
|
||||
console.log("New room created: " + roomInvite)
|
||||
console.log(`New room created: ${roomInvite}`)
|
||||
},
|
||||
|
||||
kickPlayer: (game, io, socket, playerId) => {
|
||||
@@ -75,7 +77,7 @@ const Manager = {
|
||||
return
|
||||
}
|
||||
|
||||
game.currentQuestion++
|
||||
game.currentQuestion += 1
|
||||
startRound(game, io, socket)
|
||||
},
|
||||
|
||||
@@ -101,7 +103,9 @@ const Manager = {
|
||||
},
|
||||
})
|
||||
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
game = deepClone(GAME_STATE_INIT)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,13 @@ const Player = {
|
||||
await inviteCodeValidator.validate(roomId)
|
||||
} catch (error) {
|
||||
socket.emit("game:errorMessage", error.errors[0])
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (!game.room || roomId !== game.room) {
|
||||
socket.emit("game:errorMessage", "Room not found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -24,21 +26,25 @@ const Player = {
|
||||
await usernameValidator.validate(player.username)
|
||||
} catch (error) {
|
||||
socket.emit("game:errorMessage", error.errors[0])
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (!game.room || player.room !== game.room) {
|
||||
socket.emit("game:errorMessage", "Room not found")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (game.players.find((p) => p.username === player.username)) {
|
||||
socket.emit("game:errorMessage", "Username already exists")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (game.started) {
|
||||
socket.emit("game:errorMessage", "Game already started")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -46,7 +52,7 @@ const Player = {
|
||||
|
||||
socket.join(player.room)
|
||||
|
||||
let playerData = {
|
||||
const playerData = {
|
||||
username: player.username,
|
||||
room: player.room,
|
||||
id: socket.id,
|
||||
@@ -64,6 +70,7 @@ const Player = {
|
||||
|
||||
selectedAnswer: (game, io, socket, answerKey) => {
|
||||
const player = game.players.find((player) => player.id === socket.id)
|
||||
|
||||
const question = game.questions[game.currentQuestion]
|
||||
|
||||
if (!player) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
let cooldownTimeout
|
||||
let cooldownResolve
|
||||
let cooldownTimeout = null
|
||||
let cooldownResolve = null
|
||||
|
||||
export const abortCooldown = () => {
|
||||
clearInterval(cooldownTimeout)
|
||||
@@ -20,10 +20,11 @@ export const cooldown = (ms, io, room) => {
|
||||
clearInterval(cooldownTimeout)
|
||||
resolve()
|
||||
}
|
||||
|
||||
io.to(room).emit("game:cooldown", count)
|
||||
count -= 1
|
||||
}, 1000)
|
||||
})
|
||||
}
|
||||
|
||||
export const sleep = (sec) => new Promise((r) => setTimeout(r, sec * 1000))
|
||||
export const sleep = (sec) => new Promise((r) => void setTimeout(r, sec * 1000))
|
||||
|
||||
@@ -9,7 +9,7 @@ const deepClone = (obj) => {
|
||||
|
||||
const clonedObj = {}
|
||||
for (const key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
if (Object.hasOwn(obj, key)) {
|
||||
clonedObj[key] = deepClone(obj[key])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ const generateRoomId = (length = 6) => {
|
||||
const characters = "0123456789"
|
||||
const charactersLength = characters.length
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
for (let i = 0; i < length; i += 1) {
|
||||
const randomIndex = Math.floor(Math.random() * charactersLength)
|
||||
result += characters.charAt(randomIndex)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { cooldown, sleep } from "./cooldown.js"
|
||||
|
||||
export const startRound = async (game, io, socket) => {
|
||||
export const startRound = async (game, io) => {
|
||||
const question = game.questions[game.currentQuestion]
|
||||
|
||||
if (!game.started) return
|
||||
if (!game.started) {
|
||||
return
|
||||
}
|
||||
|
||||
io.to(game.room).emit("game:updateQuestion", {
|
||||
current: game.currentQuestion + 1,
|
||||
@@ -20,7 +22,9 @@ export const startRound = async (game, io, socket) => {
|
||||
|
||||
await sleep(2)
|
||||
|
||||
if (!game.started) return
|
||||
if (!game.started) {
|
||||
return
|
||||
}
|
||||
|
||||
io.to(game.room).emit("game:status", {
|
||||
name: "SHOW_QUESTION",
|
||||
@@ -33,7 +37,9 @@ export const startRound = async (game, io, socket) => {
|
||||
|
||||
await sleep(question.cooldown)
|
||||
|
||||
if (!game.started) return
|
||||
if (!game.started) {
|
||||
return
|
||||
}
|
||||
|
||||
game.roundStartTime = Date.now()
|
||||
|
||||
@@ -50,31 +56,35 @@ export const startRound = async (game, io, socket) => {
|
||||
|
||||
await cooldown(question.time, io, game.room)
|
||||
|
||||
if (!game.started) return
|
||||
if (!game.started) {
|
||||
return
|
||||
}
|
||||
|
||||
game.players.map(async (player) => {
|
||||
let playerAnswer = await game.playersAnswer.find((p) => p.id === player.id)
|
||||
const playerAnswer = await game.playersAnswer.find(
|
||||
(p) => p.id === player.id,
|
||||
)
|
||||
|
||||
let isCorrect = playerAnswer
|
||||
const isCorrect = playerAnswer
|
||||
? playerAnswer.answer === question.solution
|
||||
: false
|
||||
|
||||
let points =
|
||||
const points =
|
||||
(isCorrect && Math.round(playerAnswer && playerAnswer.points)) || 0
|
||||
|
||||
player.points += points
|
||||
|
||||
let sortPlayers = game.players.sort((a, b) => b.points - a.points)
|
||||
const 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]
|
||||
const rank = sortPlayers.findIndex((p) => p.id === player.id) + 1
|
||||
const aheadPlayer = sortPlayers[rank - 2]
|
||||
|
||||
io.to(player.id).emit("game:status", {
|
||||
name: "SHOW_RESULT",
|
||||
data: {
|
||||
correct: isCorrect,
|
||||
message: isCorrect ? "Nice !" : "Too bad",
|
||||
points: points,
|
||||
points,
|
||||
myPoints: player.points,
|
||||
rank,
|
||||
aheadOfMe: aheadPlayer ? aheadPlayer.username : null,
|
||||
@@ -82,7 +92,7 @@ export const startRound = async (game, io, socket) => {
|
||||
})
|
||||
})
|
||||
|
||||
let totalType = {}
|
||||
const totalType = {}
|
||||
|
||||
game.playersAnswer.forEach(({ answer }) => {
|
||||
totalType[answer] = (totalType[answer] || 0) + 1
|
||||
|
||||
Reference in New Issue
Block a user