adding pause and resume in game state

This commit is contained in:
RandyJC
2025-12-09 14:02:52 +01:00
parent 3ac9d5ac39
commit ab7ddfed4b
6 changed files with 72 additions and 12 deletions

View File

@@ -234,6 +234,10 @@ io.on("connection", (socket) => {
withGame(gameId, socket, (game) => game.resumeCooldown(socket))
)
socket.on("manager:setBreak", ({ gameId, active }) =>
withGame(gameId, socket, (game) => game.setBreak(socket, active))
)
socket.on("manager:endGame", ({ gameId }) =>
withGame(gameId, socket, (game) => game.endGame(socket, registry))
)

View File

@@ -46,6 +46,7 @@ class Game {
timer: NodeJS.Timeout | null
resolve: (() => void) | null
}
breakActive: boolean
constructor(io: Server, socket: Socket, quizz: Quizz) {
if (!io) {
@@ -84,6 +85,7 @@ class Game {
timer: null,
resolve: null,
}
this.breakActive = false
const roomInvite = createInviteCode()
this.inviteCode = roomInvite
@@ -137,6 +139,7 @@ class Game {
timer: null,
resolve: null,
}
game.breakActive = snapshot.breakActive || false
if (game.cooldown.active && game.cooldown.remaining > 0 && !game.cooldown.paused) {
game.startCooldown(game.cooldown.remaining)
@@ -193,6 +196,7 @@ class Game {
paused: this.cooldown.paused,
remaining: this.cooldown.remaining,
},
breakActive: this.breakActive,
}
}
@@ -453,6 +457,27 @@ class Game {
this.persist()
}
setBreak(socket: Socket, active: boolean) {
if (this.manager.id !== socket.id) {
return
}
this.breakActive = active
if (this.cooldown.active) {
if (active) {
this.cooldown.paused = true
} else {
this.cooldown.paused = false
}
this.io.to(this.gameId).emit("game:cooldownPause", this.cooldown.paused)
}
this.io.to(this.gameId).emit("game:break", active)
this.io.to(this.manager.id).emit("manager:break", active)
this.persist()
}
skipQuestionIntro(socket: Socket) {
if (this.manager.id !== socket.id) {
return