Suite du jeu et toujours pas fin

This commit is contained in:
batche
2022-12-19 21:37:58 +01:00
parent dbd95089a8
commit 8a11d9c591
3 changed files with 50 additions and 20 deletions

View File

@@ -14,7 +14,7 @@
import { initAudio } from './client/audio';
import { pong, gc} from './client/global'
import Header from '../../pieces/Header.svelte';
import { push } from "svelte-spa-router";
let user;
@@ -38,7 +38,7 @@
let showWaitPage = false;
let waitingMessage = "Please wait..."
let errorMessageWhenAttemptingToGetATicket;
let errorMessageWhenAttemptingToGetATicket = "";
let token = ""
let responseToCheckIfOtherUserHasAnswered;
@@ -98,29 +98,53 @@
waitingMessage = `Waiting for ${playerTwoUsername}'s answer !`
let intervalId = setInterval(checkIfOtherUserIsReady , 1000 * 1);
let timeOutId = setTimeout(()=> {
showError = true;
console.log("FIN DU TIMEOUT")
errorMessageWhenAttemptingToGetATicket = "The second player took too much time to answer"
showWaitPage = false
optionsAreNotSet = true
showError = true;
isSecondPlayerAnsweredYes = false
playerTwoUsername = "";
matchOption = 0;
token = ""
clearInterval(intervalId)
isSomethingWentWrong = true
}, 1000 * 60 * 3)
}, 1000 * 5)
console.log("EN DESSOUS DE TIMEOUT")
if (isSecondPlayerAnsweredYes === true)
{
clearTimeout(timeOutId)
clearInterval(intervalId)
isSomethingWentWrong = false;
}
else if (isSecondPlayerRefused === true)
if (isSecondPlayerRefused === true)
{
clearTimeout(timeOutId)
clearInterval(intervalId)
}
}
if (isSomethingWentWrong === false)
{
const res = await fetch("http://transcendance:8080/api/v2/game/gameServer/validate", {
method : "POST",
headers : {'Content-Type': 'application/json'},
body : JSON.stringify({
playerOneUsername : user.username,
playerTwoUsername : playerTwoUsername,
gameOptions : matchOption,
isGameIsWithInvitation : invite_someone,
token : token
})
})
.then(x => x.json())
.catch(error => {
console.log(error)
})
if (res.status !== 200)
{
showError = true;
errorMessageWhenAttemptingToGetATicket = "Your session is not valid. Please try again."
return ;
}
initAudio(sound)
initMatchOptions(matchOption)
initPong(new GameArea())
@@ -132,7 +156,7 @@
async function checkIfOtherUserIsReady(token : string) {
responseToCheckIfOtherUserHasAnswered = await fetch("http://transcendance:8080/api/v2/game/pending",{
responseToCheckIfOtherUserHasAnswered = await fetch("http://transcendance:8080/api/v2/game/requested",{
method : "POST",
headers : { 'Content-Type': 'application/json'},
body : JSON.stringify({
@@ -140,6 +164,7 @@
})
})
const data = await responseToCheckIfOtherUserHasAnswered.json();
console.log(data)
if (data.isSecondUserAcceptedRequest === true)
isSecondPlayerAnsweredYes = true
if (data.status === 404 || data.status === 403)
@@ -211,18 +236,22 @@
<body>
<div id="preload_font">.</div>
{#if showError === true}
<div id="error_notification" >
<p>{errorMessageWhenAttemptingToGetATicket}</p>
</div>
<div id="div_game">
<fieldset>
<legend>Error</legend>
<p>{errorMessageWhenAttemptingToGetATicket}</p>
<button id="pong_button" on:click={() => push('/game')}>Retry</button>
</fieldset>
</div>
{/if}
{#if showWaitPage === true}
<fieldset>
<legend>Connecting to the game...</legend>
<div id="div_game">
<p>{waitingMessage}</p>
</div>
</fieldset>
<div id="div_game">
<fieldset>
<legend>Connecting to the game...</legend>
<p>{waitingMessage}</p>
</fieldset>
</div>
{/if}
{#if optionsAreNotSet}