Game, suite et presque fin ?

This commit is contained in:
batche
2022-12-19 20:58:46 +01:00
parent 27a4bfae7a
commit dbd95089a8
14 changed files with 384 additions and 238 deletions

View File

@@ -2,7 +2,7 @@
// routing
// may not need {link} here
import Router, { link, replace } from "svelte-spa-router";
import { primaryRoutes } from "./routes/primaryRoutes.js";
import { primaryRoutes } from "./routes/primaryRoutes.js";
// import primaryRoutes from "./routes/primaryRoutes.svelte";
const conditionsFailed = (event) => {

View File

@@ -1,106 +0,0 @@
<script lang="ts">
// import { initDom } from "../game/client/pong.js";
import {onMount} from 'svelte';
onMount(() => {
// initDom();
})
</script>
<body>
<div id="div_game_options">
<fieldset>
<legend>game options</legend>
<div>
<input type="checkbox" id="multi_balls" name="multi_balls">
<label for="multi_balls">multiples balls</label>
</div>
<div>
<input type="checkbox" id="moving_walls" name="moving_walls">
<label for="moving_walls">moving walls</label>
</div>
<div>
<label>sound :</label>
<input type="radio" id="sound_on" name="sound_selector" checked>
<label for="sound_on">on</label>
<input type="radio" id="sound_off" name="sound_selector">
<label for="sound_off">off</label>
</div>
<div>
<button id="play_pong_button">PLAY</button>
</div>
</fieldset>
</div>
<div id="div_game_instructions">
<h2>--- keys ---</h2>
<p>move up: 'w' or 'up arrow'</p>
<p>move down: 's' OR 'down arrow'</p>
<p>grid on/off: 'g'</p>
</div>
<div id="canvas_container">
<!-- <p> =) </p> -->
</div>
<!-- <script src="http://localhost:8080/js/pong.js" type="module" defer></script> -->
</body>
<style>
@font-face {
font-family: "Bit5x3";
src: url("/fonts/Bit5x3.woff2") format("woff2"),
url("/fonts/Bit5x3.woff") format("woff");
font-weight: normal;
font-style: normal;
font-display: swap;
}
body {
margin: 0;
background-color: #222425;
}
#canvas_container {
margin-top: 20px;
text-align: center;
/* border: dashed rgb(245, 245, 245) 5px; */
/* max-height: 80vh; */
/* overflow: hidden; */
}
#div_game_instructions {
text-align: center;
font-family: "Bit5x3";
color: rgb(245, 245, 245);
font-size: large;
}
#div_game_options {
margin-top: 20px;
text-align: center;
font-family: "Bit5x3";
color: rgb(245, 245, 245);
font-size: x-large;
}
#div_game_options fieldset {
max-width: 50vw;
width: auto;
margin: 0 auto;
}
#div_game_options fieldset div {
padding: 10px;
}
#play_pong_button {
font-family: "Bit5x3";
color: rgb(245, 245, 245);
background-color: #333333;
font-size: x-large;
padding: 10px;
}
canvas {
background-color: #333333;
max-width: 75vw;
/* max-height: 100vh; */
width: 80%;
}
</style>

View File

