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

@@ -107,6 +107,7 @@ async function clientAnnounceListener(this: WebSocket, data: string)
console.log("catch /game/gameserver/validate: ", error); console.log("catch /game/gameserver/validate: ", error);
this.send(JSON.stringify( new ev.EventError("validate token error") )); this.send(JSON.stringify( new ev.EventError("validate token error") ));
clientTerminate(clientsMap.get(this.id)); clientTerminate(clientsMap.get(this.id));
return;
}); });
player.matchOptions = announce.matchOptions; player.matchOptions = announce.matchOptions;
@@ -350,6 +351,7 @@ async function playerReadyConfirmationListener(this: WebSocket, data: string)
client.gameSession = null; client.gameSession = null;
clientTerminate(client); clientTerminate(client);
}); });
return;
}); });
gameSession.playersMap.forEach( (client) => { gameSession.playersMap.forEach( (client) => {

View File

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

View File

@@ -72,10 +72,11 @@ export class GameService {
return encryptedTextToReturn return encryptedTextToReturn
} }
async deleteToken(user : User){ async deletePublicTokens(user : User){
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame') const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username}) .where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.orWhere('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : user.username}) .orWhere('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : user.username})
.where('tokengame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation : false})
.getMany(); .getMany();
if (tokenGame) if (tokenGame)
return this.tokenGameRepository.remove(tokenGame); return this.tokenGameRepository.remove(tokenGame);
@@ -86,7 +87,7 @@ export class GameService {
console.log(user.status); console.log(user.status);
if (user.status === STATUS.IN_POOL || user.status === STATUS.IN_GAME) 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) { if (user.status === STATUS.IN_POOL) {
user.status = STATUS.CONNECTED; user.status = STATUS.CONNECTED;
} }
@@ -106,7 +107,6 @@ export class GameService {
const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_' const encryptedTextToReturn = await this.encryptToken(user.username + '_' + secondUser.username + '_'
+ grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date()) + grantTicketDto.gameOptions + '_' + grantTicketDto.isGameIsWithInvitation + '_' + new Date())
const tok = this.tokenGameRepository.create(grantTicketDto); const tok = this.tokenGameRepository.create(grantTicketDto);
tok.isSecondUserAcceptedRequest = false;
tok.numberOfRegisteredUser = 0; tok.numberOfRegisteredUser = 0;
tok.token = encryptedTextToReturn; tok.token = encryptedTextToReturn;
this.tokenGameRepository.save(tok); this.tokenGameRepository.save(tok);
@@ -130,34 +130,30 @@ export class GameService {
async validateToken(validateTicketDto : ValidateTicketDto) { async validateToken(validateTicketDto : ValidateTicketDto) {
if (validateTicketDto.isGameIsWithInvitation === true) if (validateTicketDto.isGameIsWithInvitation === true)
{ {
console.log("validateToken() PRIVATE");
const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame') const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername}) .where('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
.andWhere('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : validateTicketDto.playerTwoUsername}) .andWhere('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : validateTicketDto.playerTwoUsername})
.andWhere('tokengame.gameOptions = :gameOption', {gameOption : validateTicketDto.gameOptions}) .andWhere('tokengame.gameOptions = :gameOption', {gameOption : validateTicketDto.gameOptions})
.andWhere('tokengame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: true}) .andWhere('tokengame.isGameIsWithInvitation = :isGameIsWithInvitation', {isGameIsWithInvitation: true})
.andWhere('tokengame.isSecondUserAcceptedRequest = :choice', {choice : true})
.andWhere('tokengame.token = :token', {token : validateTicketDto.token}) .andWhere('tokengame.token = :token', {token : validateTicketDto.token})
.getOne(); .getOne();
if (tokenGame) if (tokenGame)
{ {
console.log("playerOneUsername: " + tokenGame.playerOneUsername + "| playerTwoUsername: " + tokenGame.playerTwoUsername);
tokenGame.numberOfRegisteredUser++; tokenGame.numberOfRegisteredUser++;
this.tokenGameRepository.save(tokenGame);
if (tokenGame.numberOfRegisteredUser === 2) if (tokenGame.numberOfRegisteredUser === 2)
{ {
this.tokenGameRepository.remove(tokenGame) 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; return true;
} }
} }
else if (validateTicketDto.isGameIsWithInvitation === false) else if (validateTicketDto.isGameIsWithInvitation === false)
{ {
console.log("validateToken() PUBLIC");
const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame') const tokenGame : TokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername}) .where('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : validateTicketDto.playerOneUsername})
.andWhere('tokengame.gameOptions = :gameOption', {gameOption : validateTicketDto.gameOptions}) .andWhere('tokengame.gameOptions = :gameOption', {gameOption : validateTicketDto.gameOptions})
@@ -168,10 +164,6 @@ export class GameService {
{ {
this.tokenGameRepository.remove(tokenGame) this.tokenGameRepository.remove(tokenGame)
console.log("USERNAME : " + tokenGame.playerOneUsername) 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; return true;
} }
} }
@@ -181,8 +173,8 @@ export class GameService {
async findInvitations(user : User, @Res() res : Response) { async findInvitations(user : User, @Res() res : Response) {
const game = await this.tokenGameRepository.createQueryBuilder('tokengame') const game = await this.tokenGameRepository.createQueryBuilder('tokengame')
.where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username}) .where('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username})
.orWhere('tokengame.playerOneUsername = :playerOneUsername', {playerOneUsername : user.username})
.andWhere('tokengame.isGameIsWithInvitation = :invit', {invit : true}) .andWhere('tokengame.isGameIsWithInvitation = :invit', {invit : true})
.andWhere('tokengame.isSecondUserAcceptedRequest = :choice', {choice : false})
.getMany(); .getMany();
if (!game) if (!game)
return res.status(HttpStatus.NOT_FOUND).send({message : "No invitation found"}); 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) 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") console.log("On décline l'invitation")
const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame') const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokengame')
.andWhere('tokengame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username}) .where('tokengame.token = :token', {token : token})
.andWhere('tokengame.token = :token', {token : token})
.getOne(); .getOne();
if (tokenGame) if (tokenGame)
{ {
@@ -238,17 +225,11 @@ export class GameService {
async acceptInvitation(user : User, token : string, @Res() res : Response) 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') const tokenGame = await this.tokenGameRepository.createQueryBuilder('tokenGame')
.andWhere('tokenGame.playerTwoUsername = :playerTwoUsername', {playerTwoUsername : user.username}) .where('tokenGame.token = :token', {token : token})
.andWhere('tokenGame.token = :token', {token : token})
.getOne(); .getOne();
if (tokenGame) if (tokenGame)
{ {
tokenGame.isSecondUserAcceptedRequest = true;
this.tokenGameRepository.save(tokenGame) this.tokenGameRepository.save(tokenGame)
return res.status(HttpStatus.OK).json({message : "Invitation accepted."}); return res.status(HttpStatus.OK).json({message : "Invitation accepted."});
} }

View File

@@ -24,7 +24,6 @@
//boolean for html page //boolean for html page
let hiddenGame = true; let hiddenGame = true;
let optionsAreNotSet = true;
let showGameOptions = true; let showGameOptions = true;
let showInvitations = false; let showInvitations = false;
let showError = false; let showError = false;
@@ -63,7 +62,6 @@
function resetPage() { function resetPage() {
hiddenGame = true; hiddenGame = true;
optionsAreNotSet = true;
showGameOptions = true; showGameOptions = true;
showInvitations = false; showInvitations = false;
showError = false; showError = false;
@@ -74,7 +72,6 @@
const initGame = async() => const initGame = async() =>
{ {
optionsAreNotSet = false;
showWaitPage = true; showWaitPage = true;
const matchOptions = pong.computeMatchOptions(options); const matchOptions = pong.computeMatchOptions(options);
try { try {
@@ -91,13 +88,18 @@
console.log("status : " + response.status); console.log("status : " + response.status);
const responseBody = await response.json(); const responseBody = await response.json();
const token : string = responseBody.token; const token : string = responseBody.token;
showWaitPage = false;
if (response.ok && token) if (response.ok && token)
{ {
watchMatchStartInterval = setInterval(watchMatchStart, watchMatchStartIntervalRate); if (options.isSomeoneIsInvited) {
watchGameStateInterval = setInterval(watchGameState, watchGameStateIntervalRate); options.reset(user.username);
pong.init(matchOptions, options, gameAreaId, token); fetchInvitations();
hiddenGame = false; }
else {
watchMatchStartInterval = setInterval(watchMatchStart, watchMatchStartIntervalRate);
watchGameStateInterval = setInterval(watchGameState, watchGameStateIntervalRate);
pong.init(matchOptions, options, gameAreaId, token);
hiddenGame = false;
}
} }
else else
{ {
@@ -107,7 +109,6 @@
showError = true; showError = true;
options.reset(user.username); options.reset(user.username);
setTimeout(() => { setTimeout(() => {
optionsAreNotSet = true;
showError = false; showError = false;
errorMessage = ""; errorMessage = "";
}, 5000); }, 5000);
@@ -115,11 +116,11 @@
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
showWaitPage = false;
} }
const initGameForInvitedPlayer = async(invitation : any) => const initInvitationGame = async(invitation : any) =>
{ {
optionsAreNotSet = false;
showWaitPage = true; showWaitPage = true;
console.log("invitation : "); console.log("invitation : ");
console.log(invitation); console.log(invitation);
@@ -130,7 +131,9 @@
options.playerOneUsername = invitation.playerOneUsername; options.playerOneUsername = invitation.playerOneUsername;
options.playerTwoUsername = invitation.playerTwoUsername; options.playerTwoUsername = invitation.playerTwoUsername;
options.isSomeoneIsInvited = true; options.isSomeoneIsInvited = true;
options.isInvitedPerson = true; if (user.username === invitation.playerTwoUsername) {
options.isInvitedPerson = true;
}
pong.init(invitation.gameOptions, options, gameAreaId, invitation.token); pong.init(invitation.gameOptions, options, gameAreaId, invitation.token);
showWaitPage = false; showWaitPage = false;
hiddenGame = false; hiddenGame = false;
@@ -169,10 +172,11 @@
} }
const fetchInvitations = async() => { const fetchInvitations = async() => {
showGameOptions = false; console.log("fetchInvitations");
showInvitations = true;
invitations = await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/game/invitations`) invitations = await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/game/invitations`)
.then(x => x.json()); .then(x => x.json());
showGameOptions = false;
showInvitations = true;
} }
const rejectInvitation = async(invitation) => { const rejectInvitation = async(invitation) => {
@@ -199,7 +203,7 @@
}).catch(error => console.log(error)); }).catch(error => console.log(error));
if (res && res.ok) { if (res && res.ok) {
initGameForInvitedPlayer(invitation); initInvitationGame(invitation);
} }
} }
@@ -273,7 +277,7 @@
<!-- --> <!-- -->
{#if optionsAreNotSet} {#if hiddenGame}
{#if showGameOptions} {#if showGameOptions}
<div id="game_option"> <div id="game_option">
<div class="div_game"> <div class="div_game">
@@ -332,7 +336,7 @@
{#if invitations.length !== 0} {#if invitations.length !== 0}
{#each invitations as invitation} {#each invitations as invitation}
<div> <div>
{invitation.playerOneUsername} has invited you to play a pong ! {invitation.playerOneUsername} VS {invitation.playerTwoUsername}
<button class="pong_button" on:click={() => acceptInvitation(invitation)}>V</button> <button class="pong_button" on:click={() => acceptInvitation(invitation)}>V</button>
<button class="pong_button" on:click={() => rejectInvitation(invitation)}>X</button> <button class="pong_button" on:click={() => rejectInvitation(invitation)}>X</button>
</div> </div>