mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-13 20:15:35 +01:00
121 lines
2.8 KiB
JavaScript
121 lines
2.8 KiB
JavaScript
import { GAME_STATE_INIT } from "../../config.mjs"
|
|
import { abortCooldown, cooldown, sleep } from "../utils/cooldown.js"
|
|
import deepClone from "../utils/deepClone.js"
|
|
import generateRoomId from "../utils/generateRoomId.js"
|
|
import { startRound } from "../utils/round.js"
|
|
|
|
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()
|
|
game.room = roomInvite
|
|
game.manager = socket.id
|
|
|
|
socket.join(roomInvite)
|
|
io.to(socket.id).emit("manager:inviteCode", roomInvite)
|
|
|
|
console.log("New room created: " + roomInvite)
|
|
},
|
|
|
|
kickPlayer: (game, io, socket, playerId) => {
|
|
if (game.manager !== socket.id) {
|
|
return
|
|
}
|
|
|
|
const player = game.players.find((p) => p.id === playerId)
|
|
game.players = game.players.filter((p) => p.id !== playerId)
|
|
|
|
io.in(playerId).socketsLeave(game.room)
|
|
io.to(player.id).emit("game:kick")
|
|
io.to(game.manager).emit("manager:playerKicked", player.id)
|
|
|
|
// Emit updated total players to all clients in the room
|
|
io.to(game.room).emit("game:totalPlayers", game.players.length)
|
|
},
|
|
|
|
startGame: async (game, io, socket) => {
|
|
if (game.started || !game.room) {
|
|
return
|
|
}
|
|
|
|
game.started = true
|
|
io.to(game.room).emit("game:status", {
|
|
name: "SHOW_START",
|
|
data: {
|
|
time: 3,
|
|
subject: game.subject,
|
|
},
|
|
})
|
|
|
|
await sleep(3)
|
|
io.to(game.room).emit("game:startCooldown")
|
|
|
|
await cooldown(3, io, game.room)
|
|
startRound(game, io, socket)
|
|
},
|
|
|
|
nextQuestion: (game, io, socket) => {
|
|
if (!game.started) {
|
|
return
|
|
}
|
|
|
|
if (socket.id !== game.manager) {
|
|
return
|
|
}
|
|
|
|
if (!game.questions[game.currentQuestion + 1]) {
|
|
return
|
|
}
|
|
|
|
game.currentQuestion++
|
|
startRound(game, io, socket)
|
|
},
|
|
|
|
abortQuiz: (game, io, socket) => {
|
|
if (!game.started) {
|
|
return
|
|
}
|
|
|
|
if (socket.id !== game.manager) {
|
|
return
|
|
}
|
|
|
|
abortCooldown(game, io, game.room)
|
|
},
|
|
|
|
showLoaderboard: (game, io, socket) => {
|
|
if (!game.questions[game.currentQuestion + 1]) {
|
|
socket.emit("game:status", {
|
|
name: "FINISH",
|
|
data: {
|
|
subject: game.subject,
|
|
top: game.players.slice(0, 3).sort((a, b) => b.points - a.points),
|
|
},
|
|
})
|
|
|
|
game = deepClone(GAME_STATE_INIT)
|
|
return
|
|
}
|
|
|
|
socket.emit("game:status", {
|
|
name: "SHOW_LEADERBOARD",
|
|
data: {
|
|
leaderboard: game.players
|
|
.sort((a, b) => b.points - a.points)
|
|
.slice(0, 5),
|
|
},
|
|
})
|
|
},
|
|
}
|
|
|
|
export default Manager
|