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

View File

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

View File

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