quelques changements mineur et d'améklioration du code
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { Console } from 'console';
|
||||
import { request } from 'http';
|
||||
import { use } from 'passport';
|
||||
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { Response } from 'express';
|
||||
import { CreateGameDto } from './dto/createGame.dto';
|
||||
import { GrantTicketDto } from './dto/grantTicket.dto';
|
||||
import { UpdateGameDto } from './dto/updateGame.dto';
|
||||
import { ValidateTicketDto } from './dto/validateTicket.dto';
|
||||
import { TokenGame } from './entity/tokenGame.entity';
|
||||
import { GameService } from './game.service';
|
||||
|
||||
@Controller('game')
|
||||
@@ -29,51 +25,40 @@ export class GameController {
|
||||
@Post('ticket')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async grantTicket(@Req() req, @Body() grantTicketDto : GrantTicketDto)
|
||||
async grantTicket(@Req() req, @Body() grantTicketDto : GrantTicketDto, @Res() res : Response)
|
||||
{
|
||||
const user : User = req.user
|
||||
if (grantTicketDto.playerOneUsername != user.username)
|
||||
return new HttpException('You can\'t request a game for another person.', 403 )
|
||||
// else if (user.status !== "connected")
|
||||
// return new HttpException('You must not be in game...', HttpStatus.FORBIDDEN )
|
||||
return this.gameService.generateToken(user, grantTicketDto);
|
||||
}
|
||||
|
||||
@Post('requested')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async requestIfAnotherUserHasRespondToquestForGame(@Req() req, @Body('token') token)
|
||||
{
|
||||
const user : User = req.user;
|
||||
return this.gameService.requestIfAnotherUserHasRespondToquestForGame(user, token);
|
||||
return res.status(HttpStatus.BAD_REQUEST).json({message : 'You can\'t grant a ticket to another user'});
|
||||
return this.gameService.generateToken(user, grantTicketDto, res);
|
||||
}
|
||||
|
||||
@Post('decline')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async declineInvitation(@Body('token') token, @Req() req)
|
||||
async declineInvitation(@Body('token') token, @Req() req, @Res() res : Response)
|
||||
{
|
||||
const user : User = req.user;
|
||||
return this.gameService.declineInvitation(user, token);
|
||||
return this.gameService.declineInvitation(user, token, res);
|
||||
}
|
||||
|
||||
@Post('accept')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async acceptInvitation(@Body('token') token, @Req() req)
|
||||
async acceptInvitation(@Body('token') token, @Req() req, @Res() res : Response)
|
||||
{
|
||||
const user : User = req.user;
|
||||
return this.gameService.acceptInvitation(user, token);
|
||||
return this.gameService.acceptInvitation(user, token, res);
|
||||
}
|
||||
|
||||
|
||||
@Get('invitations')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async findInvitations(@Req() request)
|
||||
async findInvitations(@Req() request, @Res() res : Response)
|
||||
{
|
||||
const user : User = request.user;
|
||||
return this.gameService.findInvitations(user);
|
||||
return this.gameService.findInvitations(user, res);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -95,9 +80,6 @@ export class GameController {
|
||||
return this.gameService.createGame(creategameDto);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Post('gameserver/updategame')
|
||||
async updateGame(@Body() updateGameDto : UpdateGameDto)
|
||||
{
|
||||
@@ -111,6 +93,4 @@ export class GameController {
|
||||
{
|
||||
return this.gameService.destroySession(token);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { ConsoleLogger, HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { HttpException, HttpStatus, Injectable, Res } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { createCipheriv, randomBytes, scrypt } from 'crypto';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { promisify } from 'util';
|
||||
import { Response } from 'express';
|
||||
import { GrantTicketDto } from './dto/grantTicket.dto';
|
||||
import { Game } from './entity/game.entity';
|
||||
import { ValidateTicketDto } from './dto/validateTicket.dto';
|
||||
@@ -65,8 +66,7 @@ export class GameService {
|
||||
return this.tokenGameRepository.remove(tokenGame);
|
||||
}
|
||||
|
||||
|
||||
async generateToken(user : User, grantTicketDto : GrantTicketDto)
|
||||
async generateToken(user : User, grantTicketDto : GrantTicketDto, @Res() res : Response)
|
||||
{
|
||||
console.log(user.status);
|
||||
if (user.status === STATUS.IN_POOL || user.status === STATUS.IN_GAME)
|
||||
@@ -79,7 +79,7 @@ export class GameService {
|
||||
{
|
||||
const secondUser : Partial<User> = await this.userService.findOneByUsername(user.id.toString(), grantTicketDto.playerTwoUsername)
|
||||
if (!secondUser || secondUser.username === user.username)
|
||||
return new HttpException("The requested second player does not exist OR you want to play against yourself. :P", HttpStatus.NOT_FOUND);
|
||||
return res.status(HttpStatus.NOT_FOUND).json({message : "User not found OR you want to play with yourself."});
|
||||
const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_'
|
||||
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
|
||||
const tok = this.tokenGameRepository.create(grantTicketDto);
|
||||
@@ -88,7 +88,7 @@ export class GameService {
|
||||
tok.token = encryptedTextToReturn;
|
||||
this.tokenGameRepository.save(tok);
|
||||
this.userService.updateStatus(user.id, "In Pool")
|
||||
return { token : encryptedTextToReturn };
|
||||
return res.status(HttpStatus.OK).json({ token : encryptedTextToReturn });
|
||||
}
|
||||
else if (grantTicketDto.isGameIsWithInvitation === false) {
|
||||
const encryptedTextToReturn = await this.encryptToken(user.username + '_'
|
||||
@@ -98,9 +98,9 @@ export class GameService {
|
||||
tok.token = encryptedTextToReturn;
|
||||
this.tokenGameRepository.save(tok);
|
||||
this.userService.updateStatus(user.id, "In Pool")
|
||||
return { token : encryptedTextToReturn };
|
||||
return res.status(HttpStatus.OK).json({ token : encryptedTextToReturn });
|
||||
}
|
||||
return new HttpException("Something went wrong !", HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({message : "Internal Server Error"});
|
||||
}
|
||||
|
||||
async validateToken(validateTicketDto : ValidateTicketDto) {
|
||||
@@ -157,14 +157,14 @@ export class GameService {
|
||||
return false;
|
||||
}
|
||||
|
||||
async findInvitations(user : User) {
|
||||
async findInvitations(user : User, @Res() res : Response) {
|
||||
const game = await this.tokenGameRepository.createQueryBuilder('tokengame')
|
||||
.where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
|
||||
.andWhere('tokengame.isGameIsWithInvitation = :invit', {invit : true})
|
||||
.andWhere('tokengame.isSecondUserAcceptedRequest = :choice', {choice : false})
|
||||
.getMany();
|
||||
if (!game)
|
||||
return new HttpException( "No invitations !", HttpStatus.NOT_FOUND);
|
||||
return res.status(HttpStatus.NOT_FOUND).send({message : "No invitation found"});
|
||||
let partialGame : Partial<TokenGame>[] = [];
|
||||
for (const gameToken of game) {
|
||||
partialGame.push({
|
||||
@@ -174,21 +174,24 @@ export class GameService {
|
||||
token : gameToken.token,
|
||||
});
|
||||
}
|
||||
return partialGame;
|
||||
return res.status(HttpStatus.OK).json(partialGame);
|
||||
}
|
||||
|
||||
async declineInvitation(user : User, token : string)
|
||||
async declineInvitation(user : User, token : string, @Res() res : Response)
|
||||
{
|
||||
if (user.status !== "Connected")
|
||||
return new HttpException("You must finish your game before decline.", HttpStatus.FORBIDDEN)
|
||||
return res.status(HttpStatus.FORBIDDEN).json({message : "You must not be in game to decline an invitation"});
|
||||
console.log("On décline l'invitation")
|
||||
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
|
||||
.andWhere('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
|
||||
.andWhere('tokengame.token = :token', {token : token})
|
||||
.getOne();
|
||||
if (tokenGame)
|
||||
return this.tokenGameRepository.remove(tokenGame);
|
||||
return new HttpException("Invitation not found !", HttpStatus.NOT_FOUND)
|
||||
{
|
||||
this.tokenGameRepository.remove(tokenGame);
|
||||
return res.status(HttpStatus.OK).json({message : "Invitation declined."});
|
||||
}
|
||||
return res.status(HttpStatus.NOT_FOUND).json({message : "No invitation found !"});
|
||||
}
|
||||
|
||||
async destroySession(token : string)
|
||||
@@ -210,10 +213,10 @@ export class GameService {
|
||||
return new HttpException("Token not found !", HttpStatus.NOT_FOUND)
|
||||
}
|
||||
|
||||
async acceptInvitation(user : User, token : string)
|
||||
async acceptInvitation(user : User, token : string, @Res() res : Response)
|
||||
{
|
||||
if (user.status !== "Connected")
|
||||
return new HttpException("You must finish your game before accept.", HttpStatus.FORBIDDEN)
|
||||
return res.status(HttpStatus.FORBIDDEN).send("")
|
||||
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
|
||||
.andWhere('tokenGame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
|
||||
.andWhere('tokenGame.token = :token', {token : token})
|
||||
@@ -222,27 +225,11 @@ export class GameService {
|
||||
{
|
||||
tokenGame.isSecondUserAcceptedRequest = true;
|
||||
this.tokenGameRepository.save(tokenGame)
|
||||
return HttpStatus.OK
|
||||
return res.status(HttpStatus.OK).json({message : "Invitation accepted."});
|
||||
}
|
||||
return new HttpException("Invitation not found !", HttpStatus.NOT_FOUND)
|
||||
return res.status(HttpStatus.NOT_FOUND).json({message : "No invitation found !"});
|
||||
}
|
||||
|
||||
async requestIfAnotherUserHasRespondToquestForGame(user : User, token : string) {
|
||||
if (user.status !== "Connected")
|
||||
return new HttpException("You can't do that.", HttpStatus.BAD_REQUEST)
|
||||
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
|
||||
.where('tokenGame.token = :token', {token : token})
|
||||
.andWhere('tokenGame.isSecondUserAcceptedRequest = :isSecondUserAcceptedRequest', {isSecondUserAcceptedRequest : true})
|
||||
.getOne();
|
||||
if (tokenGame && tokenGame.isSecondUserAcceptedRequest === true)
|
||||
return {isSecondUserAcceptedRequest : true}
|
||||
else if (tokenGame && tokenGame.isSecondUserAcceptedRequest === false)
|
||||
return {isSecondUserAcceptedRequest : false}
|
||||
else if (!tokenGame)
|
||||
return new HttpException("Not Found", HttpStatus.NOT_FOUND)
|
||||
}
|
||||
|
||||
|
||||
async createGame(creategameDto : CreateGameDto)
|
||||
{
|
||||
if (creategameDto.playerOneUsername === "" || creategameDto.playerTwoUsername === ""
|
||||
|
||||
@@ -82,7 +82,8 @@
|
||||
const responseInjson = await responseFromServer.json();
|
||||
const token : string = responseInjson.token;
|
||||
showWaitPage = false;
|
||||
if (!responseFromServer.ok || (responseFromServer.status != 200 && responseFromServer.status != 201))
|
||||
console.log("status : " + responseFromServer.status)
|
||||
if (responseFromServer.status != 200)
|
||||
{
|
||||
console.log(responseInjson)
|
||||
console.log("On refuse le ticket");
|
||||
@@ -137,10 +138,12 @@
|
||||
clearInterval(idOfIntevalCheckTerminationOfTheMatch);
|
||||
console.log("matchTermitation was called")
|
||||
showWaitPage = false
|
||||
matchAbort ? errorMessageWhenAttemptingToGetATicket = "The match has been aborted" : errorMessageWhenAttemptingToGetATicket = "The match is finished !"
|
||||
matchAbort ?
|
||||
errorMessageWhenAttemptingToGetATicket = "The match has been aborted"
|
||||
: errorMessageWhenAttemptingToGetATicket = "The match is finished !"
|
||||
matchAbort ? showError = true : showMatchEnded = true;
|
||||
hiddenGame = true;
|
||||
setTimeout(() => {
|
||||
hiddenGame = true;
|
||||
showError = false;
|
||||
showMatchEnded = false;
|
||||
optionsAreNotSet = true
|
||||
@@ -216,25 +219,22 @@
|
||||
Might become useless after CSS rework. -->
|
||||
<div id="game_page">
|
||||
|
||||
<div id="canvas_container" hidden={hiddenGame}>
|
||||
<canvas id={gameAreaId}/>
|
||||
</div>
|
||||
|
||||
{#if showError === true}
|
||||
<div id="div_game" in:fly="{{ y: 10, duration: 1000 }}">
|
||||
<fieldset>
|
||||
<legend>Error</legend>
|
||||
<p>{errorMessageWhenAttemptingToGetATicket}</p>
|
||||
<button id="pong_button">Retry</button>
|
||||
</fieldset>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if showMatchEnded === true}
|
||||
<div id="div_game" in:fly="{{ y: 10, duration: 1000 }}">
|
||||
<p>{errorMessageWhenAttemptingToGetATicket}</p>
|
||||
</div>
|
||||
{/if}
|
||||
{#if showError === true}
|
||||
<div id="div_game" in:fly="{{ y: 10, duration: 1000 }}">
|
||||
<fieldset>
|
||||
<legend>Error</legend>
|
||||
<p>{errorMessageWhenAttemptingToGetATicket}</p>
|
||||
</fieldset>
|
||||
</div>
|
||||
{/if}
|
||||
<div id="canvas_container" hidden={hiddenGame}>
|
||||
<canvas id={gameAreaId}/>
|
||||
</div>
|
||||
|
||||
{#if showWaitPage === true}
|
||||
<div id="div_game" in:fly="{{ y: 10, duration: 1000 }}">
|
||||
|
||||
Reference in New Issue
Block a user