refactoring Game.svelte (INVITATION NOT TESTED !)
This commit is contained in:
@@ -1,41 +1,23 @@
|
|||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
/* TODO Luke: refactoring */
|
|
||||||
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 { drawLoop } from './client/draw';
|
|
||||||
import { countdown } from './client/utils'
|
|
||||||
import { initWebSocket } from './client/ws';
|
|
||||||
import { initAudio } from './client/audio';
|
|
||||||
import { pong, gc} from './client/global'
|
|
||||||
|
|
||||||
import { onMount, onDestroy } from "svelte";
|
import { onMount, onDestroy } from "svelte";
|
||||||
import Header from '../../pieces/Header.svelte';
|
import Header from '../../pieces/Header.svelte';
|
||||||
import { fade, fly } from 'svelte/transition';
|
import { fade, fly } from 'svelte/transition';
|
||||||
|
|
||||||
|
import * as enumeration from './shared_js/enums';
|
||||||
|
import * as pong from "./client/pong"
|
||||||
|
import * as pongSpectator from "./client/pongSpectator" // TODO init spectator
|
||||||
|
|
||||||
//user's stuff
|
//user's stuff
|
||||||
let user;
|
let user;
|
||||||
let allUsers;
|
let allUsers;
|
||||||
|
|
||||||
//Game's stuff nest side
|
//Game's stuff
|
||||||
let optionsAreNotSet = true;
|
let optionsAreNotSet = true;
|
||||||
let isSomeoneIsInvited = false;
|
const options = new pong.InitOptions();
|
||||||
let playerTwoUsername = "";
|
|
||||||
|
|
||||||
//Game's stuff client side only
|
//Game's stuff client side only
|
||||||
let sound = "on";
|
const gameAreaId = "game_area";
|
||||||
let gameAreaId = "game_area";
|
|
||||||
//Game's stuff gameserver side
|
|
||||||
let multi_balls = false;
|
|
||||||
let moving_walls = false;
|
|
||||||
let matchOption : enumeration.MatchOptions = enumeration.MatchOptions.noOption;
|
|
||||||
|
|
||||||
//html boolean for pages
|
//html boolean for pages
|
||||||
let showWaitPage = false;
|
let showWaitPage = false;
|
||||||
@@ -47,37 +29,44 @@
|
|||||||
let isThereAnyInvitation = false;
|
let isThereAnyInvitation = false;
|
||||||
let invitations = [];
|
let invitations = [];
|
||||||
|
|
||||||
|
|
||||||
let waitingMessage = "Please wait..."
|
let waitingMessage = "Please wait..."
|
||||||
let errorMessageWhenAttemptingToGetATicket = "";
|
let errorMessageWhenAttemptingToGetATicket = "";
|
||||||
|
|
||||||
|
|
||||||
onMount( async() => {
|
onMount( async() => {
|
||||||
user = await fetch('http://transcendance:8080/api/v2/user')
|
user = await fetch('http://transcendance:8080/api/v2/user')
|
||||||
.then( x => x.json() );
|
.then( x => x.json() );
|
||||||
allUsers = await fetch('http://transcendance:8080/api/v2/user/all')
|
allUsers = await fetch('http://transcendance:8080/api/v2/user/all')
|
||||||
.then( x => x.json() );
|
.then( x => x.json() );
|
||||||
|
|
||||||
|
options.playerOneUsername = user.username;
|
||||||
})
|
})
|
||||||
|
|
||||||
onDestroy( async() => {
|
onDestroy( async() => {
|
||||||
|
// TODO: destroy() in pong
|
||||||
|
pong.destroy();
|
||||||
})
|
})
|
||||||
|
|
||||||
const initGame = async() => {
|
const initGame = async() =>
|
||||||
optionsAreNotSet = false
|
{
|
||||||
showWaitPage = true
|
optionsAreNotSet = false;
|
||||||
if (multi_balls === true)
|
showWaitPage = true;
|
||||||
matchOption |= enumeration.MatchOptions.multiBalls
|
|
||||||
if (moving_walls === true )
|
|
||||||
matchOption |= enumeration.MatchOptions.movingWalls
|
|
||||||
|
|
||||||
|
let matchOptions = enumeration.MatchOptions.noOption;
|
||||||
|
if (options.multi_balls === true) {
|
||||||
|
matchOptions |= enumeration.MatchOptions.multiBalls
|
||||||
|
}
|
||||||
|
if (options.moving_walls === true) {
|
||||||
|
matchOptions |= enumeration.MatchOptions.movingWalls
|
||||||
|
}
|
||||||
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'},
|
||||||
body : JSON.stringify({
|
body : JSON.stringify({
|
||||||
playerOneUsername : user.username,
|
playerOneUsername : options.playerOneUsername,
|
||||||
playerTwoUsername : playerTwoUsername,
|
playerTwoUsername : options.playerTwoUsername,
|
||||||
gameOptions : matchOption,
|
gameOptions : matchOptions,
|
||||||
isGameIsWithInvitation : isSomeoneIsInvited
|
isGameIsWithInvitation : options.isSomeoneIsInvited
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
const responseFromServer = await responseWhenGrantToken;
|
const responseFromServer = await responseWhenGrantToken;
|
||||||
@@ -92,65 +81,34 @@
|
|||||||
showError = false;
|
showError = false;
|
||||||
showWaitPage = false
|
showWaitPage = false
|
||||||
optionsAreNotSet = true
|
optionsAreNotSet = true
|
||||||
playerTwoUsername = "";
|
options.playerTwoUsername = "";
|
||||||
matchOption = 0;
|
matchOptions = enumeration.MatchOptions.noOption;
|
||||||
}, 5000)
|
}, 5000)
|
||||||
}
|
}
|
||||||
else if (token)
|
else if (token)
|
||||||
{
|
{
|
||||||
(sound === "off") ? initAudio(true) : initAudio(false);
|
pong.init(options, gameAreaId, token);
|
||||||
initMatchOptions(matchOption)
|
|
||||||
initPong(new GameArea(gameAreaId))
|
|
||||||
initGc(new GameComponentsClient(matchOption, pong.ctx))
|
|
||||||
initStartFunction(start)
|
|
||||||
hiddenGame = false;
|
hiddenGame = false;
|
||||||
isSomeoneIsInvited ?
|
|
||||||
initWebSocket(matchOption, token, user.username, true, playerTwoUsername) :
|
|
||||||
initWebSocket(matchOption, token, user.username)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const initGameForPrivateParty = async(invitation : any) => {
|
// Pour Cherif: renommer en un truc du genre "initGameForInvitedPlayer" ?
|
||||||
|
const initGameForPrivateParty = async(invitation : any) =>
|
||||||
|
{
|
||||||
optionsAreNotSet = false
|
optionsAreNotSet = false
|
||||||
showWaitPage = true
|
showWaitPage = true
|
||||||
console.log("invitation : ")
|
console.log("invitation : ")
|
||||||
console.log(invitation)
|
console.log(invitation)
|
||||||
if (invitation.token)
|
if (invitation.token)
|
||||||
{
|
{
|
||||||
initMatchOptions(matchOption)
|
options.playerOneUsername = invitation.playerOneUsername;
|
||||||
initPong(new GameArea(gameAreaId))
|
options.playerTwoUsername = user.username;
|
||||||
initGc(new GameComponentsClient(matchOption, pong.ctx))
|
pong.init(options, gameAreaId, invitation.token);
|
||||||
initStartFunction(start)
|
|
||||||
showWaitPage = false
|
showWaitPage = false
|
||||||
hiddenGame = false;
|
hiddenGame = false;
|
||||||
initWebSocket(matchOption, invitation.token, invitation.playerOneUsername, true, user.username)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function start() : void {
|
|
||||||
gc.text1.pos.assign(constants.w*0.5, constants.h*0.75);
|
|
||||||
countdown(constants.matchStartDelay/1000, (count: number) => {
|
|
||||||
gc.text1.clear();
|
|
||||||
gc.text1.text = `${count}`;
|
|
||||||
gc.text1.update();
|
|
||||||
}, responseWhenGrantTokenume);
|
|
||||||
}
|
|
||||||
|
|
||||||
function responseWhenGrantTokenume(): void {
|
|
||||||
gc.text1.text = "";
|
|
||||||
window.addEventListener('keydown', function (e) {
|
|
||||||
pong.addKey(e.key);
|
|
||||||
});
|
|
||||||
window.addEventListener('keyup', function (e) {
|
|
||||||
pong.deleteKey(e.key);
|
|
||||||
});
|
|
||||||
pong.handleInputInterval = window.setInterval(handleInput, constants.handleInputIntervalMS);
|
|
||||||
pong.gameLoopInterval = window.setInterval(gameLoop, constants.gameLoopIntervalMS);
|
|
||||||
pong.drawLoopInterval = window.setInterval(drawLoop, constants.drawLoopIntervalMS);
|
|
||||||
}
|
|
||||||
|
|
||||||
const showOptions = () => {
|
const showOptions = () => {
|
||||||
showGameOption = true
|
showGameOption = true
|
||||||
showInvitations = false
|
showInvitations = false
|
||||||
@@ -234,26 +192,26 @@
|
|||||||
<fieldset>
|
<fieldset>
|
||||||
<legend>game options</legend>
|
<legend>game options</legend>
|
||||||
<div>
|
<div>
|
||||||
<input type="checkbox" id="multi_balls" name="multi_balls" bind:checked={multi_balls}>
|
<input type="checkbox" id="multi_balls" name="multi_balls" bind:checked={options.multi_balls}>
|
||||||
<label for="multi_balls">Multiples balls</label>
|
<label for="multi_balls">Multiples balls</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<input type="checkbox" id="moving_walls" name="moving_walls" bind:checked={moving_walls}>
|
<input type="checkbox" id="moving_walls" name="moving_walls" bind:checked={options.moving_walls}>
|
||||||
<label for="moving_walls">Moving walls</label>
|
<label for="moving_walls">Moving walls</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<p>sound :</p>
|
<p>sound :</p>
|
||||||
<input type="radio" id="sound_on" name="sound_selector" bind:group={sound} value="on">
|
<input type="radio" id="sound_on" name="sound_selector" bind:group={options.sound} value="on">
|
||||||
<label for="sound_on">on</label>
|
<label for="sound_on">on</label>
|
||||||
<input type="radio" id="sound_off" name="sound_selector" bind:group={sound} value="off">
|
<input type="radio" id="sound_off" name="sound_selector" bind:group={options.sound} value="off">
|
||||||
<label for="sound_off">off</label>
|
<label for="sound_off">off</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<input type="checkbox" id="isSomeoneIsInvited" bind:checked={isSomeoneIsInvited}>
|
<input type="checkbox" id="isSomeoneIsInvited" bind:checked={options.isSomeoneIsInvited}>
|
||||||
<label for="moving_walls">Invite a friend</label>
|
<label for="moving_walls">Invite a friend</label>
|
||||||
</div>
|
</div>
|
||||||
{#if isSomeoneIsInvited === true}
|
{#if options.isSomeoneIsInvited === true}
|
||||||
<select bind:value={playerTwoUsername}>
|
<select bind:value={options.playerTwoUsername}>
|
||||||
{#each allUsers as user }
|
{#each allUsers as user }
|
||||||
<option value={user.username}>{user.username}</option>
|
<option value={user.username}>{user.username}</option>
|
||||||
{/each}
|
{/each}
|
||||||
@@ -293,7 +251,8 @@
|
|||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
|
||||||
|
</div> <!-- div "game_page" -->
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
@font-face {
|
@font-face {
|
||||||
|
|||||||
@@ -4,8 +4,13 @@ import * as c from "./constants.js"
|
|||||||
export const soundPongArr: HTMLAudioElement[] = [];
|
export const soundPongArr: HTMLAudioElement[] = [];
|
||||||
export const soundRoblox = new Audio("http://transcendance:8080/sound/roblox-oof.ogg");
|
export const soundRoblox = new Audio("http://transcendance:8080/sound/roblox-oof.ogg");
|
||||||
|
|
||||||
export function initAudio(muteFlag: boolean)
|
export function initAudio(sound: string)
|
||||||
{
|
{
|
||||||
|
let muteFlag = true;
|
||||||
|
if (sound === "on") {
|
||||||
|
muteFlag = false;
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i <= 32; i++) {
|
for (let i = 0; i <= 32; i++) {
|
||||||
soundPongArr.push(new Audio("http://transcendance:8080/sound/pong/"+i+".ogg"));
|
soundPongArr.push(new Audio("http://transcendance:8080/sound/pong/"+i+".ogg"));
|
||||||
soundPongArr[i].volume = c.soundPongVolume;
|
soundPongArr[i].volume = c.soundPongVolume;
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
|
|
||||||
initDom();
|
|
||||||
function initDom() {
|
|
||||||
document.getElementById("play_pong_button").addEventListener("click", init);
|
|
||||||
}
|
|
||||||
|
|
||||||
import * as c from "./constants.js"
|
import * as c from "./constants.js"
|
||||||
import * as en from "../shared_js/enums.js"
|
import * as en from "../shared_js/enums.js"
|
||||||
import { GameArea } from "./class/GameArea.js";
|
import { GameArea } from "./class/GameArea.js";
|
||||||
@@ -21,35 +16,43 @@ import { initAudio } from "./audio.js";
|
|||||||
import { pong, gc } from "./global.js"
|
import { pong, gc } from "./global.js"
|
||||||
import { initPong, initGc, initMatchOptions, initStartFunction } from "./global.js"
|
import { initPong, initGc, initMatchOptions, initStartFunction } from "./global.js"
|
||||||
|
|
||||||
|
export class InitOptions {
|
||||||
|
sound = "off";
|
||||||
|
multi_balls = false;
|
||||||
|
moving_walls = false;
|
||||||
|
isSomeoneIsInvited = false;
|
||||||
|
playerOneUsername = "";
|
||||||
|
playerTwoUsername = "";
|
||||||
|
}
|
||||||
|
|
||||||
function init()
|
export function init(options: InitOptions, gameAreaId: string, token: string)
|
||||||
{
|
{
|
||||||
console.log("multi_balls:"+(<HTMLInputElement>document.getElementById("multi_balls")).checked);
|
let matchOptions = en.MatchOptions.noOption;
|
||||||
console.log("moving_walls:"+(<HTMLInputElement>document.getElementById("moving_walls")).checked);
|
if (options.multi_balls === true) {
|
||||||
console.log("sound_on:"+(<HTMLInputElement>document.getElementById("sound_on")).checked);
|
matchOptions |= en.MatchOptions.multiBalls
|
||||||
|
}
|
||||||
|
if (options.moving_walls === true) {
|
||||||
|
matchOptions |= en.MatchOptions.movingWalls
|
||||||
|
}
|
||||||
|
|
||||||
let soundMutedFlag = false;
|
|
||||||
if ( (<HTMLInputElement>document.getElementById("sound_off")).checked ) {
|
|
||||||
soundMutedFlag = true;
|
|
||||||
}
|
|
||||||
initAudio(soundMutedFlag);
|
|
||||||
|
|
||||||
let matchOptions: en.MatchOptions = en.MatchOptions.noOption;
|
|
||||||
if ( (<HTMLInputElement>document.getElementById("multi_balls")).checked ) {
|
|
||||||
matchOptions |= en.MatchOptions.multiBalls;
|
|
||||||
}
|
|
||||||
if ( (<HTMLInputElement>document.getElementById("moving_walls")).checked ) {
|
|
||||||
matchOptions |= en.MatchOptions.movingWalls;
|
|
||||||
}
|
|
||||||
initMatchOptions(matchOptions);
|
initMatchOptions(matchOptions);
|
||||||
|
initAudio(options.sound);
|
||||||
|
|
||||||
document.getElementById("div_game_options").remove();
|
initPong(new GameArea(gameAreaId));
|
||||||
document.getElementById("div_game_instructions").remove();
|
|
||||||
|
|
||||||
initPong(new GameArea());
|
|
||||||
initGc(new GameComponentsClient(matchOptions, pong.ctx));
|
initGc(new GameComponentsClient(matchOptions, pong.ctx));
|
||||||
initStartFunction(start);
|
initStartFunction(start);
|
||||||
initWebSocket(matchOptions);
|
|
||||||
|
if (options.isSomeoneIsInvited) {
|
||||||
|
initWebSocket(matchOptions, token, options.playerOneUsername, true, options.playerTwoUsername);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
initWebSocket(matchOptions, token, options.playerOneUsername);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function destroy()
|
||||||
|
{
|
||||||
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
function start()
|
function start()
|
||||||
|
|||||||
Reference in New Issue
Block a user