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 8d9c77ea..830d0844 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 @@ -279,7 +279,8 @@ export class GameSession { gc.scoreRight = 0; } } - await fetch(c.addressBackEnd + "/game/gameserver/updategame", + + fetch(`${c.addressBackEnd}/game/gameserver/updategame`, { method: "POST", headers: { @@ -290,6 +291,14 @@ export class GameSession { playerOneUsernameResult: gc.scoreLeft, playerTwoUsernameResult: gc.scoreRight, }) + }) + .then((response) => { + if (!response.ok) { + throw new Error("HTTP " + response.status); + } + }) + .catch((error) => { + console.log("catch /game/gameserver/updategame: ", error); }); setTimeout(this.destroy, 15000, this); 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 8af02ec6..e3add956 100644 --- a/srcs/requirements/game_server/game_back/src/server/wsServer.ts +++ b/srcs/requirements/game_server/game_back/src/server/wsServer.ts @@ -89,7 +89,8 @@ async function clientAnnounceListener(this: WebSocket, data: string) if (announce.privateMatch) { body.playerTwoUsername = announce.playerTwoUsername; } - const response = await fetch(c.addressBackEnd + "/game/gameserver/validate", + + fetch(`${c.addressBackEnd}/game/gameserver/validate`, { method: "POST", headers: { @@ -97,17 +98,18 @@ async function clientAnnounceListener(this: WebSocket, data: string) }, body: JSON.stringify(body) }) - .catch(error => console.log("ERROR : " + error)); - if (!response || !response.ok) - { - let errMessage = "validate token error"; - if (response) { - errMessage = (await response.json()).message; + .then((response) => { + if (!response.ok) { + throw new Error("HTTP " + response.status); } - this.send(JSON.stringify( new ev.EventError(errMessage) )); + }) + .catch((error) => { + console.log("catch /game/gameserver/validate: ", error); + this.send(JSON.stringify( new ev.EventError("validate token error") )); clientTerminate(clientsMap.get(this.id)); return; - } + }); + player.matchOptions = announce.matchOptions; player.token = announce.token; player.username = announce.username; @@ -232,14 +234,26 @@ function privateMatchmaking(player: ClientPlayer) if (player.socket.OPEN) { player.socket.send(JSON.stringify( new ev.EventMatchAbort() )); } - const response = await fetch(c.addressBackEnd + "/game/gameserver/destroysession",{ + + fetch(`${c.addressBackEnd}/game/gameserver/destroysession`, + { method: "POST", - headers : {"Content-Type": "application/json"}, - body : JSON.stringify({ + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ token : player.token }) }) - .catch(error => console.log("ERROR : " + error)); + .then((response) => { + if (!response.ok) { + throw new Error("HTTP " + response.status); + } + }) + .catch((error) => { + console.log("catch /game/gameserver/destroysession: ", error); + }); + clientTerminate(player); } }, 60000); @@ -315,16 +329,22 @@ async function playerReadyConfirmationListener(this: WebSocket, data: string) playerOneUsernameResult : 0, playerTwoUsernameResult : 0 }; - const response = await fetch(c.addressBackEnd + "/game/gameserver/creategame", + + fetch(`${c.addressBackEnd}/game/gameserver/creategame`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(body) - }); - if (!response.ok) - { + }) + .then((response) => { + if (!response.ok) { + throw new Error("HTTP " + response.status); + } + }) + .catch((error) => { + console.log("catch /game/gameserver/creategame: ", error); gameSessionsMap.delete(gameSession.id); gameSession.playersMap.forEach((client) => { client.socket.send(JSON.stringify( new ev.EventMatchAbort() )); @@ -332,7 +352,7 @@ async function playerReadyConfirmationListener(this: WebSocket, data: string) clientTerminate(client); }); return; - } + }); gameSession.playersMap.forEach( (client) => { client.socket.send(JSON.stringify( new ev.ServerEvent(en.EventTypes.matchStart) )); @@ -445,18 +465,22 @@ export async function clientTerminate(client: Client) if (client.role === en.ClientRole.player) { const player = client as ClientPlayer; - console.log("/resetuserstatus " + player.username); - const response = await fetch(c.addressBackEnd + "/game/gameserver/resetuserstatus", + fetch(`${c.addressBackEnd}/game/gameserver/resetuserstatus`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({username: player.username}) + }) + .then((response) => { + if (!response.ok) { + throw new Error("HTTP " + response.status); + } + }) + .catch((error) => { + console.log("catch /game/gameserver/resetuserstatus: ", error); }); - if (!response.ok) { - console.log("/resetuserstatus " + player.username + " failed"); - } } } diff --git a/srcs/requirements/nestjs/api_back/src/game/entity/tokenGame.entity.ts b/srcs/requirements/nestjs/api_back/src/game/entity/tokenGame.entity.ts index 9481b32e..53f6ad81 100644 --- a/srcs/requirements/nestjs/api_back/src/game/entity/tokenGame.entity.ts +++ b/srcs/requirements/nestjs/api_back/src/game/entity/tokenGame.entity.ts @@ -15,8 +15,6 @@ export class TokenGame { isGameIsWithInvitation : boolean @Column({default: 0, nullable: true}) numberOfRegisteredUser : number - @Column({default : false}) - isSecondUserAcceptedRequest : boolean @Column() token : string } 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 4e6d4805..37297153 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.service.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.service.ts @@ -72,10 +72,11 @@ export class GameService { return encryptedTextToReturn } - async deleteToken(user : User){ + async deletePublicTokens(user : User){ const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame') .where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username}) .orWhere('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : user.username}) + .where('tokengame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation : false}) .getMany(); if (tokenGame) return this.tokenGameRepository.remove(tokenGame); @@ -86,7 +87,7 @@ export class GameService { console.log(user.status); if (user.status === STATUS.IN_POOL || user.status === STATUS.IN_GAME) { - await this.deleteToken(user); + await this.deletePublicTokens(user); if (user.status === STATUS.IN_POOL) { user.status = STATUS.CONNECTED; } @@ -106,7 +107,6 @@ export class GameService { const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_' + grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date()) const tok = this.tokenGameRepository.create(grantTicketDto); - tok.isSecondUserAcceptedRequest = false; tok.numberOfRegisteredUser = 0; tok.token = encryptedTextToReturn; this.tokenGameRepository.save(tok); @@ -130,34 +130,30 @@ export class GameService { async validateToken(validateTicketDto : ValidateTicketDto) { if (validateTicketDto.isGameIsWithInvitation === true) { + console.log("validateToken() PRIVATE"); const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame') .where('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername}) .andWhere('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : validateTicketDto.playerTwoUsername}) .andWhere('tokengame.gameOptions = :gameOption', {gameOption : validateTicketDto.gameOptions}) .andWhere('tokengame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: true}) - .andWhere('tokengame.isSecondUserAcceptedRequest = :choice', {choice : true}) .andWhere('tokengame.token = :token', {token : validateTicketDto.token}) .getOne(); if (tokenGame) { + console.log("playerOneUsername: " + tokenGame.playerOneUsername + "| playerTwoUsername: " + tokenGame.playerTwoUsername); + tokenGame.numberOfRegisteredUser++; + this.tokenGameRepository.save(tokenGame); if (tokenGame.numberOfRegisteredUser === 2) { this.tokenGameRepository.remove(tokenGame) - const userOne : User = await this.userRepository.createQueryBuilder('user') - .where("user.username = :username", {username : tokenGame.playerOneUsername}) - .getOne(); - const userTwo : User = await this.userRepository.createQueryBuilder('user') - .where("user.username = :username", {username : tokenGame.playerTwoUsername}) - .getOne(); - this.deleteToken(userOne) - this.deleteToken(userTwo) } return true; } } else if (validateTicketDto.isGameIsWithInvitation === false) { + console.log("validateToken() PUBLIC"); const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame') .where('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername}) .andWhere('tokengame.gameOptions = :gameOption', {gameOption : validateTicketDto.gameOptions}) @@ -168,10 +164,6 @@ export class GameService { { this.tokenGameRepository.remove(tokenGame) console.log("USERNAME : " + tokenGame.playerOneUsername) - const user : User = await this.userRepository.createQueryBuilder('user') - .where("user.username = :username", {username : tokenGame.playerOneUsername}) - .getOne(); - this.deleteToken(user) return true; } } @@ -181,8 +173,8 @@ export class GameService { async findInvitations(user : User, @Res() res : Response) { const game = await this.tokenGameRepository.createQueryBuilder('tokengame') .where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username}) + .orWhere('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : user.username}) .andWhere('tokengame.isGameIsWithInvitation = :invit', {invit : true}) - .andWhere('tokengame.isSecondUserAcceptedRequest = :choice', {choice : false}) .getMany(); if (!game) return res.status(HttpStatus.NOT_FOUND).send({message : "No invitation found"}); @@ -200,14 +192,9 @@ export class GameService { async declineInvitation(user : User, token : string, @Res() res : Response) { - /* Luke: le check de user.status n'est pas fonctionnel avec l'implémentation des invitations dans le front. - Ça me semble dispensable, je désactive donc pour le moment plutôt que de refaire l'implémentation front. */ - // if (user.status !== STATUS.CONNECTED) - // 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}) + .where('tokengame.token = :token', {token : token}) .getOne(); if (tokenGame) { @@ -238,17 +225,11 @@ export class GameService { async acceptInvitation(user : User, token : string, @Res() res : Response) { - /* Luke: le check de user.status n'est pas fonctionnel avec l'implémentation des invitations dans le front. - Ça me semble dispensable, je désactive donc pour le moment plutôt que de refaire l'implémentation front. */ - // if (user.status !== STATUS.CONNECTED) - // 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}) + .where('tokenGame.token = :token', {token : token}) .getOne(); if (tokenGame) { - tokenGame.isSecondUserAcceptedRequest = true; this.tokenGameRepository.save(tokenGame) return res.status(HttpStatus.OK).json({message : "Invitation accepted."}); } diff --git a/srcs/requirements/svelte/api_front/public/global.css b/srcs/requirements/svelte/api_front/public/global.css index 3383d87c..ef988c0e 100644 --- a/srcs/requirements/svelte/api_front/public/global.css +++ b/srcs/requirements/svelte/api_front/public/global.css @@ -12,11 +12,23 @@ body { box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; /* tmp? */ - background: bisque; + background-color: #333; display: flex; flex-direction: column; } +@font-face { + font-family: "Bit5x3"; + src: + url("./fonts/Bit5x3.woff2") format("woff2"), + local("Bit5x3"), + url("./fonts/Bit5x3.woff") format("woff"); + font-weight: normal; + font-style: normal; + font-display: swap; +} + + a { color: rgb(0,100,200); text-decoration: none; @@ -30,6 +42,13 @@ a:visited { color: rgb(0,80,160); } +.background-pages { + background-color: #333; + font-family: "Bit5x3"; + font-size: 2vw; + color: white; +} + label { display: block; } diff --git a/srcs/requirements/svelte/api_front/src/App.svelte b/srcs/requirements/svelte/api_front/src/App.svelte index 83e4b4fb..dddbb5ac 100644 --- a/srcs/requirements/svelte/api_front/src/App.svelte +++ b/srcs/requirements/svelte/api_front/src/App.svelte @@ -1,7 +1,9 @@ +{#if ($location !== '/')} +
+{/if} + diff --git a/srcs/requirements/svelte/api_front/src/pages/NotFound.svelte b/srcs/requirements/svelte/api_front/src/pages/NotFound.svelte index f34342e6..357ece2e 100644 --- a/srcs/requirements/svelte/api_front/src/pages/NotFound.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/NotFound.svelte @@ -2,9 +2,24 @@ import { link } from "svelte-spa-router"; -

We are sorry!

-

This isn't a url that we use.

-

Go home you're drunk.

- -

Take me home →

-
\ No newline at end of file + +
+
+

We are sorry!

+

This isn't a url that we use.

+

Go home you're drunk.

+
+
+ + \ No newline at end of file diff --git a/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte b/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte index 996728b8..c944f34e 100644 --- a/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte @@ -42,16 +42,6 @@ diff --git a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileDisplay.svelte b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileDisplay.svelte index efe486b0..6d7466f3 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileDisplay.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileDisplay.svelte @@ -15,22 +15,24 @@ -
- {#if user !== undefined} - - - {:else} -

Sorry

-
Failed to load current
- {/if} +
+
+ {#if user !== undefined} + + + {:else} +

Sorry

+
Failed to load current
+ {/if} +
diff --git a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte index 71c780c6..8dc48d37 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -24,22 +24,10 @@ onMount( async() => { - // DO I ACTUALLY NEED TO ON MOUNT ALL THIS STUFF? - // ALSO I COULD JUST USE THE FUNCITONS I MADE... - - - // yea no idea what - // i mean do i fetch user? i will for now user = await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user`) .then( (x) => x.json() ); fetchAll(); - // ok this shit works! - // const interval = setInterval(() => { - // fetchAll(); - // }, 1000); - - // return () => clearInterval(interval); }); const fetchAll = async() => { @@ -234,6 +222,7 @@ +
@@ -353,7 +342,7 @@
- +
diff --git a/srcs/requirements/svelte/api_front/src/pieces/utils.ts b/srcs/requirements/svelte/api_front/src/pieces/utils.ts index 76125f46..bc498c49 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/utils.ts +++ b/srcs/requirements/svelte/api_front/src/pieces/utils.ts @@ -4,7 +4,7 @@ export async function fetchAvatar(username: string) return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/avatar?username=${username}`) .then((response) => { if (!response.ok) { - throw new Error("Avatar not retrieved"); + throw new Error("HTTP " + response.status); } return response.blob(); }) @@ -22,7 +22,7 @@ export async function fetchUser() return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user`) .then((response) => { if (!response.ok) { - throw new Error("User not retrieved"); + throw new Error("HTTP " + response.status); } return response.json(); }) @@ -40,7 +40,7 @@ export async function fetchAllUsers() return fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user/all`) .then((response) => { if (!response.ok) { - throw new Error("All Users not retrieved"); + throw new Error("HTTP " + response.status); } return response.json(); })