merge done

This commit is contained in:
Alexandre POMMIER 2023-06-26 08:00:30 +02:00
parent a6dfe6940f
commit baa9b90e5d

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/17 01:00:00 by apommier #+# #+# */ /* Created: 2023/06/17 01:00:00 by apommier #+# #+# */
/* Updated: 2023/06/26 07:50:53 by apommier ### ########.fr */ /* Updated: 2023/06/26 08:00:13 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,27 +17,12 @@ import { AuthService } from './auth/auth.service';
import { loginClass } from './auth/login42' import { loginClass } from './auth/login42'
import { ChatService } from './chat/chat.service'; import { ChatService } from './chat/chat.service';
import { UsersService } from './users/users.service'; import { UsersService } from './users/users.service';
import { MatchLog } from './model/user.entity' import { MatchLog } from './model/user.entity'
import { generate } from 'rxjs';
// import { generateQRcode } from './users/2fa';
import { generateOTP } from './users/2fa'; import { generateOTP } from './users/2fa';
import { VerifyOTP } from './users/2fa'; import { VerifyOTP } from './users/2fa';
import { ValidateOTP } from './users/2fa'; import { ValidateOTP } from './users/2fa';
import { privateDecrypt } from 'crypto';
import { formatWithOptions } from 'util';
//2fa
// import { initStorage, getUser, setUser } from './storage';
// import { AuthGuard } from '@nestjs/passport';
// import { Login42 } from './auth/login42'
// import { loginClass } from './auth/test'
@Controller('/api') @Controller('/api')
export class AppController { export class AppController {
@ -49,49 +34,39 @@ export class AppController {
kFactor = 36; kFactor = 36;
scaleFactor = 400; scaleFactor = 400;
//========================================================================================================
//======================================================================================================== //========================================================================================================
//======================================================================================================== // User
// User //========================================================================================================
//======================================================================================================== //========================================================================================================
//========================================================================================================
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('/profile') @Get('/profile')
async getProfile(@Request() req) { async getProfile(@Request() req) {
// const myJSON = JSON.stringify(req.user);
// console.log(`req user api= ${req.user}`)
// console.log(`json user api= ${myJSON}`)
// return req.user;
return await this.userService.findOne(req.user.username); return await this.userService.findOne(req.user.username);
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('/user') @Post('/user')
async getUser(@Body() data: any) { async getUser(@Body() data: any) {
console.log(`usernamewwww= ${data.username}`)
return await this.userService.findOne(data.username); return await this.userService.findOne(data.username);
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('/users') @Get('/users')
async getUsers(@Body() data: any) { async getUsers(@Body() data: any) {
console.log(`usernamewwww= ${data.username}`)
return await this.userService.findAll(); return await this.userService.findAll();
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('/friends') @Get('/friends')
async getFriends(@Request() req) { async getFriends(@Request() req) {
// return await this.userService.getFriends(req.user.username);
return await this.userService.getFriends(req.user.username); return await this.userService.getFriends(req.user.username);
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('/friend')//need to do it 2 time when user accept one for each @Post('/friend')
async newFriend(@Request() req, @Body() data: any) { async newFriend(@Request() req, @Body() data: any) {
// return await this.userService.getFriends(req.user.username);
console.log(`user= ${req.user.username}`)
const user = await this.userService.findOne(req.user.username) const user = await this.userService.findOne(req.user.username)
if (!user) if (!user)
return (0); return (0);
@ -100,13 +75,6 @@ export class AppController {
this.userService.save(user); this.userService.save(user);
return (1); return (1);
} }
//create personnal conv for user
//await this.userService.addFriend(user, data.username);
// const amIhere = data.members.includes(req.user.username);
// if (!amIhere)
const conv = { const conv = {
id: null, id: null,
name: req.user.username + ", " + data.username, name: req.user.username + ", " + data.username,
@ -131,8 +99,6 @@ export class AppController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('/block') @Post('/block')
async newBlocked(@Request() req, @Body() data: any) { async newBlocked(@Request() req, @Body() data: any) {
// return await this.userService.getFriends(req.user.username);
console.log(`user= ${req.user.username}`)
if (data.username === req.user.username) if (data.username === req.user.username)
return (0); return (0);
const user = await this.userService.findOne(req.user.username) const user = await this.userService.findOne(req.user.username)
@ -142,30 +108,23 @@ export class AppController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('/invite') @Post('/invite')
async newInvite(@Request() req, @Body() data: any) { async newInvite(@Request() req, @Body() data: any) {
console.log(`user= ${req.user.username}`)
if (data.username === req.user.username) if (data.username === req.user.username)
return (0); return (0);
const user = await this.userService.findOne(data.username) const user = await this.userService.findOne(data.username)
if (!user) if (!user)
return (0); return (0);
console.log(`CALL IT!!!!!`)
return await this.userService.newInvite(user, req.user.username); return await this.userService.newInvite(user, req.user.username);
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('/inviteRequest') @Get('/inviteRequest')
async getInvite(@Request() req) { async getInvite(@Request() req) {
// return await this.userService.getFriends(req.user.username);
console.log(`useawdawd\n\n\nr= ${req.user.username}`)
// const user = await this.userService.findOne(req.user.username)
return await this.userService.getInvite(req.user.username); return await this.userService.getInvite(req.user.username);
} }
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('/refuseInvite') @Post('/refuseInvite')
async refuseInvite(@Request() req, @Body() data: any) { async refuseInvite(@Request() req, @Body() data: any) {
// return await this.userService.getFriends(req.user.username);
// console.log(`useawdawd\n\n\nr= ${req.user.username}`)
const user = await this.userService.findOne(req.user.username) const user = await this.userService.findOne(req.user.username)
return await this.userService.refuseInvite(user, data.username); return await this.userService.refuseInvite(user, data.username);
} }
@ -182,7 +141,6 @@ export class AppController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('/nickname') @Post('/nickname')
async setNickname(@Request() req, @Body() data: any) { async setNickname(@Request() req, @Body() data: any) {
console.log(`user= ${req.user.username}`)
const taken = await this.userService.findNickname(data.nickname) const taken = await this.userService.findNickname(data.nickname)
if (taken) if (taken)
return (0); return (0);
@ -209,6 +167,16 @@ export class AppController {
return await this.userService.getPic(data.username) return await this.userService.getPic(data.username)
} }
@UseGuards(JwtAuthGuard)
@Post('/addSession')
async addSession(@Request() req) {
const user = await this.userService.findOne(req.user.username);
user.sessionNumber += 1;
if (user.status !== 2)
user.status = 1;
await this.userService.save(user);
}
//======================================================================================================== //========================================================================================================
//======================================================================================================== //========================================================================================================
// Pong // Pong
@ -244,7 +212,6 @@ export class AppController {
newMatch.opScore = data.opScore; newMatch.opScore = data.opScore;
newMatch.opponent = data.opName; newMatch.opponent = data.opName;
newMatch.parent = user; newMatch.parent = user;
console.log(`newMatch Loose= ${newMatch}`);
await this.userService.saveChild(user, newMatch); await this.userService.saveChild(user, newMatch);
} }
@ -303,7 +270,7 @@ export class AppController {
user.sessionNumber--; user.sessionNumber--;
if (!user.sessionNumber) if (!user.sessionNumber)
user.status = 0; user.status = 0;
console.log("quit sessionNUmber :", user.sessionNumber); await this.userService.save(user);
} }
//======================================================================================================== //========================================================================================================