@@ -1,39 +1,153 @@
<script lang="ts">
import { onMount } from "svelte";
import * as enumeration from './shared_js/enums'
import * as constants from './client/constants'
import { initPong, initGc, initMatchOptions, initStartFunction } from './client/global'
import { GameArea } from './client/class/GameArea';
import { GameComponentsClient } from './client/class/GameComponentsClient';
import { handleInput } from './client/handleInput';
import {gameLoop} from './client/gameLoop'
import { gameLoop } from './client/gameLoop'
import { drawLoop } from './client/draw';
import {countdown} from './client/utils'
import { countdown } from './client/utils'
import { initWebSocket } from './client/ws';
import { initAudio } from './client/audio';
import { pong, gc} from './client/global'
import Header from '../../pieces/Header.svelte';
let user;
let optionsAreNotSet = true
let sound = false;
let multi_balls = false;
let moving_walls = false;
let matchOption : enumeration.MatchOptions = enumeration.MatchOptions.noOption;
let invite_someone = false;
let allUsers;
let playerTwoUsername;
let showInvitations = false;
let showGameOption = true;
let isThereAnyInvitation = false;
let invitations;
let showError = false;
let showWaitPage = false;
let waitingMessage = "Please wait..."
let errorMessageWhenAttemptingToGetATicket;
let token = ""
let responseToCheckIfOtherUserHasAnswered;
let isSecondPlayerAnsweredYes = false;
let isSecondPlayerRefused = false;
let isSomethingWentWrong = true;
onMount( async() => {
user = await fetch('http://transcendance:8080/api/v2/user')
.then( (x) => x.json() );
allUsers = await fetch('http://transcendance:8080/api/v2/user/all')
.then( x => x.json() );
})
// En async au cas où pour la suite mais apriori inutile, les check se feront sûrement au onMount
const init = async() => {
optionsAreNotSet = false
showWaitPage = true
console.log("Player two username " + playerTwoUsername)
if (multi_balls === true)
matchOption |= enumeration.MatchOptions.multiBalls
if (moving_walls === true )
matchOption |= enumeration.MatchOptions.movingWalls
initAudio(sound)
initMatchOptions(matchOption)
optionsAreNotSet = false
initPong(new GameArea())
initGc(new GameComponentsClient(matchOption, pong.ctx))
initStartFunction(start)
initWebSocket(matchOption)
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 ;
}
if (invite_someone === true)
{
token = res.token;
waitingMessage = `Waiting for ${playerTwoUsername}'s answer !`
let intervalId = setInterval(checkIfOtherUserIsReady , 1000 * 1);
let timeOutId = setTimeout(()=> {
showError = true;
errorMessageWhenAttemptingToGetATicket = "The second player took too much time to answer"
showWaitPage = false
optionsAreNotSet = true
isSecondPlayerAnsweredYes = false
playerTwoUsername = "";
matchOption = 0;
token = ""
clearInterval(intervalId)
isSomethingWentWrong = true
}, 1000 * 60 * 3)
if (isSecondPlayerAnsweredYes === true)
{
clearTimeout(timeOutId)
isSomethingWentWrong = false;
}
else if (isSecondPlayerRefused === true)
{
clearTimeout(timeOutId)
}
}
if (isSomethingWentWrong === false)
{
initAudio(sound)
initMatchOptions(matchOption)
initPong(new GameArea())
initGc(new GameComponentsClient(matchOption, pong.ctx))
initStartFunction(start)
initWebSocket(matchOption)
}
}
async function checkIfOtherUserIsReady(token : string) {
responseToCheckIfOtherUserHasAnswered = await fetch("http://transcendance:8080/api/v2/game/pending",{
method : "POST",
headers : { 'Content-Type': 'application/json'},
body : JSON.stringify({
token : token
})
})
const data = await responseToCheckIfOtherUserHasAnswered.json();
if (data.isSecondUserAcceptedRequest === true)
isSecondPlayerAnsweredYes = true
if (data.status === 404 || data.status === 403)
{
isSecondPlayerRefused = true
errorMessageWhenAttemptingToGetATicket = data.message;
}
}
function start() : void {
@@ -54,47 +168,121 @@
pong.deleteKey(e.key);
});
pong.handleInputInterval = window.setInterval(handleInput, constants.handleInputIntervalMS);
// pong.handleInputInterval = window.setInterval(sendLoop, c.sendLoopIntervalMS);
pong.gameLoopInterval = window.setInterval(gameLoop, constants.gameLoopIntervalMS);
pong.drawLoopInterval = window.setInterval(drawLoop, constants.drawLoopIntervalMS);
}
const showOptions = () => {
showGameOption = true
showInvitations = false
}
const showInvitation = async() => {
showGameOption = false;
showInvitations = true;
invitations = await fetch("http://transcendance:8080/api/v2/game/invitations")
.then(x => x.json())
}
const rejectInvitation = async() => {
await fetch("http://transcendance:8080/api/v2/game/decline",{
method: "POST",
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
})
})
}
const acceptInvitation = async() => {
await fetch("http://transcendance:8080/api/v2/game/accept",{
method: "POST",
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
})
})
}
</script>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<Header />
<body>
<div id="preload_font">.</div>
{#if optionsAreNotSet}
<form on:submit|preventDefault={init}>
<div id="div_game_options">
<fieldset>
<legend>game options</legend>
<div>
<input type="checkbox" id="multi_balls" name="multi_balls" bind:checked={multi_balls}>
<label for="multi_balls">Multiples balls</label>
</div>
<div>
<input type="checkbox" id="moving_walls" name="moving_walls" bind:checked={moving_walls}>
<label for="moving_walls">Moving walls</label>
</div>
<div>
<input type="checkbox" id="moving_walls" name="moving_walls" bind:checked={sound}>
<label for="moving_walls">Sound</label>
</div>
<div>
<button id="play_pong_button" >PLAY</button>
</div>
</fieldset>
{#if showError === true}
<div id="error_notification" >
<p>{errorMessageWhenAttemptingToGetATicket}</p>
</div>
</form>
<div id="div_game_instructions">
<h2>--- keys ---</h2>
<p>move up: 'w' or 'up arrow'</p>
<p>move down: 's' OR 'down arrow'</p>
<p>grid on/off: 'g'</p>
</div>
{/if}
{#if showWaitPage === true}
<fieldset>
<legend>Connecting to the game...</legend>
<div id="div_game">
<p>{waitingMessage}</p>
</div>
</fieldset>
{/if}
{#if optionsAreNotSet}
{#if showGameOption === true}
<form on:submit|preventDefault={init}>
<div id="div_game">
<button id="pong_button" on:click={showInvitation}>Show invitations</button>
<fieldset>
<legend>game options</legend>
<div>
<input type="checkbox" id="multi_balls" name="multi_balls" bind:checked={multi_balls}>
<label for="multi_balls">Multiples balls</label>
</div>
<div>
<input type="checkbox" id="moving_walls" name="moving_walls" bind:checked={moving_walls}>
<label for="moving_walls">Moving walls</label>
</div>
<div>
<input type="checkbox" id="moving_walls" name="moving_walls" bind:checked={sound}>
<label for="moving_walls">Sound</label>
</div>
<div>
<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>
</fieldset>
</div>
</form>
{/if}
{#if showInvitations}
<div id="div_game">
<button id="pong_button" on:click={showOptions}>Play a Game</button>
<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>
{/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}
<div id="canvas_container">
@@ -128,21 +316,29 @@ body {
/* max-height: 80vh; */
/* overflow: hidden; */
}
#div_game_options {
#div_game {
text-align: center;
font-family: "Bit5x3";
color: rgb(245, 245, 245);
font-size: x-large;
}
#div_game_options fieldset {
#error_notification {
text-align: center;
display: block;
font-family: "Bit5x3";
color: rgb(143, 19, 19);
font-size: x-large;
}
#div_game fieldset {
max-width: 50vw;
width: auto;
margin: 0 auto;
}
#div_game_options fieldset div {
#div_game fieldset div {
padding: 10px;
}
#play_pong_button {
#pong_button {
font-family: "Bit5x3";
color: rgb(245, 245, 245);
background-color: #333333;

View File

@@ -24,6 +24,7 @@
<img src="/img/potato_logo.png" alt="Potato Pong Logo" on:click={() => (push('/'))}>
<h1>Potato Pong</h1>
<nav>
<button on:click={() => (push('/game'))}>Game</button>
{#if $location !== '/profile'}
<button on:click={() => (push('/profile'))}>My Profile</button>
{:else if $location === '/profile'}
@@ -52,11 +53,11 @@
src:url('/fonts/Bondi.ttf.woff') format('woff'),
url('/fonts/Bondi.ttf.svg#Bondi') format('svg'),
url('/fonts/Bondi.ttf.eot'),
url('/fonts/Bondi.ttf.eot?#iefix') format('embedded-opentype');
url('/fonts/Bondi.ttf.eot?#iefix') format('embedded-opentype');
font-weight: normal;
font-style: normal;
}
/* There is a bunch of unncessary shit in here... why so many flex grids, why is everything the same class? just seemed easier but... */
@@ -116,4 +117,4 @@
/* .none{
} */
</style>
</style>