Skip to content

Commit

Permalink
fix(api): disconnect player with invalid room id
Browse files Browse the repository at this point in the history
  • Loading branch information
Neosoulink committed Nov 27, 2024
1 parent e62d4b9 commit c01cbc1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
22 changes: 12 additions & 10 deletions apps/api/src/players/gateways/players.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}`
);
Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions apps/api/src/players/services/players.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down

0 comments on commit c01cbc1

Please sign in to comment.