Add Start cooldown & Patch socket event

This commit is contained in:
Ralex91
2024-02-02 13:55:55 +01:00
parent 6c7f31a6d4
commit cd723f7efd
19 changed files with 298 additions and 151 deletions

View File

@@ -17,6 +17,10 @@ io.listen(5157)
io.on("connection", (socket) => {
console.log(`A user connected ${socket.id}`)
socket.on("player:checkRoom", (roomId) =>
Player.checkRoom(gameState, io, socket, roomId),
)
socket.on("player:join", (player) =>
Player.join(gameState, io, socket, player),
)

View File

@@ -1,6 +1,7 @@
import { GAME_STATE_INIT } from "../quizz.config.js"
import { startRound } from "../utils/round.js"
import generateRoomId from "../utils/generateRoomId.js"
import { cooldown, sleep } from "../utils/cooldown.js"
const Manager = {
createRoom: (game, io, socket) => {
@@ -31,13 +32,24 @@ const Manager = {
io.to(game.manager).emit("manager:playerKicked", player.id)
},
startGame: (game, io, socket) => {
startGame: async (game, io, socket) => {
if (game.started || !game.room) {
return
}
game.started = true
io.to(game.room).emit("startGame", game.room)
io.to(game.room).emit("game:status", {
name: "SHOW_START",
data: {
time: 3,
subject: "Adobe",
},
})
await sleep(3)
io.to(game.room).emit("game:startCooldown")
await cooldown(3, io, game.room)
startRound(game, io, socket)
},

View File

@@ -1,7 +1,17 @@
import convertTimeToPoint from "../utils/convertTimeToPoint.js"
import { abortCooldown } from "../utils/round.js"
import { abortCooldown } from "../utils/cooldown.js"
const Player = {
checkRoom: (game, io, socket, roomId) => {
if (!game.room || roomId !== game.room) {
io.to(socket.id).emit("game:errorMessage", "Room not found")
console.log("zaza")
return
}
io.to(socket.id).emit("game:successRoom", roomId)
},
join: (game, io, socket, player) => {
if (!player.room || !player.room || game.started) {
return

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))

View File

@@ -1,29 +1,4 @@
let cooldownTimeout
let cooldownResolve
export const abortCooldown = () => {
clearInterval(cooldownTimeout)
cooldownResolve()
}
function 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)
})
}
const sleep = (sec) => new Promise((r) => setTimeout(r, sec * 1000))
import { cooldown, sleep } from "./cooldown.js"
export const startRound = async (game, io, socket) => {
const question = game.questions[game.currentQuestion]