Skip to content

Commit

Permalink
feat: activate loggers on game
Browse files Browse the repository at this point in the history
  • Loading branch information
iaurg committed Nov 27, 2023
1 parent dc90d62 commit 5d1555b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions backend/src/game/game.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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')
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions backend/src/game/lobby/game.lobby.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { GameDto } from '../dto/game.dto';

@Injectable()
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit 5d1555b

Please sign in to comment.