Merge branch 'master' into hugo
This commit is contained in:
@@ -5,6 +5,7 @@ import { Response } from 'express';
|
||||
import { TwoFaDto } from './dto/2fa.dto';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { STATUS } from 'src/common/constants/constants';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthenticationController {
|
||||
@@ -36,6 +37,7 @@ export class AuthenticationController {
|
||||
console.log('On redirige');
|
||||
const user : User = request.user
|
||||
if (user.isEnabledTwoFactorAuth === false || user.isTwoFactorAuthenticated === true){
|
||||
this.userService.updateStatus(user.id, STATUS.CONNECTED)
|
||||
console.log('ON VA VERS PROFILE');
|
||||
return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/profile');
|
||||
}
|
||||
@@ -51,7 +53,7 @@ export class AuthenticationController {
|
||||
@UseGuards(AuthenticateGuard)
|
||||
logout(@Req() request, @Res() response, @Next() next) {
|
||||
this.userService.setIsTwoFactorAuthenticatedWhenLogout(request.user.id);
|
||||
this.userService.updateStatus(request.user.id, 'disconnected');
|
||||
this.userService.updateStatus(request.user.id, STATUS.DISCONNECTED);
|
||||
request.logout(function(err) {
|
||||
if (err) { return next(err); }
|
||||
response.redirect('/');
|
||||
@@ -83,6 +85,7 @@ export class AuthenticationController {
|
||||
throw new UnauthorizedException('Wrong Code.');
|
||||
await this.userService.authenticateUserWith2FA(request.user.id);
|
||||
console.log('ON REDIRIGE');
|
||||
this.userService.updateStatus(user.id, STATUS.CONNECTED)
|
||||
return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/profile');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,4 +101,9 @@ export class GameController {
|
||||
{
|
||||
return this.gameService.destroySession(token);
|
||||
}
|
||||
|
||||
@Post('gameserver/resetuserstatus')
|
||||
async resetUserStatus(@Body('username') username){
|
||||
this.gameService.resetStatus(username);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,12 +92,17 @@ export class GameService {
|
||||
}
|
||||
this.userRepository.save(user);
|
||||
}
|
||||
// if (grantTicketDto.isGameIsWithInvitation === true && user.status !== STATUS.IN_GAME) // WIP: need to fix STATUS.IN_GAME
|
||||
if (grantTicketDto.isGameIsWithInvitation === true)
|
||||
{
|
||||
const secondUser : Partial<User> = await this.userService.findOne(grantTicketDto.playerTwoUsername)
|
||||
if (!secondUser || secondUser.username === user.username)
|
||||
return res.status(HttpStatus.NOT_FOUND).json({message : "User not found OR you want to play with yourself."});
|
||||
if (grantTicketDto.playerTwoUsername === user.username) {
|
||||
return res.status(HttpStatus.BAD_REQUEST).json({message : "You cant play against yourself."});
|
||||
}
|
||||
const secondUser : User = await this.userRepository.createQueryBuilder('user')
|
||||
.where("user.username = :username", {username : grantTicketDto.playerTwoUsername})
|
||||
.getOne();
|
||||
if (!secondUser) {
|
||||
return res.status(HttpStatus.NOT_FOUND).json({message : "Invited user not found"});
|
||||
}
|
||||
const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_'
|
||||
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
|
||||
const tok = this.tokenGameRepository.create(grantTicketDto);
|
||||
@@ -106,9 +111,9 @@ export class GameService {
|
||||
tok.token = encryptedTextToReturn;
|
||||
this.tokenGameRepository.save(tok);
|
||||
this.userService.updateStatus(user.id, STATUS.IN_POOL)
|
||||
this.userService.updateStatus(secondUser.id, STATUS.IN_POOL)
|
||||
return res.status(HttpStatus.OK).json({ token : encryptedTextToReturn });
|
||||
}
|
||||
// else if (grantTicketDto.isGameIsWithInvitation === false && user.status !== STATUS.IN_GAME) { // WIP: need to fix STATUS.IN_GAME
|
||||
else if (grantTicketDto.isGameIsWithInvitation === false) {
|
||||
const encryptedTextToReturn = await this.encryptToken(user.username + '_'
|
||||
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
|
||||
@@ -142,13 +147,11 @@ export class GameService {
|
||||
const userOne : User = await this.userRepository.createQueryBuilder('user')
|
||||
.where("user.username = :username", {username : tokenGame.playerOneUsername})
|
||||
.getOne();
|
||||
this.userService.updateStatus(userOne.id, STATUS.IN_GAME)
|
||||
const userTwo : User = await this.userRepository.createQueryBuilder('user')
|
||||
.where("user.username = :username", {username : tokenGame.playerTwoUsername})
|
||||
.getOne();
|
||||
this.deleteToken(userOne)
|
||||
this.deleteToken(userTwo)
|
||||
this.userService.updateStatus(userTwo.id, STATUS.IN_GAME)
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -168,7 +171,6 @@ export class GameService {
|
||||
const user : User = await this.userRepository.createQueryBuilder('user')
|
||||
.where("user.username = :username", {username : tokenGame.playerOneUsername})
|
||||
.getOne();
|
||||
this.userService.updateStatus(user.id, STATUS.IN_GAME)
|
||||
this.deleteToken(user)
|
||||
return true;
|
||||
}
|
||||
@@ -259,6 +261,14 @@ export class GameService {
|
||||
this.gameRepository.save(game);
|
||||
if (!game)
|
||||
return HttpStatus.INTERNAL_SERVER_ERROR
|
||||
const playerOne : User = await this.userRepository.createQueryBuilder('user')
|
||||
.where("user.username = :username", {username : creategameDto.playerOneUsername})
|
||||
.getOne();
|
||||
const playerTwo : User = await this.userRepository.createQueryBuilder('user')
|
||||
.where("user.username = :username", {username : creategameDto.playerTwoUsername})
|
||||
.getOne();
|
||||
this.userService.updateStatus(playerOne.id, STATUS.IN_GAME)
|
||||
this.userService.updateStatus(playerTwo.id, STATUS.IN_GAME)
|
||||
console.log("200 retourné pour la création de partie")
|
||||
return HttpStatus.OK
|
||||
}
|
||||
@@ -280,6 +290,8 @@ export class GameService {
|
||||
const playerTwo = await this.userRepository.findOneBy({username : game.playerTwoUsername})
|
||||
if (!playerOne || !playerTwo)
|
||||
return new HttpException("Internal Server Error. Impossible to update the database", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
this.userService.updateStatus(playerOne.id, STATUS.CONNECTED);
|
||||
this.userService.updateStatus(playerTwo.id, STATUS.CONNECTED);
|
||||
if (game.playerOneUsernameResult === game.playerTwoUsernameResult)
|
||||
{
|
||||
this.userService.incrementDraws(playerOne.id)
|
||||
@@ -296,9 +308,14 @@ export class GameService {
|
||||
this.userService.incrementVictories(playerOne.id)
|
||||
this.userService.incrementDefeats(playerTwo.id)
|
||||
}
|
||||
this.userService.updateStatus(playerOne.id, STATUS.CONNECTED)
|
||||
this.userService.updateStatus(playerTwo.id, STATUS.CONNECTED)
|
||||
return HttpStatus.OK
|
||||
}
|
||||
|
||||
async resetStatus(username : string){
|
||||
const user : User = await this.userRepository.findOneBy({username : username})
|
||||
if (!user)
|
||||
return HttpStatus.NOT_FOUND;
|
||||
this.userService.updateStatus(user.id, STATUS.CONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user