1
0

Post game cleanup

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-03-17 16:31:24 -07:00
parent 89327f0c65
commit c2f8e74265
2 changed files with 52 additions and 7 deletions

View File

@ -175,7 +175,8 @@ const Winner = ({ winnerDismissed, setWinnerDismissed }) => {
<div>The game took {totalTime}.</div> <div>The game took {totalTime}.</div>
{ robber } { robber }
{ max !== 0 && <> { max !== 0 && <>
<div>Player{ playerStolen.length === 1 ? '' : 's'} that had the most (<b>{max}</b>) resources stolen by the thief or other players:</div> <div>The thief (and other players) stole the most resources
from:</div>
<div className="PlayerStolenList"> <div className="PlayerStolenList">
{ playerStolen } { playerStolen }
</div> </div>

View File

@ -3082,13 +3082,23 @@ const departLobby = (game, session, color) => {
if (session.name) { if (session.name) {
if (session.color) { if (session.color) {
addChatMessage(game, null, `${session.name} has disconnected from the game.`); addChatMessage(game, null, `${session.name} has disconnected ` +
`from the game.`);
} else { } else {
addChatMessage(game, null, `${session.name} has left the lobby.`); addChatMessage(game, null, `${session.name} has left the lobby.`);
} }
update.chat = game.chat; update.chat = game.chat;
} else {
console.log(`${session.id}: departLobby - ${getName(session)} is ` +
`being removed from ${game.id}'s sessions.`);
for (let id in game.sessions) {
if (game.sessions[id] === session) {
delete game.sessions[id];
break;
}
}
} }
sendUpdateToPlayers(game, update); sendUpdateToPlayers(game, update);
} }
@ -3154,11 +3164,13 @@ const sendUpdateToPlayers = async (game, update) => {
const _session = game.sessions[key]; const _session = game.sessions[key];
/* Only send player and game data to named players */ /* Only send player and game data to named players */
if (!_session.name) { if (!_session.name) {
console.log(`${session.id}: -> sendUpdateToPlayers:${getName(_session)} - only sending empty name`); console.log(`${session.id}: -> sendUpdateToPlayers:` +
`${getName(_session)} - only sending empty name`);
message = JSON.stringify({ name: "" }); message = JSON.stringify({ name: "" });
} }
if (!_session.ws) { if (!_session.ws) {
console.log(`${_session.id}: -> sendUpdateToPlayers: Currently no connection.`); console.log(`${_session.id}: -> sendUpdateToPlayers: ` +
`Currently no connection.`);
} else { } else {
_session.ws.send(message); _session.ws.send(message);
} }
@ -3435,6 +3447,8 @@ router.ws("/ws/:id", async (ws, req) => {
}); });
ws.on('close', async (event) => { ws.on('close', async (event) => {
console.log(`${short} - closed connection`);
const game = await loadGame(gameId); const game = await loadGame(gameId);
if (!game) { if (!game) {
return; return;
@ -3455,8 +3469,38 @@ router.ws("/ws/:id", async (ws, req) => {
} }
departLobby(game, session); departLobby(game, session);
console.log(`${short} - closed connection`); /* Check for a game in the Winner state with no more connections
* and remove it */
if (game.state === 'winner' || game.state === 'lobby') {
let dead = true;
for (let id in game.sessions) {
if (game.sessions[id].live && game.sessions[id].color) {
dead = false;
}
}
if (dead) {
console.log(`${session.id}: No more players in ${game.id}. Removing.`);
addChatMessage(game, null, `No more active players in game. ` +
`It is being removed from the server.`);
sendUpdateToPlayers(game, {
chat: game.chat
});
for (let id in game.sessions) {
if (game.sessions[id].ws) {
game.sessions[id].ws.close();
delete game.sessions[id];
}
}
delete audio[id];
delete games[id];
try {
fs.unlinkSync(`games/${id}`);
} catch (error) {
console.error(`${session.id}: Unable to remove games/${id}`);
}
}
}
}); });
ws.on('message', async (message) => { ws.on('message', async (message) => {