GameSession delete + log

This commit is contained in:
LuckyLaszlo
2022-11-30 19:42:16 +01:00
parent 68e529fec2
commit e3fd130729
4 changed files with 50 additions and 14 deletions

View File

@@ -11,10 +11,10 @@ Done:
- interpolation (mis à jour progressif des mouvements de l'adversaire) - interpolation (mis à jour progressif des mouvements de l'adversaire)
- traitement groupé des inputs clients toutes les x millisecondes - traitement groupé des inputs clients toutes les x millisecondes
(BUG désynchronisation: revenu à un traitement immédiat en attendant) (BUG désynchronisation: revenu à un traitement immédiat en attendant)
- Détruire les GameSession une fois finies.
TODO: TODO:
- mode multi-balles - mode multi-balles
- Détruire les GameSession une fois finies.
- certaines utilisations de Math.floor() superflu ? Vérifier les appels. - certaines utilisations de Math.floor() superflu ? Vérifier les appels.
- (prediction de l'avancement de la balle basé sur la latence serveur ?) - (prediction de l'avancement de la balle basé sur la latence serveur ?)
----------- -----------

View File

@@ -5,7 +5,7 @@ import * as c from "../constants.js"
import { ClientPlayer } from "./Client"; import { ClientPlayer } from "./Client";
import { GameComponentsServer } from "./GameComponentsServer.js"; import { GameComponentsServer } from "./GameComponentsServer.js";
import { clientInputListener } from "../wsServer.js"; import { clientInputListener } from "../wsServer.js";
import { random } from "../../shared_js/utils.js"; import { random } from "../utils.js";
/* /*
Arg "s: GameSession" replace "this: GameSession" for use with setTimeout(), Arg "s: GameSession" replace "this: GameSession" for use with setTimeout(),

8
src/server/utils.ts Normal file
View File

@@ -0,0 +1,8 @@
export * from "../shared_js/utils.js"
function shortId(id: string): string {
return id.substring(0, id.indexOf("-"));
}
export {shortId}

View File

@@ -12,6 +12,7 @@ import * as en from "../shared_js/enums.js"
import * as ev from "../shared_js/class/Event.js" import * as ev from "../shared_js/class/Event.js"
import { Client, ClientPlayer } from "./class/Client.js" import { Client, ClientPlayer } from "./class/Client.js"
import { GameSession } from "./class/GameSession.js" import { GameSession } from "./class/GameSession.js"
import { shortId } from "./utils.js";
// pas indispensable d'avoir un autre port si le WebSocket est relié à un serveur http préexistant ? // pas indispensable d'avoir un autre port si le WebSocket est relié à un serveur http préexistant ?
const wsPort = 8042; const wsPort = 8042;
@@ -35,7 +36,7 @@ function connectionListener(socket: WebSocket, request: IncomingMessage)
socket.on("pong", function heartbeat() { socket.on("pong", function heartbeat() {
client.isAlive = true; client.isAlive = true;
console.log("%i: client %s is alive", Date.now(), client.id); // console.log(`client ${shortId(client.id)} is alive`);
}); });
socket.on("message", function log(data: string) { socket.on("message", function log(data: string) {
@@ -57,7 +58,8 @@ function clientAnnounceListener(this: WebSocket, data: string)
{ {
try { try {
const msg : ev.ClientAnnounce = JSON.parse(data); const msg : ev.ClientAnnounce = JSON.parse(data);
if (msg.type === en.EventTypes.clientAnnounce) { if (msg.type === en.EventTypes.clientAnnounce)
{
// TODO: reconnection with msg.clientId ? // TODO: reconnection with msg.clientId ?
// TODO: spectator/player distinction with msg.role ? // TODO: spectator/player distinction with msg.role ?
@@ -121,7 +123,8 @@ function playerReadyConfirmationListener(this: WebSocket, data: string)
{ {
try { try {
const msg : ev.ClientEvent = JSON.parse(data); const msg : ev.ClientEvent = JSON.parse(data);
if (msg.type === en.EventTypes.clientPlayerReady) { if (msg.type === en.EventTypes.clientPlayerReady)
{
const client = clientsMap.get(this.id); const client = clientsMap.get(this.id);
const gameSession = client.gameSession; const gameSession = client.gameSession;
gameSession.unreadyPlayersMap.delete(this.id); gameSession.unreadyPlayersMap.delete(this.id);
@@ -149,7 +152,8 @@ export function clientInputListener(this: WebSocket, data: string)
try { try {
// const input: ev.ClientEvent = JSON.parse(data); // const input: ev.ClientEvent = JSON.parse(data);
const input: ev.EventInput = JSON.parse(data); const input: ev.EventInput = JSON.parse(data);
if (input.type === en.EventTypes.clientInput) { if (input.type === en.EventTypes.clientInput)
{
const client = clientsMap.get(this.id) as ClientPlayer; const client = clientsMap.get(this.id) as ClientPlayer;
client.inputBuffer = input; client.inputBuffer = input;
client.gameSession.instantInputDebug(client); // wip client.gameSession.instantInputDebug(client); // wip
@@ -167,22 +171,46 @@ export function clientInputListener(this: WebSocket, data: string)
//////////// ////////////
const pingInterval = setInterval( () => { const pingInterval = setInterval( () => {
let deleteLog = "";
clientsMap.forEach( (client, key, map) => { clientsMap.forEach( (client, key, map) => {
if (client.isAlive === false) { if (!client.isAlive) {
client.socket.terminate(); clientTerminate(client, key, map);
map.delete(key); deleteLog += ` ${shortId(key)} |`;
if (matchmakingPlayersMap.has(key)) {
matchmakingPlayersMap.delete(key);
}
console.log("%i: client %s is no more :'(", Date.now(), key);
return;
} }
client.isAlive = false; client.isAlive = false;
client.socket.ping(); client.socket.ping();
}); });
if (deleteLog) {
console.log(`Disconnected:${deleteLog}`);
}
console.log("gameSessionMap size: " + gameSessionsMap.size);
console.log("clientsMap size: " + clientsMap.size);
console.log("matchmakingPlayersMap size: " + matchmakingPlayersMap.size);
console.log("");
}, 5000); }, 5000);
function clientTerminate(client: Client, key: string, map: Map<string, Client>)
{
client.socket.terminate();
if (client.gameSession)
{
client.gameSession.playersMap.delete(key);
if (client.gameSession.playersMap.size === 0)
{
clearInterval(client.gameSession.clientsUpdateInterval);
clearInterval(client.gameSession.gameLoopInterval);
gameSessionsMap.delete(client.gameSession.id);
}
}
map.delete(key);
if (matchmakingPlayersMap.has(key)) {
matchmakingPlayersMap.delete(key);
}
}
function closeListener() function closeListener()
{ {
clearInterval(pingInterval); clearInterval(pingInterval);