Le multijoueur fonctionne enfin. Mise à jour des scores. Et de la partie.

This commit is contained in:
batche
2022-12-21 15:22:07 +01:00
parent d6da8bf70b
commit cc797d1118
11 changed files with 218 additions and 124 deletions

View File

@@ -12,4 +12,8 @@ export class CreateGameDto {
playerOneUsername : string
@IsString()
playerTwoUsername : string
@IsNumber()
playerTwoUsernameResult : number
@IsNumber()
playerOneUsernameResult : number
}

View File

@@ -1,15 +1,5 @@
import { OmitType } from "@nestjs/mapped-types";
import { IsBoolean, IsNotEmpty, IsNumber, IsString } from "class-validator";
import { CreateGameDto } from "./createGame.dto";
export class UpdateGameDto {
@IsString()
@IsNotEmpty()
gameServerIdOfTheMatch : string
@IsNumber()
@IsNotEmpty()
playerOneUsernameResult : number
@IsNumber()
@IsNotEmpty()
playerTwoUsernameResult : number
@IsBoolean()
isMatchIsFinished : boolean
}
export class UpdateGameDto extends OmitType(CreateGameDto, ['playerOneUsername', 'playerTwoUsername', 'gameOptions'] as const){}

View File

@@ -17,7 +17,7 @@ export class Game {
@Column({default : 0, nullable : true})
playerTwoUsernameResult : number
@Column()
@Column({unique : true})
gameServerIdOfTheMatch: string
@Column({default: false, nullable : true}) //éric pourra trouver un meilleur mot : ongoing ?

View File

@@ -7,6 +7,7 @@ import { User } from 'src/users/entities/user.entity';
import { UsersService } from 'src/users/users.service';
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';
@@ -78,9 +79,19 @@ export class GameController {
@Post('gameserver/creategame')
async createGame(@Body() creategameDto : CreateGameDto)
{
console.log("On est dans create game")
console.log(creategameDto)
return this.gameService.createGame(creategameDto);
}
@Post('gameserver/updategame')
async updateGame(@Body() updateGameDto : UpdateGameDto)
{
console.log("On est dans update game")
console.log(updateGameDto)
return this.gameService.updateGame(updateGameDto);
}
}

View File

@@ -10,6 +10,7 @@ import { ValidateTicketDto } from './dto/validateTicket.dto';
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';
@Injectable()
export class GameService {
@@ -68,6 +69,8 @@ export class GameService {
}
async validateToken(validateTicketDto : ValidateTicketDto) {
console.log("On valide le token pour : ")
console.log(validateTicketDto)
if (validateTicketDto.isGameIsWithInvitation === true)
{
const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
@@ -138,6 +141,7 @@ export class GameService {
token : gameToken.token,
});
}
console.log("Il y a des invitations !")
return partialGame;
}
@@ -145,6 +149,7 @@ export class GameService {
{
// if (user.status === "In Game")
// return new HttpException("You must finish your game before decline.", HttpStatus.FORBIDDEN)
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})
@@ -158,6 +163,7 @@ export class GameService {
{
// if (user.status === "In Game")
// return new HttpException("You must finish your game before accept.", HttpStatus.FORBIDDEN)
console.log("On accepte l'invitation")
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
.andWhere('tokenGame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.andWhere('tokenGame.token = :token', {token : token})
@@ -190,11 +196,43 @@ export class GameService {
async createGame(creategameDto : CreateGameDto)
{
const game = this.gameRepository.create(creategameDto)
game.isMatchIsFinished = false;
this.gameRepository.save(game);
if (!game)
return HttpStatus.INTERNAL_SERVER_ERROR
console.log("200 retourné pour la création de partie")
return HttpStatus.OK
}
async updateGame(updateGameDto : UpdateGameDto) {
const game = await this.gameRepository.preload(
{gameServerIdOfTheMatch : updateGameDto.gameServerIdOfTheMatch,
...updateGameDto}
)
if (!game)
throw new HttpException(`The game could not be updated.`,HttpStatus.NOT_FOUND);
game.isMatchIsFinished = true;
this.userRepository.save(game);
const playerOne = await this.userRepository.findOneBy({username : game.playerOneUsername})
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);
if (game.playerOneUsernameResult === game.playerTwoUsernameResult)
{
this.userService.incrementDraws(playerOne.id)
this.userService.incrementDraws(playerTwo.id)
}
else if (game.playerOneUsernameResult < game.playerTwoUsernameResult)
{
this.userService.incrementDefeats(playerOne.id)
this.userService.incrementVictories(playerTwo.id)
}
else
{
this.userService.incrementVictories(playerOne.id)
this.userService.incrementDefeats(playerTwo.id)
}
return HttpStatus.OK
}
}

View File

@@ -2,7 +2,7 @@
// et de les mettre comme optionnelles. De plus on peut hériter
// des décorateurs de la classe parente (par exemple @IsString()).
import { OmitType, PartialType } from "@nestjs/mapped-types";
import { OmitType } from "@nestjs/mapped-types";
import { CreateUsersDto } from "./create-users.dto";
export class UpdateUsersDto extends OmitType(CreateUsersDto, ['fortyTwoId', 'email', 'image_url', 'status'] as const){}