diff --git a/make_env.sh b/make_env.sh index e097ccd7..bdc6363f 100644 --- a/make_env.sh +++ b/make_env.sh @@ -17,20 +17,21 @@ # This script is used to create a new environment for the project. # ENV_FILE_DOCKER=./srcs/.env - ENV_FILE_NESTJS=./srcs/requirements/nestjs/api_back/.env + ENV_FILE_SVELTE=./srcs/requirements/svelte/api_front/.env # Check for existing .env - if [ -f "$ENV_FILE_DOCKER" ] && [ -f "$ENV_FILE_NESTJS" ]; then - echo "The file $ENV_FILE_DOCKER and $ENV_FILE_NESTJS already exists. Do you want to overwrite them ? (y/n)" + if [ -f "$ENV_FILE_DOCKER" ]; then + echo "The file $ENV_FILE_DOCKER already exists. Do you want to overwrite it ? (y/n)" OVERWRITE="" # Ask to overwrite the .env files while [ "$OVERWRITE" != "y" ] && [ "$OVERWRITE" != "n" ]; do read -p "Enter your choice : " OVERWRITE done if [ "$OVERWRITE" = "y" ]; then - rm "$ENV_FILE_DOCKER" && rm "$ENV_FILE_NESTJS" + rm "$ENV_FILE_DOCKER" && rm "$ENV_FILE_SVELTE" docker rmi -f postgres else - echo "The file $ENV_FILE_DOCKER and $ENV_FILE_NESTJS will not be overwritten. The script will exit." + cp "$ENV_FILE_DOCKER" "$ENV_FILE_SVELTE" + echo "The file $ENV_FILE_DOCKER and $ENV_FILE_SVELTE will not be overwritten. The script will exit." exit 0 fi fi @@ -63,33 +64,19 @@ echo "REDIS_HOST=redis" >> "$ENV_FILE_DOCKER" echo "REDIS_PORT=6379" >> "$ENV_FILE_DOCKER" echo "REDIS_PASSWORD=$(generate_password)" >> "$ENV_FILE_DOCKER" - - - -# Create a new environment for nestjs -# - echo "Creating a new environment for nestjs" - echo "NODE_ENV=\$NODE_ENV" > "$ENV_FILE_NESTJS" - echo "WEBSITE_HOST=\$WEBSITE_HOST" >> "$ENV_FILE_NESTJS" - echo "WEBSITE_PORT=\$WEBSITE_PORT" >> "$ENV_FILE_NESTJS" - echo "POSTGRES_USER=\$POSTGRES_USER" >> "$ENV_FILE_NESTJS" - echo "POSTGRES_PASSWORD=\$POSTGRES_PASSWORD" >> "$ENV_FILE_NESTJS" - echo "POSTGRES_DB=\$POSTGRES_DB" >> "$ENV_FILE_NESTJS" - echo "POSTGRES_HOST=\$POSTGRES_HOST" >> "$ENV_FILE_NESTJS" - echo "POSTGRES_PORT=\$POSTGRES_PORT" >> "$ENV_FILE_NESTJS" # Connection to 42 echo "In the next steps, we'll need to enter the client secret and client id of the 42 api" read -p "Enter the client id of the 42 api : " CLIENT_ID - echo "FORTYTWO_CLIENT_ID=$CLIENT_ID" >> "$ENV_FILE_NESTJS" + echo "FORTYTWO_CLIENT_ID=$CLIENT_ID" >> "$ENV_FILE_DOCKER" read -p "Enter the client secret of the 42 api : " CLIENT_SECRET - echo "FORTYTWO_CLIENT_SECRET=$CLIENT_SECRET" >> "$ENV_FILE_NESTJS" + echo "FORTYTWO_CLIENT_SECRET=$CLIENT_SECRET" >> "$ENV_FILE_DOCKER" FT_CALLBACK="http://\$WEBSITE_HOST:\$WEBSITE_PORT/api/v2/auth/redirect" - echo "FORTYTWO_CALLBACK_URL=$FT_CALLBACK" >> "$ENV_FILE_NESTJS" + echo "FORTYTWO_CALLBACK_URL=$FT_CALLBACK" >> "$ENV_FILE_DOCKER" # Other configs - echo "COOKIE_SECRET=$(generate_password)" >> "$ENV_FILE_NESTJS" - echo "PORT=3000" >> "$ENV_FILE_NESTJS" - echo "TWO_FACTOR_AUTHENTICATION_APP_NAME=Transcendance" >> "$ENV_FILE_NESTJS" - echo "TICKET_FOR_PLAYING_GAME_SECRET=$(generate_password)" >> "$ENV_FILE_NESTJS" + echo "COOKIE_SECRET=$(generate_password)" >> "$ENV_FILE_DOCKER" + echo "PORT=3000" >> "$ENV_FILE_DOCKER" + echo "TWO_FACTOR_AUTHENTICATION_APP_NAME=Transcendance" >> "$ENV_FILE_DOCKER" + echo "TICKET_FOR_PLAYING_GAME_SECRET=$(generate_password)" >> "$ENV_FILE_DOCKER" # it's finished ! diff --git a/srcs/docker-compose.yml b/srcs/docker-compose.yml index f579cd49..7f3945c2 100644 --- a/srcs/docker-compose.yml +++ b/srcs/docker-compose.yml @@ -6,23 +6,27 @@ services: target: development dockerfile: Dockerfile no_cache: true - args: - - NODE_ENV=${NODE_ENV} - - WEBSITE_HOST=${WEBSITE_HOST} - - WEBSITE_PORT=${WEBSITE_PORT} - - POSTGRES_USER=${POSTGRES_USER} - - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - - POSTGRES_DB=${POSTGRES_DB} - - POSTGRES_HOST=${POSTGRES_HOST} - - POSTGRES_PORT=${POSTGRES_PORT} + environment: + POSTGRES_USER: "${POSTGRES_USER}" + POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}" + POSTGRES_DB: "${POSTGRES_DB}" + POSTGRES_HOST: "${POSTGRES_HOST}" + POSTGRES_PORT: "${POSTGRES_PORT}" + NODE_ENV: "${NODE_ENV}" + WEBSITE_HOST: "${WEBSITE_HOST}" + WEBSITE_PORT: "${WEBSITE_PORT}" + REDIS_HOST: "${REDIS_HOST}" + REDIS_PORT: "${REDIS_PORT}" + FORTYTWO_CLIENT_ID: "${FORTYTWO_CLIENT_ID}" + FORTYTWO_CLIENT_SECRET : "${FORTYTWO_CLIENT_SECRET}" + FORTYTWO_CALLBACK_URL: "${FORTYTWO_CALLBACK_URL}" + COOKIE_SECRET: "${COOKIE_SECRET}" + TWO_FACTOR_AUTHENTICATION_APP_NAME : "${TWO_FACTOR_AUTHENTICATION_APP_NAME}" + TICKET_FOR_PLAYING_GAME_SECRET : "${TICKET_FOR_PLAYING_GAME_SECRET}" volumes: - ./requirements/nestjs/api_back/src:/usr/app/src - ./requirements/nestjs/api_back/test:/usr/app/test/ - nestjs_photos_volume:/usr/app/src/uploads/avatars - env_file: - - .env - environment: - NODE_ENV: "${NODE_ENV}" restart: unless-stopped depends_on: - postgresql @@ -35,6 +39,8 @@ services: dockerfile: Dockerfile environment: NODE_ENV: "${NODE_ENV}" + WEBSITE_HOST: "${WEBSITE_HOST}" + WEBSITE_PORT: "${WEBSITE_PORT}" restart: unless-stopped ports: - "8042:8042" @@ -52,9 +58,9 @@ services: - ./requirements/svelte/api_front/public:/usr/app/public/ ports: - "35729:35729" - env_file: - - .env environment: + WEBSITE_HOST: "${WEBSITE_HOST}" + WEBSITE_PORT: "${WEBSITE_PORT}" NODE_ENV: "${NODE_ENV}" restart: unless-stopped depends_on: 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 e31ce997..24988b86 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 @@ -249,6 +249,7 @@ export class GameSession { this._matchEnd(en.PlayerSide.right, true); } } + private async _matchEnd(winner: en.PlayerSide, forfeit_flag: boolean = false) { this.matchEnded = true; @@ -262,6 +263,19 @@ export class GameSession { // TODO: mettre à jour la route pour gerer les forfaits (actuellement le plus haut score gagne par defaut) const gc = this.components; + console.log("================================= MATCH ENDED"); + if (forfeit_flag) { + if (winner === en.PlayerSide.left) + { + gc.scoreLeft = 3 + gc.scoreRight = 0 + } + else + { + gc.scoreLeft = 0 + gc.scoreRight = 3 + } + } await fetch(c.addressBackEnd + "/game/gameserver/updategame", { method: "POST", 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 01d0941c..99fc6597 100644 --- a/srcs/requirements/game_server/game_back/src/server/wsServer.ts +++ b/srcs/requirements/game_server/game_back/src/server/wsServer.ts @@ -397,6 +397,7 @@ export function clientTerminate(client: Client) if (client.gameSession) { client.gameSession.playersMap.delete(client.id); + client.gameSession.spectatorsMap.delete(client.id); if (client.gameSession.playersMap.size === 0) { client.gameSession.destroy(); diff --git a/srcs/requirements/nestjs/Dockerfile b/srcs/requirements/nestjs/Dockerfile index 2da3b747..8773c1b1 100644 --- a/srcs/requirements/nestjs/Dockerfile +++ b/srcs/requirements/nestjs/Dockerfile @@ -2,46 +2,8 @@ FROM node:alpine AS development WORKDIR /usr/app -ARG NODE_ENV -ARG WEBSITE_HOST -ARG WEBSITE_PORT -ARG POSTGRES_USER -ARG POSTGRES_PASSWORD -ARG POSTGRES_DB -ARG POSTGRES_HOST -ARG POSTGRES_PORT - COPY ./api_back ./ COPY ./api_back/src/uploads/avatars/default.png ./uploads/avatars/default.png -COPY ./api_back/.env ./.env -#RUN sed -i "s/\$NODE_ENV/${NODE_ENV}/g" ./.env && \ -# sed -i "s/\$WEBSITE_HOST/${WEBSITE_HOST}/g" ./.env && \ -# sed -i "s/\$WEBSITE_PORT/${WEBSITE_PORT}/g" ./.env && \ -# sed -i "s/\$POSTGRES_USER/${POSTGRES_USER}/g" ./.env && \ -# sed -i "s/\$POSTGRES_PASSWORD/${POSTGRES_PASSWORD}/g" ./.env && \ -# sed -i "s/\$POSTGRES_DB/${POSTGRES_DB}/g" ./.env && \ -# sed -i "s/\$POSTGRES_HOST/${POSTGRES_HOST}/g" ./.env && \ -# sed -i "s/\$POSTGRES_PORT/${POSTGRES_PORT}/g" ./.env - -RUN sed -i "s/\$NODE_ENV/${NODE_ENV}/g" ./.env -RUN sed -i "s/\$WEBSITE_HOST/${WEBSITE_HOST}/g" ./.env -RUN sed -i "s/\$WEBSITE_PORT/${WEBSITE_PORT}/g" ./.env -RUN sed -i "s/\$POSTGRES_USER/${POSTGRES_USER}/g" ./.env - -RUN echo ["$POSTGRESS_PASSWORD"] && \ - echo ["$POSTGRESS_PASSWORD"] && \ - echo ["$POSTGRESS_PASSWORD"] && \ - echo ["$POSTGRESS_PASSWORD"] && \ - echo ["$POSTGRESS_PASSWORD"] && \ - echo ["$POSTGRESS_PASSWORD"] && \ - echo ["$POSTGRESS_PASSWORD"] && \ - sed -i "s/\$POSTGRES_PASSWORD/'${POSTGRESS_PASSWORD}'/g" ./.env - -RUN sed -i "s/\$POSTGRES_DB/${POSTGRES_DB}/g" ./.env -RUN sed -i "s/\$POSTGRES_HOST/${POSTGRES_HOST}/g" ./.env -RUN sed -i "s/\$POSTGRES_PORT/${POSTGRES_PORT}/g" ./.env - -RUN npm install RUN npm ci CMD [ "npm", "run", "start:dev" ] 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 1af581b0..11386c4b 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,6 +17,9 @@ export class Game { @Column({default : 0, nullable : true}) playerTwoUsernameResult : number + @Column({default : 0}) + gameOptions: number + @Column({unique : true}) gameServerIdOfTheMatch: string 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 b6b3770b..b393eaf5 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.controller.ts @@ -13,6 +13,14 @@ export class GameController { constructor (private readonly gameService : GameService) { } + @Get('match/all') + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + async getMatchesForSpectator() + { + return this.gameService.getMatchesForSpectator(); + } + @Get('ranking') @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) 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 f00c941d..437abd5e 100644 --- a/srcs/requirements/nestjs/api_back/src/game/game.service.ts +++ b/srcs/requirements/nestjs/api_back/src/game/game.service.ts @@ -28,6 +28,21 @@ export class GameService { private readonly friendShipService : FriendshipService ) { } + async getMatchesForSpectator() { + const games = await this.gameRepository.createQueryBuilder("game") + .where('game.isMatchIsFinished = :isMatchIsFinished', {isMatchIsFinished : false}) + .getMany(); + const gamesToReturn : Partial[] = [] + for (const game of games) + { + gamesToReturn.push({gameServerIdOfTheMatch : game.gameServerIdOfTheMatch, + gameOptions : game.gameOptions, playerOneUsername : game.playerOneUsername, + playerTwoUsername : game.playerTwoUsername}) + console.log("Is match is finished : " + game.isMatchIsFinished) + } + return gamesToReturn; + } + async getRankingForAllUsers(currentUser : User) { const users = await this.userRepository.createQueryBuilder("user") .leftJoinAndSelect("user.stats", "stats") @@ -255,6 +270,8 @@ export class GameService { game.playerOneUsernameResult = updateGameDto.playerOneUsernameResult game.playerTwoUsernameResult = updateGameDto.playerTwoUsernameResult this.gameRepository.save(game); + console.log("On a sauvegardé la partie. Game :") + console.log(game) const playerOne = await this.userRepository.findOneBy({username : game.playerOneUsername}) const playerTwo = await this.userRepository.findOneBy({username : game.playerTwoUsername}) if (!playerOne || !playerTwo) diff --git a/srcs/requirements/svelte/api_front/package-lock.json b/srcs/requirements/svelte/api_front/package-lock.json index 65b6d6f6..943549d1 100644 --- a/srcs/requirements/svelte/api_front/package-lock.json +++ b/srcs/requirements/svelte/api_front/package-lock.json @@ -8,6 +8,7 @@ "name": "svelte-app", "version": "1.0.0", "dependencies": { + "dotenv": "^16.0.3", "sirv-cli": "^2.0.0", "socket.io-client": "^4.5.4", "svelte-spa-router": "^3.3.0" @@ -17,6 +18,7 @@ "@rollup/plugin-node-resolve": "^11.0.0", "@rollup/plugin-typescript": "^8.0.0", "@tsconfig/svelte": "^2.0.0", + "@types/node": "^18.11.18", "rollup": "^2.3.4", "rollup-plugin-css-only": "^3.1.0", "rollup-plugin-livereload": "^2.0.0", @@ -267,9 +269,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "18.11.9", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz", - "integrity": "sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==", + "version": "18.11.18", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.18.tgz", + "integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==", "dev": true }, "node_modules/@types/pug": { @@ -522,6 +524,14 @@ "node": ">=8" } }, + "node_modules/dotenv": { + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", + "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", + "engines": { + "node": ">=12" + } + }, "node_modules/engine.io-client": { "version": "6.2.3", "resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.2.3.tgz", diff --git a/srcs/requirements/svelte/api_front/package.json b/srcs/requirements/svelte/api_front/package.json index ca1cf396..0fcd7ed5 100644 --- a/srcs/requirements/svelte/api_front/package.json +++ b/srcs/requirements/svelte/api_front/package.json @@ -13,6 +13,7 @@ "@rollup/plugin-node-resolve": "^11.0.0", "@rollup/plugin-typescript": "^8.0.0", "@tsconfig/svelte": "^2.0.0", + "@types/node": "^18.11.18", "rollup": "^2.3.4", "rollup-plugin-css-only": "^3.1.0", "rollup-plugin-livereload": "^2.0.0", @@ -25,6 +26,7 @@ "typescript": "^4.0.0" }, "dependencies": { + "dotenv": "^16.0.3", "sirv-cli": "^2.0.0", "socket.io-client": "^4.5.4", "svelte-spa-router": "^3.3.0" diff --git a/srcs/requirements/svelte/api_front/rollup.config.js b/srcs/requirements/svelte/api_front/rollup.config.js index 1cf48810..2f0a7243 100644 --- a/srcs/requirements/svelte/api_front/rollup.config.js +++ b/srcs/requirements/svelte/api_front/rollup.config.js @@ -30,6 +30,7 @@ function serve() { }; } + export default { input: 'src/main.ts', output: { diff --git a/srcs/requirements/svelte/api_front/src/Constantes.svelte b/srcs/requirements/svelte/api_front/src/Constantes.svelte deleted file mode 100644 index 04392cdd..00000000 --- a/srcs/requirements/svelte/api_front/src/Constantes.svelte +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/srcs/requirements/svelte/api_front/src/Constantes.ts b/srcs/requirements/svelte/api_front/src/Constantes.ts new file mode 100644 index 00000000..e2ead3eb --- /dev/null +++ b/srcs/requirements/svelte/api_front/src/Constantes.ts @@ -0,0 +1,3 @@ +export const Domain = "transcendance"; +export const Port = "8080"; +export const PortIo = Port; diff --git a/srcs/requirements/svelte/api_front/src/main.js b/srcs/requirements/svelte/api_front/src/main.js index 34f779eb..7cd72a7c 100644 --- a/srcs/requirements/svelte/api_front/src/main.js +++ b/srcs/requirements/svelte/api_front/src/main.js @@ -1,4 +1,7 @@ import App from './App.svelte'; +import dotenv from 'dotenv'; + +dotenv.config(); const app = new App({ target: document.body, props: { @@ -6,4 +9,4 @@ const app = new App({ } }); export default app; -//# sourceMappingURL=main.js.map \ No newline at end of file +//# sourceMappingURL=main.js.map diff --git a/srcs/requirements/svelte/api_front/src/main.ts b/srcs/requirements/svelte/api_front/src/main.ts index dbcefc37..cfd43edb 100644 --- a/srcs/requirements/svelte/api_front/src/main.ts +++ b/srcs/requirements/svelte/api_front/src/main.ts @@ -7,4 +7,4 @@ const app = new App({ } }); -export default app; \ No newline at end of file +export default app; diff --git a/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte b/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte index 7f2d4c20..2b66674f 100644 --- a/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte @@ -3,13 +3,13 @@ import { push } from "svelte-spa-router"; import { onMount } from 'svelte'; import { get } from "svelte/store"; - import { Domain, Port } from "../Constantes.svelte"; + import { Domain, Port } from "../Constantes"; let user; onMount(async () => { - user = await fetch(`http://${Domain}:${Port}/api/v2/user`) + user = await fetch(`http://${process.env.WEBSITE_HOST}:${Port}/api/v2/user`) .then((resp) => resp.json()) // i mean i could do a failed to load user or some shit, maybe with a .catch or something? but atm why bother diff --git a/srcs/requirements/svelte/api_front/src/pages/TwoFactorAuthentication.svelte b/srcs/requirements/svelte/api_front/src/pages/TwoFactorAuthentication.svelte index 74b5afae..d36019ee 100644 --- a/srcs/requirements/svelte/api_front/src/pages/TwoFactorAuthentication.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/TwoFactorAuthentication.svelte @@ -1,19 +1,7 @@ @@ -93,7 +106,10 @@
- options + {#if hiddenMatchList} + no match available + {:else} + options

sound :

@@ -101,13 +117,15 @@
+ + {#each matchList as match} + initGameSpectator(match.gameServerIdOfTheMatch, match.gameOptions)} /> + {/each} + + {/if}
- - {#each matchList as match} - initGameSpectator(match.gameSessionId, match.matchOptions)} /> - {/each} - +
{:else}
diff --git a/srcs/requirements/svelte/api_front/src/pages/game/Ranking.svelte b/srcs/requirements/svelte/api_front/src/pages/game/Ranking.svelte index e92ba54f..3d458c5d 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/Ranking.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/game/Ranking.svelte @@ -2,7 +2,7 @@