diff --git a/backend/src/game/game.gateway.ts b/backend/src/game/game.gateway.ts index e95fbdc1..c06d2d45 100644 --- a/backend/src/game/game.gateway.ts +++ b/backend/src/game/game.gateway.ts @@ -11,12 +11,14 @@ import { GameDto } from './dto/game.dto'; import { GameLobbyService } from './lobby/game.lobby.service'; import { GameMoveDto } from './dto/game.move'; import { JwtService } from '@nestjs/jwt'; +import { Logger } from '@nestjs/common'; @WebSocketGateway({ namespace: '/game' }) export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect { private PADDLE_WIDTH = 10; private PADDLE_HEIGHT = 150; private FRAMES_PER_SECOND = 60; + private readonly logger = new Logger(GameGateway.name); constructor( private gameService: GameService, @@ -41,14 +43,14 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect { // Decode user from JWT const decodedUser = this.jwtService.decode(user).sub; - console.log(`Client ${decodedUser} connected`); + this.logger.log(`Client ${decodedUser} connected`); } handleDisconnect(client: Socket) { const gameId = this.finishGame(client); client.leave(gameId); this.gameServer.to(gameId).emit('gameAbandoned', this.gamesPlaying[gameId]); - console.log(`Client ${client.id} disconnected`); + this.logger.log(`Client ${client.id} disconnected`); } @SubscribeMessage('joinGame') @@ -57,7 +59,7 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect { const decodedUser = this.jwtService.decode(user).sub; if (this.gameLobby.joinPlayer1(client, decodedUser)) { - console.log(`waiting Player 2`); + this.logger.log(`Client ${client.id} joined game`); client.emit('waitingPlayer2', `game_${client.id}`); } else { const game = this.gameLobby.joinPlayer2(client, decodedUser); @@ -77,10 +79,12 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect { this.gameService.addPoint(game); this.gameService.restartBall(game); } + if (this.gameService.isGameFinished(game)) { this.gameServer.to(gameId).emit('gameFinished', game); this.finishGame(client); } + if (game.finished) { return; } diff --git a/backend/src/game/lobby/game.lobby.service.ts b/backend/src/game/lobby/game.lobby.service.ts index 6d559b83..685a5c9c 100644 --- a/backend/src/game/lobby/game.lobby.service.ts +++ b/backend/src/game/lobby/game.lobby.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { GameDto } from '../dto/game.dto'; @Injectable() @@ -9,13 +9,14 @@ export class GameLobbyService { private PLAYER_INITIAL_X = 0; private CANVAS_WIDTH = 858; private CANVAS_HEIGHT = 525; + private readonly logger = new Logger(GameLobbyService.name); joinPlayer1(player: any, login: string): boolean { if (this.lobby.length == 0) { const gameDto = this.initGame(player.id); this.lobby.push(gameDto); gameDto.player1.login = login; - console.log('player 1 joined'); + this.logger.log(`Client player 1 joined`); player.join(`game_${gameDto.player1.socketId}`); return true; } else { @@ -35,7 +36,7 @@ export class GameLobbyService { height: this.PADDLE_HEIGHT, }; player.join(`game_${gameDto.player1.socketId}`); - console.log('Player 2 joined'); + this.logger.log(`Client player 2 joined`); this.lobby.splice(0, 1); return gameDto; }