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

View File

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