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' 'image/png': 'png'
}; };
export enum STATUS {
CONNECTED = 'Connected',
DISCONNECTED = 'Disconnected',
IN_GAME = 'In Game',
IN_POOL = 'In Pool',
}
export const storageForAvatar = { export const storageForAvatar = {
storage: diskStorage({ storage: diskStorage({

View File

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

View File

@@ -26,13 +26,14 @@
let showGameOption = true; let showGameOption = true;
let showError = false; let showError = false;
let hiddenGame = true; let hiddenGame = true;
let showMatchEnded = false;
let isThereAnyInvitation = false; let isThereAnyInvitation = false;
let invitations = []; let invitations = [];
let waitingMessage = "Please wait..." let waitingMessage = "Please wait..."
let errorMessageWhenAttemptingToGetATicket = ""; let errorMessageWhenAttemptingToGetATicket = "";
let idOfIntevalCheckTerminationOfTheMatch;
onMount( async() => { onMount( async() => {
user = await fetch('http://transcendance:8080/api/v2/user') user = await fetch('http://transcendance:8080/api/v2/user')
@@ -43,16 +44,30 @@
}) })
onDestroy( async() => { onDestroy( async() => {
options.playerOneUsername = user.username;
showError = false;
showMatchEnded = false;
optionsAreNotSet = true
options.playerTwoUsername = "";
options.isSomeoneIsInvited = false;
options.isInvitedPerson = false;
options.moving_walls = false;
options.multi_balls = false;
errorMessageWhenAttemptingToGetATicket = "";
hiddenGame = true;
isThereAnyInvitation = false;
invitations = [];
pong.destroy(); pong.destroy();
clearInterval(idOfIntevalCheckTerminationOfTheMatch);
}) })
const initGame = async() => const initGame = async() =>
{ {
optionsAreNotSet = false; optionsAreNotSet = false;
showWaitPage = true; showWaitPage = true;
idOfIntevalCheckTerminationOfTheMatch = setInterval(matchTermitation, 1000);
const matchOptions = pong.computeMatchOptions(options); const matchOptions = pong.computeMatchOptions(options);
const responseWhenGrantToken = fetch("http://transcendance:8080/api/v2/game/ticket", { const responseWhenGrantToken = fetch("http://transcendance:8080/api/v2/game/ticket", {
method : "POST", method : "POST",
headers : {'Content-Type': 'application/json'}, headers : {'Content-Type': 'application/json'},
@@ -67,15 +82,24 @@
const responseInjson = await responseFromServer.json(); const responseInjson = await responseFromServer.json();
const token : string = responseInjson.token; const token : string = responseInjson.token;
showWaitPage = false; showWaitPage = false;
if (!responseFromServer.ok) if (!responseFromServer.ok || (responseFromServer.status != 200 && responseFromServer.status != 201))
{ {
console.log(responseInjson)
console.log("On refuse le ticket");
clearInterval(idOfIntevalCheckTerminationOfTheMatch);
errorMessageWhenAttemptingToGetATicket = responseInjson.message; errorMessageWhenAttemptingToGetATicket = responseInjson.message;
showError = true; showError = true;
options.playerTwoUsername = "";
options.playerOneUsername = user.username;
options.isSomeoneIsInvited = false;
options.isInvitedPerson = false;
options.moving_walls = false;
options.multi_balls = false;
setTimeout(() => { setTimeout(() => {
showError = false; showError = false;
showWaitPage = false showWaitPage = false
optionsAreNotSet = true optionsAreNotSet = true
options.playerTwoUsername = "";
}, 5000); }, 5000);
} }
else if (token) else if (token)
@@ -89,6 +113,7 @@
// Pour Cherif: renommer en un truc du genre "initGameForInvitedPlayer" ? // Pour Cherif: renommer en un truc du genre "initGameForInvitedPlayer" ?
const initGameForPrivateParty = async(invitation : any) => const initGameForPrivateParty = async(invitation : any) =>
{ {
idOfIntevalCheckTerminationOfTheMatch = setInterval(matchTermitation, 1000);
optionsAreNotSet = false optionsAreNotSet = false
showWaitPage = true showWaitPage = true
console.log("invitation : ") console.log("invitation : ")
@@ -105,6 +130,38 @@
} }
} }
const matchTermitation = () => {
console.log("Ping matchTermitation")
if (matchAbort || matchEnded)
{
clearInterval(idOfIntevalCheckTerminationOfTheMatch);
console.log("matchTermitation was called")
showWaitPage = false
matchAbort ? errorMessageWhenAttemptingToGetATicket = "The match has been aborted" : errorMessageWhenAttemptingToGetATicket = "The match is finished !"
matchAbort ? showError = true : showMatchEnded = true;
hiddenGame = true;
setTimeout(() => {
showError = false;
showMatchEnded = false;
optionsAreNotSet = true
options.playerTwoUsername = "";
options.playerOneUsername = user.username;
options.isSomeoneIsInvited = false;
options.isInvitedPerson = false;
options.moving_walls = false;
options.multi_balls = false;
errorMessageWhenAttemptingToGetATicket = "";
options.playerTwoUsername = "";
hiddenGame = true;
isThereAnyInvitation = false;
invitations = [];
pong.destroy();
console.log("matchTermitation : setTimeout")
}, 5000);
}
}
const showOptions = () => { const showOptions = () => {
showGameOption = true showGameOption = true
showInvitations = false showInvitations = false
@@ -132,7 +189,7 @@
} }
const acceptInvitation = async(invitation : any) => { const acceptInvitation = async(invitation : any) => {
await fetch("http://transcendance:8080/api/v2/game/accept",{ const res = await fetch("http://transcendance:8080/api/v2/game/accept",{
method: "POST", method: "POST",
headers: { 'Content-Type': 'application/json'}, headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({ body: JSON.stringify({
@@ -142,9 +199,14 @@
.then(x => x.json()) .then(x => x.json())
.catch(error => { .catch(error => {
console.log(error) console.log(error)
}) })
showInvitation() // maybe useless if (res.status === 200)
{
showInvitation()
initGameForPrivateParty(invitation)
}
//Au final c'est utile !
initGameForPrivateParty(invitation) initGameForPrivateParty(invitation)
} }
</script> </script>
@@ -168,6 +230,12 @@
</div> </div>
{/if} {/if}
{#if showMatchEnded === true}
<div id="div_game" in:fly="{{ y: 10, duration: 1000 }}">
<p>{errorMessageWhenAttemptingToGetATicket}</p>
</div>
{/if}
{#if showWaitPage === true} {#if showWaitPage === true}
<div id="div_game" in:fly="{{ y: 10, duration: 1000 }}"> <div id="div_game" in:fly="{{ y: 10, duration: 1000 }}">
<fieldset> <fieldset>

View File

@@ -101,6 +101,9 @@ function preMatchListener(this: WebSocket, event: MessageEvent)
matchAbort = true; matchAbort = true;
socket.removeEventListener("message", preMatchListener); socket.removeEventListener("message", preMatchListener);
msg.matchAbort(); msg.matchAbort();
setTimeout(() => {
matchAbort = false;
}, 1000);
break; break;
} }
} }
@@ -212,6 +215,9 @@ function matchEnd(data: ev.EventMatchEnd)
else { else {
msg.lose(); msg.lose();
} }
setTimeout(() => {
matchEnded = false;
}, 1000);
} }
/* Spectator */ /* Spectator */
@@ -320,3 +326,4 @@ function matchEndSpectator(data: ev.EventMatchEnd)
msg.forfeit(clientInfo.side); msg.forfeit(clientInfo.side);
} */ } */
} }