diff --git a/.gitignore b/.gitignore index 813ca863..ace7e3a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,15 @@ #.env backend/node_modules/ -backend/dist/ +containers/backend/dist/ api/node_modules/ -api/dist/ +containers/api/dist/ pong/node_modules/ -pong/dist/ +containers/pong/dist/ */node_modules/ -*/dist/ \ No newline at end of file +*/*/node_modules/ + +*/dist/ +*/*/dist/ \ No newline at end of file diff --git a/Makefile b/Makefile index faa7ab2a..dd39d9f4 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,13 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/03/19 09:29:27 by apommier #+# #+# # -# Updated: 2023/04/04 18:14:43 by apommier ### ########.fr # +# Updated: 2023/05/10 13:37:27 by apommier ### ########.fr # # # # **************************************************************************** # all: # -mkdir -p /home/apommier/data/wordpress - -mkdir -p /home/apommier/data/pgsql +# -mkdir -p /home/apommier/data/pgsql docker-compose -f docker-compose.yml up --build fclean: down diff --git a/api/.eslintrc.js b/containers/api/.eslintrc.js similarity index 100% rename from api/.eslintrc.js rename to containers/api/.eslintrc.js diff --git a/api/.gitignore b/containers/api/.gitignore similarity index 100% rename from api/.gitignore rename to containers/api/.gitignore diff --git a/api/.prettierrc b/containers/api/.prettierrc similarity index 100% rename from api/.prettierrc rename to containers/api/.prettierrc diff --git a/api/README.md b/containers/api/README.md similarity index 100% rename from api/README.md rename to containers/api/README.md diff --git a/api/nest-cli.json b/containers/api/nest-cli.json similarity index 100% rename from api/nest-cli.json rename to containers/api/nest-cli.json diff --git a/api/package-lock.json b/containers/api/package-lock.json similarity index 100% rename from api/package-lock.json rename to containers/api/package-lock.json diff --git a/api/package.json b/containers/api/package.json similarity index 100% rename from api/package.json rename to containers/api/package.json diff --git a/api/src/app.controller.spec.ts b/containers/api/src/app.controller.spec.ts similarity index 100% rename from api/src/app.controller.spec.ts rename to containers/api/src/app.controller.spec.ts diff --git a/api/src/app.controller.ts b/containers/api/src/app.controller.ts similarity index 52% rename from api/src/app.controller.ts rename to containers/api/src/app.controller.ts index d6100fbb..7e8f41e1 100644 --- a/api/src/app.controller.ts +++ b/containers/api/src/app.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Request, Req, Get, Post, UseGuards, Redirect, Res } from '@nestjs/common'; +import { Controller, Request, Req, Get, Post, UseGuards, Redirect, Res, Body } from '@nestjs/common'; import { JwtAuthGuard } from './auth/jwt-auth.guard'; import { AuthService } from './auth/auth.service'; @@ -6,6 +6,8 @@ import { loginClass } from './auth/login42' import { ChatService } from './chat/chat.service'; import { UsersService } from './users/users.service'; +import { MatchLog } from './model/user.entity' + // import { AuthGuard } from '@nestjs/passport'; // import { Login42 } from './auth/login42' // import { loginClass } from './auth/test' @@ -18,11 +20,8 @@ export class AppController { private chatService: ChatService, private userService: UsersService, ) {} -// @Post('auth/login') -// async login() { -// const user = Login42(); -// return this.authService.login5(user); -// } + kFactor = 36; + scaleFactor = 400; @Redirect('http://localhost/token', 302) @Get('auth/login') @@ -39,14 +38,6 @@ export class AppController { console.log(`data in api = ${(await data).access_token}`) const token = (await data).access_token; return { url: `http://localhost/token?data=${encodeURIComponent(JSON.stringify(token))}` }; - // console.log("login function"); - // console.log(`url = ${url}`); - // const user = this.loginClass.Login42(url); - // console.log("login42 done"); - // console.log(`user= ${user}`); - // console.log(`dataaaa = ${data}`); - // console.log("before return"); - // return { data }; } @UseGuards(JwtAuthGuard) @@ -56,40 +47,72 @@ export class AppController { console.log(`req user api= ${req.user}`) console.log(`json user api= ${myJSON}`) return req.user; - // const user = req.user; - // const returned = { - // username: user.username, - // sub: user.sub, - // }; - // console.log(`user in api = ${returned}`) - // return returned; } @UseGuards(JwtAuthGuard) @Post('/win') - async addWin(@Request() req) { + async addWin(@Request() req, @Body() data: any) { const user = await this.userService.findOne(req.user.username); user.win++; - this.userService.save(user); + const Esp = 1 / (1 + Math.pow(10, (data.opRank - user.rank) / this.scaleFactor)) + const newRank = user.rank + this.kFactor * (1 - Esp); + + user.rank = newRank; + console.log(`win new rank= ${newRank}`); + console.log(`data win = ${data}`) + + const newMatch = new MatchLog; + newMatch.myScore = data.myScore; + newMatch.opScore = data.opScore; + newMatch.opponent = data.opName; + newMatch.parent = user; + + await this.userService.saveChild(user, newMatch); } @UseGuards(JwtAuthGuard) @Post('/loss') - async addLoss(@Request() req) { + async addLoss(@Request() req, @Body() data: any) { const user = await this.userService.findOne(req.user.username); user.loss++; - this.userService.save(user); + + const Esp = 1 / (1 + Math.pow(10, (data.opRank - user.rank) / this.scaleFactor)) + const newRank = user.rank + this.kFactor * (0 - Esp); + + user.rank = newRank; + console.log(`loss new rank= ${newRank}`); + console.log(`data loss = ${data}`) + + const newMatch = new MatchLog; + newMatch.myScore = data.myScore; + newMatch.opScore = data.opScore; + newMatch.opponent = data.opName; + newMatch.parent = user; + + await this.userService.saveChild(user, newMatch); } -// @UseGuards(JwtAuthGuard) -// @Post('/api/victory') -// addVictory() { -// this.userService.findOneBy() -// } - - @Get('/api/chat') - async Chat(@Res() res) { - const messages = await this.chatService.getMessages(); - res.json(messages); + @UseGuards(JwtAuthGuard) + @Get('/rank') + async getRank(@Request() req) + { + const user = await this.userService.findOne(req.user.username); + return user.rank; } + + @UseGuards(JwtAuthGuard) + @Post('/quit') + async setOffline(@Request() req) { + + const user = await this.userService.findOne(req.user.username); + + user.status = 0; + await this.userService.save(user); + } + +// @Get('/chat') +// async Chat(@Res() res) { +// const messages = await this.chatService.getMessages(); +// res.json(messages); +// } } \ No newline at end of file diff --git a/api/src/app.module.ts b/containers/api/src/app.module.ts similarity index 100% rename from api/src/app.module.ts rename to containers/api/src/app.module.ts diff --git a/api/src/app.service.ts b/containers/api/src/app.service.ts similarity index 100% rename from api/src/app.service.ts rename to containers/api/src/app.service.ts diff --git a/api/src/auth/auth.module.ts b/containers/api/src/auth/auth.module.ts similarity index 100% rename from api/src/auth/auth.module.ts rename to containers/api/src/auth/auth.module.ts diff --git a/api/src/auth/auth.service.spec.ts b/containers/api/src/auth/auth.service.spec.ts similarity index 100% rename from api/src/auth/auth.service.spec.ts rename to containers/api/src/auth/auth.service.spec.ts diff --git a/api/src/auth/auth.service.ts b/containers/api/src/auth/auth.service.ts similarity index 100% rename from api/src/auth/auth.service.ts rename to containers/api/src/auth/auth.service.ts diff --git a/api/src/auth/constants.ts b/containers/api/src/auth/constants.ts similarity index 100% rename from api/src/auth/constants.ts rename to containers/api/src/auth/constants.ts diff --git a/api/src/auth/jwt-auth.guard.ts b/containers/api/src/auth/jwt-auth.guard.ts similarity index 100% rename from api/src/auth/jwt-auth.guard.ts rename to containers/api/src/auth/jwt-auth.guard.ts diff --git a/api/src/auth/jwt.strategy.ts b/containers/api/src/auth/jwt.strategy.ts similarity index 100% rename from api/src/auth/jwt.strategy.ts rename to containers/api/src/auth/jwt.strategy.ts diff --git a/api/src/auth/local.strategy.ts b/containers/api/src/auth/local.strategy.ts similarity index 100% rename from api/src/auth/local.strategy.ts rename to containers/api/src/auth/local.strategy.ts diff --git a/api/src/auth/login42.ts b/containers/api/src/auth/login42.ts similarity index 95% rename from api/src/auth/login42.ts rename to containers/api/src/auth/login42.ts index 19e1de13..f42598e9 100644 --- a/api/src/auth/login42.ts +++ b/containers/api/src/auth/login42.ts @@ -5,7 +5,7 @@ import { Injectable } from '@nestjs/common'; import { Repository } from 'typeorm'; -import { User } from '../model/user.entity'; +import { MatchLog } from '../model/user.entity'; @Injectable() export class loginClass { @@ -56,8 +56,6 @@ export class loginClass { if (!user) { console.log(`no user, creating one`); user = { - // name: null, - // description: null, id: null, password: null, username: userName, @@ -66,6 +64,8 @@ export class loginClass { loss: 0, rank: 1200, userId: userId, + children: null, + status: 1, }; await this.usersService.create(user); } diff --git a/api/src/chat/chat.module.ts b/containers/api/src/chat/chat.module.ts similarity index 100% rename from api/src/chat/chat.module.ts rename to containers/api/src/chat/chat.module.ts diff --git a/api/src/chat/chat.service.spec.ts b/containers/api/src/chat/chat.service.spec.ts similarity index 100% rename from api/src/chat/chat.service.spec.ts rename to containers/api/src/chat/chat.service.spec.ts diff --git a/api/src/chat/chat.service.ts b/containers/api/src/chat/chat.service.ts similarity index 100% rename from api/src/chat/chat.service.ts rename to containers/api/src/chat/chat.service.ts diff --git a/api/src/config/config.service.ts b/containers/api/src/config/config.service.ts similarity index 100% rename from api/src/config/config.service.ts rename to containers/api/src/config/config.service.ts diff --git a/api/src/main.ts b/containers/api/src/main.ts similarity index 100% rename from api/src/main.ts rename to containers/api/src/main.ts diff --git a/api/src/model/chat.entity.ts b/containers/api/src/model/chat.entity.ts similarity index 75% rename from api/src/model/chat.entity.ts rename to containers/api/src/model/chat.entity.ts index 865e162c..f72b7f3d 100644 --- a/api/src/model/chat.entity.ts +++ b/containers/api/src/model/chat.entity.ts @@ -1,7 +1,6 @@ import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, BaseEntity } from 'typeorm'; @Entity() -// export class Chat extends BaseEntity { export class Chat{ @PrimaryGeneratedColumn('uuid') id: number; @@ -15,5 +14,10 @@ import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, BaseEntity } @CreateDateColumn() createdAt: Date; + //ban user + //user list + //blocked user (in user model ?) + //op list + //a way to stock conv ? } \ No newline at end of file diff --git a/api/src/model/user.entity.ts b/containers/api/src/model/user.entity.ts similarity index 53% rename from api/src/model/user.entity.ts rename to containers/api/src/model/user.entity.ts index 3e895e4e..f4a147cf 100644 --- a/api/src/model/user.entity.ts +++ b/containers/api/src/model/user.entity.ts @@ -1,5 +1,4 @@ // item.entity.ts -import { Entity, Column, PrimaryGeneratedColumn, BaseEntity } from 'typeorm'; // import { BaseEntity } from './base.entity'; // @Column({ type: 'varchar', length: 300 , nullable: true}) @@ -7,37 +6,62 @@ import { Entity, Column, PrimaryGeneratedColumn, BaseEntity } from 'typeorm'; // @Column({ type: 'varchar', length: 300 , nullable: true}) // description: string; + + +import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; +import { ManyToOne, OneToMany } from 'typeorm'; + + + + @Entity({ name: 'User' }) -// export class User extends BaseEntity { export class User { @PrimaryGeneratedColumn() id: number; - + @Column({ nullable: true }) nickname: string; - + @Column({ nullable: true }) username: string; @Column({ nullable: true }) password: string; - - // @Column({ nullable: true }) - // email: string; - - // @Column({ nullable: true }) - // password: string; - + @Column({ default: 0 }) win: number; - + @Column({ default: 0 }) loss: number; - + @Column({ default: 0 }) rank: number; + @Column({ default: 0 }) //0 = offline | 1 = connected | 2 = in game + status: number; + @Column({ default: 0 }) userId: number; + + @OneToMany(() => MatchLog, child => child.parent) + children: MatchLog[]; +} + +@Entity() +export class MatchLog { + @PrimaryGeneratedColumn() + id: number; + + @Column() + opponent: string; + + @Column({ default: 0 }) + myScore: number; + + @Column({ default: 0 }) + opScore: number; + + @ManyToOne(() => User, parent => parent.children) + parent: User; } \ No newline at end of file diff --git a/api/src/users/users.module.ts b/containers/api/src/users/users.module.ts similarity index 82% rename from api/src/users/users.module.ts rename to containers/api/src/users/users.module.ts index eae169d5..cd116f29 100644 --- a/api/src/users/users.module.ts +++ b/containers/api/src/users/users.module.ts @@ -3,13 +3,14 @@ import { UsersService} from './users.service'; import { TypeOrmModule } from '@nestjs/typeorm'; import { getTypeOrmConfig } from '../config/config.service'; -import { User } from '../model/user.entity'; +import { MatchLog, User } from '../model/user.entity'; @Module({ imports: [ TypeOrmModule.forRoot(getTypeOrmConfig()), TypeOrmModule.forFeature([User]), + TypeOrmModule.forFeature([MatchLog]), // TypeOrmModule.forFeature([UserRepository]), ], providers:[UsersService], diff --git a/api/src/users/users.service.spec.ts b/containers/api/src/users/users.service.spec.ts similarity index 100% rename from api/src/users/users.service.spec.ts rename to containers/api/src/users/users.service.spec.ts diff --git a/api/src/users/users.service.ts b/containers/api/src/users/users.service.ts similarity index 80% rename from api/src/users/users.service.ts rename to containers/api/src/users/users.service.ts index c27f3986..49cc884c 100644 --- a/api/src/users/users.service.ts +++ b/containers/api/src/users/users.service.ts @@ -3,12 +3,14 @@ import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { User } from '../model/user.entity'; +import { MatchLog } from '../model/user.entity'; @Injectable() export class UsersService { constructor( @InjectRepository(User) private userRepository: Repository, + @InjectRepository(MatchLog) private readonly matchRepository: Repository, ) {} getHello(): string { @@ -30,6 +32,12 @@ export class UsersService { async save(user: User): Promise { return await this.userRepository.save(user); } + + async saveChild(user: User, match: MatchLog): Promise { + // user.match = savedChild; + await this.matchRepository.save(match); + return await this.userRepository.save(user); + } } diff --git a/api/test/app.e2e-spec.ts b/containers/api/test/app.e2e-spec.ts similarity index 100% rename from api/test/app.e2e-spec.ts rename to containers/api/test/app.e2e-spec.ts diff --git a/api/test/jest-e2e.json b/containers/api/test/jest-e2e.json similarity index 100% rename from api/test/jest-e2e.json rename to containers/api/test/jest-e2e.json diff --git a/api/tsconfig.build.json b/containers/api/tsconfig.build.json similarity index 100% rename from api/tsconfig.build.json rename to containers/api/tsconfig.build.json diff --git a/api/tsconfig.json b/containers/api/tsconfig.json similarity index 100% rename from api/tsconfig.json rename to containers/api/tsconfig.json diff --git a/chat/.eslintrc.js b/containers/chat/.eslintrc.js similarity index 100% rename from chat/.eslintrc.js rename to containers/chat/.eslintrc.js diff --git a/chat/.gitignore b/containers/chat/.gitignore similarity index 100% rename from chat/.gitignore rename to containers/chat/.gitignore diff --git a/chat/.prettierrc b/containers/chat/.prettierrc similarity index 100% rename from chat/.prettierrc rename to containers/chat/.prettierrc diff --git a/chat/README.md b/containers/chat/README.md similarity index 100% rename from chat/README.md rename to containers/chat/README.md diff --git a/chat/nest-cli.json b/containers/chat/nest-cli.json similarity index 100% rename from chat/nest-cli.json rename to containers/chat/nest-cli.json diff --git a/chat/package-lock.json b/containers/chat/package-lock.json similarity index 100% rename from chat/package-lock.json rename to containers/chat/package-lock.json diff --git a/chat/package.json b/containers/chat/package.json similarity index 100% rename from chat/package.json rename to containers/chat/package.json diff --git a/chat/src/app.controller.spec.ts b/containers/chat/src/app.controller.spec.ts similarity index 100% rename from chat/src/app.controller.spec.ts rename to containers/chat/src/app.controller.spec.ts diff --git a/chat/src/app.controller.ts b/containers/chat/src/app.controller.ts similarity index 100% rename from chat/src/app.controller.ts rename to containers/chat/src/app.controller.ts diff --git a/chat/src/app.module.ts b/containers/chat/src/app.module.ts similarity index 100% rename from chat/src/app.module.ts rename to containers/chat/src/app.module.ts diff --git a/chat/src/app.service.ts b/containers/chat/src/app.service.ts similarity index 100% rename from chat/src/app.service.ts rename to containers/chat/src/app.service.ts diff --git a/chat/src/chat/chat.gateway.spec.ts b/containers/chat/src/chat/chat.gateway.spec.ts similarity index 100% rename from chat/src/chat/chat.gateway.spec.ts rename to containers/chat/src/chat/chat.gateway.spec.ts diff --git a/chat/src/chat/chat.gateway.ts b/containers/chat/src/chat/chat.gateway.ts similarity index 100% rename from chat/src/chat/chat.gateway.ts rename to containers/chat/src/chat/chat.gateway.ts diff --git a/chat/src/main.ts b/containers/chat/src/main.ts similarity index 100% rename from chat/src/main.ts rename to containers/chat/src/main.ts diff --git a/chat/test/app.e2e-spec.ts b/containers/chat/test/app.e2e-spec.ts similarity index 100% rename from chat/test/app.e2e-spec.ts rename to containers/chat/test/app.e2e-spec.ts diff --git a/chat/test/jest-e2e.json b/containers/chat/test/jest-e2e.json similarity index 100% rename from chat/test/jest-e2e.json rename to containers/chat/test/jest-e2e.json diff --git a/chat/tsconfig.build.json b/containers/chat/tsconfig.build.json similarity index 100% rename from chat/tsconfig.build.json rename to containers/chat/tsconfig.build.json diff --git a/chat/tsconfig.json b/containers/chat/tsconfig.json similarity index 100% rename from chat/tsconfig.json rename to containers/chat/tsconfig.json diff --git a/pong/.eslintrc.js b/containers/pong/.eslintrc.js similarity index 100% rename from pong/.eslintrc.js rename to containers/pong/.eslintrc.js diff --git a/pong/.prettierrc b/containers/pong/.prettierrc similarity index 100% rename from pong/.prettierrc rename to containers/pong/.prettierrc diff --git a/pong/README.md b/containers/pong/README.md similarity index 100% rename from pong/README.md rename to containers/pong/README.md diff --git a/pong/nest-cli.json b/containers/pong/nest-cli.json similarity index 100% rename from pong/nest-cli.json rename to containers/pong/nest-cli.json diff --git a/pong/package-lock.json b/containers/pong/package-lock.json similarity index 100% rename from pong/package-lock.json rename to containers/pong/package-lock.json diff --git a/pong/package.json b/containers/pong/package.json similarity index 100% rename from pong/package.json rename to containers/pong/package.json diff --git a/pong/src/app.controller.spec.ts b/containers/pong/src/app.controller.spec.ts similarity index 100% rename from pong/src/app.controller.spec.ts rename to containers/pong/src/app.controller.spec.ts diff --git a/pong/src/app.controller.ts b/containers/pong/src/app.controller.ts similarity index 100% rename from pong/src/app.controller.ts rename to containers/pong/src/app.controller.ts diff --git a/pong/src/app.module.ts b/containers/pong/src/app.module.ts similarity index 100% rename from pong/src/app.module.ts rename to containers/pong/src/app.module.ts diff --git a/pong/src/app.service.ts b/containers/pong/src/app.service.ts similarity index 100% rename from pong/src/app.service.ts rename to containers/pong/src/app.service.ts diff --git a/pong/src/main.ts b/containers/pong/src/main.ts similarity index 100% rename from pong/src/main.ts rename to containers/pong/src/main.ts diff --git a/pong/src/pong/pong.gateway.spec.ts b/containers/pong/src/pong/pong.gateway.spec.ts similarity index 100% rename from pong/src/pong/pong.gateway.spec.ts rename to containers/pong/src/pong/pong.gateway.spec.ts diff --git a/pong/src/pong/pong.gateway.ts b/containers/pong/src/pong/pong.gateway.ts similarity index 92% rename from pong/src/pong/pong.gateway.ts rename to containers/pong/src/pong/pong.gateway.ts index 6532e9fa..9ca1c80b 100644 --- a/pong/src/pong/pong.gateway.ts +++ b/containers/pong/src/pong/pong.gateway.ts @@ -58,6 +58,7 @@ export class PongGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa console.log(`Player ${player.id} joined game ${gameId}`); }); players.forEach((player) => { + // const playersIds = game.map(socket => socket.id); player.emit('pong:gameId', gameId); }); } @@ -227,6 +228,22 @@ export class PongGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa } } + @SubscribeMessage('pong:name') + getName(client: Socket, payload: any): void + { + const game = this.games.get(payload.gameId); + const playersIds = game.map(socket => socket.id); + + console.log(`name of client= ${payload.name}`); + if (playersIds[0] === payload.id) + { + this.clients[playersIds[1]].emit('pong:name', payload.name); + } + if (playersIds[1] === payload.id) + { + this.clients[playersIds[0]].emit('pong:name', payload.name); + } + } }//end of Web Socket Gateway diff --git a/pong/test/app.e2e-spec.ts b/containers/pong/test/app.e2e-spec.ts similarity index 100% rename from pong/test/app.e2e-spec.ts rename to containers/pong/test/app.e2e-spec.ts diff --git a/pong/test/jest-e2e.json b/containers/pong/test/jest-e2e.json similarity index 100% rename from pong/test/jest-e2e.json rename to containers/pong/test/jest-e2e.json diff --git a/pong/tsconfig.build.json b/containers/pong/tsconfig.build.json similarity index 100% rename from pong/tsconfig.build.json rename to containers/pong/tsconfig.build.json diff --git a/pong/tsconfig.json b/containers/pong/tsconfig.json similarity index 100% rename from pong/tsconfig.json rename to containers/pong/tsconfig.json diff --git a/react/.gitignore b/containers/react/.gitignore similarity index 100% rename from react/.gitignore rename to containers/react/.gitignore diff --git a/react/README.md b/containers/react/README.md similarity index 100% rename from react/README.md rename to containers/react/README.md diff --git a/react/package-lock.json b/containers/react/package-lock.json similarity index 100% rename from react/package-lock.json rename to containers/react/package-lock.json diff --git a/react/package.json b/containers/react/package.json similarity index 100% rename from react/package.json rename to containers/react/package.json diff --git a/react/public/favicon.ico b/containers/react/public/favicon.ico similarity index 100% rename from react/public/favicon.ico rename to containers/react/public/favicon.ico diff --git a/react/public/index.html b/containers/react/public/index.html similarity index 100% rename from react/public/index.html rename to containers/react/public/index.html diff --git a/react/public/logo192.png b/containers/react/public/logo192.png similarity index 100% rename from react/public/logo192.png rename to containers/react/public/logo192.png diff --git a/react/public/logo512.png b/containers/react/public/logo512.png similarity index 100% rename from react/public/logo512.png rename to containers/react/public/logo512.png diff --git a/react/public/manifest.json b/containers/react/public/manifest.json similarity index 100% rename from react/public/manifest.json rename to containers/react/public/manifest.json diff --git a/react/public/robots.txt b/containers/react/public/robots.txt similarity index 100% rename from react/public/robots.txt rename to containers/react/public/robots.txt diff --git a/containers/react/src/components/App.js b/containers/react/src/components/App.js new file mode 100644 index 00000000..4950e722 --- /dev/null +++ b/containers/react/src/components/App.js @@ -0,0 +1,92 @@ +import { useEffect, useState } from "react"; +import { Navigate, Route, Routes, useNavigate } from "react-router-dom"; +import "../styles/App.css"; + +import Field from './Field'; +import PlayButton from './PlayButton'; +import SuccessToken from '../script/tokenSuccess' +import Home from './Home'; + +import api from '../script/axiosApi'; + +// import Login42 from './Login42'; + +// const navigate = useNavigate(); + +// const [isLoggedIn, setisLoggedIn] = useState(false); +// useEffect(() => { +// if (!localStorage.getItem('token')) +// { +// navigate("/"); +// } +// else +// { +// setisLoggedIn(true); +// } +// },); + +function App() { + + useEffect(() => { + const handleUnload = async (event) => { + await api.post('/quit'); + // Custom logic when the user is quitting the app + // You can perform any necessary cleanup or trigger actions here + // This function will be called when the user leaves the app + }; + + // Add the event listener when the component mounts + window.addEventListener('beforeunload', handleUnload); + + // Remove the event listener when the component unmounts + return () => { + window.removeEventListener('beforeunload', handleUnload); + }; + }, []); + + return ( + <> + + }/> + }/> + }/> + }/> + + } /> + + + + ); +} + +export default App; + +// {/* +// */} + +// {/* Gestion des pages inexistantes */} + +// {/* ------- ROUTE FOR CHAT APP HERE --------- */} +// {/* }/> */} + + +// {/* +// {/* {redirectToUrl} */} +// }/> +// }/> +// }/> +// }/> + +// } /> + +// {/* +// */} + +// {/* Gestion des pages inexistantes */} + +// {/* ------- ROUTE FOR CHAT APP HERE --------- */} +// {/* }/> */} + + + +// */} \ No newline at end of file diff --git a/react/src/components/Chat.js b/containers/react/src/components/Chat.js similarity index 100% rename from react/src/components/Chat.js rename to containers/react/src/components/Chat.js diff --git a/react/src/components/Field.js b/containers/react/src/components/Field.js similarity index 100% rename from react/src/components/Field.js rename to containers/react/src/components/Field.js diff --git a/react/src/components/Footer.js b/containers/react/src/components/Footer.js similarity index 100% rename from react/src/components/Footer.js rename to containers/react/src/components/Footer.js diff --git a/react/src/components/Head.js b/containers/react/src/components/Head.js similarity index 100% rename from react/src/components/Head.js rename to containers/react/src/components/Head.js diff --git a/react/src/components/Header.js b/containers/react/src/components/Header.js similarity index 100% rename from react/src/components/Header.js rename to containers/react/src/components/Header.js diff --git a/containers/react/src/components/Home.js b/containers/react/src/components/Home.js new file mode 100644 index 00000000..f882ba31 --- /dev/null +++ b/containers/react/src/components/Home.js @@ -0,0 +1,49 @@ +import '../styles/old.css'; +import '../styles/field.css'; + +import { useLocation } from 'react-router-dom'; +import api from '../script/axiosApi'; + +function Home() +{ + const login2 = () => { + console.log('Hello from myFunction'); + api.get('/profile').then((response) => { + const data = response; + const myJSON = JSON.stringify(response.data); + console.log(`data response= ${myJSON}`) + }); + } + + const location = useLocation(); + + const handleButtonClick = () => { + const token = localStorage.getItem('token') + console.log(`token type= ${typeof token}`); + if (token !== null) + { + console.log(`already token= ${localStorage.getItem('token')}`) + return ; + } + // else + let path = "https://api.intra.42.fr/oauth/authorize?client_id=u-s4t2ud-6d29dfa49ba7146577ffd8bf595ae8d9e5aaa3e0a9615df18777171ebf836a41&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Fapi%2Fauth%2Flogin&response_type=code"; + window.location.replace(path); + }; + + return ( +
+ +
+ +
+
+ +
+
+ +
+
+ ); +} + +export default Home; \ No newline at end of file diff --git a/react/src/components/PlayButton.js b/containers/react/src/components/PlayButton.js similarity index 100% rename from react/src/components/PlayButton.js rename to containers/react/src/components/PlayButton.js diff --git a/react/src/components/SetAuthCookie.js b/containers/react/src/components/SetAuthCookie.js similarity index 100% rename from react/src/components/SetAuthCookie.js rename to containers/react/src/components/SetAuthCookie.js diff --git a/react/src/components/canvas.js b/containers/react/src/components/canvas.js similarity index 84% rename from react/src/components/canvas.js rename to containers/react/src/components/canvas.js index 9018d668..82226953 100644 --- a/react/src/components/canvas.js +++ b/containers/react/src/components/canvas.js @@ -1,6 +1,8 @@ // import io from 'socket.io-client'; -import { useEffect } from 'react'; +import api from '../script/axiosApi'; + +// import { useEffect } from 'react'; import io from 'socket.io-client'; // const socket = io('http://192.168.1.14:4000'); // const socket = io('http://86.209.110.20:4000'); @@ -22,6 +24,8 @@ export function drawCanvas() { //socket let myId = 0; let gameId = 0; + let opName; + let opRank; //general canvas const scale = window.devicePixelRatio; @@ -58,6 +62,7 @@ export function drawCanvas() { //score let myScore = 0; let hisScore = 0; + const maxScore = 5; let lastUpdateTime = performance.now(); @@ -79,8 +84,64 @@ export function drawCanvas() { socket.emit('pong:matchmaking', info); } - socket.on('pong:gameId', (data) => { + // socket.on('pong:gameId', (data) => { + // console.log("gameId received") + // gameId = data; + // // api.get('/profile'); + + // let myName; + + // api.get('/profile').then((data) => { + // // Faire quelque chose avec les données + // console.log(data); + // myName = data.data.username; + // console.log(`myname= ${myName}`); + // }).catch((error) => { + // console.log(error); + // // exit() ; + // return; + // }); + + // const info = { + // id: myId, + // name: myName, + // gameId: gameId, + // }; + // console.log("emit to name") + // socket.emit('pong:name', info); + // }); + + socket.on('pong:gameId', async (data) => { + console.log("gameId received"); gameId = data; + + try { + let response = await api.get('/profile'); + const myName = response.data.username; + response = await api.get('/rank'); + opRank = response.data + console.log(`rank= ${opRank}`); + console.log(`myname= ${myName}`); + + const info = { + id: myId, + name: myName, + gameId: gameId, + rank: opRank, + }; + + console.log("emit to name"); + socket.emit('pong:name', info); + } catch (error) { + console.log(error); + // Handle error here + return; + } + }); + + socket.on('pong:name', (data) => { + opName = data; + console.log(`opponent Name= ${opName}`) }); socket.on('connect', () => { @@ -240,23 +301,43 @@ function draw(timestamp) { if (gameId === 0 ) { - requestAnimationFrame(draw) - return; + requestAnimationFrame(draw); + return ; + } + if (myScore === maxScore || hisScore === maxScore) + { + const data = { + myScore: myScore, + opScore: hisScore, + opName: opName, + opRank: opRank, + }; + if (myScore === maxScore) + { + api.post('/win', data); + console.log("send win"); + } + else + { + api.post('/loss', data); + console.log("send loose"); + } + window.location.replace("http://localhost/pong"); + return ; } - const deltaTime = timestamp - lastUpdateTime; - lastUpdateTime = timestamp; + const deltaTime = timestamp - lastUpdateTime; + lastUpdateTime = timestamp; + ballX += vX * deltaTime * canvas.width; + ballY += vY * deltaTime * canvas.width; - ballX += vX * deltaTime * canvas.width; - ballY += vY * deltaTime * canvas.width; - - ctx.clearRect(0, 0, canvas.width, canvas.height); - drawPaddle(); - drawcenter(); - drawball(); - is_collision(); - is_out(); - requestAnimationFrame(draw); + ctx.clearRect(0, 0, canvas.width, canvas.height); + drawPaddle(); + drawcenter(); + drawball(); + is_collision(); + is_out(); + requestAnimationFrame(draw); } requestAnimationFrame(draw); diff --git a/containers/react/src/index.js b/containers/react/src/index.js new file mode 100644 index 00000000..73807913 --- /dev/null +++ b/containers/react/src/index.js @@ -0,0 +1,58 @@ +import React from 'react'; +import ReactDOM from 'react-dom/client'; + +import './styles/index.css'; +import App from './components/App'; +import Header from './components/Header'; +// import Home from './components/Home'; +// import Login42 from './components/Login42'; +import Head from './components/Head'; +// import Field from './components/Field'; +// import PlayButton from './components/PlayButton'; +import reportWebVitals from './reportWebVitals'; +// import SuccessToken from './script/tokenSuccess' +import { BrowserRouter, Route, Routes, Navigate} from 'react-router-dom' + +// let redirectToUrl; +// if (localStorage.getItem('token') !== null) //check condition +// { +// redirectToUrl = ; +// } + +const root = ReactDOM.createRoot(document.getElementById('root')); +root.render( + <> + +
+ + + + +); + +// If you want to start measuring performance in your app, pass a function +// to log results (for example: reportWebVitals(console.log)) +// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals +reportWebVitals(); + +{/* }/> */} + // + // {/* {redirectToUrl} */} + // }/> + // }/> + // }/> + // }/> + + // } /> + + // {/* + // */} + + // {/* Gestion des pages inexistantes */} + + // {/* ------- ROUTE FOR CHAT APP HERE --------- */} + // {/* }/> */} + + + + // \ No newline at end of file diff --git a/react/src/logo.svg b/containers/react/src/logo.svg similarity index 100% rename from react/src/logo.svg rename to containers/react/src/logo.svg diff --git a/react/src/reportWebVitals.js b/containers/react/src/reportWebVitals.js similarity index 100% rename from react/src/reportWebVitals.js rename to containers/react/src/reportWebVitals.js diff --git a/react/src/script/axiosApi.js b/containers/react/src/script/axiosApi.js similarity index 100% rename from react/src/script/axiosApi.js rename to containers/react/src/script/axiosApi.js diff --git a/react/src/script/tokenSuccess.js b/containers/react/src/script/tokenSuccess.js similarity index 100% rename from react/src/script/tokenSuccess.js rename to containers/react/src/script/tokenSuccess.js diff --git a/react/src/styles/App.css b/containers/react/src/styles/App.css similarity index 100% rename from react/src/styles/App.css rename to containers/react/src/styles/App.css diff --git a/react/src/styles/chat.css b/containers/react/src/styles/chat.css similarity index 100% rename from react/src/styles/chat.css rename to containers/react/src/styles/chat.css diff --git a/react/src/styles/field.css b/containers/react/src/styles/field.css similarity index 100% rename from react/src/styles/field.css rename to containers/react/src/styles/field.css diff --git a/react/src/styles/index.css b/containers/react/src/styles/index.css similarity index 100% rename from react/src/styles/index.css rename to containers/react/src/styles/index.css diff --git a/react/src/styles/old.css b/containers/react/src/styles/old.css similarity index 100% rename from react/src/styles/old.css rename to containers/react/src/styles/old.css diff --git a/docker-compose.yml b/docker-compose.yml index 1f56fa37..d7d8eb53 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,7 +12,7 @@ services: - 80:80 volumes: - ./conf/nginx.conf:/etc/nginx/conf.d/default.conf - - ./frontend:/var/www/html + # - ./containers/frontend:/var/www/html networks: - pongNetwork @@ -25,7 +25,7 @@ services: ports: - 8080:8080 volumes: - - ./react:/app + - ./containers/react:/app networks: - pongNetwork entrypoint: ["sh", "-c" , "npm install && npm run start:dev"] @@ -43,29 +43,9 @@ services: networks: - pongNetwork volumes: - - ./api:/app + - ./containers/api:/app entrypoint: ["sh", "-c" , "npm install && npm run start:dev"] - # backend: - # # image: node:latest - # build: - # context : ./ - # dockerfile: ./dockerfiles/Dockerfile.backEnd - # container_name: backend - # ports: - # - 3000:3000 - # env_file: .env - # depends_on: - # - postgresql - # networks: - # - pongNetwork - # # working_dir: /app - # # volumes: - # # - ./backend:/app - # # entrypoint: ["sh", "-c" , "npm install && ls && ls dist && npm run build && npm run start"] - # # entrypoint: ["/bin/sh", "-c" , "npm install && npm run start:dev "] - - postgresql: image: postgres:14.1-alpine restart: unless-stopped @@ -91,7 +71,7 @@ services: networks: - pongNetwork volumes: - - ./pong:/app + - ./containers/pong:/app entrypoint: ["sh", "-c" , "npm install && npm run start:dev"] # chat: diff --git a/dockerfiles/.dockerignore b/dockerfiles/.dockerignore deleted file mode 100644 index b9470778..00000000 --- a/dockerfiles/.dockerignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules/ -dist/ diff --git a/dockerfiles/Dockerfile.PostgreSQL b/dockerfiles/Dockerfile.PostgreSQL deleted file mode 100644 index c9b144d9..00000000 --- a/dockerfiles/Dockerfile.PostgreSQL +++ /dev/null @@ -1,19 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Dockerfile.PostgreSQL :+: :+: :+: # -# +:+ +:+ +:+ # -# By: apommier +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2023/03/19 09:28:30 by apommier #+# #+# # -# Updated: 2023/04/12 23:40:10 by apommier ### ########.fr # -# # -# **************************************************************************** # - -FROM postgres:alpine -COPY conf/init.sql /docker-entrypoint-initdb.d/ -# COPY database/init.sh /docker-entrypoint-initdb.d//init-user-db.sh - -# RUN sed -i 's/bind-address/bind-address = 0.0.0.0 #/i' /etc/mysql/mariadb.conf.d/50-server.cnf -EXPOSE 5432 -CMD ["postgres"] \ No newline at end of file diff --git a/dockerfiles/Dockerfile.backEnd b/dockerfiles/Dockerfile.backEnd deleted file mode 100644 index bdd82978..00000000 --- a/dockerfiles/Dockerfile.backEnd +++ /dev/null @@ -1,28 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Dockerfile.backEnd :+: :+: :+: # -# +:+ +:+ +:+ # -# By: apommier +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2023/03/19 09:28:33 by apommier #+# #+# # -# Updated: 2023/04/17 05:22:39 by apommier ### ########.fr # -# # -# **************************************************************************** # - -FROM node:latest - -WORKDIR /app -COPY /backend/package*.json ./ -RUN npm install - -COPY ./.env . -COPY /backend/tsconfig*.json ./ -COPY /backend/src ./src - -EXPOSE 3000 - -RUN npm run build -RUN rm -rf ./src - -CMD ["npm", "run", "start"] \ No newline at end of file diff --git a/dockerfiles/Dockerfile.nginx b/dockerfiles/Dockerfile.nginx deleted file mode 100644 index 5df31a2c..00000000 --- a/dockerfiles/Dockerfile.nginx +++ /dev/null @@ -1,24 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Dockerfile.nginx :+: :+: :+: # -# +:+ +:+ +:+ # -# By: apommier +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2023/03/19 09:28:32 by apommier #+# #+# # -# Updated: 2023/04/07 13:38:20 by apommier ### ########.fr # -# # -# **************************************************************************** # - -FROM nginx:latest - -RUN apt-get update -y -RUN apt-get install -y postgresql-client -# RUN apt update -y -# RUN apt-get install -y openssl - -# RUN mkdir -p /etc/ssl/private -# RUN mkdir -p /etc/ssl/certs -# RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt -subj "/C=FR/ST=17/L=StJeanDeLiversay/O=42/CN=apommier" - -COPY conf/nginx.conf /etc/nginx/conf.d/default.conf \ No newline at end of file diff --git a/dockerfiles/Dockerfile.react b/dockerfiles/Dockerfile.react deleted file mode 100644 index 119d8110..00000000 --- a/dockerfiles/Dockerfile.react +++ /dev/null @@ -1,20 +0,0 @@ -# Utilisez l'image de base de Node.js version 14 -FROM node:latest - -# Définissez le répertoire de travail à /app -WORKDIR /app - -# Copiez le fichier package.json et package-lock.json à l'intérieur de l'image -COPY /frontend/pong/my-app/package*.json ./ - -# Installez les dépendances de l'application -RUN npm install - -# Copiez le reste des fichiers de l'application dans l'image -COPY /frontend/pong/my-app/ . - -# Exposez le port 3000 pour accéder à l'application dans le navigateur -EXPOSE 8080 - -# Démarrez l'application avec la commande "npm start" -CMD ["npm", "run", "start"] \ No newline at end of file diff --git a/react/src/components/App.js b/react/src/components/App.js deleted file mode 100644 index 34684811..00000000 --- a/react/src/components/App.js +++ /dev/null @@ -1,25 +0,0 @@ -import logo from '../logo.svg'; -import '../styles/App.css'; - -function App() { - return ( -
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
-
- ); -} - -export default App; diff --git a/react/src/components/Home.js b/react/src/components/Home.js deleted file mode 100644 index dba8e770..00000000 --- a/react/src/components/Home.js +++ /dev/null @@ -1,107 +0,0 @@ -import '../styles/old.css'; -import '../styles/field.css'; - -// import { useNavigate } from "react-router-dom"; -import { useLocation } from 'react-router-dom'; -import api from '../script/axiosApi'; -// import { login } from '../script/login.js' -// import { login2 } from '../script/login.js' -// import axios from 'axios'; - -function Home() -{ - // const login = async () => { - const login2 = () => { - console.log('Hello from myFunction'); - api.get('/profile').then((response) => { - const data = response; - // const parsedData = JSON.parse(response.data); - // console.log(`response= ${parsedData}`) - - const myJSON = JSON.stringify(response.data); - console.log(`data response= ${myJSON}`) - // console.log(`response= ${data}`) - - }); - // alert("Le bouton a été cliqué !"); - // var formulaire = document.getElementById("loginForm"); - // formulaire.submit(); - } - - const location = useLocation(); - - const handleButtonClick = () => { - let path = "https://api.intra.42.fr/oauth/authorize?client_id=u-s4t2ud-6d29dfa49ba7146577ffd8bf595ae8d9e5aaa3e0a9615df18777171ebf836a41&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Fapi%2Fauth%2Flogin&response_type=code"; - window.location.replace(path); - }; - - return ( -
- - {/*
*/} -
- -
-
- -
-
- -
-
- // href="https://api.intra.42.fr/oauth/authorize?client_id=u-s4t2ud-6d29dfa49ba7146577ffd8bf595ae8d9e5aaa3e0a9615df18777171ebf836a41&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Fapi%2Fauth%2Flogin&response_type=code"> - // console.log('simple login button clicked'); - - // const url = 'https://api.intra.42.fr/oauth/authorize?client_id=u-s4t2ud-6d29dfa49ba7146577ffd8bf595ae8d9e5aaa3e0a9615df18777171ebf836a41&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Fapi%2Fauth%2Flogin&response_type=code'; - // // const response = await fetch(url); - // // console.log(`${response}`); - // try - // { - // const response = await axios.post(url); - // const token = response.data.token; // extract token from response - // localStorage.setItem('token', token); // store token in localStorage - // console.log(`token = ${token}`); - // } - // catch (error) - // { - // console.error(error); - // } - // }; - - // const login = async() => { - // console.log('simple login button clicked'); - - // const url = 'http://localhost/api/login'; - // const response = await fetch(url); - // const data = await response.json(); // extract token from response - // localStorage.setItem('token', data.token); // store token in localStorage - // console.log(`token= ${data.token}`) - // } - - // localStorage.setItem('token', '${response}'); - // alert("Le bouton a été cliqué !"); - // var formulaire = document.getElementById("loginForm"); - // formulaire.submit(); - - - //
- // - //
- // className="center pong">PONG - - //
- //
- //

