qZMOSJKQMLSjkJKS

This commit is contained in:
batche
2022-12-22 16:42:55 +01:00
parent 49e09a77a6
commit 1e1a32bb78
4 changed files with 20 additions and 34 deletions

View File

@@ -18,7 +18,7 @@ import { CreateUsersDto } from "src/users/dto/create-users.dto";
async validate(accessToken: string, refreshToken: string, profile: Profile, callbackURL: string) {
console.log("Validate inside strategy.ts");
console.log(profile.id, profile.username, profile.phoneNumbers[0].value, profile.emails[0].value, profile.photos[0].value);
const userDTO: CreateUsersDto = { fortyTwoId: profile.id, username: profile.username, email: profile.emails[0].value, image_url: 'default.png', isEnabledTwoFactorAuth: false , status: "connected" };
const userDTO: CreateUsersDto = { fortyTwoId: profile.id, username: profile.username, email: profile.emails[0].value, image_url: 'default.png', isEnabledTwoFactorAuth: false , status: "Connected" };
const user = await this.authenticationService.validateUser(userDTO);
if (!user)
throw new UnauthorizedException();

View File

@@ -58,10 +58,9 @@ export class GameService {
async generateToken(user : User, grantTicketDto : GrantTicketDto)
{
// if (user.status === "In Game")
// return new HttpException("You can't play two games", HttpStatus.FORBIDDEN);
// else
if (grantTicketDto.isGameIsWithInvitation === true)
if (user.status === "In Game" || user.status === "In Pool")
return new HttpException("You can't play two games", HttpStatus.FORBIDDEN);
else if (grantTicketDto.isGameIsWithInvitation === true)
{
const secondUser : Partial<User> = await this.userService.findOneByUsername(user.id.toString(), grantTicketDto.playerTwoUsername)
if (!secondUser || secondUser.username === user.username)
@@ -73,6 +72,7 @@ export class GameService {
tok.numberOfRegisteredUser = 0;
tok.token = encryptedTextToReturn;
this.tokenGameRepository.save(tok);
this.userService.updateStatus(user.id, "In Pool")
return { token : encryptedTextToReturn };
}
else if (grantTicketDto.isGameIsWithInvitation === false) {
@@ -82,14 +82,13 @@ export class GameService {
tok.numberOfRegisteredUser = 0;
tok.token = encryptedTextToReturn;
this.tokenGameRepository.save(tok);
this.userService.updateStatus(user.id, "In Pool")
return { token : encryptedTextToReturn };
}
return new HttpException("Something went wrong !", HttpStatus.INTERNAL_SERVER_ERROR)
}
async validateToken(validateTicketDto : ValidateTicketDto) {
console.log("On valide le token pour : ")
console.log(validateTicketDto)
if (validateTicketDto.isGameIsWithInvitation === true)
{
const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
@@ -109,13 +108,11 @@ export class GameService {
const userOne : User = await this.userRepository.createQueryBuilder('user')
.where("user.username = :username", {username : tokenGame.playerOneUsername})
.getOne();
userOne.status = "In Game";
this.userRepository.save(userOne);
this.userService.updateStatus(userOne.id, "In Game")
const userTwo : User = await this.userRepository.createQueryBuilder('user')
.where("user.username = :username", {username : tokenGame.playerTwoUsername})
.getOne();
userTwo.status = "In Game";
this.userRepository.save(userTwo);
this.userService.updateStatus(userTwo.id, "In Game")
}
return true;
}
@@ -135,8 +132,7 @@ export class GameService {
const user : User = await this.userRepository.createQueryBuilder('user')
.where("user.username = :username", {username : tokenGame.playerOneUsername})
.getOne();
user.status = "In Game";
this.userRepository.save(user);
this.userService.updateStatus(user.id, "In Game")
return true;
}
}
@@ -160,13 +156,12 @@ export class GameService {
token : gameToken.token,
});
}
console.log("Il y a des invitations !")
return partialGame;
}
async declineInvitation(user : User, token : string)
{
if (user.status === "In Game")
if (user.status !== "Connected")
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')
@@ -188,10 +183,10 @@ export class GameService {
{
const playerOne = await this.userRepository.findOneBy({username : tokenGame.playerOneUsername})
const playerTwo = await this.userRepository.findOneBy({username : tokenGame.playerTwoUsername})
playerOne.status = "Connected"
playerTwo.status = "Connected"
this.userRepository.save(playerOne)
this.userRepository.save(playerTwo)
if (playerOne.status !== "Disconnected")
this.userService.updateStatus(playerOne.id, "Connected")
if (playerTwo.status !== "Disconnected")
this.userService.updateStatus(playerTwo.id, "Connected")
return this.tokenGameRepository.remove(tokenGame);
}
return new HttpException("Token not found !", HttpStatus.NOT_FOUND)
@@ -199,6 +194,8 @@ export class GameService {
async acceptInvitation(user : User, token : string)
{
if (user.status !== "Connected")
return new HttpException("You must finish your game before accept.", HttpStatus.FORBIDDEN)
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
.andWhere('tokenGame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.andWhere('tokenGame.token = :token', {token : token})
@@ -213,7 +210,7 @@ export class GameService {
}
async requestIfAnotherUserHasRespondToquestForGame(user : User, token : string) {
if (user.status === "In Game")
if (user.status !== "Connected")
return new HttpException("You can't do that.", HttpStatus.BAD_REQUEST)
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
.where('tokenGame.token = :token', {token : token})
@@ -241,10 +238,6 @@ export class GameService {
console.log("200 retourné pour la création de partie")
const playerOne = await this.userRepository.findOneBy({username : game.playerOneUsername})
const playerTwo = await this.userRepository.findOneBy({username : game.playerTwoUsername})
playerOne.status = "In Game"
playerTwo.status = "In Game"
this.userRepository.save(playerOne)
this.userRepository.save(playerTwo)
return HttpStatus.OK
}
@@ -276,10 +269,8 @@ export class GameService {
this.userService.incrementVictories(playerOne.id)
this.userService.incrementDefeats(playerTwo.id)
}
playerOne.status = "Connected"
playerTwo.status = "Connected"
this.userRepository.save(playerOne)
this.userRepository.save(playerTwo)
this.userService.updateStatus(playerOne.id, "Connected")
this.userService.updateStatus(playerTwo.id, "Connected")
return HttpStatus.OK
}
}

View File

@@ -28,7 +28,7 @@ export class User {
@Column({ nullable: true })
phone: string;
@Column({ default: 'disconnected' })
@Column({ default: 'Disconnected' })
status: string;
// @Column()

View File

@@ -49,11 +49,9 @@ export class UsersController {
@Get()
findOne(@Query('username') username: string, @Req() req) {
if (username === undefined) {
console.log("Backend Getting current user");
return this.usersService.findOne(req.user.id);
} else {
const user : User = req.user;
console.log('we have a query: ' + username)
return this.usersService.findOneByUsername(user.id.toString(),username);
}
}
@@ -64,7 +62,6 @@ export class UsersController {
@Get('search')
findOneByUsername(@Query('username') username: string, @Req() req) {
const user : User = req.user;
console.log('searching for user' + user.username);
return this.usersService.findOneByUsername(user.id.toString(),username);
}
@@ -81,11 +78,9 @@ export class UsersController {
@UseGuards(TwoFactorGuard)
@Patch()
async update(@Req() req, @Body(new ValidationPipe()) usersUpdateDto: UpdateUsersDto, @Res() response : Response) {
console.log("DANS PATCH USERS");
const user = await this.usersService.update(req.user.id, usersUpdateDto);
if (user.isEnabledTwoFactorAuth === false && user.isTwoFactorAuthenticated === true)
this.usersService.setIsTwoFactorAuthenticatedWhenLogout(user.id);
console.log ("Enable 2FA " + user.isEnabledTwoFactorAuth + " Is authenticated " + user.isTwoFactorAuthenticated);
if (user.isEnabledTwoFactorAuth === true && user.isTwoFactorAuthenticated === false)
{
response.status(201).send('2FA redirect')