remove players on disconnect

This commit is contained in:
Gavin McDonald
2025-04-21 16:13:30 -04:00
parent 3c986fc7fa
commit 626094cc2c
2 changed files with 22 additions and 4 deletions

View File

@@ -7,10 +7,12 @@ const deck = new Deck();
export default class GameStore {
private dms: Map<string, GameState>;
private spectators: Map<string, GameState>;
private players: Map<string, string>;
constructor() {
this.dms = new Map();
this.spectators = new Map();
this.players = new Map();
}
createGameIDs() {
@@ -61,6 +63,7 @@ export default class GameStore {
game.players.add(playerID);
game.lastUpdated = Date.now();
this.players.set(playerID, gameID);
return this.gameUpdate(game);
}
@@ -108,6 +111,15 @@ export default class GameStore {
return { dmID, spectatorID, cards, settings };
}
playerExit(playerID: string): GameState {
const gameID = this.players.get(playerID);
if (!gameID) throw new Error(`Player ${playerID} not found`);
this.players.delete(playerID);
return this.leaveGame(gameID, playerID);
}
deleteGame(gameID: string): void {
this.dms.delete(gameID);
this.spectators.delete(gameID);

View File

@@ -52,7 +52,7 @@ app.prepare().then(() => {
} catch (e) {
const error = e instanceof Error ? e.message : e;
console.error(Date.now(), 'Error', error);
console.error(Date.now(), 'Error[join]', error);
socket.emit('join-error', error);
}
});
@@ -67,7 +67,7 @@ app.prepare().then(() => {
} catch (e) {
const error = e instanceof Error ? e.message : e;
console.error(Date.now(), 'Error', error);
console.error(Date.now(), 'Error[flip-card]', error);
socket.emit('flip-error', error);
}
});
@@ -78,12 +78,18 @@ app.prepare().then(() => {
broadcast('game-update', gameUpdate);
} catch (e) {
const error = e instanceof Error ? e.message : e;
console.error(Date.now(), 'Error', error);
console.error(Date.now(), 'Error[settings]', error);
}
});
socket.on('disconnect', () => {
console.log(Date.now(), `Client disconnected: ${socket.id}`);
try {
const { dmID } = gameStore.playerExit(socket.id);
console.log(Date.now(), `Client disconnected: ${socket.id} from ${dmID}`);
} catch (e) {
const error = e instanceof Error ? e.message : e;
console.error(Date.now(), 'Error[disconnect]', error);
}
});
});