minor wsServer changes

("for...of" loop replace a "map.forEach" to make early break possible)
This commit is contained in:
LuckyLaszlo
2022-12-06 07:40:15 +01:00
parent 38c0acb917
commit 6985d2218f
2 changed files with 24 additions and 12 deletions

View File

@@ -62,6 +62,9 @@ function clientAnnounceListener(this: WebSocket, data: string)
{
// TODO: reconnection with msg.clientId ?
// TODO: spectator/player distinction with msg.role ?
// msg.role is probably not a good idea.
// something like a different route could be better
// "/pong" to play, "/ID_OF_A_GAMESESSION" to spectate
const player = clientsMap.get(this.id) as ClientPlayer;
player.matchOptions = msg.matchOptions;
@@ -89,15 +92,16 @@ function matchmaking(player: ClientPlayer)
matchmakingPlayersMap.set(player.id, player);
const compatiblePlayers: ClientPlayer[] = [];
matchmakingPlayersMap.forEach((client) => {
if (compatiblePlayers.length === maxPlayersNumber) {
return; // how can we stop forEach entierly and not just this step ???
}
if (client.matchOptions === matchOptions) {
for (const [id, client] of matchmakingPlayersMap)
{
if (client.matchOptions === matchOptions)
{
compatiblePlayers.push(client);
// PLACE complete forEach stop here
if (compatiblePlayers.length === maxPlayersNumber) {
break;
}
}
});
}
if (compatiblePlayers.length < minPlayersNumber) {
return;
@@ -118,8 +122,11 @@ function matchmaking(player: ClientPlayer)
// Could be done in gameSession maybe ?
compatiblePlayers[0].racket = gameSession.components.playerRight;
compatiblePlayers[1].racket = gameSession.components.playerLeft;
compatiblePlayers[0].socket.once("message", playerReadyConfirmationListener);
compatiblePlayers[1].socket.once("message", playerReadyConfirmationListener);
compatiblePlayers.forEach((client) => {
client.socket.once("message", playerReadyConfirmationListener);
});
compatiblePlayers[0].socket.send(JSON.stringify( new ev.EventMatchmakingComplete(en.PlayerSide.right) ));
compatiblePlayers[1].socket.send(JSON.stringify( new ev.EventMatchmakingComplete(en.PlayerSide.left) ));
}
@@ -183,8 +190,10 @@ const pingInterval = setInterval( () => {
clientTerminate(client, key, map);
deleteLog += ` ${shortId(key)} |`;
}
client.isAlive = false;
client.socket.ping();
else {
client.isAlive = false;
client.socket.ping();
}
});
if (deleteLog) {
@@ -194,7 +203,7 @@ const pingInterval = setInterval( () => {
console.log("clientsMap size: " + clientsMap.size);
console.log("matchmakingPlayersMap size: " + matchmakingPlayersMap.size);
console.log("");
}, 5000);
}, 4200);
function clientTerminate(client: Client, key: string, map: Map<string, Client>)