From cfdc1def480c6955eaf36b4c2e45d8536a98e9d8 Mon Sep 17 00:00:00 2001 From: vvandenb Date: Sat, 11 Mar 2023 17:00:36 +0100 Subject: [PATCH] * Pong backend: Fixed matchmaking bugs * Auth backend: reduced logs size on login --- back/volume/src/auth/42.strategy.ts | 3 +-- back/volume/src/pong/game/MatchmakingQueue.ts | 15 ++++++++------- back/volume/src/pong/pong.gateway.ts | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/back/volume/src/auth/42.strategy.ts b/back/volume/src/auth/42.strategy.ts index 8d4a0cc..8beab27 100644 --- a/back/volume/src/auth/42.strategy.ts +++ b/back/volume/src/auth/42.strategy.ts @@ -30,9 +30,8 @@ export class FtStrategy extends PassportStrategy(Strategy, '42') { cb: VerifyCallback ): Promise { request.session.accessToken = accessToken - console.log('accessToken', accessToken, 'refreshToken', refreshToken) const ftId = profile.id as number - console.log(profile) + console.log('Validated ', profile.username) if ((await this.usersService.findUser(ftId)) === null) { const newUser = new User() newUser.ftId = profile.id as number diff --git a/back/volume/src/pong/game/MatchmakingQueue.ts b/back/volume/src/pong/game/MatchmakingQueue.ts index 12932d1..666f5a2 100644 --- a/back/volume/src/pong/game/MatchmakingQueue.ts +++ b/back/volume/src/pong/game/MatchmakingQueue.ts @@ -12,23 +12,24 @@ export class MatchmakingQueue { this.queue = [] } - addPlayer (name: string, socket: WebSocket, uuid: string): boolean { - let succeeded: boolean = false - if (!this.alreadyInQueue(name)) { + addPlayer (name: string, socket: WebSocket, uuid: string): void { + if (!this.isInQueue(name)) { + console.log('Adding player to queue: ', name) this.queue.push({ name, socket, uuid }) if (this.canCreateGame()) { this.createGame() } - succeeded = true } - return succeeded } removePlayer (name: string): void { - this.queue = this.queue.filter((player) => player.name !== name) + if (this.isInQueue(name)) { + console.log('Removing player from queue: ', name) + this.queue = this.queue.filter((player) => player.name !== name) + } } - alreadyInQueue (name: string): boolean { + isInQueue (name: string): boolean { return this.queue.some((player) => player.name === name) } diff --git a/back/volume/src/pong/pong.gateway.ts b/back/volume/src/pong/pong.gateway.ts index c30ddbe..5f707b3 100644 --- a/back/volume/src/pong/pong.gateway.ts +++ b/back/volume/src/pong/pong.gateway.ts @@ -62,6 +62,7 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect { } if (name !== undefined) { console.log('Disconnected ', this.socketToPlayerName.get(client)) + this.matchmakingQueue.removePlayer(name) this.socketToPlayerName.delete(client) } } @@ -202,20 +203,19 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect { client: WebSocketWithId, @MessageBody() matchmakingUpdateData: MatchmakingDtoValidated ): { event: string, data: MatchmakingDtoValidated } { - let isMatchmaking: boolean = false + let matchmaking: boolean = false const name: string | undefined = this.socketToPlayerName.get(client) if (name !== undefined) { - if (matchmakingUpdateData.matchmaking) { - if (this.matchmakingQueue.addPlayer(name, client, client.id)) { - isMatchmaking = true - } + if (matchmakingUpdateData.matchmaking && !this.games.isInAGame(name)) { + this.matchmakingQueue.addPlayer(name, client, client.id) } else { this.matchmakingQueue.removePlayer(name) } + matchmaking = this.matchmakingQueue.isInQueue(name) } return { event: GAME_EVENTS.MATCHMAKING, - data: { matchmaking: isMatchmaking } + data: { matchmaking } } }