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>
{ robber }
{ 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">
{ playerStolen }
</div>

View File

@ -3082,11 +3082,21 @@ const departLobby = (game, session, color) => {
if (session.name) {
if (session.color) {
addChatMessage(game, null, `${session.name} has disconnected from the game.`);
addChatMessage(game, null, `${session.name} has disconnected ` +
`from the game.`);
} else {
addChatMessage(game, null, `${session.name} has left the lobby.`);
}
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);
@ -3154,11 +3164,13 @@ const sendUpdateToPlayers = async (game, update) => {
const _session = game.sessions[key];
/* Only send player and game data to named players */
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: "" });
}
if (!_session.ws) {
console.log(`${_session.id}: -> sendUpdateToPlayers: Currently no connection.`);
console.log(`${_session.id}: -> sendUpdateToPlayers: ` +
`Currently no connection.`);
} else {
_session.ws.send(message);
}
@ -3435,6 +3447,8 @@ router.ws("/ws/:id", async (ws, req) => {
});
ws.on('close', async (event) => {
console.log(`${short} - closed connection`);
const game = await loadGame(gameId);
if (!game) {
return;
@ -3456,7 +3470,37 @@ router.ws("/ws/:id", async (ws, req) => {
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) => {