From 242d1dae8c55ce8cef5100f5252f8191e5199805 Mon Sep 17 00:00:00 2001 From: Nathan Mande Date: Wed, 18 Dec 2024 05:38:39 +0200 Subject: [PATCH] refactor: handle game reset --- apps/api/src/players/services/players.service.ts | 16 ++++++++++------ .../main-menu/new-section-human.component.tsx | 1 + .../play/_components/with-human.component.tsx | 3 ++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/apps/api/src/players/services/players.service.ts b/apps/api/src/players/services/players.service.ts index 44e7d1b..f09cfdf 100644 --- a/apps/api/src/players/services/players.service.ts +++ b/apps/api/src/players/services/players.service.ts @@ -62,7 +62,7 @@ export class PlayersService { }); const waitingRoomID = this.randomGameRoomsPool.find( - (roomID) => this.rooms[roomID].players.length === 1 + (roomID) => this.rooms[roomID]?.players?.length === 1 ); player.color = getOppositeColorSide( @@ -85,13 +85,17 @@ export class PlayersService { randomGame === "true" && this.randomGameRoomsPool.length > 0 ) { - roomID = this.randomGameRoomsPool.pop() as UUID; - const room = this.rooms[roomID]; + const _roomID = this.randomGameRoomsPool.pop() as UUID; + const room = this.rooms[_roomID]; - player.color = getOppositeColorSide(room.players[0].color); - player.host = false; + if (room) { + roomID = _roomID; - this.rooms[roomID].players.push(player); + player.color = getOppositeColorSide(room.players[0].color); + player.host = false; + + this.rooms[roomID].players.push(player); + } } if (!roomID) { diff --git a/apps/web/src/routes/_components/main-menu/new-section-human.component.tsx b/apps/web/src/routes/_components/main-menu/new-section-human.component.tsx index 70069df..d4fb033 100644 --- a/apps/web/src/routes/_components/main-menu/new-section-human.component.tsx +++ b/apps/web/src/routes/_components/main-menu/new-section-human.component.tsx @@ -72,6 +72,7 @@ export const NewGameHumanSection: FC = () => { to="/play?mode=human&random=true" type="submit" className="shadow-md p-2 rounded capitalize flex justify-center items-center" + onClick={() => resetGame()} > Random Match diff --git a/apps/web/src/routes/play/_components/with-human.component.tsx b/apps/web/src/routes/play/_components/with-human.component.tsx index 8a79774..359ce2e 100644 --- a/apps/web/src/routes/play/_components/with-human.component.tsx +++ b/apps/web/src/routes/play/_components/with-human.component.tsx @@ -123,9 +123,10 @@ export const WithHumanComponent: FC = () => { } resetBoardPieces(data.room.fen); + setSearchParams((prev) => [...prev, ["roomID", data.roomID]]); console.log("Joined room:", data); }, - [currentPlayer, resetBoardPieces, socket] + [currentPlayer, resetBoardPieces, setSearchParams, socket] ); const onDisconnect = useCallback(() => {