jeu suite

This commit is contained in:
batche
2022-12-20 17:56:18 +01:00
parent bd66fe60ff
commit fb7bfa0097

View File

@@ -34,10 +34,11 @@
let token = ""; let token = "";
//Game's stuff gameserver side //Game's stuff gameserver side
let sound = "on"; // possible de faire un boolean avec svelte et radio buttons ? let sound = "";
let multi_balls = false; let multi_balls = false;
let moving_walls = false; let moving_walls = false;
let matchOptions : enumeration.MatchOptions = enumeration.MatchOptions.noOption; let matchOption : enumeration.MatchOptions = enumeration.MatchOptions.noOption;
//html boolean for pages //html boolean for pages
let showWaitPage = false; let showWaitPage = false;
let showInvitations = false; let showInvitations = false;
@@ -60,18 +61,72 @@
}) })
const init = async() => { const init = async() => {
optionsAreNotSet = false
showWaitPage = true
console.log("Player two username " + playerTwoUsername)
if (multi_balls === true) if (multi_balls === true)
matchOption |= enumeration.MatchOptions.multiBalls matchOption |= enumeration.MatchOptions.multiBalls
if (moving_walls === true ) if (moving_walls === true )
matchOption |= enumeration.MatchOptions.movingWalls matchOption |= enumeration.MatchOptions.movingWalls
const res = await fetch("http://transcendance:8080/api/v2/game/ticket", {
method : "POST",
headers : {'Content-Type': 'application/json'},
body : JSON.stringify({
playerOneUsername : user.username,
playerTwoUsername : playerTwoUsername,
gameOptions : matchOption,
isGameIsWithInvitation : invite_someone
})
})
.then(x => x.json())
.catch(error => {
console.log(error)
})
if (res.status === 403 || res.status === 404 || res.status === 500)
{
errorMessageWhenAttemptingToGetATicket = res.message;
showError = true;
setTimeout(() => {
showError = false;
showWaitPage = false
optionsAreNotSet = true
playerTwoUsername = "";
matchOption = 0;
token = ""
}, 5000)
return ;
}
else if (res.status === 200)
{
initAudio(sound) initAudio(sound)
initMatchOptions(matchOption) initMatchOptions(matchOption)
optionsAreNotSet = false
initPong(new GameArea()) initPong(new GameArea())
initGc(new GameComponentsClient(matchOption, pong.ctx)) initGc(new GameComponentsClient(matchOption, pong.ctx))
initStartFunction(start) initStartFunction(start)
initWebSocket(matchOption) initWebSocket(matchOption)
} }
// const fetch = 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 ;
// }
}
function start() : void { function start() : void {
gc.text1.pos.assign(constants.w*0.5, constants.h*0.75); gc.text1.pos.assign(constants.w*0.5, constants.h*0.75);
@@ -133,9 +188,30 @@
<body> <body>
<div id="preload_font">.</div> <div id="preload_font">.</div>
{#if showError === true}
<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}
<div id="div_game">
<fieldset>
<legend>Connecting to the game...</legend>
<p>{waitingMessage}</p>
</fieldset>
</div>
{/if}
{#if optionsAreNotSet} {#if optionsAreNotSet}
{#if showGameOption === true}
<form on:submit|preventDefault={init}> <form on:submit|preventDefault={init}>
<div id="div_game_options"> <div id="div_game">
<button id="pong_button" on:click={showInvitation}>Show invitations</button>
<fieldset> <fieldset>
<legend>game options</legend> <legend>game options</legend>
<div> <div>
@@ -151,17 +227,44 @@
<label for="moving_walls">Sound</label> <label for="moving_walls">Sound</label>
</div> </div>
<div> <div>
<button id="play_pong_button" >PLAY</button> <input type="checkbox" id="invite_someone" name="moving_walls" bind:checked={invite_someone}>
<label for="moving_walls">Invite a friend</label>
</div>
{#if invite_someone === true}
<select bind:value={playerTwoUsername}>
{#each allUsers as user }
<option value={user.username}>{user.username}</option>
{/each}
</select>
{/if}
<div>
<button id="pong_button" >PLAY</button>
</div> </div>
</fieldset> </fieldset>
</div> </div>
</form> </form>
<div id="div_game_instructions"> {/if}
<h2>--- keys ---</h2> {#if showInvitations}
<p>move up: 'w' or 'up arrow'</p> <div id="div_game">
<p>move down: 's' OR 'down arrow'</p> <button id="pong_button" on:click={showOptions}>Play a Game</button>
<p>grid on/off: 'g'</p> <fieldset>
<legend>Current invitation(s)</legend>
{#if isThereAnyInvitation}
{#each invitations as invitation }
<div>
{invitation.playerOneUsername} has invited you to play a pong !
<button id="pong_button" on:click={acceptInvitation}>V</button>
<button id="pong_button" on:click={rejectInvitation}>X</button>
</div> </div>
{/each}
{/if}
{#if isThereAnyInvitation ===false}
<p>Currently, no one asked to play with you.</p>
<button id="pong_button" on:click={showInvitation}>Reload</button>
{/if}
</fieldset>
</div>
{/if}
{/if} {/if}
<div id="canvas_container"> <div id="canvas_container">
@@ -173,26 +276,29 @@
@font-face { @font-face {
font-family: "Bit5x3"; font-family: "Bit5x3";
src: src: url("/fonts/Bit5x3.woff2") format("woff2"),local("Bit5x3"), url("/fonts/Bit5x3.woff") format("woff");
url("/fonts/Bit5x3.woff2") format("woff2"),
local("Bit5x3"),
url("/fonts/Bit5x3.woff") format("woff");
font-weight: normal; font-weight: normal;
font-style: normal; font-style: normal;
font-display: swap; font-display: swap;
} }
#preload_font {
font-family: "Bit5x3";
opacity:0;
height:0;
width:0;
display:inline-block;
}
body { body {
margin: 0; margin: 0;
background-color: #222425; background-color: #222425;
} }
#canvas_container { #canvas_container {
margin-top: 20px;
text-align: center; text-align: center;
/* border: dashed rgb(245, 245, 245) 5px; */ /* border: dashed rgb(245, 245, 245) 5px; */
/* max-height: 80vh; */ /* max-height: 80vh; */
/* overflow: hidden; */ /* overflow: hidden; */
} }
#div_game_options { #div_game {
text-align: center; text-align: center;
font-family: "Bit5x3"; font-family: "Bit5x3";
color: rgb(245, 245, 245); color: rgb(245, 245, 245);