From 9586fe63fce3e989d02062432b847c308fb16833 Mon Sep 17 00:00:00 2001 From: batche Date: Thu, 22 Dec 2022 15:29:43 +0100 Subject: [PATCH] =?UTF-8?q?probl=C3=A8mes=20du=20jeu=20r=C3=A9gl=C3=A9s=20?= =?UTF-8?q?en=20grosse=20partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../game_back/src/server/wsServer.ts | 16 ++++++++++---- .../game_back/src/shared_js/class/Event.ts | 8 +++++-- .../api_back/src/game/game.controller.ts | 5 +++++ .../nestjs/api_back/src/game/game.service.ts | 22 +++++++++++++++---- .../api_front/src/pages/game/Game.svelte | 4 +++- .../api_front/src/pages/game/client/pong.ts | 3 ++- .../api_front/src/pages/game/client/ws.ts | 4 ++-- .../src/pages/game/shared_js/class/Event.ts | 8 +++++-- 8 files changed, 54 insertions(+), 16 deletions(-) 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 94d9dca5..ece2c0e3 100644 --- a/srcs/requirements/game_server/game_back/src/server/wsServer.ts +++ b/srcs/requirements/game_server/game_back/src/server/wsServer.ts @@ -96,8 +96,7 @@ async function clientAnnounceListener(this: WebSocket, data: string) const player = clientsMap.get(this.id) as ClientPlayer; player.matchOptions = announce.matchOptions; player.token = announce.token; - player.username = announce.username; - + announce.isInvitedPerson ? player.username = announce.playerTwoUsername : player.username = announce.username; this.send(JSON.stringify( new ev.EventAssignId(this.id) )); // unused this.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchmakingInProgress) )); if (announce.privateMatch) { @@ -173,7 +172,7 @@ function privateMatchmaking(player: ClientPlayer) const compatiblePlayers: ClientPlayer[] = []; for (const [id, client] of privateMatchmakingMap) { - if (client.token === token && client.username !== player.username) + if (client.token === token) { compatiblePlayers.push(client); if (compatiblePlayers.length === maxPlayersNumber) { @@ -189,11 +188,20 @@ function privateMatchmaking(player: ClientPlayer) createGameSession(compatiblePlayers, matchOptions); } else { - setTimeout(function abortMatch() { + setTimeout(async function abortMatch() { if (!player.gameSession) { // TODO: informe le back de l'invalidité du token d'invitation player.socket.send(JSON.stringify( new ev.EventMatchAbort() )); + const responseTobakc = await fetch(c.addressBackEnd + "/game/gameserver/destroysession",{ + method: "POST", + headers : {"Content-Type": "application/json"}, + body : JSON.stringify({ + token : player.token + }) + }) + .then(x => x.json()) + .catch(error => console.log("ERROR : " + error)) clientTerminate(player); } }, 60000); diff --git a/srcs/requirements/game_server/game_back/src/shared_js/class/Event.ts b/srcs/requirements/game_server/game_back/src/shared_js/class/Event.ts index 6f27b219..28b3576b 100644 --- a/srcs/requirements/game_server/game_back/src/shared_js/class/Event.ts +++ b/srcs/requirements/game_server/game_back/src/shared_js/class/Event.ts @@ -72,7 +72,7 @@ export class EventMatchEnd extends ServerEvent { } export class EventMatchAbort extends ServerEvent { - + constructor() { super(en.EventTypes.matchAbort); } @@ -102,12 +102,16 @@ export class ClientAnnouncePlayer extends ClientAnnounce { username: string; privateMatch: boolean; playerTwoUsername?: string; - constructor(matchOptions: en.MatchOptions, token: string, username: string, privateMatch: boolean = false, playerTwoUsername?: string) { + isInvitedPerson? : boolean; + constructor(matchOptions: en.MatchOptions, token: string, username: string, privateMatch: boolean = false, playerTwoUsername?: string, isInvitedPerson? : boolean) { super(en.ClientRole.player); this.matchOptions = matchOptions; this.token = token; this.username = username; this.privateMatch = privateMatch; + if (isInvitedPerson) { + this.isInvitedPerson = isInvitedPerson; + } if (playerTwoUsername) { this.playerTwoUsername = playerTwoUsername; } 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 ed867b4f..f3b0e857 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.controller.ts @@ -102,6 +102,11 @@ export class GameController { return this.gameService.updateGame(updateGameDto); } + @Post('gameserver/destroysession') + async destroySession(@Body('token') token) + { + return this.gameService.destroySession(token); + } } 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 b7904f7a..7523230b 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.service.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.service.ts @@ -166,8 +166,8 @@ export class GameService { async declineInvitation(user : User, token : string) { - // if (user.status === "In Game") - // return new HttpException("You must finish your game before decline.", HttpStatus.FORBIDDEN) + 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}) @@ -178,6 +178,17 @@ export class GameService { return new HttpException("Invitation not found !", HttpStatus.NOT_FOUND) } + async destroySession(token : string) + { + console.log("On détruit le token et la session qui va avec") + const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame') + .where('tokengame.token = :token', {token : token}) + .getOne(); + if (tokenGame) + return this.tokenGameRepository.remove(tokenGame); + return new HttpException("Invitation not found !", HttpStatus.NOT_FOUND) + } + async acceptInvitation(user : User, token : string) { const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame') @@ -194,8 +205,8 @@ export class GameService { } async requestIfAnotherUserHasRespondToquestForGame(user : User, token : string) { - // if (user.status === "In Game") - // return new HttpException("You can't do that.", HttpStatus.BAD_REQUEST) + if (user.status === "In Game") + 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}) @@ -211,6 +222,9 @@ export class GameService { async createGame(creategameDto : CreateGameDto) { + if (creategameDto.playerOneUsername === "" || creategameDto.playerTwoUsername === "" + || creategameDto.playerOneUsername === creategameDto.playerTwoUsername) + return HttpStatus.INTERNAL_SERVER_ERROR const game = this.gameRepository.create(creategameDto) game.isMatchIsFinished = false; this.gameRepository.save(game); 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 0e5c0409..39abb380 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/Game.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/game/Game.svelte @@ -16,7 +16,7 @@ //Game's stuff let optionsAreNotSet = true; const options = new pong.InitOptions(); - + //Game's stuff client side only const gameAreaId = "game_area"; @@ -88,6 +88,7 @@ } else if (token) { + options.isInvitedPerson = false pong.init(options, gameAreaId, token); hiddenGame = false; } @@ -105,6 +106,7 @@ options.playerOneUsername = invitation.playerOneUsername; options.playerTwoUsername = invitation.playerTwoUsername; options.isSomeoneIsInvited = true; + options.isInvitedPerson = true pong.init(options, gameAreaId, invitation.token); showWaitPage = false hiddenGame = false; diff --git a/srcs/requirements/svelte/api_front/src/pages/game/client/pong.ts b/srcs/requirements/svelte/api_front/src/pages/game/client/pong.ts index 9fa34f74..124b6b87 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/client/pong.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/client/pong.ts @@ -21,6 +21,7 @@ export class InitOptions { multi_balls = false; moving_walls = false; isSomeoneIsInvited = false; + isInvitedPerson = false; playerOneUsername = ""; playerTwoUsername = ""; } @@ -43,7 +44,7 @@ export function init(options: InitOptions, gameAreaId: string, token: string) initStartFunction(start); if (options.isSomeoneIsInvited) { - initWebSocket(matchOptions, token, options.playerOneUsername, true, options.playerTwoUsername); + initWebSocket(matchOptions, token, options.playerOneUsername, true, options.playerTwoUsername, options.isInvitedPerson); } else { initWebSocket(matchOptions, token, options.playerOneUsername); 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 fb55e9d8..728778ff 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,14 +34,14 @@ export const clientInfo = new ClientInfo(); export const clientInfoSpectator = new ClientInfoSpectator(); // WIP, could refactor this -export function initWebSocket(options: en.MatchOptions, token: string, username: string, privateMatch = false, playerTwoUsername?: string) +export function initWebSocket(options: en.MatchOptions, token: string, username: string, privateMatch = false, playerTwoUsername?: string, isInvitedPerson? : boolean) { 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) )); + socket.send(JSON.stringify( new ev.ClientAnnouncePlayer(options, token, username, privateMatch, playerTwoUsername, isInvitedPerson) )); } else { socket.send(JSON.stringify( new ev.ClientAnnouncePlayer(options, token, username) )); diff --git a/srcs/requirements/svelte/api_front/src/pages/game/shared_js/class/Event.ts b/srcs/requirements/svelte/api_front/src/pages/game/shared_js/class/Event.ts index 6f27b219..28b3576b 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/shared_js/class/Event.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/shared_js/class/Event.ts @@ -72,7 +72,7 @@ export class EventMatchEnd extends ServerEvent { } export class EventMatchAbort extends ServerEvent { - + constructor() { super(en.EventTypes.matchAbort); } @@ -102,12 +102,16 @@ export class ClientAnnouncePlayer extends ClientAnnounce { username: string; privateMatch: boolean; playerTwoUsername?: string; - constructor(matchOptions: en.MatchOptions, token: string, username: string, privateMatch: boolean = false, playerTwoUsername?: string) { + isInvitedPerson? : boolean; + constructor(matchOptions: en.MatchOptions, token: string, username: string, privateMatch: boolean = false, playerTwoUsername?: string, isInvitedPerson? : boolean) { super(en.ClientRole.player); this.matchOptions = matchOptions; this.token = token; this.username = username; this.privateMatch = privateMatch; + if (isInvitedPerson) { + this.isInvitedPerson = isInvitedPerson; + } if (playerTwoUsername) { this.playerTwoUsername = playerTwoUsername; }