From eb1a5b47dd6096b8c8ec674c4c46c1ac7409240e Mon Sep 17 00:00:00 2001 From: batche Date: Wed, 21 Dec 2022 17:13:50 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20ranking=20c=C3=B4t=C3=A9=20back,?= =?UTF-8?q?=20reste=20=C3=A0=20l'impl=C3=A9menter=20c=C3=B4t=C3=A9=20front?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api_back/src/game/game.controller.ts | 32 ++++++++++++------- .../nestjs/api_back/src/game/game.service.ts | 21 +++++++++++- srcs/requirements/nginx/conf/default.conf | 4 +++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/srcs/requirements/nestjs/api_back/src/game/game.controller.ts b/srcs/requirements/nestjs/api_back/src/game/game.controller.ts index 540ebe3d..ed867b4f 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.controller.ts @@ -16,6 +16,16 @@ import { GameService } from './game.service'; export class GameController { constructor (private readonly gameService : GameService) { } + + @Get('ranking') + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + async getRankingForAllUsers(@Req() req) + { + const currentUser : User = req.user + return this.gameService.getRankingForAllUsers(currentUser); + } + @Post('ticket') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) @@ -38,17 +48,6 @@ export class GameController { return this.gameService.requestIfAnotherUserHasRespondToquestForGame(user, token); } - - //N'est valable que pour le game-serveur. - @Post('gameserver/validate') - async validateTicket(@Body() validateTicketDto : ValidateTicketDto, @Req() request) - { - if (await this.gameService.validateToken(validateTicketDto) === false) - return new HttpException("The token is not valid", HttpStatus.NOT_FOUND); - console.log("200 retourné côté nest") - return HttpStatus.OK; - } - @Post('decline') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) @@ -76,6 +75,17 @@ export class GameController { return this.gameService.findInvitations(user); } + // + //N'est valable que pour le game-serveur. + @Post('gameserver/validate') + async validateTicket(@Body() validateTicketDto : ValidateTicketDto, @Req() request) + { + if (await this.gameService.validateToken(validateTicketDto) === false) + return new HttpException("The token is not valid", HttpStatus.NOT_FOUND); + console.log("200 retourné côté nest") + return HttpStatus.OK; + } + @Post('gameserver/creategame') async createGame(@Body() creategameDto : CreateGameDto) { diff --git a/srcs/requirements/nestjs/api_back/src/game/game.service.ts b/srcs/requirements/nestjs/api_back/src/game/game.service.ts index 11fdf377..b7904f7a 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.service.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.service.ts @@ -11,6 +11,7 @@ import { TokenGame } from './entity/tokenGame.entity'; import { UsersService } from 'src/users/users.service'; import { CreateGameDto } from './dto/createGame.dto'; import { UpdateGameDto } from './dto/updateGame.dto'; +import { FriendshipService } from 'src/friendship/friendship.service'; @Injectable() export class GameService { @@ -21,9 +22,27 @@ export class GameService { private readonly userRepository : Repository, @InjectRepository(TokenGame) private readonly tokenGameRepository : Repository, - private readonly userService : UsersService + private readonly userService : UsersService, + private readonly friendShipService : FriendshipService ) { } + async getRankingForAllUsers(currentUser : User) { + const users = await this.userRepository.createQueryBuilder("user") + .leftJoinAndSelect("user.stats", "stats") + .orderBy('stats.winGame', "DESC") + .getMany(); + if (!users) + return new HttpException("No ranking for now.", HttpStatus.NOT_FOUND) + const partialUser : Partial[] = [] + for (const user of users) + { + if (await this.friendShipService.findIfUserIsBlockedOrHasBlocked(currentUser.id.toString(), user.id.toString()) === false) + partialUser.push({username : user.username, stats : user.stats }) + } + return partialUser; + } + + async encryptToken(toEncrypt : string) : Promise { const iv = randomBytes(16); const password = process.env.TICKET_FOR_PLAYING_GAME_SECRET + new Date(); diff --git a/srcs/requirements/nginx/conf/default.conf b/srcs/requirements/nginx/conf/default.conf index 3e75fb56..27e5cb23 100644 --- a/srcs/requirements/nginx/conf/default.conf +++ b/srcs/requirements/nginx/conf/default.conf @@ -11,6 +11,10 @@ server { proxy_pass http://backend_dev:3000; } + location /api/v2/game/gameserver { + deny all; + } + location /pong { proxy_pass http://game_server:8042/pong; proxy_http_version 1.1;