diff --git a/srcs/requirements/game_server/game_back/src/server/class/GameSession.ts b/srcs/requirements/game_server/game_back/src/server/class/GameSession.ts index 92680332..896dfacb 100644 --- a/srcs/requirements/game_server/game_back/src/server/class/GameSession.ts +++ b/srcs/requirements/game_server/game_back/src/server/class/GameSession.ts @@ -240,9 +240,9 @@ export class GameSession { "Content-Type": "application/json", }, body: JSON.stringify({ - id: this.id, - scoreLeft: gc.scoreLeft, - scoreRight: gc.scoreRight, + gameServerIdOfTheMatch: this.id, + playerOneUsernameResult: gc.scoreLeft, + playerTwoUsernameResult: gc.scoreRight, }) }); diff --git a/srcs/requirements/game_server/game_back/src/server/wsServer.ts b/srcs/requirements/game_server/game_back/src/server/wsServer.ts index 1bfb3761..509450b3 100644 --- a/srcs/requirements/game_server/game_back/src/server/wsServer.ts +++ b/srcs/requirements/game_server/game_back/src/server/wsServer.ts @@ -168,7 +168,7 @@ function privateMatchmaking(player: ClientPlayer) const maxPlayersNumber = 2; privateMatchmakingMap.set(player.id, player); const matchOptions = player.matchOptions; - + console.log(player) const token = player.token; const compatiblePlayers: ClientPlayer[] = []; for (const [id, client] of privateMatchmakingMap) @@ -254,6 +254,8 @@ async function playerReadyConfirmationListener(this: WebSocket, data: string) gameOptions: gameSession.matchOptions, playerOneUsername: (gameSessionPlayersIterator.next().value).username, playerTwoUsername: (gameSessionPlayersIterator.next().value).username, + playerOneUsernameResult : 0, + playerTwoUsernameResult : 0 }) }); diff --git a/srcs/requirements/nestjs/api_back/src/game/dto/createGame.dto.ts b/srcs/requirements/nestjs/api_back/src/game/dto/createGame.dto.ts index 86931237..ddce81b3 100644 --- a/srcs/requirements/nestjs/api_back/src/game/dto/createGame.dto.ts +++ b/srcs/requirements/nestjs/api_back/src/game/dto/createGame.dto.ts @@ -12,4 +12,8 @@ export class CreateGameDto { playerOneUsername : string @IsString() playerTwoUsername : string + @IsNumber() + playerTwoUsernameResult : number + @IsNumber() + playerOneUsernameResult : number } diff --git a/srcs/requirements/nestjs/api_back/src/game/dto/updateGame.dto.ts b/srcs/requirements/nestjs/api_back/src/game/dto/updateGame.dto.ts index 1073adf6..29f55473 100644 --- a/srcs/requirements/nestjs/api_back/src/game/dto/updateGame.dto.ts +++ b/srcs/requirements/nestjs/api_back/src/game/dto/updateGame.dto.ts @@ -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){} diff --git a/srcs/requirements/nestjs/api_back/src/game/entity/game.entity.ts b/srcs/requirements/nestjs/api_back/src/game/entity/game.entity.ts index 44257efe..1af581b0 100644 --- a/srcs/requirements/nestjs/api_back/src/game/entity/game.entity.ts +++ b/srcs/requirements/nestjs/api_back/src/game/entity/game.entity.ts @@ -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 ? 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 da476766..540ebe3d 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.controller.ts @@ -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); + } + } 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 e6bb953c..dbd653c3 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.service.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.service.ts @@ -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 + } } diff --git a/srcs/requirements/nestjs/api_back/src/users/dto/update-users.dto.ts b/srcs/requirements/nestjs/api_back/src/users/dto/update-users.dto.ts index 53e00fb3..eaad2c5b 100644 --- a/srcs/requirements/nestjs/api_back/src/users/dto/update-users.dto.ts +++ b/srcs/requirements/nestjs/api_back/src/users/dto/update-users.dto.ts @@ -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){} diff --git a/srcs/requirements/svelte/api_front/src/pages/game/Game.svelte b/srcs/requirements/svelte/api_front/src/pages/game/Game.svelte index 17adceb4..b45a42ac 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/Game.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/game/Game.svelte @@ -1,6 +1,6 @@ @@ -169,90 +195,100 @@
.
{#if showError === true} -
-
- Error -

{errorMessageWhenAttemptingToGetATicket}

- -
-
+
+
+ Error +

{errorMessageWhenAttemptingToGetATicket}

+ +
+
{/if} -{#if optionsAreNotSet} - {#if showGameOption === true} -
-
- -
- game options -
- - -
-
- - -
-
- - - - - - -
-
- - -
- {#if isSomeoneIsIvited === true} - - {/if} -
- -
-
-
-
- {/if} - {#if showInvitations} -
- -
- Current invitation(s) - {#if isThereAnyInvitation} - {#each invitations as invitation } -
- {invitation.playerOneUsername} has invited you to play a pong ! - - -
- {/each} - {/if} - {#if isThereAnyInvitation ===false} -

Currently, no one asked to play with you.

- - {/if} -
-
- {/if} -{/if} - -
- {#if showWaitPage === true} -
+ {#if showWaitPage === true} +
Connecting to the game...

{waitingMessage}

{/if} - -
+ + + +{#if optionsAreNotSet} + {#if showGameOption === true} +
+
initGame()}> +
+ +
+ game options +
+ + +
+
+ + +
+
+

sound :

+ + + + +
+
+ + +
+ {#if isSomeoneIsIvited === true} + + {/if} +
+ +
+
+
+ +
+
+ {/if} + + {#if showInvitations} +
+
+ +
+ Current invitation(s) + {#if isThereAnyInvitation} + {#each invitations as invitation } +
+ {invitation.playerOneUsername} has invited you to play a pong ! + + +
+ {/each} + {/if} + {#if isThereAnyInvitation === false} +

Currently, no one asked to play with you.

+ + {/if} +
+
+
+ {/if} +{/if} + + + +
+ +
@@ -284,6 +320,14 @@ body { /* max-height: 80vh; */ /* overflow: hidden; */ } + +#users_name { + text-align: center; + font-family: "Bit5x3"; + color: rgb(245, 245, 245); + font-size: x-large; +} + #div_game { text-align: center; font-family: "Bit5x3"; diff --git a/srcs/requirements/svelte/api_front/src/pages/game/client/class/GameArea.ts b/srcs/requirements/svelte/api_front/src/pages/game/client/class/GameArea.ts index cd11aba6..5483baa4 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/client/class/GameArea.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/client/class/GameArea.ts @@ -9,10 +9,13 @@ export class GameArea { canvas: HTMLCanvasElement; ctx: CanvasRenderingContext2D; constructor() { - this.canvas = document.getElementById("gameArea"); + this.canvas = document.createElement("canvas"); this.ctx = this.canvas.getContext("2d") as CanvasRenderingContext2D; this.canvas.width = c.CanvasWidth; this.canvas.height = c.CanvasWidth / c.CanvasRatio; + let container = document.getElementById("canvas_container"); + if (container) + container.insertBefore(this.canvas, container.childNodes[0]); } addKey(key: string) { key = key.toLowerCase(); diff --git a/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts b/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts index dc9a88fd..0f741150 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts @@ -34,6 +34,8 @@ export const clientInfoSpectator = new ClientInfoSpectator(); // WIP, could refa export function initWebSocket(options: en.MatchOptions, token: string, username: string, privateMatch = false, playerTwoUsername?: string) { socket = new WebSocket(wsUrl, "json"); + console.log("Infos from ws.ts : options => " + options + " token => " + token + " username => " + username + " priavte match => " + privateMatch + + " player two => " + playerTwoUsername) socket.addEventListener("open", (event) => { if (privateMatch) { socket.send(JSON.stringify( new ev.ClientAnnouncePlayer(options, token, username, privateMatch, playerTwoUsername) ));