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

@@ -24,6 +24,9 @@ TODO:
(éventuellement Math.round() ?) (éventuellement Math.round() ?)
- un autre mode de jeu alternatif ? - un autre mode de jeu alternatif ?
- changer les "localhost:8080" dans le code. - changer les "localhost:8080" dans le code.
- sélection couleur des raquettes (your color/opponent color) dans le profil utilisateur.
Enregistrement dans la DB.
init des couleurs dans GameComponentsClient() basé sur les variables de l'utilsateur connecté.
----------- -----------
idées modes de jeu : idées modes de jeu :
- mode 2 raquettes (un joueur haut/gauche et bas/droite) - mode 2 raquettes (un joueur haut/gauche et bas/droite)

View File

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