Skip to content

Commit

Permalink
refactor: handle game reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Neosoulink committed Dec 18, 2024
1 parent 97bc7b4 commit 242d1da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
16 changes: 10 additions & 6 deletions apps/api/src/players/services/players.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ export const WithHumanComponent: FC<WithHumanComponentProps> = () => {
}

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(() => {
Expand Down

0 comments on commit 242d1da

Please sign in to comment.