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

@@ -5,7 +5,7 @@ import * as c from "../constants.js"
import { ClientPlayer } from "./Client";
import { GameComponentsServer } from "./GameComponentsServer.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(),

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 { Client, ClientPlayer } from "./class/Client.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 ?
const wsPort = 8042;
@@ -35,7 +36,7 @@ function connectionListener(socket: WebSocket, request: IncomingMessage)
socket.on("pong", function heartbeat() {
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) {
@@ -57,7 +58,8 @@ function clientAnnounceListener(this: WebSocket, data: string)
{
try {
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: spectator/player distinction with msg.role ?
@@ -121,7 +123,8 @@ function playerReadyConfirmationListener(this: WebSocket, data: string)
{
try {
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 gameSession = client.gameSession;
gameSession.unreadyPlayersMap.delete(this.id);
@@ -149,7 +152,8 @@ export function clientInputListener(this: WebSocket, data: string)
try {
// const input: ev.ClientEvent = 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;
client.inputBuffer = input;
client.gameSession.instantInputDebug(client); // wip
@@ -167,22 +171,46 @@ export function clientInputListener(this: WebSocket, data: string)
////////////
const pingInterval = setInterval( () => {
let deleteLog = "";
clientsMap.forEach( (client, key, map) => {
if (client.isAlive === false) {
client.socket.terminate();
map.delete(key);
if (matchmakingPlayersMap.has(key)) {
matchmakingPlayersMap.delete(key);
}
console.log("%i: client %s is no more :'(", Date.now(), key);
return;
if (!client.isAlive) {
clientTerminate(client, key, map);
deleteLog += ` ${shortId(key)} |`;
}
client.isAlive = false;
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);
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()
{
clearInterval(pingInterval);