the landing page opens a socket, disconnecting should not throw a 'not found' error

This commit is contained in:
Gavin McDonald
2025-04-22 09:13:59 -04:00
parent 2e2399a58b
commit cc62e3d06b
2 changed files with 26 additions and 14 deletions

View File

@@ -34,6 +34,7 @@ export default class GameStore {
private totalExpired: number; private totalExpired: number;
private totalUnused: number; private totalUnused: number;
private startUps: Set<string>;
private dms: Map<string, GameState>; private dms: Map<string, GameState>;
private spectators: Map<string, GameState>; private spectators: Map<string, GameState>;
private players: Map<string, string>; private players: Map<string, string>;
@@ -44,6 +45,7 @@ export default class GameStore {
this.totalExpired = 0; this.totalExpired = 0;
this.totalUnused = 0; this.totalUnused = 0;
this.startUps = new Set();
this.dms = new Map(); this.dms = new Map();
this.spectators = new Map(); this.spectators = new Map();
this.players = new Map(); this.players = new Map();
@@ -73,7 +75,7 @@ export default class GameStore {
}; };
} }
createGame(): GameState { createGame(startUpID: string): GameState {
const { dmID, spectatorID } = this.createGameIDs(); const { dmID, spectatorID } = this.createGameIDs();
const newGame: GameState = { const newGame: GameState = {
@@ -92,6 +94,7 @@ export default class GameStore {
}; };
this.totalCreated++; this.totalCreated++;
this.startUps.add(startUpID);
this.dms.set(dmID, newGame); this.dms.set(dmID, newGame);
this.spectators.set(spectatorID, newGame); this.spectators.set(spectatorID, newGame);
@@ -151,13 +154,19 @@ export default class GameStore {
return { dmID, spectatorID, cards, settings }; return { dmID, spectatorID, cards, settings };
} }
playerExit(playerID: string): GameState { playerExit(playerID: string): GameState | null {
const gameID = this.players.get(playerID); if (this.startUps.has(playerID)) {
this.startUps.delete(playerID);
if (!gameID) throw new Error(`Player ${playerID} not found`); return null;
} else {
const gameID = this.players.get(playerID);
this.players.delete(playerID); if (!gameID) throw new Error(`Player ${playerID} not found`);
return this.leaveGame(gameID, playerID);
this.players.delete(playerID);
return this.leaveGame(gameID, playerID);
}
} }
log() { log() {

View File

@@ -29,7 +29,7 @@ app.prepare().then(() => {
console.log(Date.now(), `Client connected: ${socket.id}`); console.log(Date.now(), `Client connected: ${socket.id}`);
socket.on('start', () => { socket.on('start', () => {
const gameUpdate = gameStore.createGame(); const gameUpdate = gameStore.createGame(socket.id);
console.log(Date.now(), `Socket ${socket.id} started game ${gameUpdate.dmID}`); console.log(Date.now(), `Socket ${socket.id} started game ${gameUpdate.dmID}`);
@@ -44,10 +44,7 @@ app.prepare().then(() => {
? socket.handshake.headers['x-forwarded-for'][0] ? socket.handshake.headers['x-forwarded-for'][0]
: socket.handshake.headers['x-forwarded-for']?.split(',')[0]; : socket.handshake.headers['x-forwarded-for']?.split(',')[0];
console.log(Date.now(), `Socket ${socket.id} joined game ${gameID}`); console.log(Date.now(), `Socket ${socket.id}[${ipAddress}] joined game ${gameID}`);
console.log('x-forwarded-for', socket.handshake.headers['x-forwarded-for']);
console.log('client IP', ipAddress);
console.log('proxy IP', socket.handshake.address);
socket.join(gameID); socket.join(gameID);
@@ -66,7 +63,7 @@ app.prepare().then(() => {
socket.on('flip-card', ({ gameID, cardIndex }: ClientUpdate) => { socket.on('flip-card', ({ gameID, cardIndex }: ClientUpdate) => {
try { try {
console.log(Date.now(), 'Card flipped:', { gameID, cardIndex }); //console.log(Date.now(), 'Card flipped:', { gameID, cardIndex });
const gameUpdate = gameStore.flipCard(gameID, cardIndex); const gameUpdate = gameStore.flipCard(gameID, cardIndex);
@@ -91,8 +88,14 @@ app.prepare().then(() => {
socket.on('disconnect', () => { socket.on('disconnect', () => {
try { try {
const { dmID } = gameStore.playerExit(socket.id); const game = gameStore.playerExit(socket.id);
console.log(Date.now(), `Client disconnected: ${socket.id} from ${dmID}`);
if (game) {
console.log(
Date.now(),
`Client disconnected: ${socket.id} from ${game.dmID}/${game.spectatorID}`,
);
}
} catch (e) { } catch (e) {
const error = e instanceof Error ? e.message : e; const error = e instanceof Error ? e.message : e;
console.error(Date.now(), 'Error[disconnect]', error); console.error(Date.now(), 'Error[disconnect]', error);