From d56abe3693207f643f55b440472551e81958c86e Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 Jul 2024 09:57:43 +0200 Subject: [PATCH] Fix : opti : use equals instead of toHexString comparisons --- src/games/services/games.lobby.service.ts | 6 +++--- src/games/services/turn.service.ts | 2 +- src/games/utils.ts | 10 +++------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/games/services/games.lobby.service.ts b/src/games/services/games.lobby.service.ts index c0392d2..4d3dc31 100644 --- a/src/games/services/games.lobby.service.ts +++ b/src/games/services/games.lobby.service.ts @@ -179,7 +179,7 @@ export class LobbyService { await this.gamesService.fillMarket(game, session); for (const team of game.teams) { for (const player of team.players) { - if (team._id.toHexString() == game.currentTurn.currentTeam) { + if (team._id.equals(game.currentTurn.currentTeam)) { await this.gamesService.drawToHand(3, player, game, session); } else { await this.gamesService.drawToHand(5, player, game, session); @@ -187,8 +187,8 @@ export class LobbyService { } } - const currentTeam = game.teams.find( - (t) => t._id.toHexString() == game.currentTurn.currentTeam, + const currentTeam = game.teams.find((t) => + t._id.equals(game.currentTurn.currentTeam), ); for (const player of currentTeam.players) { diff --git a/src/games/services/turn.service.ts b/src/games/services/turn.service.ts index 3fe4d2a..0fc58c1 100644 --- a/src/games/services/turn.service.ts +++ b/src/games/services/turn.service.ts @@ -24,7 +24,7 @@ export class TurnService { cardId: string, session?: mongoose.mongo.ClientSession, ) { - const card = player.board.cards.find((c) => c._id.toHexString() == cardId); + const card = player.board.cards.find((c) => c._id.equals(cardId)); if (!card) { throw new HttpException( 'Card not found in player board', diff --git a/src/games/utils.ts b/src/games/utils.ts index e89c29d..b04197b 100644 --- a/src/games/utils.ts +++ b/src/games/utils.ts @@ -16,9 +16,7 @@ export function shuffle(a) { } export function getCurrentTeam(game: Document & Game) { - return game.teams.find( - (t) => t._id.toHexString() == game.currentTurn.currentTeam, - ); + return game.teams.find((t) => t._id.equals(game.currentTurn.currentTeam)); } export function getPlayer(session: Record, game: Game) { @@ -28,7 +26,7 @@ export function getPlayer(session: Record, game: Game) { } for (const team of game.teams) { for (const player of team.players) { - if (player.user._id.toHexString() == user._id) { + if (player.user._id.equals(user._id)) { return player as Document & Player; } } @@ -40,9 +38,7 @@ export function getPlayer(session: Record, game: Game) { } export function isInTeam(team: Team, player: Player) { - if ( - team.players.some((p) => p._id.toHexString() == player._id.toHexString()) - ) { + if (team.players.some((p) => p._id.equals(player._id))) { return true; } return false;