init pong refactoring for spectator mode

+ pongSpectator.ts should be good
+ EventError wip, not tested
This commit is contained in:
LuckyLaszlo
2022-12-22 17:40:14 +01:00
parent dca3df1bde
commit 43f2dd85c4
12 changed files with 136 additions and 80 deletions

View File

@@ -88,7 +88,7 @@ async function clientAnnounceListener(this: WebSocket, data: string)
}); });
if (!response.ok) if (!response.ok)
{ {
// send message to client ? or just gtfo ? this.send(JSON.stringify( new ev.EventError((await response.json()).message)));
clientTerminate(clientsMap.get(this.id)); clientTerminate(clientsMap.get(this.id));
return; return;
} }
@@ -111,7 +111,7 @@ async function clientAnnounceListener(this: WebSocket, data: string)
const announce: ev.ClientAnnounceSpectator = <ev.ClientAnnounceSpectator>msg; const announce: ev.ClientAnnounceSpectator = <ev.ClientAnnounceSpectator>msg;
const gameSession = gameSessionsMap.get(announce.gameSessionId); const gameSession = gameSessionsMap.get(announce.gameSessionId);
if (!gameSession) { if (!gameSession) {
// WIP: send "invalid game session" this.send(JSON.stringify( new ev.EventError("invalid gameSessionId")));
clientTerminate(clientsMap.get(this.id)); clientTerminate(clientsMap.get(this.id));
return; return;
} }
@@ -187,13 +187,15 @@ function privateMatchmaking(player: ClientPlayer)
}); });
createGameSession(compatiblePlayers, matchOptions); createGameSession(compatiblePlayers, matchOptions);
} }
else { else
{
setTimeout(async function abortMatch() { setTimeout(async function abortMatch() {
if (!player.gameSession) if (!player.gameSession)
{ {
// TODO: informe le back de l'invalidité du token d'invitation if (player.socket.OPEN) {
player.socket.send(JSON.stringify( new ev.EventMatchAbort() )); player.socket.send(JSON.stringify( new ev.EventMatchAbort() ));
const responseTobakc = await fetch(c.addressBackEnd + "/game/gameserver/destroysession",{ }
const response = await fetch(c.addressBackEnd + "/game/gameserver/destroysession",{
method: "POST", method: "POST",
headers : {"Content-Type": "application/json"}, headers : {"Content-Type": "application/json"},
body : JSON.stringify({ body : JSON.stringify({
@@ -201,7 +203,7 @@ function privateMatchmaking(player: ClientPlayer)
}) })
}) })
.then(x => x.json()) .then(x => x.json())
.catch(error => console.log("ERROR : " + error)) .catch(error => console.log("ERROR : " + error));
clientTerminate(player); clientTerminate(player);
} }
}, 60000); }, 60000);

View File

@@ -72,12 +72,19 @@ export class EventMatchEnd extends ServerEvent {
} }
export class EventMatchAbort extends ServerEvent { export class EventMatchAbort extends ServerEvent {
constructor() { constructor() {
super(en.EventTypes.matchAbort); super(en.EventTypes.matchAbort);
} }
} }
export class EventError extends ServerEvent {
message: string;
constructor(message: string) {
super(en.EventTypes.error);
this.message = message;
}
}
/* From Client */ /* From Client */
export class ClientEvent { export class ClientEvent {

View File

@@ -6,6 +6,7 @@ export enum EventTypes {
matchEnd, matchEnd,
assignId, assignId,
matchmakingComplete, matchmakingComplete,
error,
// Generic // Generic
matchmakingInProgress, matchmakingInProgress,

View File

@@ -7,6 +7,8 @@
import * as enumeration from './shared_js/enums'; import * as enumeration from './shared_js/enums';
import * as pong from "./client/pong" import * as pong from "./client/pong"
import * as pongSpectator from "./client/pongSpectator" // TODO init spectator import * as pongSpectator from "./client/pongSpectator" // TODO init spectator
// Pour Chérif: variables indiquant l'état du match
import { matchEnded, matchAbort } from "./client/ws"; import { matchEnded, matchAbort } from "./client/ws";
//user's stuff //user's stuff
@@ -43,7 +45,6 @@
}) })
onDestroy( async() => { onDestroy( async() => {
// TODO: destroy() in pong
pong.destroy(); pong.destroy();
}) })
@@ -52,13 +53,8 @@
optionsAreNotSet = false; optionsAreNotSet = false;
showWaitPage = true; showWaitPage = true;
let matchOptions = enumeration.MatchOptions.noOption; const matchOptions = pong.computeMatchOptions(options);
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'},
@@ -72,7 +68,7 @@
const responseFromServer = await responseWhenGrantToken; const responseFromServer = await responseWhenGrantToken;
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)
{ {
errorMessageWhenAttemptingToGetATicket = responseInjson.message; errorMessageWhenAttemptingToGetATicket = responseInjson.message;
@@ -82,8 +78,7 @@
showWaitPage = false showWaitPage = false
optionsAreNotSet = true optionsAreNotSet = true
options.playerTwoUsername = ""; options.playerTwoUsername = "";
matchOptions = enumeration.MatchOptions.noOption; }, 5000);
}, 5000)
} }
else if (token) else if (token)
{ {

View File

@@ -0,0 +1,10 @@
export class InitOptions {
sound = "off";
multi_balls = false;
moving_walls = false;
isSomeoneIsInvited = false;
isInvitedPerson = false;
playerOneUsername = "";
playerTwoUsername = "";
}

View File

@@ -0,0 +1,48 @@
import * as c from "./constants.js"
import * as en from "../shared_js/enums.js"
import { GameArea } from "./class/GameArea.js";
import { GameComponentsClient } from "./class/GameComponentsClient.js";
import { socket } from "./ws.js";
import { initAudio } from "./audio.js";
import type { InitOptions } from "./class/InitOptions.js";
import { pong } from "./global.js"
import { initPong, initGc, initMatchOptions } from "./global.js"
export function computeMatchOptions(options: InitOptions)
{
let matchOptions = en.MatchOptions.noOption;
if (options.multi_balls === true) {
matchOptions |= en.MatchOptions.multiBalls
}
if (options.moving_walls === true) {
matchOptions |= en.MatchOptions.movingWalls
}
return matchOptions;
}
export function initBase(options: InitOptions, gameAreaId: string)
{
const matchOptions = computeMatchOptions(options);
initMatchOptions(matchOptions);
initAudio(options.sound);
initPong(new GameArea(gameAreaId));
initGc(new GameComponentsClient(matchOptions, pong.ctx));
}
export function destroyBase()
{
if (pong)
{
clearInterval(pong.handleInputInterval);
clearInterval(pong.gameLoopInterval);
clearInterval(pong.drawLoopInterval);
initPong(null);
}
if (socket && socket.OPEN) {
socket.close();
}
}

View File

@@ -1,11 +1,23 @@
import * as c from "./constants.js" import * as c from "./constants.js"
import { gc } from "./global.js" import { gc, pong } from "./global.js"
import * as en from "../shared_js/enums.js" import * as en from "../shared_js/enums.js"
/* /*
before game before game
*/ */
export function error(message: string)
{
console.log("msg.error()");
pong.clear();
const text = "error: " + message;
console.log(text);
gc.text2.clear();
gc.text2.pos.assign(c.w*0.2, c.h*0.5);
gc.text2.text = text;
gc.text2.update();
}
export function matchmaking() export function matchmaking()
{ {
const text = "searching..."; const text = "searching...";

View File

@@ -1,48 +1,26 @@
import * as c from "./constants.js" import * as c from "./constants.js"
import * as en from "../shared_js/enums.js"
import { GameArea } from "./class/GameArea.js";
import { GameComponentsClient } from "./class/GameComponentsClient.js";
import { handleInput } from "./handleInput.js"; import { handleInput } from "./handleInput.js";
// import { sendLoop } from "./handleInput.js";
import { gameLoop } from "./gameLoop.js" import { gameLoop } from "./gameLoop.js"
import { drawLoop } from "./draw.js"; import { drawLoop } from "./draw.js";
import { countdown } from "./utils.js"; import { countdown } from "./utils.js";
import { initWebSocket } from "./ws.js"; import { initWebSocket } from "./ws.js";
import { initAudio } from "./audio.js"; import type { InitOptions } from "./class/InitOptions.js";
export { InitOptions } from "./class/InitOptions.js";
import { initBase, destroyBase, computeMatchOptions } from "./init.js";
export { computeMatchOptions } from "./init.js";
/* TODO: A way to delay the init of variables, but still use "const" not "let" ? */ /* TODO: A way to delay the init of variables, but still use "const" not "let" ? */
import { pong, gc } from "./global.js" import { pong, gc } from "./global.js"
import { initPong, initGc, initMatchOptions, initStartFunction } from "./global.js" import { initStartFunction } from "./global.js"
export class InitOptions {
sound = "off";
multi_balls = false;
moving_walls = false;
isSomeoneIsInvited = false;
isInvitedPerson = false;
playerOneUsername = "";
playerTwoUsername = "";
}
export function init(options: InitOptions, gameAreaId: string, token: string) export function init(options: InitOptions, gameAreaId: string, token: string)
{ {
let matchOptions = en.MatchOptions.noOption; initBase(options, gameAreaId);
if (options.multi_balls === true) {
matchOptions |= en.MatchOptions.multiBalls
}
if (options.moving_walls === true) {
matchOptions |= en.MatchOptions.movingWalls
}
initMatchOptions(matchOptions);
initAudio(options.sound);
initPong(new GameArea(gameAreaId));
initGc(new GameComponentsClient(matchOptions, pong.ctx));
initStartFunction(start); initStartFunction(start);
const matchOptions = computeMatchOptions(options);
if (options.isSomeoneIsInvited) { if (options.isSomeoneIsInvited) {
initWebSocket(matchOptions, token, options.playerOneUsername, true, options.playerTwoUsername, options.isInvitedPerson); initWebSocket(matchOptions, token, options.playerOneUsername, true, options.playerTwoUsername, options.isInvitedPerson);
} }
@@ -53,14 +31,7 @@ export function init(options: InitOptions, gameAreaId: string, token: string)
export function destroy() export function destroy()
{ {
// TODO destroyBase();
if (pong)
{
clearInterval(pong.handleInputInterval);
clearInterval(pong.gameLoopInterval);
clearInterval(pong.drawLoopInterval);
initPong(null);
}
} }
function start() function start()

View File

@@ -1,37 +1,29 @@
initSpectator();
function initSpectator() {
// Wip
init();
}
import * as c from "./constants.js" import * as c from "./constants.js"
import * as en from "../shared_js/enums.js"
import { GameArea } from "./class/GameArea.js";
import { GameComponentsClient } from "./class/GameComponentsClient.js";
import { gameLoopSpectator } from "./gameLoop.js" import { gameLoopSpectator } from "./gameLoop.js"
import { drawLoop } from "./draw.js"; import { drawLoop } from "./draw.js";
import { initWebSocketSpectator } from "./ws.js"; import { initWebSocketSpectator } from "./ws.js";
import { initAudio } from "./audio.js"; import type { InitOptions } from "./class/InitOptions.js";
export { InitOptions } from "./class/InitOptions.js";
import { initBase, destroyBase, computeMatchOptions } from "./init.js";
export { computeMatchOptions } from "./init.js";
/* TODO: A way to delay the init of variables, but still use "const" not "let" ? */ /* TODO: A way to delay the init of variables, but still use "const" not "let" ? */
import { pong, gc } from "./global.js" import { pong, gc } from "./global.js"
import { initPong, initGc, initMatchOptions, initStartFunction } from "./global.js" import { initStartFunction } from "./global.js"
function init()
export function init(options: InitOptions, gameAreaId: string, gameSessionId: string)
{ {
initAudio(false); initBase(options, gameAreaId);
// WIP matchOptions
let matchOptions: en.MatchOptions = en.MatchOptions.noOption;
initMatchOptions(matchOptions);
initPong(new GameArea());
initGc(new GameComponentsClient(matchOptions, pong.ctx));
initStartFunction(start); initStartFunction(start);
initWebSocketSpectator(c.gameSessionIdPLACEHOLDER); initWebSocketSpectator(gameSessionId);
}
export function destroy()
{
destroyBase();
} }
function start() function start()

View File

@@ -48,6 +48,7 @@ export function initWebSocket(options: en.MatchOptions, token: string, username:
} }
}); });
// socket.addEventListener("message", logListener); // for testing purpose // socket.addEventListener("message", logListener); // for testing purpose
socket.addEventListener("message", errorListener);
socket.addEventListener("message", preMatchListener); socket.addEventListener("message", preMatchListener);
} }
@@ -55,6 +56,15 @@ function logListener(this: WebSocket, event: MessageEvent) {
console.log("%i: " + event.data, Date.now()); console.log("%i: " + event.data, Date.now());
} }
function errorListener(this: WebSocket, event: MessageEvent) {
console.log("errorListener");
const data: ev.ServerEvent = JSON.parse(event.data);
if (data.type === en.EventTypes.error) {
console.log("actual Error");
msg.error((data as ev.EventError).message);
}
}
function preMatchListener(this: WebSocket, event: MessageEvent) function preMatchListener(this: WebSocket, event: MessageEvent)
{ {
const data: ev.ServerEvent = JSON.parse(event.data); const data: ev.ServerEvent = JSON.parse(event.data);

View File

@@ -72,12 +72,19 @@ export class EventMatchEnd extends ServerEvent {
} }
export class EventMatchAbort extends ServerEvent { export class EventMatchAbort extends ServerEvent {
constructor() { constructor() {
super(en.EventTypes.matchAbort); super(en.EventTypes.matchAbort);
} }
} }
export class EventError extends ServerEvent {
message: string;
constructor(message: string) {
super(en.EventTypes.error);
this.message = message;
}
}
/* From Client */ /* From Client */
export class ClientEvent { export class ClientEvent {

View File

@@ -6,6 +6,7 @@ export enum EventTypes {
matchEnd, matchEnd,
assignId, assignId,
matchmakingComplete, matchmakingComplete,
error,
// Generic // Generic
matchmakingInProgress, matchmakingInProgress,