Invitations changes.

Now require confirmation from both side to start game.
This commit is contained in:
LuckyLaszlo
2023-01-15 19:02:16 +01:00
parent a2b2168884
commit e3407b8a9d
4 changed files with 34 additions and 49 deletions

View File

@@ -15,8 +15,6 @@ export class TokenGame {
isGameIsWithInvitation : boolean
@Column({default: 0, nullable: true})
numberOfRegisteredUser : number
@Column({default : false})
isSecondUserAcceptedRequest : boolean
@Column()
token : string
}

View File

@@ -72,10 +72,11 @@ export class GameService {
return encryptedTextToReturn
}
async deleteToken(user : User){
async deletePublicTokens(user : User){
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.orWhere('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : user.username})
.where('tokengame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation : false})
.getMany();
if (tokenGame)
return this.tokenGameRepository.remove(tokenGame);
@@ -86,7 +87,7 @@ export class GameService {
console.log(user.status);
if (user.status === STATUS.IN_POOL || user.status === STATUS.IN_GAME)
{
await this.deleteToken(user);
await this.deletePublicTokens(user);
if (user.status === STATUS.IN_POOL) {
user.status = STATUS.CONNECTED;
}
@@ -106,7 +107,6 @@ export class GameService {
const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_'
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
const tok = this.tokenGameRepository.create(grantTicketDto);
tok.isSecondUserAcceptedRequest = false;
tok.numberOfRegisteredUser = 0;
tok.token = encryptedTextToReturn;
this.tokenGameRepository.save(tok);
@@ -130,34 +130,30 @@ export class GameService {
async validateToken(validateTicketDto : ValidateTicketDto) {
if (validateTicketDto.isGameIsWithInvitation === true)
{
console.log("validateToken() PRIVATE");
const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
.andWhere('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : validateTicketDto.playerTwoUsername})
.andWhere('tokengame.gameOptions = :gameOption', {gameOption : validateTicketDto.gameOptions})
.andWhere('tokengame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: true})
.andWhere('tokengame.isSecondUserAcceptedRequest = :choice', {choice : true})
.andWhere('tokengame.token = :token', {token : validateTicketDto.token})
.getOne();
if (tokenGame)
{
console.log("playerOneUsername: " + tokenGame.playerOneUsername + "| playerTwoUsername: " + tokenGame.playerTwoUsername);
tokenGame.numberOfRegisteredUser++;
this.tokenGameRepository.save(tokenGame);
if (tokenGame.numberOfRegisteredUser === 2)
{
this.tokenGameRepository.remove(tokenGame)
const userOne : User = await this.userRepository.createQueryBuilder('user')
.where("user.username = :username", {username : tokenGame.playerOneUsername})
.getOne();
const userTwo : User = await this.userRepository.createQueryBuilder('user')
.where("user.username = :username", {username : tokenGame.playerTwoUsername})
.getOne();
this.deleteToken(userOne)
this.deleteToken(userTwo)
}
return true;
}
}
else if (validateTicketDto.isGameIsWithInvitation === false)
{
console.log("validateToken() PUBLIC");
const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
.andWhere('tokengame.gameOptions = :gameOption', {gameOption : validateTicketDto.gameOptions})
@@ -168,10 +164,6 @@ export class GameService {
{
this.tokenGameRepository.remove(tokenGame)
console.log("USERNAME : " + tokenGame.playerOneUsername)
const user : User = await this.userRepository.createQueryBuilder('user')
.where("user.username = :username", {username : tokenGame.playerOneUsername})
.getOne();
this.deleteToken(user)
return true;
}
}
@@ -181,8 +173,8 @@ export class GameService {
async findInvitations(user : User, @Res() res : Response) {
const game = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.orWhere('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : user.username})
.andWhere('tokengame.isGameIsWithInvitation = :invit', {invit : true})
.andWhere('tokengame.isSecondUserAcceptedRequest = :choice', {choice : false})
.getMany();
if (!game)
return res.status(HttpStatus.NOT_FOUND).send({message : "No invitation found"});
@@ -200,14 +192,9 @@ export class GameService {
async declineInvitation(user : User, token : string, @Res() res : Response)
{
/* Luke: le check de user.status n'est pas fonctionnel avec l'implémentation des invitations dans le front.
Ça me semble dispensable, je désactive donc pour le moment plutôt que de refaire l'implémentation front. */
// if (user.status !== STATUS.CONNECTED)
// return res.status(HttpStatus.FORBIDDEN).json({message : "You must not be in game to decline an invitation"});
console.log("On décline l'invitation")
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.andWhere('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.andWhere('tokengame.token = :token', {token : token})
.where('tokengame.token = :token', {token : token})
.getOne();
if (tokenGame)
{
@@ -238,17 +225,11 @@ export class GameService {
async acceptInvitation(user : User, token : string, @Res() res : Response)
{
/* Luke: le check de user.status n'est pas fonctionnel avec l'implémentation des invitations dans le front.
Ça me semble dispensable, je désactive donc pour le moment plutôt que de refaire l'implémentation front. */
// if (user.status !== STATUS.CONNECTED)
// return res.status(HttpStatus.FORBIDDEN).send("")
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
.andWhere('tokenGame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.andWhere('tokenGame.token = :token', {token : token})
.where('tokenGame.token = :token', {token : token})
.getOne();
if (tokenGame)
{
tokenGame.isSecondUserAcceptedRequest = true;
this.tokenGameRepository.save(tokenGame)
return res.status(HttpStatus.OK).json({message : "Invitation accepted."});
}