mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
Add Start cooldown & Patch socket event
This commit is contained in:
@@ -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),
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
},
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
26
socket/src/utils/cooldown.js
Normal file
26
socket/src/utils/cooldown.js
Normal 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))
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user