-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
987 lines (833 loc) · 33.1 KB
/
server.js
File metadata and controls
987 lines (833 loc) · 33.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
const logger = require('./server/utils/logger');
logger.info('Starting Planning Poker server...');
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const { v4: uuidv4 } = require('uuid');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
logger.info('Dependencies loaded successfully');
logger.info('Creating Express app and HTTP server...');
const app = express();
const server = http.createServer(app);
logger.info('Configuring Socket.IO...');
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
},
pingTimeout: 60000, // 1 minute - faster detection of dead connections
pingInterval: 15000, // 15 seconds - more frequent health checks
connectionStateRecovery: {
maxDisconnectionDuration: 10 * 60 * 1000, // 10 minutes - extended recovery window
skipMiddlewares: true,
},
transports: ['websocket', 'polling'], // WebSocket with polling fallback
upgrade: true, // Allow transport upgrades
rememberUpgrade: true, // Remember transport preference
serveClient: true, // Serve client files
allowEIO3: true // Backward compatibility
});
logger.info('Socket.IO configured successfully');
logger.info('Configuring Express middleware...');
app.use(cors());
app.use(express.json());
// Serve static files from the React app build directory
const staticPath = path.join(__dirname, 'client/build');
logger.info(`Serving static files from: ${staticPath}`);
app.use(express.static(staticPath));
logger.info('Express middleware configured successfully');
// Store active games
const games = new Map();
// Track active connections and their game associations
const activeConnections = new Map(); // socketId -> { gameId, playerName, isWatcher, lastSeen }
const connectionHealth = new Map(); // socketId -> { status, lastPing, latency, disconnectCount, reconnectCount }
const connectionMetrics = {
totalConnections: 0,
totalDisconnections: 0,
totalReconnections: 0,
disconnectReasons: new Map(),
startTime: new Date()
};
// File persistence configuration
logger.info('Setting up file persistence...');
const DATA_DIR = path.join(__dirname, 'data');
const GAMES_FILE = path.join(DATA_DIR, 'games.json');
logger.debug(`Data directory: ${DATA_DIR}`);
logger.debug(`Games file: ${GAMES_FILE}`);
// Ensure data directory exists
try {
if (!fs.existsSync(DATA_DIR)) {
logger.info(`Creating data directory: ${DATA_DIR}`);
fs.mkdirSync(DATA_DIR, { recursive: true });
logger.info('✅ Data directory created successfully');
} else {
logger.info('✅ Data directory already exists');
}
// Check if we can write to the data directory
const testFile = path.join(DATA_DIR, '.write-test');
try {
fs.writeFileSync(testFile, 'test');
fs.unlinkSync(testFile);
logger.info('✅ Data directory is writable');
} catch (writeError) {
logger.error('❌ ERROR: Cannot write to data directory:', writeError.message);
logger.error('❌ This will cause game saving to fail!');
}
// Check if games file exists and is readable
if (fs.existsSync(GAMES_FILE)) {
try {
fs.accessSync(GAMES_FILE, fs.constants.R_OK);
console.log(`[${new Date().toISOString()}] ✅ Games file exists and is readable`);
} catch (readError) {
console.error(`[${new Date().toISOString()}] ❌ ERROR: Games file exists but is not readable:`, readError.message);
}
} else {
logger.info('ℹ️ Games file does not exist yet (will be created on first save)');
}
} catch (error) {
logger.error('❌ CRITICAL ERROR: Failed to set up data directory:', error.message);
logger.error('❌ Game persistence will not work!');
logger.error('❌ Error details:', error);
}
// Card deck configurations
const CARD_DECKS = {
fibonacci: {
name: 'Fibonacci',
cards: ['0', '1', '2', '3', '5', '8', '13', '21', '34', '55', '∞', '?', '☕']
},
tshirt: {
name: 'T-Shirt Sizing',
cards: ['XS', 'S', 'M', 'L', 'XL', 'XXL', '?', '☕']
},
powersOf2: {
name: 'Powers of 2',
cards: ['0', '1', '2', '4', '8', '16', '32', '?', '☕']
},
linear: {
name: 'Linear (1-10)',
cards: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '?', '☕']
}
};
// Game state management
class Game {
constructor(id, deckType = 'fibonacci') {
this.id = id;
this.deckType = deckType;
this.players = new Map();
this.votes = new Map();
this.revealed = false;
this.deck = CARD_DECKS[deckType] || CARD_DECKS.fibonacci;
this.customDeck = null; // Store custom deck data
this.createdAt = new Date();
}
addPlayer(socketId, name, isWatcher = false) {
// Check if player already exists and update their info
const existingPlayer = this.players.get(socketId);
if (existingPlayer) {
existingPlayer.name = name || existingPlayer.name;
existingPlayer.isWatcher = isWatcher;
existingPlayer.lastSeen = new Date();
return existingPlayer;
}
this.players.set(socketId, {
id: socketId,
name: name || `Player ${this.players.size + 1}`,
isWatcher: isWatcher,
hasVoted: false,
lastSeen: new Date()
});
return this.players.get(socketId);
}
removePlayer(socketId) {
this.players.delete(socketId);
this.votes.delete(socketId);
}
castVote(socketId, card) {
if (this.revealed) return false;
const player = this.players.get(socketId);
if (!player || player.isWatcher) return false;
this.votes.set(socketId, card);
player.hasVoted = true;
return true;
}
revealVotes() {
this.revealed = true;
return true;
}
resetGame() {
this.votes.clear();
this.revealed = false;
this.players.forEach(player => {
player.hasVoted = false;
});
}
createCustomDeck(name, cards) {
this.customDeck = {
name: name.trim(),
cards: cards.filter(card => card.trim() !== '') // Remove empty cards
};
this.deckType = 'custom';
this.deck = this.customDeck;
this.resetGame(); // Reset votes when changing deck
}
editCustomDeck(name, cards) {
if (this.customDeck) {
this.customDeck.name = name.trim();
this.customDeck.cards = cards.filter(card => card.trim() !== '');
this.deck = this.customDeck;
this.resetGame(); // Reset votes when editing deck
}
}
getGameState() {
const nonWatcherPlayers = Array.from(this.players.values()).filter(p => !p.isWatcher);
const hasAnyVotes = this.votes.size > 0;
return {
id: this.id,
deckType: this.deckType,
deck: this.deck,
customDeck: this.customDeck,
players: Array.from(this.players.values()),
votes: this.revealed ? Object.fromEntries(this.votes) : {},
revealed: this.revealed,
allVoted: Array.from(this.players.values()).every(p => p.isWatcher || p.hasVoted),
canReveal: hasAnyVotes && !this.revealed
};
}
serialize() {
return {
id: this.id,
deckType: this.deckType,
deck: this.deck,
customDeck: this.customDeck,
players: Array.from(this.players.entries()),
votes: Array.from(this.votes.entries()),
revealed: this.revealed,
createdAt: this.createdAt,
lastActivity: this.lastActivity || this.createdAt
};
}
static deserialize(data) {
const game = new Game(data.id, data.deckType);
game.deck = data.deck;
game.customDeck = data.customDeck;
game.players = new Map(data.players);
game.votes = new Map(data.votes);
game.revealed = data.revealed;
game.createdAt = new Date(data.createdAt);
game.lastActivity = new Date(data.lastActivity);
return game;
}
}
// File persistence functions
let saveTimeout = null;
const SAVE_DEBOUNCE_MS = 1000; // 1 second debounce
function saveGames() {
// Clear existing timeout
if (saveTimeout) {
clearTimeout(saveTimeout);
}
// Set new timeout
saveTimeout = setTimeout(() => {
saveGamesImmediate();
}, SAVE_DEBOUNCE_MS);
}
function loadGames() {
try {
if (fs.existsSync(GAMES_FILE)) {
logger.debug(`Reading games file: ${GAMES_FILE}`);
const data = JSON.parse(fs.readFileSync(GAMES_FILE, 'utf8'));
logger.debug(`Parsed ${data.length} games from file`);
data.forEach((gameData, index) => {
try {
const game = Game.deserialize(gameData);
games.set(game.id, game);
} catch (gameError) {
logger.warn(`⚠️ WARNING: Failed to deserialize game at index ${index}:`, gameError.message);
logger.warn('⚠️ Skipping corrupted game data');
}
});
logger.info(`✅ Successfully loaded ${games.size} games from disk`);
} else {
logger.info('ℹ️ No games file found, starting with empty game list');
}
} catch (error) {
logger.error('❌ CRITICAL ERROR loading games:', error.message);
logger.error('❌ Error details:', error);
logger.error('❌ Continuing with empty game list');
}
}
function saveGamesImmediate() {
try {
logger.debug(`Saving ${games.size} games to disk...`);
const gamesData = Array.from(games.values()).map(game => game.serialize());
const jsonData = JSON.stringify(gamesData, null, 2);
// Check if data directory is still writable
if (!fs.existsSync(DATA_DIR)) {
logger.error(`❌ ERROR: Data directory no longer exists: ${DATA_DIR}`);
return;
}
fs.writeFileSync(GAMES_FILE, jsonData);
logger.debug(`✅ Successfully saved ${games.size} games to disk`);
} catch (error) {
logger.error('❌ ERROR saving games:', error.message);
logger.error('❌ Error code:', error.code);
logger.error('❌ Error details:', error);
// Provide specific guidance based on error type
if (error.code === 'EACCES') {
logger.error('❌ Permission denied - check file/directory permissions');
} else if (error.code === 'ENOSPC') {
logger.error('❌ No space left on device');
} else if (error.code === 'ENOENT') {
logger.error('❌ Directory or file not found');
}
}
}
// Load games on startup
logger.info('Loading games from disk...');
loadGames();
// Save games every 5 minutes
setInterval(() => {
saveGamesImmediate();
}, 5 * 60 * 1000); // 5 minutes
// Performance monitoring every 5 minutes
setInterval(() => {
const memUsage = process.memoryUsage();
const memUsageMB = {
rss: Math.round(memUsage.rss / 1024 / 1024),
heapTotal: Math.round(memUsage.heapTotal / 1024 / 1024),
heapUsed: Math.round(memUsage.heapUsed / 1024 / 1024),
external: Math.round(memUsage.external / 1024 / 1024)
};
// Count unhealthy connections
const now = Date.now();
let unhealthyConnections = 0;
let highLatencyConnections = 0;
connectionHealth.forEach((health, socketId) => {
if (now - health.lastPing > 60000) { // 1 minute without ping
health.status = 'unhealthy';
unhealthyConnections++;
}
if (health.latency > 1000) { // High latency connections
highLatencyConnections++;
}
});
// Calculate uptime
const uptime = Math.round((Date.now() - connectionMetrics.startTime.getTime()) / 1000 / 60);
// Log disconnect reasons summary
const disconnectReasonsSummary = Array.from(connectionMetrics.disconnectReasons.entries())
.map(([reason, count]) => `${reason}: ${count}`)
.join(', ');
logger.info(`[METRICS] Performance stats: Active games: ${games.size}, Active connections: ${activeConnections.size}, Unhealthy: ${unhealthyConnections}, High latency: ${highLatencyConnections}, Memory: ${memUsageMB.heapUsed}MB/${memUsageMB.heapTotal}MB heap, ${memUsageMB.rss}MB RSS, Uptime: ${uptime}min`);
logger.info(`[METRICS] Connection stats: Total connections: ${connectionMetrics.totalConnections}, Total disconnections: ${connectionMetrics.totalDisconnections}, Total reconnections: ${connectionMetrics.totalReconnections}`);
logger.info(`[METRICS] Disconnect reasons: ${disconnectReasonsSummary}`);
}, 5 * 60 * 1000); // 5 minutes
// Save games on server shutdown
process.on('SIGINT', () => {
logger.info('Server shutting down, saving games...');
// Clear any pending save timeout and save immediately
if (saveTimeout) {
clearTimeout(saveTimeout);
}
saveGamesImmediate();
process.exit(0);
});
process.on('SIGTERM', () => {
logger.info('Server shutting down, saving games...');
// Clear any pending save timeout and save immediately
if (saveTimeout) {
clearTimeout(saveTimeout);
}
saveGamesImmediate();
process.exit(0);
});
// Cleanup stale connections periodically
setInterval(() => {
const now = new Date();
const staleThreshold = 3 * 60 * 1000; // 3 minutes - faster cleanup for better stability
let staleCount = 0;
activeConnections.forEach((connection, socketId) => {
if (now - connection.lastSeen > staleThreshold) {
staleCount++;
logger.debug(`[CLEANUP] Cleaning up stale connection: ${socketId}, Last seen: ${connection.lastSeen.toISOString()}, Player: ${connection.playerName}, Game: ${connection.gameId}`);
const game = games.get(connection.gameId);
if (game) {
game.removePlayer(socketId);
game.lastActivity = new Date(); // Update last activity
if (game.players.size === 0) {
logger.debug(`[CLEANUP] Game ${connection.gameId} is now empty, will expire in 24 hours (stale cleanup)`);
} else {
io.to(connection.gameId).emit('player-left', game.getGameState());
}
}
activeConnections.delete(socketId);
connectionHealth.delete(socketId);
}
});
if (staleCount > 0) {
logger.info(`[CLEANUP] Removed ${staleCount} stale connections, Active connections: ${activeConnections.size}`);
}
}, 30000); // Check every 30 seconds - more frequent cleanup
// Cleanup expired games (24 hours) every hour
setInterval(() => {
const now = new Date();
const expiredGames = [];
games.forEach((game, gameId) => {
const timeSinceLastActivity = now - game.lastActivity;
if (timeSinceLastActivity > 24 * 60 * 60 * 1000) { // 24 hours
expiredGames.push(gameId);
}
});
expiredGames.forEach(gameId => {
games.delete(gameId);
logger.info(`Game ${gameId} expired and deleted (24+ hours old)`);
});
if (expiredGames.length > 0) {
saveGamesImmediate(); // Save after cleanup
}
}, 60 * 60 * 1000); // Check every hour
// Socket.io connection handling
logger.info('Setting up Socket.IO event handlers...');
io.on('connection', (socket) => {
connectionMetrics.totalConnections++;
logger.info(`[CONNECT] User connected: ${socket.id}, Total connections: ${connectionMetrics.totalConnections}, Active connections: ${activeConnections.size + 1}`);
// Track connection
activeConnections.set(socket.id, {
gameId: null,
playerName: null,
isWatcher: false,
lastSeen: new Date(),
connectedAt: new Date(),
transport: socket.conn.transport.name
});
// Initialize connection health tracking
connectionHealth.set(socket.id, {
status: 'healthy',
lastPing: Date.now(),
latency: 0,
disconnectCount: 0,
reconnectCount: 0,
transport: socket.conn.transport.name
});
logger.debug(`[CONNECT] Connection details: ${socket.id}, Transport: ${socket.conn.transport.name}`);
socket.on('create-game', (data) => {
const gameId = uuidv4().substring(0, 8);
const game = new Game(gameId, data.deckType || 'fibonacci');
game.lastActivity = new Date();
games.set(gameId, game);
game.addPlayer(socket.id, data.playerName, data.isWatcher);
socket.join(gameId);
// Update connection tracking
const connection = activeConnections.get(socket.id);
if (connection) {
connection.gameId = gameId;
connection.playerName = data.playerName;
connection.isWatcher = data.isWatcher;
connection.lastSeen = new Date();
}
// Save game to disk immediately after creation
saveGames();
logger.info(`Game created and saved to disk: ${gameId}, Total games: ${games.size}`);
socket.emit('game-created', { gameId, game: game.getGameState() });
});
socket.on('join-game', (data) => {
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Join game failed: ${socket.id}, Game not found: ${data.gameId}`);
socket.emit('error', { message: 'Game not found.' });
return;
}
// Update last activity when someone joins
game.lastActivity = new Date();
// Check if player is already in the game (reconnection scenario)
const existingPlayer = game.players.get(socket.id);
let playerAdded = false;
if (existingPlayer) {
// Update existing player info and last seen
existingPlayer.name = data.playerName || existingPlayer.name;
// Only update isWatcher if explicitly provided and different from current state
// This prevents reconnections from accidentally changing player roles
if (data.isWatcher !== undefined && data.isWatcher !== existingPlayer.isWatcher) {
existingPlayer.isWatcher = data.isWatcher;
logger.info(`Player role updated on reconnection: ${socket.id}, Player: ${existingPlayer.name}, New role: ${data.isWatcher ? 'Watcher' : 'Player'}, Game: ${data.gameId}`);
}
existingPlayer.lastSeen = new Date();
// Preserve vote status if player already voted and vote still exists
if (game.votes.has(socket.id)) {
existingPlayer.hasVoted = true;
logger.info(`Player reconnected with preserved vote: ${socket.id}, Player: ${existingPlayer.name}, Game: ${data.gameId}`);
} else {
existingPlayer.hasVoted = false;
logger.info(`Player reconnected without vote: ${socket.id}, Player: ${existingPlayer.name}, Game: ${data.gameId}`);
}
} else {
game.addPlayer(socket.id, data.playerName, data.isWatcher);
playerAdded = true;
logger.info(`New player joined: ${socket.id}, Player: ${data.playerName}, Game: ${data.gameId}`);
}
socket.join(data.gameId);
// Update connection tracking
const connection = activeConnections.get(socket.id);
if (connection) {
connection.gameId = data.gameId;
connection.playerName = data.playerName;
connection.isWatcher = data.isWatcher;
connection.lastSeen = new Date();
}
// Only save and emit if this was a new player join (not a reconnection)
if (playerAdded) {
// Save game to disk immediately after new player joins
saveGames();
logger.info(`Player joined and game saved to disk: ${data.gameId}: ${data.playerName}, Players in game: ${game.players.size}`);
io.to(data.gameId).emit('player-joined', game.getGameState());
} else {
// For reconnections, just send the current game state without triggering save
socket.emit('game-state', game.getGameState());
}
});
// Heartbeat mechanism with health tracking
socket.on('ping', () => {
const connection = activeConnections.get(socket.id);
if (connection) {
connection.lastSeen = new Date();
}
// Track connection health
const now = Date.now();
const health = connectionHealth.get(socket.id) || { status: 'healthy', lastPing: now, latency: 0, disconnectCount: 0, reconnectCount: 0 };
const previousPing = health.lastPing;
health.lastPing = now;
health.status = 'healthy';
// Calculate latency if we have a previous ping
if (previousPing && previousPing !== now) {
health.latency = now - previousPing;
}
connectionHealth.set(socket.id, health);
// Log high latency connections
if (health.latency > 2000) { // Log if latency > 2 seconds
logger.warn(`[HEARTBEAT] High latency detected: ${socket.id}, Latency: ${health.latency}ms, Player: ${connection?.playerName || 'Unknown'}, Game: ${connection?.gameId || 'None'}`);
}
socket.emit('pong');
});
// Socket error handling
socket.on('error', (error) => {
logger.error(`[ERROR] Socket error: ${socket.id}, Error: ${error.message}`);
});
socket.on('cast-vote', (data) => {
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Vote cast failed: ${socket.id}, Game not found: ${data.gameId}`);
return;
}
// Update last seen
const connection = activeConnections.get(socket.id);
if (connection) {
connection.lastSeen = new Date();
}
const success = game.castVote(socket.id, data.card);
if (success) {
game.lastActivity = new Date(); // Update last activity
const player = game.players.get(socket.id);
const playerName = player ? player.name : 'Unknown';
logger.info(`Vote cast: ${socket.id}, Player: ${playerName}, Card: ${data.card}, Game: ${data.gameId}`);
// Check if all players have voted
const gameState = game.getGameState();
if (gameState.allVoted) {
logger.info(`All players voted: Game ${data.gameId}, ${game.votes.size} votes`);
}
// Save game to disk immediately after vote is cast
saveGames();
io.to(data.gameId).emit('vote-cast', game.getGameState());
} else {
const player = game.players.get(socket.id);
const playerName = player ? player.name : 'Unknown';
logger.warn(`[WARN] Invalid vote attempt: ${socket.id}, Player: ${playerName}, Card: ${data.card}, Game: ${data.gameId}`);
}
});
socket.on('reveal-votes', (data) => {
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Reveal votes failed: ${socket.id}, Game not found: ${data.gameId}`);
return;
}
game.revealVotes();
game.lastActivity = new Date(); // Update last activity
logger.info(`Votes revealed: Game ${data.gameId}, ${game.votes.size} votes`);
// Check for consensus (all votes are the same)
const votes = Array.from(game.votes.values());
if (votes.length > 1) {
const firstVote = votes[0];
const allSame = votes.every(vote => vote === firstVote);
if (allSame) {
logger.info(`Consensus reached: Game ${data.gameId}, Card: ${firstVote}, Players: ${votes.length}`);
}
}
// Save game to disk immediately after votes are revealed
saveGames();
io.to(data.gameId).emit('votes-revealed', game.getGameState());
});
socket.on('reset-game', (data) => {
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Game reset failed: ${socket.id}, Game not found: ${data.gameId}`);
return;
}
game.resetGame();
game.lastActivity = new Date(); // Update last activity
logger.info(`Game reset: Game ${data.gameId}`);
// Save game to disk immediately after game is reset
saveGames();
io.to(data.gameId).emit('game-reset', game.getGameState());
});
socket.on('change-deck', (data) => {
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Deck change failed: ${socket.id}, Game not found: ${data.gameId}`);
return;
}
if (data.deckType === 'custom' && game.customDeck) {
game.deckType = 'custom';
game.deck = game.customDeck;
} else {
game.deckType = data.deckType;
game.deck = CARD_DECKS[data.deckType] || CARD_DECKS.fibonacci;
}
game.resetGame();
game.lastActivity = new Date(); // Update last activity
logger.info(`Deck changed: Game ${data.gameId}, New deck: ${data.deckType}`);
// Save game to disk immediately after deck is changed
saveGames();
io.to(data.gameId).emit('deck-changed', game.getGameState());
});
socket.on('create-custom-deck', (data) => {
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Custom deck creation failed: ${socket.id}, Game not found: ${data.gameId}`);
return;
}
game.createCustomDeck(data.name, data.cards);
game.lastActivity = new Date(); // Update last activity
logger.info(`Custom deck created: Game ${data.gameId}, Deck: ${data.name}, Cards: ${data.cards.length}`);
// Save game to disk immediately after custom deck is created
saveGames();
io.to(data.gameId).emit('custom-deck-created', game.getGameState());
});
socket.on('edit-custom-deck', (data) => {
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Custom deck edit failed: ${socket.id}, Game not found: ${data.gameId}`);
return;
}
game.editCustomDeck(data.name, data.cards);
game.lastActivity = new Date(); // Update last activity
logger.info(`Custom deck edited: Game ${data.gameId}, Deck: ${data.name}, Cards: ${data.cards.length}`);
// Save game to disk immediately after custom deck is edited
saveGames();
io.to(data.gameId).emit('custom-deck-edited', game.getGameState());
});
socket.on('change-player-name', (data) => {
logger.debug(`[SERVER] Received change-player-name event:`, data);
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Player name change failed: ${socket.id}, Game not found: ${data.gameId}`);
socket.emit('error', { message: 'Game not found.' });
return;
}
const player = game.players.get(socket.id);
if (!player) {
logger.error(`[ERROR] Player name change failed: ${socket.id}, Player not found in game: ${data.gameId}`);
socket.emit('error', { message: 'You are not in this game.' });
return;
}
// Validate the new name
if (!data.newName || typeof data.newName !== 'string') {
socket.emit('error', { message: 'Invalid name provided.' });
return;
}
const trimmedName = data.newName.trim();
if (trimmedName.length === 0 || trimmedName.length > 20) {
socket.emit('error', { message: 'Name must be between 1 and 20 characters.' });
return;
}
// Update the player's name
const oldName = player.name;
player.name = trimmedName;
player.lastSeen = new Date();
game.lastActivity = new Date();
// Update connection tracking
const connection = activeConnections.get(socket.id);
if (connection) {
connection.playerName = trimmedName;
connection.lastSeen = new Date();
}
// Save game to disk
saveGames();
logger.info(`Player name changed: ${socket.id}, Old name: ${oldName}, New name: ${trimmedName}, Game: ${data.gameId}`);
// Notify all players in the game
io.to(data.gameId).emit('player-name-changed', game.getGameState());
});
socket.on('toggle-role', (data) => {
const game = games.get(data.gameId);
if (!game) {
logger.error(`[ERROR] Role toggle failed: ${socket.id}, Game not found: ${data.gameId}`);
return;
}
const player = game.players.get(socket.id);
if (!player) {
logger.error(`[ERROR] Role toggle failed: ${socket.id}, Player not found in game: ${data.gameId}`);
return;
}
// Log current state before toggle
console.log(`[${new Date().toISOString()}] Role toggle request: ${socket.id}, Player: ${player.name}, Current role: ${player.isWatcher ? 'Watcher' : 'Player'}, Game: ${data.gameId}`);
// Toggle role
player.isWatcher = !player.isWatcher;
// If switching to watcher, remove their vote
if (player.isWatcher) {
game.votes.delete(socket.id);
player.hasVoted = false;
}
game.lastActivity = new Date(); // Update last activity
console.log(`[${new Date().toISOString()}] Role toggled: ${socket.id}, Player: ${player.name}, New role: ${player.isWatcher ? 'Watcher' : 'Player'}, Game: ${data.gameId}`);
// Save game to disk immediately after role is toggled
saveGames();
io.to(data.gameId).emit('role-toggled', game.getGameState());
});
socket.on('leave-game', (data) => {
const game = games.get(data.gameId);
if (!game) {
console.log(`[${new Date().toISOString()}] [ERROR] Leave game failed: ${socket.id}, Game not found: ${data.gameId}`);
return;
}
// Remove player from game
game.removePlayer(socket.id);
socket.leave(data.gameId);
// Update last activity instead of deleting immediately
game.lastActivity = new Date();
if (game.players.size === 0) {
console.log(`[${new Date().toISOString()}] Game ${data.gameId} is now empty, will expire in 24 hours`);
} else {
// Notify remaining players
const player = game.players.get(socket.id);
const playerName = player ? player.name : 'Unknown';
console.log(`[${new Date().toISOString()}] Player left: ${socket.id}, Player: ${playerName}, Game: ${data.gameId}`);
io.to(data.gameId).emit('player-left', game.getGameState());
}
});
socket.on('disconnect', (reason) => {
connectionMetrics.totalDisconnections++;
// Track disconnect reasons
const currentCount = connectionMetrics.disconnectReasons.get(reason) || 0;
connectionMetrics.disconnectReasons.set(reason, currentCount + 1);
const connection = activeConnections.get(socket.id);
const health = connectionHealth.get(socket.id);
const playerInfo = connection ?
`Player: ${connection.playerName}, Game: ${connection.gameId}` :
'Unknown player';
// Calculate connection duration
const connectionDuration = connection ?
Math.round((Date.now() - connection.connectedAt.getTime()) / 1000) : 0;
console.log(`[${new Date().toISOString()}] [DISCONNECT] User disconnected: ${socket.id}, Reason: ${reason}, ${playerInfo}, Connection duration: ${connectionDuration}s, Transport: ${health?.transport || 'unknown'}`);
// Log if this was a problematic disconnect
if (reason === 'ping timeout' || reason === 'transport close') {
console.log(`[${new Date().toISOString()}] [DISCONNECT] Problematic disconnect detected: ${socket.id}, Reason: ${reason}, Last ping: ${health?.lastPing ? new Date(health.lastPing).toISOString() : 'never'}, Latency: ${health?.latency || 0}ms`);
}
// Clean up connection tracking
activeConnections.delete(socket.id);
connectionHealth.delete(socket.id);
// Find and remove player from all games
games.forEach((game, currentGameId) => {
if (game.players.has(socket.id)) {
game.removePlayer(socket.id);
game.lastActivity = new Date(); // Update last activity
if (game.players.size === 0) {
console.log(`[${new Date().toISOString()}] [DISCONNECT] Game ${currentGameId} is now empty, will expire in 24 hours`);
} else {
console.log(`[${new Date().toISOString()}] [DISCONNECT] Notifying remaining players in game ${currentGameId}, Players remaining: ${game.players.size}`);
io.to(currentGameId).emit('player-left', game.getGameState());
}
}
});
});
});
// API routes
app.get('/api/decks', (req, res) => {
res.json(CARD_DECKS);
});
// Read version once at startup for security
const APP_VERSION = require('./package.json').version;
app.get('/api/version', (req, res) => {
// Return pre-loaded version (no file system access at runtime)
res.json({ version: APP_VERSION });
});
app.get('/api/game/:gameId', (req, res) => {
const game = games.get(req.params.gameId);
if (!game) {
return res.status(404).json({ error: 'Game not found.' });
}
res.json(game.getGameState());
});
// Connection health monitoring endpoint
app.get('/api/health', (req, res) => {
const now = Date.now();
let healthyConnections = 0;
let unhealthyConnections = 0;
let highLatencyConnections = 0;
let totalLatency = 0;
let latencyCount = 0;
connectionHealth.forEach((health, socketId) => {
if (now - health.lastPing > 60000) { // 1 minute without ping
unhealthyConnections++;
} else {
healthyConnections++;
}
if (health.latency > 1000) { // High latency connections
highLatencyConnections++;
}
if (health.latency > 0) {
totalLatency += health.latency;
latencyCount++;
}
});
const avgLatency = latencyCount > 0 ? Math.round(totalLatency / latencyCount) : 0;
const uptime = Math.round((Date.now() - connectionMetrics.startTime.getTime()) / 1000);
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: uptime,
connections: {
total: activeConnections.size,
healthy: healthyConnections,
unhealthy: unhealthyConnections,
highLatency: highLatencyConnections
},
games: {
total: games.size,
active: Array.from(games.values()).filter(game => game.players.size > 0).length
},
metrics: {
totalConnections: connectionMetrics.totalConnections,
totalDisconnections: connectionMetrics.totalDisconnections,
totalReconnections: connectionMetrics.totalReconnections,
avgLatency: avgLatency,
disconnectReasons: Object.fromEntries(connectionMetrics.disconnectReasons)
},
memory: {
used: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
total: Math.round(process.memoryUsage().heapTotal / 1024 / 1024),
rss: Math.round(process.memoryUsage().rss / 1024 / 1024)
}
});
});
// Serve React app for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
const PORT = process.env.PORT || 3001;
logger.info(`Starting server on port ${PORT}...`);
server.on('error', (error) => {
console.error(`[${new Date().toISOString()}] ❌ Server error:`, error);
});
server.listen(PORT, () => {
logger.info(`✅ Server successfully started and running on port ${PORT}`);
logger.info('✅ Planning Poker server is ready to accept connections');
});