Merge de l'intégration du jeu avec master + corrections et améliorations des messages d'erreurs, de retour en page de reset de variable...

This commit is contained in:
batche
2022-12-22 21:03:46 +01:00
parent 991bbce4af
commit 4c6f832252
4 changed files with 110 additions and 11 deletions

View File

@@ -8,6 +8,12 @@ const MIME_TYPES = {
'image/png': 'png'
};
export enum STATUS {
CONNECTED = 'Connected',
DISCONNECTED = 'Disconnected',
IN_GAME = 'In Game',
IN_POOL = 'In Pool',
}
export const storageForAvatar = {
storage: diskStorage({

View File

@@ -12,6 +12,7 @@ import { UsersService } from 'src/users/users.service';
import { CreateGameDto } from './dto/createGame.dto';
import { UpdateGameDto } from './dto/updateGame.dto';
import { FriendshipService } from 'src/friendship/friendship.service';
import { STATUS } from 'src/common/constants/constants';
@Injectable()
export class GameService {
@@ -55,11 +56,25 @@ export class GameService {
return encryptedTextToReturn
}
async deleteToken(user : User){
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.orWhere('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : user.username})
.getMany();
if (tokenGame)
return this.tokenGameRepository.remove(tokenGame);
}
async generateToken(user : User, grantTicketDto : GrantTicketDto)
{
console.log(user.status);
if (user.status === "In Game" || user.status === "In Pool")
return new HttpException("You can't play two games", HttpStatus.FORBIDDEN);
if (user.status === STATUS.IN_POOL || user.status === STATUS.IN_GAME)
{
await this.deleteToken(user);
user.status = STATUS.CONNECTED;
this.userRepository.save(user);
}
if (grantTicketDto.isGameIsWithInvitation === true)
{
const secondUser : Partial<User> = await this.userService.findOneByUsername(user.id.toString(), grantTicketDto.playerTwoUsername)
@@ -112,6 +127,8 @@ export class GameService {
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, "In Game")
}
return true;
@@ -133,6 +150,7 @@ export class GameService {
.where("user.username = :username", {username : tokenGame.playerOneUsername})
.getOne();
this.userService.updateStatus(user.id, "In Game")
this.deleteToken(user)
return true;
}
}
@@ -249,7 +267,7 @@ export class GameService {
game.isMatchIsFinished = true;
game.playerOneUsernameResult = updateGameDto.playerOneUsernameResult
game.playerTwoUsernameResult = updateGameDto.playerTwoUsernameResult
this.userRepository.save(game);
this.gameRepository.save(game);
const playerOne = await this.userRepository.findOneBy({username : game.playerOneUsername})
const playerTwo = await this.userRepository.findOneBy({username : game.playerTwoUsername})
if (!playerOne || !playerTwo)