lien serveur de jeu => serveur nestjs
This commit is contained in:
@@ -7,7 +7,7 @@ import { ConfigModule } from '@nestjs/config';
|
||||
import { FriendshipsModule } from './friendship/friendships.module';
|
||||
import { AuthenticationModule } from './auth/42/authentication.module';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
// import { GameModule } from './game/game/game.module';
|
||||
import { GameModule } from './game/game.module';
|
||||
|
||||
@Module({
|
||||
imports: [UsersModule,
|
||||
@@ -27,7 +27,7 @@ import { PassportModule } from '@nestjs/passport';
|
||||
//avec une classe pour le module
|
||||
synchronize: true,
|
||||
}),
|
||||
// GameModule,
|
||||
GameModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { IsBoolean, IsInt, IsNotEmpty, IsNumber, IsString } from "class-validator";
|
||||
|
||||
export class GrantTicketDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
playerOneUsername : string
|
||||
@IsString()
|
||||
playerTwousername : string
|
||||
@IsNumber()
|
||||
gameOptions : number
|
||||
@IsBoolean()
|
||||
isGameIsWithInvitation : boolean
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { IsBase64, IsBoolean, IsNotEmpty, IsNumber, IsString } from "class-validator";
|
||||
|
||||
export class ValidateTicketDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
playerOneUsername : string
|
||||
@IsString()
|
||||
playerTwousername : string
|
||||
@IsNumber()
|
||||
gameOptions : number
|
||||
@IsBoolean()
|
||||
isGameIsWithInvitation : boolean
|
||||
@IsNumber()
|
||||
numberOfRegisteredUser : number
|
||||
@IsBase64()
|
||||
@IsNotEmpty()
|
||||
token : string
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
@Entity('game')
|
||||
export class game {
|
||||
|
||||
export class Game {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
|
||||
@Entity('tokenGame')
|
||||
export class TokenGame {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@Column()
|
||||
playerOneUsername : string
|
||||
@Column({nullable: true})
|
||||
playerTwousername : string
|
||||
@Column()
|
||||
gameOptions : number
|
||||
@Column()
|
||||
isGameIsWithInvitation : boolean
|
||||
@Column({default: 0, nullable: true})
|
||||
numberOfRegisteredUser : number
|
||||
@Column()
|
||||
token : string
|
||||
}
|
||||
@@ -1,4 +1,58 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { request } from 'http';
|
||||
import { use } from 'passport';
|
||||
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 { GrantTicketDto } from './dto/grantTicket.dto';
|
||||
import { ValidateTicketDto } from './dto/validateTicket.dto';
|
||||
import { GameService } from './game.service';
|
||||
|
||||
@Controller('game')
|
||||
export class GameController {}
|
||||
export class GameController {
|
||||
constructor (private readonly gameService : GameService,
|
||||
private readonly userService : UsersService) { }
|
||||
|
||||
@Post('ticket')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async grantTicket(@Req() req, @Body() grantTicketDto : GrantTicketDto)
|
||||
{
|
||||
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...', 403 )
|
||||
return this.gameService.generateToken(user, grantTicketDto);
|
||||
}
|
||||
|
||||
//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);
|
||||
return HttpStatus.OK;
|
||||
}
|
||||
|
||||
@Post('decline')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async declineInvitation(@Body() validateTicketDto : ValidateTicketDto, @Req() req)
|
||||
{
|
||||
const user : User = req.user;
|
||||
if (user.username !== validateTicketDto.playerTwousername)
|
||||
return new HttpException("This cannot be done", HttpStatus.FORBIDDEN);
|
||||
return this.gameService.declineInvitation(validateTicketDto);
|
||||
}
|
||||
|
||||
@Get('invitations')
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
async findInvitations(@Req() request)
|
||||
{
|
||||
const user : User = request.user;
|
||||
return this.gameService.findInvitations(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
// import { Module } from '@nestjs/common';
|
||||
// import { GameController } from './game.controller';
|
||||
// import { GameService } from './game.service';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { TokenGame } from './entity/tokenGame.entity';
|
||||
import { GameController } from './game.controller';
|
||||
import { GameService } from './game.service';
|
||||
|
||||
// @Module({
|
||||
// controllers: [GameController],
|
||||
// providers: [GameService]
|
||||
// })
|
||||
// export class GameModule {}
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([TokenGame, User])],
|
||||
controllers: [GameController],
|
||||
providers: [GameService, UsersService]
|
||||
})
|
||||
export class GameModule {}
|
||||
|
||||
@@ -1,5 +1,150 @@
|
||||
// import { Injectable } from '@nestjs/common';
|
||||
import { HttpException, HttpStatus, Injectable } 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 { GrantTicketDto } from './dto/grantTicket.dto';
|
||||
import { Game } from './entity/game.entity';
|
||||
import { ValidateTicketDto } from './dto/validateTicket.dto';
|
||||
import { TokenGame } from './entity/tokenGame.entity';
|
||||
|
||||
// @Injectable('game')
|
||||
// export class GameService {}
|
||||
@Injectable()
|
||||
export class GameService {
|
||||
constructor (
|
||||
@InjectRepository(Game)
|
||||
private readonly gameRepository : Repository<Game>,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository : Repository<User>,
|
||||
@InjectRepository(TokenGame)
|
||||
private readonly tokenGameRepository : Repository<TokenGame>,
|
||||
) { }
|
||||
|
||||
async encryptToken(toEncrypt : string) : Promise<string> {
|
||||
const iv = randomBytes(16);
|
||||
const password = process.env.TICKET_FOR_PLAYING_GAME_SECRET + new Date();
|
||||
const key = (await promisify(scrypt)(password, 'salt', 32)) as Buffer;
|
||||
const cipher = createCipheriv('aes-256-ctr', key, iv);
|
||||
const encryptedText = Buffer.concat([
|
||||
cipher.update(toEncrypt),
|
||||
cipher.final(),
|
||||
]);
|
||||
const encryptedTextToReturn = encryptedText.toString('base64');
|
||||
return encryptedTextToReturn
|
||||
}
|
||||
|
||||
async generateToken(user : User, grantTicketDto : GrantTicketDto)
|
||||
{
|
||||
if (grantTicketDto.isGameIsWithInvitation === true)
|
||||
{
|
||||
let secondUser : User;
|
||||
(user.username === grantTicketDto.playerOneUsername) ?
|
||||
secondUser = await this.userRepository.findOneBy({username : grantTicketDto.playerTwousername}) :
|
||||
secondUser = await this.userRepository.findOneBy({username : grantTicketDto.playerOneUsername})
|
||||
if (!secondUser)
|
||||
return new HttpException("The requested second player does not exist", HttpStatus.NOT_FOUND);
|
||||
const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_'
|
||||
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
|
||||
const validateTicketDto : ValidateTicketDto = new ValidateTicketDto();
|
||||
validateTicketDto.playerOneUsername = grantTicketDto.playerOneUsername;
|
||||
validateTicketDto.playerTwousername = grantTicketDto.playerTwousername;
|
||||
validateTicketDto.numberOfRegisteredUser = 0;
|
||||
validateTicketDto.isGameIsWithInvitation = grantTicketDto.isGameIsWithInvitation;
|
||||
validateTicketDto.gameOptions = grantTicketDto.gameOptions;
|
||||
validateTicketDto.token = encryptedTextToReturn;
|
||||
const tok = this.tokenGameRepository.create(validateTicketDto);
|
||||
this.tokenGameRepository.save(tok);
|
||||
return { token : encryptedTextToReturn };
|
||||
}
|
||||
else if (grantTicketDto.isGameIsWithInvitation === false) {
|
||||
const encryptedTextToReturn = await this.encryptToken(user.username + '_'
|
||||
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
|
||||
const validateTicketDto : ValidateTicketDto = new ValidateTicketDto();
|
||||
validateTicketDto.playerOneUsername = grantTicketDto.playerOneUsername;
|
||||
validateTicketDto.playerTwousername = "";
|
||||
validateTicketDto.numberOfRegisteredUser = 0;
|
||||
validateTicketDto.isGameIsWithInvitation = grantTicketDto.isGameIsWithInvitation;
|
||||
validateTicketDto.gameOptions = grantTicketDto.gameOptions;
|
||||
validateTicketDto.token = encryptedTextToReturn;
|
||||
const tok = this.tokenGameRepository.create(validateTicketDto);
|
||||
this.tokenGameRepository.save(tok);
|
||||
return { token : encryptedTextToReturn };
|
||||
}
|
||||
return new HttpException("Something went wrong !", HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
async validateToken(validateTicketDto : ValidateTicketDto) {
|
||||
|
||||
let tokenGame : TokenGame;
|
||||
if (validateTicketDto.isGameIsWithInvitation === true)
|
||||
{
|
||||
tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
|
||||
.where('tokenGame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
|
||||
.andWhere('tokenGame.playerTwousername = :playerTwousername', {playerTwousername : validateTicketDto.playerTwousername})
|
||||
.andWhere('tokenGame.gameOption = :gameOption', {gameOption : validateTicketDto.gameOptions})
|
||||
.andWhere('tokenGame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: validateTicketDto.isGameIsWithInvitation})
|
||||
.andWhere('tokenGame.token = :token', {token : validateTicketDto.token})
|
||||
.getOne();
|
||||
}
|
||||
else
|
||||
{
|
||||
tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
|
||||
.where('tokenGame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
|
||||
.andWhere('tokenGame.gameOption = :gameOption', {gameOption : validateTicketDto.gameOptions})
|
||||
.andWhere('tokenGame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: validateTicketDto.isGameIsWithInvitation})
|
||||
.andWhere('tokenGame.token = :token', {token : validateTicketDto.token})
|
||||
.getOne();
|
||||
}
|
||||
if (tokenGame)
|
||||
tokenGame.numberOfRegisteredUser++;
|
||||
if (tokenGame && tokenGame.isGameIsWithInvitation === false)
|
||||
{
|
||||
this.tokenGameRepository.remove(tokenGame)
|
||||
this.userRepository.update(tokenGame.playerOneUsername, {status : "In Game"})
|
||||
return true;
|
||||
}
|
||||
else if (tokenGame && tokenGame.isGameIsWithInvitation === true)
|
||||
{
|
||||
if (tokenGame.numberOfRegisteredUser === 2)
|
||||
{
|
||||
this.tokenGameRepository.remove(tokenGame)
|
||||
this.userRepository.update(tokenGame.playerOneUsername, {status : "In Game"})
|
||||
this.userRepository.update(tokenGame.playerTwousername, {status : "In Game"})
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async findInvitations(user : User) {
|
||||
const game = await this.tokenGameRepository.createQueryBuilder('tokengame')
|
||||
.where('tokenGame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
|
||||
.getMany();
|
||||
if (!game)
|
||||
return new HttpException( "No invitations !", HttpStatus.NOT_FOUND);
|
||||
let partialGame : Partial<TokenGame>[] = [];
|
||||
for (const gameToken of game) {
|
||||
partialGame.push({
|
||||
playerOneUsername : gameToken.playerOneUsername,
|
||||
playerTwousername : gameToken.playerTwousername,
|
||||
gameOptions : gameToken.gameOptions,
|
||||
isGameIsWithInvitation : gameToken.isGameIsWithInvitation,
|
||||
token : gameToken.token,
|
||||
});
|
||||
}
|
||||
return partialGame;
|
||||
}
|
||||
|
||||
async declineInvitation(validateTicketDto : ValidateTicketDto)
|
||||
{
|
||||
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
|
||||
.where('tokenGame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
|
||||
.andWhere('tokenGame.playerTwousername = :playerTwousername', {playerTwousername : validateTicketDto.playerTwousername})
|
||||
.andWhere('tokenGame.gameOption = :gameOption', {gameOption : validateTicketDto.gameOptions})
|
||||
.andWhere('tokenGame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: validateTicketDto.isGameIsWithInvitation})
|
||||
.andWhere('tokenGame.token = :token', {token : validateTicketDto.token})
|
||||
.getOne();
|
||||
return this.tokenGameRepository.remove(tokenGame);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
import {RedisService} from "@nestjs/redis"
|
||||
@@ -83,7 +83,6 @@ export class UsersController {
|
||||
async update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto, @Res() response : Response) {
|
||||
console.log("DANS PATCH USERS");
|
||||
const user = await this.usersService.update(req.user.id, usersUpdateDto);
|
||||
// const user : User = req.user;
|
||||
if (user.isEnabledTwoFactorAuth === false && user.isTwoFactorAuthenticated === true)
|
||||
this.usersService.setIsTwoFactorAuthenticatedWhenLogout(user.id);
|
||||
console.log ("Enbale 2FA " + user.isEnabledTwoFactorAuth + " Is authenticated " + user.isTwoFactorAuthenticated);
|
||||
|
||||
Reference in New Issue
Block a user