fix(game): fix kickPlayer event to include gameId and improve reset message on player kick

This commit is contained in:
Ralex
2025-10-26 11:32:01 +01:00
parent ba29466c69
commit 2e645ae9d5
7 changed files with 18 additions and 15 deletions

View File

@@ -31,7 +31,6 @@ export interface ServerToClientEvents {
"game:errorMessage": (_message: string) => void "game:errorMessage": (_message: string) => void
"game:startCooldown": () => void "game:startCooldown": () => void
"game:cooldown": (_count: number) => void "game:cooldown": (_count: number) => void
"game:kick": () => void
"game:reset": (_message: string) => void "game:reset": (_message: string) => void
"game:updateQuestion": (_data: { current: number; total: number }) => void "game:updateQuestion": (_data: { current: number; total: number }) => void
"game:playerAnswer": (_count: number) => void "game:playerAnswer": (_count: number) => void
@@ -69,9 +68,7 @@ export interface ClientToServerEvents {
"game:create": (_quizzId: string) => void "game:create": (_quizzId: string) => void
"manager:auth": (_password: string) => void "manager:auth": (_password: string) => void
"manager:reconnect": (_message: { gameId: string }) => void "manager:reconnect": (_message: { gameId: string }) => void
"manager:kickPlayer": ( "manager:kickPlayer": (_message: { gameId: string; playerId: string }) => void
_message: MessageWithoutStatus<{ playerId: string }>
) => void
"manager:startGame": (_message: MessageGameId) => void "manager:startGame": (_message: MessageGameId) => void
"manager:abortQuiz": (_message: MessageGameId) => void "manager:abortQuiz": (_message: MessageGameId) => void
"manager:nextQuestion": (_message: MessageGameId) => void "manager:nextQuestion": (_message: MessageGameId) => void

View File

@@ -104,8 +104,8 @@ io.on("connection", (socket) => {
withGame(gameId, socket, (game) => game.join(socket, data.username)) withGame(gameId, socket, (game) => game.join(socket, data.username))
) )
socket.on("manager:kickPlayer", ({ gameId, data }) => socket.on("manager:kickPlayer", ({ gameId, playerId }) =>
withGame(gameId, socket, (game) => game.kickPlayer(socket, data.playerId)) withGame(gameId, socket, (game) => game.kickPlayer(socket, playerId))
) )
socket.on("manager:startGame", ({ gameId }) => socket.on("manager:startGame", ({ gameId }) =>

View File

@@ -158,7 +158,9 @@ class Game {
this.playerStatus.delete(playerId) this.playerStatus.delete(playerId)
this.io.in(playerId).socketsLeave(this.gameId) this.io.in(playerId).socketsLeave(this.gameId)
this.io.to(player.id).emit("game:kick") this.io
.to(player.id)
.emit("game:reset", "You have been kicked by the manager")
this.io.to(this.manager.id).emit("manager:playerKicked", player.id) this.io.to(this.manager.id).emit("manager:playerKicked", player.id)
this.io.to(this.gameId).emit("game:totalPlayers", this.players.length) this.io.to(this.gameId).emit("game:totalPlayers", this.players.length)

View File

@@ -1,6 +1,7 @@
"use client" "use client"
import { QuizzWithId } from "@rahoot/common/types/game" import { QuizzWithId } from "@rahoot/common/types/game"
import { STATUS } from "@rahoot/common/types/game/status"
import ManagerPassword from "@rahoot/web/components/game/create/ManagerPassword" import ManagerPassword from "@rahoot/web/components/game/create/ManagerPassword"
import SelectQuizz from "@rahoot/web/components/game/create/SelectQuizz" import SelectQuizz from "@rahoot/web/components/game/create/SelectQuizz"
import { useEvent, useSocket } from "@rahoot/web/contexts/socketProvider" import { useEvent, useSocket } from "@rahoot/web/contexts/socketProvider"
@@ -23,7 +24,7 @@ const Manager = () => {
useEvent("manager:gameCreated", ({ gameId, inviteCode }) => { useEvent("manager:gameCreated", ({ gameId, inviteCode }) => {
setGameId(gameId) setGameId(gameId)
setStatus("SHOW_ROOM", { text: "Waiting for the players", inviteCode }) setStatus(STATUS.SHOW_ROOM, { text: "Waiting for the players", inviteCode })
router.push(`/game/manager/${gameId}`) router.push(`/game/manager/${gameId}`)
}) })

View File

@@ -44,11 +44,6 @@ const Game = () => {
} }
}) })
useEvent("game:kick", () => {
router.replace("/")
reset()
})
useEvent("game:reset", (message) => { useEvent("game:reset", (message) => {
router.replace("/") router.replace("/")
reset() reset()

View File

@@ -1,5 +1,6 @@
"use client" "use client"
import { STATUS } from "@rahoot/common/types/game/status"
import Button from "@rahoot/web/components/Button" import Button from "@rahoot/web/components/Button"
import Form from "@rahoot/web/components/Form" import Form from "@rahoot/web/components/Form"
import Input from "@rahoot/web/components/Input" import Input from "@rahoot/web/components/Input"
@@ -11,7 +12,7 @@ import { KeyboardEvent, useState } from "react"
const Username = () => { const Username = () => {
const { socket } = useSocket() const { socket } = useSocket()
const { gameId, login } = usePlayerStore() const { gameId, login, setStatus } = usePlayerStore()
const router = useRouter() const router = useRouter()
const [username, setUsername] = useState("") const [username, setUsername] = useState("")
@@ -30,6 +31,7 @@ const Username = () => {
} }
useEvent("game:successJoin", (gameId) => { useEvent("game:successJoin", (gameId) => {
setStatus(STATUS.WAIT, { text: "Waiting for the players" })
login(username) login(username)
router.replace(`/game/${gameId}`) router.replace(`/game/${gameId}`)

View File

@@ -11,6 +11,7 @@ type Props = {
} }
const Room = ({ data: { text, inviteCode } }: Props) => { const Room = ({ data: { text, inviteCode } }: Props) => {
const { gameId } = useManagerStore()
const { socket } = useSocket() const { socket } = useSocket()
const { players } = useManagerStore() const { players } = useManagerStore()
const [playerList, setPlayerList] = useState<Player[]>(players) const [playerList, setPlayerList] = useState<Player[]>(players)
@@ -33,8 +34,13 @@ const Room = ({ data: { text, inviteCode } }: Props) => {
}) })
const handleKick = (playerId: string) => () => { const handleKick = (playerId: string) => () => {
if (!gameId) {
return
}
socket?.emit("manager:kickPlayer", { socket?.emit("manager:kickPlayer", {
data: { playerId }, gameId,
playerId,
}) })
} }