From c01cbc17bfef4b7ce50df123456e34cf6544a8b7 Mon Sep 17 00:00:00 2001 From: Nathan Mande Date: Wed, 27 Nov 2024 10:03:49 +0200 Subject: [PATCH] fix(api): disconnect player with invalid room id --- .../src/players/gateways/players.gateway.ts | 22 ++++++++++--------- .../src/players/services/players.service.ts | 13 +++++------ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/apps/api/src/players/gateways/players.gateway.ts b/apps/api/src/players/gateways/players.gateway.ts index b4a9041..dd509dd 100644 --- a/apps/api/src/players/gateways/players.gateway.ts +++ b/apps/api/src/players/gateways/players.gateway.ts @@ -32,20 +32,22 @@ export class PlayersGateway constructor(private readonly playersService: PlayersService) {} - private handleError(socket: Socket, error: Error): void { - this.handleDisconnect(socket); + private handleError(error: Error, socket?: Socket): void { + if (socket) { + this.handleDisconnect(socket); - this.server.to(socket.id).emit("error", { - message: error.message, - cause: error.cause - }); + this.server.to(socket.id).emit("error", { + message: error.message, + cause: error.cause + }); + } console.warn("Error occurred:", error.message, `<${error.cause}>`); } handleConnection(@ConnectedSocket() socket: Socket): void { const data = this.playersService.register(socket); - if (data instanceof Error) return this.handleError(socket, data); + if (data instanceof Error) return this.handleError(data, socket); const { player, roomID, room } = data; @@ -67,13 +69,13 @@ export class PlayersGateway } handleDisconnect(socket: Socket): void { + this.server.to(socket.id).disconnectSockets(); const unregisterRes = this.playersService.unregister(socket); - if (unregisterRes instanceof Error) return; + if (unregisterRes instanceof Error) return this.handleError(unregisterRes); const { player, roomID, room } = unregisterRes; - this.server.to(player.id).disconnectSockets(); console.log( `\nPlayer "${player?.id}" left room "${roomID}".\nTotal in rooms: ${room?.players.length ?? 0}` ); @@ -86,7 +88,7 @@ export class PlayersGateway ): void { const data = this.playersService.handleMove(socket, payload.move); - if (data instanceof Error) return this.handleError(socket, data); + if (data instanceof Error) return this.handleError(data, socket); console.log("\nMove performed by", socket.id, payload); this.server diff --git a/apps/api/src/players/services/players.service.ts b/apps/api/src/players/services/players.service.ts index 8afc739..efb4b51 100644 --- a/apps/api/src/players/services/players.service.ts +++ b/apps/api/src/players/services/players.service.ts @@ -40,7 +40,7 @@ export class PlayersService { typeof queryRoomID === "string" && !Array.isArray(this.rooms[queryRoomID]?.players) ) - return new Error("Invalid room ID.", { cause: "ROOM_NOT_FOUND" }); + return new Error("Unable to register.", { cause: "ROOM_NOT_FOUND" }); const player: PlayerEntity = { id: socket.id, @@ -51,7 +51,7 @@ export class PlayersService { if (typeof queryRoomID === "string") { if (this.rooms[queryRoomID]?.players?.length !== 1) - return new Error("Unable to join a room without a player or full.", { + return new Error("Unable to join an empty or full room.", { cause: this.rooms[queryRoomID] ? this.rooms[queryRoomID].players.length > 1 ? "ROOM_FULL" @@ -87,7 +87,8 @@ export class PlayersService { const { roomID } = socket.data; const room = this.rooms[roomID]; - if (!room) return new Error("Room not found.", { cause: "ROOM_NOT_FOUND" }); + if (!room) + return new Error("Unable to unregister.", { cause: "ROOM_NOT_FOUND" }); let player: PlayerEntity | undefined; @@ -111,15 +112,13 @@ export class PlayersService { handleMove(socket: Socket, move?: Move): string | Error { if (typeof move?.after !== "string" || !validateFen(move.after)) - return new Error("Invalid move.", { cause: "INVALID_MOVE" }); + return new Error("Unable to perform move.", { cause: "INVALID_MOVE" }); const roomID = socket.data?.roomID; const room = this.rooms[roomID]; if (!room || room.fen !== move.before) - return new Error("Move desynchronized with the room fen.", { - cause: "DESYNCHRONIZED" - }); + return new Error("Move desynchronized.", { cause: "DESYNCHRONIZED" }); this.rooms[roomID].fen = move.after;