Login Here

- // - // - // {/* */} - //
- //
- // - //
- // {/* */} - //
- ); -} - -export default Home; \ No newline at end of file diff --git a/react/src/index.js b/react/src/index.js deleted file mode 100644 index 12b8f130..00000000 --- a/react/src/index.js +++ /dev/null @@ -1,45 +0,0 @@ -import React from 'react'; -import ReactDOM from 'react-dom/client'; - -import './styles/index.css'; -import App from './components/App'; -import Header from './components/Header'; -import Home from './components/Home'; -// import Login42 from './components/Login42'; -import Head from './components/Head'; -import Field from './components/Field'; -import PlayButton from './components/PlayButton'; -import reportWebVitals from './reportWebVitals'; -import SuccessToken from './script/tokenSuccess' -import { BrowserRouter, Route, Routes} from 'react-router-dom' - -const root = ReactDOM.createRoot(document.getElementById('root')); -root.render( - <> - -
- - - }/> - }/> - }/> - }/> - - - - {/* ------- ROUTE FOR CHAT APP HERE --------- */} - {/* }/> */} - - - - - - -); - -// If you want to start measuring performance in your app, pass a function -// to log results (for example: reportWebVitals(console.log)) -// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals -reportWebVitals(); - -{/* }/> */} \ No newline at end of file