From 52f48ae30568f461ab74e33fc64d15881bfaa56b Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sat, 14 Jan 2023 18:34:03 +0100 Subject: [PATCH] bugfix generateToken() for invitation --- .../nestjs/api_back/src/game/game.service.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) 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 2e52357e..d6ca27c5 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.service.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.service.ts @@ -94,9 +94,15 @@ export class GameService { } if (grantTicketDto.isGameIsWithInvitation === true) { - const secondUser : Partial = await this.userService.findOne(grantTicketDto.playerTwoUsername) - if (!secondUser || secondUser.username === user.username) - return res.status(HttpStatus.NOT_FOUND).json({message : "User not found OR you want to play with yourself."}); + if (grantTicketDto.playerTwoUsername === user.username) { + return res.status(HttpStatus.BAD_REQUEST).json({message : "You cant play against yourself."}); + } + const secondUser : User = await this.userRepository.createQueryBuilder('user') + .where("user.username = :username", {username : grantTicketDto.playerTwoUsername}) + .getOne(); + if (!secondUser) { + return res.status(HttpStatus.NOT_FOUND).json({message : "Invited user not found"}); + } const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_' + grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date()) const tok = this.tokenGameRepository.create(grantTicketDto); @@ -108,7 +114,6 @@ export class GameService { this.userService.updateStatus(secondUser.id, STATUS.IN_POOL) return res.status(HttpStatus.OK).json({ token : encryptedTextToReturn }); } - // else if (grantTicketDto.isGameIsWithInvitation === false && user.status !== STATUS.IN_GAME) { // WIP: need to fix STATUS.IN_GAME else if (grantTicketDto.isGameIsWithInvitation === false) { const encryptedTextToReturn = await this.encryptToken(user.username + '_' + grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date()) @@ -285,9 +290,9 @@ export class GameService { const playerTwo = await this.userRepository.findOneBy({username : game.playerTwoUsername}) if (!playerOne || !playerTwo) return new HttpException("Internal Server Error. Impossible to update the database", HttpStatus.INTERNAL_SERVER_ERROR); - this.userService.updateStatus(playerOne.id, STATUS.CONNECTED) - this.userService.updateStatus(playerTwo.id, STATUS.CONNECTED) - if (game.playerOneUsernameResult === game.playerTwoUsernameResult) + this.userService.updateStatus(playerOne.id, STATUS.CONNECTED); + this.userService.updateStatus(playerTwo.id, STATUS.CONNECTED); + if (game.playerOneUsernameResult === game.playerTwoUsernameResult) { this.userService.incrementDraws(playerOne.id) this.userService.incrementDraws(playerTwo.id)