Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | const fs = require('fs'); const path = require('path'); const http = require('http'); const WebSocket = require('ws'); const { Pool } = require('pg'); const allFoods = require('./src/data/food.json').categories.flatMap(c => c.foods); // Constants for game modes and their configurations const MODE_CONFIG = { Easy: { fruitSpeed: 100, allowPenalty: false, allowEffect: false, }, Medium: { fruitSpeed: 125, allowPenalty: true, allowEffect: true, }, Hard: { fruitSpeed: 150, allowPenalty: true, allowEffect: true, }, }; const server = http.createServer(); const wss = new WebSocket.Server({ noServer: true }); const sessions = {}; const reconnectionTimers = {}; const sessionFilePath = path.resolve(__dirname, './src/data/sessionID.json'); const scoresBySession = {}; const fruitQueues = {}; const fruitIntervals = {}; const TARGET_FOOD_WEIGHT = 16; // Weight for the target food in the queue const sessionGameModes = {}; let foodInstanceCounter = 0 const activeFoods = {}; const lastSpawnAt = {}; const QUEUE_MAX = 10; // Reject connections from unauthorized origins const allowedOrigins = [ 'http://localhost:3000', 'http://localhost:3001', 'http://localhost:3002', 'http://localhost:3003', 'http://localhost:3004', 'http://localhost:3005', 'https://project-acc-hungry-hippos.vercel.app' ]; server.on('upgrade', (request, socket, head) => { const origin = request.headers.origin; if (!allowedOrigins.includes(origin)) { console.log(`[WSS] Connection from unauthorized origin ${origin} rejected.`); socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); socket.destroy(); return; } wss.handleUpgrade(request, socket, head, (ws) => { console.log(`[WSS] Connection from ${origin} accepted.`); wss.emit('connection', ws, request); }); }); const IS_PROD = process.env.NODE_ENV === 'production'; let pool; if (IS_PROD) { pool = new Pool({ connectionString: process.env.DATABASE_URL, ssl: { rejectUnauthorized: false } }); } // Runs once to set up the database and tables const setupDatabase = async () => { if (!IS_PROD) return; const client = await pool.connect(); try { // Check if the tables exist, if not create them await client.query(` CREATE TABLE IF NOT EXISTS sessions ( session_id VARCHAR(5) PRIMARY KEY, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); `); // Create a players table to store users in each session await client.query(` CREATE TABLE IF NOT EXISTS players ( id SERIAL PRIMARY KEY, user_id VARCHAR(255) NOT NULL, session_id VARCHAR(5) NOT NULL REFERENCES sessions(session_id) ON DELETE CASCADE, role VARCHAR(25), UNIQUE(session_id, user_id) ); `); // Create a table to store session data await client.query(` CREATE TABLE IF NOT EXISTS game_statistics ( id INT PRIMARY KEY DEFAULT 1, total_sessions_played INT DEFAULT 0, total_hippo_players INT DEFAULT 0, total_aac_users INT DEFAULT 0, hippo_color_counts JSONB DEFAULT '{}'::jsonb, mode_counts JSONB DEFAULT '{}'::jsonb, total_correct_eats INT DEFAULT 0, total_wrong_eats INT DEFAULT 0, aac_food_counts JSONB DEFAULT '{}'::jsonb, aac_verb_counts JSONB DEFAULT '{}'::jsonb, last_updated TIMESTAMPTZ ); `); await client.query(` INSERT INTO game_statistics (id) VALUES (1) ON CONFLICT (id) DO NOTHING; `); console.log('Database setup complete'); } catch (err) { console.error('Error setting up database:', err); } finally { client.release(); } } // Function to get a weighted random food item from the list // This function will give more weight to the target food, making it more likely to be selected function getWeightedRandomFood(allFoods, targetId) { if (!allFoods || allFoods.length === 0) { console.error('Food list is empty or undefined'); return null; } const weightedList = []; for (const food of allFoods) { const weight = food.id === targetId ? TARGET_FOOD_WEIGHT : 1; for (let i = 0; i < weight; i++) { weightedList.push(food); } } return weightedList[Math.floor(Math.random() * weightedList.length)]; } // Websocket Server wss.on('connection', (ws) => { console.log('WSS Client connected'); ws.on('message', async (message) => { try { const data = JSON.parse(message); // console.log('WSS Received:', data); // Validate session request if (data.type === 'VALIDATE_SESSION') { const { gameCode } = data.payload; let isValid = false; let sessionsData = { sessions: {} }; if (!IS_PROD) { // If local development, read from the session file try { if (fs.existsSync(sessionFilePath)) { sessionsData = JSON.parse(fs.readFileSync(sessionFilePath, 'utf-8')); } } catch (e) { console.error('Error reading session file:', e); } isValid = Object.hasOwn(sessionsData.sessions, gameCode); } else { // If in production, check the database try { const result = await pool.query('SELECT EXISTS (SELECT 1 FROM sessions WHERE session_id = $1)', [gameCode]); isValid = result.rows[0].exists; } catch (err) { console.error('Error validating session:', err); } } ws.send(JSON.stringify({ type: 'SESSION_VALIDATED', payload: { isValid, gameCode } })); } // Handle session creation request if (data.type === 'CREATE_SESSION') { // If local development, skip database operations let sessionId; if (!IS_PROD) { let sessionsData = { sessions: {} }; try { if (fs.existsSync(sessionFilePath)) { sessionsData = JSON.parse(fs.readFileSync(sessionFilePath, 'utf-8')); } } catch (e) { console.error('Error reading session file:', e); } sessionId = generateUniqueSessionId(Object.keys(sessionsData.sessions)); sessionsData.sessions[sessionId] = []; fs.writeFileSync(sessionFilePath, JSON.stringify(sessionsData, null, 2), 'utf-8'); } else { // If in production, insert into the database while (true) { sessionId = generateSessionId(); try { await pool.query('INSERT INTO sessions (session_id) VALUES ($1)', [sessionId]); await pool.query('UPDATE game_statistics SET total_sessions_played = total_sessions_played + 1, last_updated = NOW() WHERE id = 1'); break; } catch (err) { console.error('Error creating session:', err); } } } ws.send(JSON.stringify({ type: 'SESSION_CREATED', payload: { sessionId } })); sessions[sessionId] = new Set(); sessions[sessionId].statsLogged = false; } if (data.type === 'PLAYER_MOVE') { const { sessionId, userId, x, y } = data.payload; broadcast(sessionId, { type: 'PLAYER_MOVE_BROADCAST', payload: { userId, x, y } }); } // When a player joins, store their WebSocket connection in the correct session room if (data.type === 'PLAYER_JOIN') { const { sessionId, userId, role, color } = data.payload; ws.sessionId = sessionId; ws.userId = userId; ws.role = role; ws.color = color; if (!sessions[sessionId]) { sendError(ws, { code: 'SESSION_NOT_FOUND', message: `Session ${sessionId} not found`, sessionId, }); return; } sessions[sessionId].add(ws); console.log(`WSS User ${userId} joined session ${sessionId}. Total clients in session: ${sessions[sessionId].size}`); if (!scoresBySession[sessionId]) scoresBySession[sessionId] = {}; if (role === 'Hippo Player' && !scoresBySession[sessionId][userId]) { scoresBySession[sessionId][userId] = 0; } if (IS_PROD) { // If in production, insert the player into the database try { await pool.query(` INSERT INTO players (session_id, user_id, role) VALUES ($1, $2, $3) ON CONFLICT (session_id, user_id) DO UPDATE SET role = EXCLUDED.role`, [sessionId, userId, role]); } catch (err) { console.error('Error adding player to database:', err); } } // Broadcast to all clients in that session that a new player has joined broadcast(sessionId, { type: 'PLAYER_JOINED_BROADCAST', payload: { userId, role, color } }); // Collect all users in the session const usersInSession = Array.from(sessions[sessionId]) .filter(client => client.readyState === WebSocket.OPEN) .map(client => ({ userId: client.userId, role: client.role, color: client.color })); // Send full user list broadcast(sessionId, { type: 'USERS_LIST_UPDATE', payload: { users: usersInSession } }); console.log(`[WSS] Broadcasting USERS_LIST_UPDATE to ${sessionId}:`, usersInSession); // Broadcast initial leaderboard with everyone at score 0 broadcast(sessionId, { type: 'SCORE_UPDATE_BROADCAST', payload: { scores: scoresBySession[sessionId] } }); } // When the presenter clicks "Start Game", broadcast to all clients in the session // to signal that the game has begun. Clients will navigate to the game screen. if (data.type === 'START_GAME') { const { sessionId, mode } = data.payload; console.log(`[WSS] Start game received for session ${sessionId} with mode ${mode}`); if (IS_PROD && sessions[sessionId]) { if (!sessions[sessionId].statsLogged) { try { await pool.query( "DELETE FROM players WHERE session_id = $1 AND role = 'Presenter'", [sessionId] ); await pool.query( `UPDATE game_statistics SET mode_counts = jsonb_set( mode_counts, '{${mode}}', (COALESCE(mode_counts->>'${mode}', '0')::int + 1)::text::jsonb ) WHERE id = 1` ); let hippoCount = 0; let aacCount = 0; for (const client of sessions[sessionId]) { if (client.role === 'Hippo Player') { hippoCount++; } else if (client.role === 'AAC User') { aacCount++; } } if (hippoCount > 0 || aacCount > 0) { await pool.query( `UPDATE game_statistics SET total_hippo_players = total_hippo_players + $1, total_aac_users = total_aac_users + $2, last_updated = NOW() WHERE id = 1`, [hippoCount, aacCount] ); } sessions[sessionId].statsLogged = true; } catch (err) { console.error('[WSS] Error cleaning up presenter role:', err); } } } if (sessions[sessionId]) { sessions[sessionId].gameStarted = true; } // Store mode and reset per-session state sessionGameModes[sessionId] = mode; activeFoods[sessionId] = []; // Seed queue with objects if (!fruitQueues[sessionId]) { // const allFoods = require('./src/data/food.json').categories.flatMap(c => c.foods); // fruitQueues[sessionId] = []; // for (let i = 0; i < 10; i++) { // const food = allFoods[Math.floor(Math.random() * allFoods.length)]; // fruitQueues[sessionId].push(food); // if (fruitQueues[sessionId].length > QUEUE_MAX) { // fruitQueues[sessionId] = fruitQueues[sessionId].slice(0, QUEUE_MAX); // } // } clearFruitQueue(sessionId); for (let i = 0; i < QUEUE_MAX; i++) { const food = allFoods[Math.floor(Math.random() * allFoods.length)]; enqueueFruit(sessionId, food); } } // Target tracking sessions[sessionId].initialTargetSent = false; sessions[sessionId].currentTargetFoodId = null; const SCREEN_WIDTH = 1024; const SCREEN_HEIGHT = 1024; // spawn mark (first spawn after 3s) lastSpawnAt[sessionId] = Date.now(); // 50ms game loop; spawns happen every 3000ms const TICK_INTERVAL = 50; fruitIntervals[sessionId] = setInterval(() => { if (!sessions[sessionId]) { clearInterval(fruitIntervals[sessionId]); delete fruitIntervals[sessionId]; return; } //const allFoods = require('./src/data/food.json').categories.flatMap(c => c.foods); const gameMode = sessionGameModes[sessionId] || 'Easy'; const speed = MODE_CONFIG[gameMode].fruitSpeed; // spawn every 3s const now = Date.now(); if (now - lastSpawnAt[sessionId] >= 3000) { lastSpawnAt[sessionId] = now; if (fruitQueues[sessionId] && fruitQueues[sessionId].length > 0) { const dequeued = fruitQueues[sessionId].shift(); const nextFoodId = typeof dequeued === 'string' ? dequeued : dequeued.id; const targetFood = allFoods.find(f => f.id === nextFoodId); if (nextFoodId && targetFood) { if (!sessions[sessionId].initialTargetSent) { sessions[sessionId].currentTargetFoodId = nextFoodId; sessions[sessionId].initialTargetSent = true; broadcast(sessionId, { type: 'AAC_TARGET_FOOD', payload: { targetFoodId: nextFoodId, targetFoodData: targetFood, effect: null }, }); } const weightedFood = getWeightedRandomFood(allFoods, sessions[sessionId].currentTargetFoodId); // fruitQueues[sessionId].push(weightedFood); // keep objects // if (fruitQueues[sessionId].length > QUEUE_MAX) { // fruitQueues[sessionId] = fruitQueues[sessionId].slice(0, QUEUE_MAX); // } enqueueFruit(sessionId, weightedFood); foodInstanceCounter++; const instanceId = `food-${foodInstanceCounter}`; const hippoClients = [...sessions[sessionId]].filter(c => c.role === 'Hippo Player'); hippoClients.forEach(client => { // Assign a random angle based on the edge they selected // Each hippo will spawn from their selected edge const edge = client.edge || 'bottom'; const angleRange = getAngleRangeForEdge(edge); const angle = Math.random() * (angleRange.max - angleRange.min) + angleRange.min; const vx = (Math.cos(angle) * speed) / SCREEN_WIDTH; const vy = (Math.sin(angle) * speed) / SCREEN_HEIGHT; activeFoods[sessionId].push({ instanceId: `${instanceId}-${client.userId}`, foodId: nextFoodId, x: 0.5, y: 0.5, vx, vy, effect: (nextFoodId === sessions[sessionId].currentTargetFoodId) ? sessions[sessionId].currentTargetEffect : null, }); }); // Broadcast the new food state to all clients in the session broadcast(sessionId, { type: 'FOOD_STATE_UPDATE', payload: { foods: activeFoods[sessionId] } }); //console.log('[WSS] Spawned', nextFoodId, 'queueLen=', fruitQueues[sessionId].length); } else { console.warn('[WSS] Unknown food in queue, skipping spawn:', dequeued); } } } // physics tick @ 50ms const timeStep = TICK_INTERVAL / 1000; // convert to seconds activeFoods[sessionId].forEach(food => { food.x += food.vx * timeStep; food.y += food.vy * timeStep; }); // broadcast motion broadcast(sessionId, { type: 'FOOD_STATE_UPDATE', payload: { foods: activeFoods[sessionId] } }); // cull off-screen const BOUNDARY_BUFFER = 100; activeFoods[sessionId] = activeFoods[sessionId].filter(food => food.x > -BOUNDARY_BUFFER && food.x < 1024 + BOUNDARY_BUFFER && food.y > -BOUNDARY_BUFFER && food.y < 1024 + BOUNDARY_BUFFER ); }, TICK_INTERVAL); broadcast(sessionId, { type: 'START_GAME_BROADCAST', payload: { sessionId, mode }, }); } if (data.type === 'START_TIMER') { const { sessionId } = data.payload; //console.log(`[WSS] Starting timer for session ${sessionId}`); let secondsLeft = 180; //console.log('[WSS] SECONDSLEFT INIT:', secondsLeft); const interval = setInterval(() => { if (secondsLeft <= 0) { //console.log(`[WSS] Timer ended for session ${sessionId}`); broadcast(sessionId, { type: 'TIMER_UPDATE', secondsLeft: 0 }); broadcast(sessionId, { type: 'GAME_OVER' }); clearInterval(fruitIntervals[sessionId]); cleanupSession(sessionId); delete fruitIntervals[sessionId]; delete fruitQueues[sessionId]; clearInterval(interval); } else { broadcast(sessionId, { type: 'TIMER_UPDATE', secondsLeft }); secondsLeft--; } }, 1000); } if (data.type === 'SET_EDGE') { const { sessionId, userId, edge } = data.payload; for (const client of sessions[sessionId]) { if (client.userId === userId) { client.edge = edge; //console.log(`[WSS] Stored edge "${edge}" for user ${userId}`); break; } } const assignedEdges = [...sessions[sessionId]].map(c => `${c.userId}: ${c.edge}`); // console.log(`[WSS DEBUG] Current edge map for session ${sessionId}:`, assignedEdges); } // When an AAC user selects a food, broadcast it to the session if (data.type === 'AAC_FOOD_SELECTED') { const { sessionId, food, effect } = data.payload; //console.log(`WSS Food selected in session ${sessionId}:`, food, effect); const gameMode = sessionGameModes[sessionId] || 'Easy'; const finalEffect = MODE_CONFIG[gameMode].allowEffect ? effect : null; if (IS_PROD) { await pool.query( `UPDATE game_statistics SET aac_food_counts = jsonb_set( aac_food_counts, '{${food.id}}', (COALESCE(aac_food_counts->>'${food.id}', '0')::int + 1)::text::jsonb ) WHERE id = 1` ); if (finalEffect) { await pool.query( `UPDATE game_statistics SET aac_verb_counts = jsonb_set( aac_verb_counts, '{${finalEffect.id}}', (COALESCE(aac_verb_counts->>'${finalEffect.id}', '0')::int + 1)::text::jsonb ) WHERE id = 1` ); } } if (fruitQueues[sessionId]) { // Pushes AAC-selected food to the front of the queue // Keep queue entries as full objects, not ids const head = fruitQueues[sessionId][0]; const headId = head && (head.id || head); // tolerate any stray ids if (headId !== food.id) { // fruitQueues[sessionId].unshift(food); // if (fruitQueues[sessionId].length > QUEUE_MAX) { // fruitQueues[sessionId] = fruitQueues[sessionId].slice(0, QUEUE_MAX); // } enqueueFruit(sessionId, food, { front: true }); } } // Updates the session's current weighted target sessions[sessionId].currentTargetFoodId = food.id; sessions[sessionId].currentTargetEffect = finalEffect; // Broadcasts the selected food as the official target broadcast(sessionId, { type: 'AAC_TARGET_FOOD', payload: { targetFoodId: food.id, targetFoodData: food, effect: finalEffect } }); } // When a player eats a target food with an effect, broadcast it to the session if (data.type === 'PLAYER_EFFECT_APPLIED') { const { sessionId, targetUserId, effect } = data.payload; broadcast(sessionId, { type: 'PLAYER_EFFECT_BROADCAST', payload: { targetUserId, effect } }); } // Defines angle ranges in radians function getAngleRangeForEdge(edge) { switch (edge) { case 'top': return { min: -Math.PI * 3/4, max: -Math.PI / 4 }; // Up: -135° to -45° case 'bottom': return { min: Math.PI / 4, max: Math.PI * 3/4 }; // Down: +45° to +135° case 'left': return { min: Math.PI * 7/8, max: Math.PI * 9/8 }; // Left: 157.5° to 202.5° case 'right': return { min: -Math.PI / 4, max: Math.PI / 4 }; // Right: -45° to +45° default: return { min: 0, max: 2 * Math.PI }; } } // Notify all players in the session to remove the fruit if (data.type === 'FRUIT_EATEN') { const { sessionId, instanceId } = data.payload; if (activeFoods[sessionId]) { activeFoods[sessionId] = activeFoods[sessionId].filter(f => f.instanceId !== instanceId); } broadcast(sessionId, { type: 'REMOVE_FOOD', payload: { instanceId } }); } // Broadcast updated scores if (data.type === 'FRUIT_EATEN_BY_PLAYER') { const { sessionId, userId, isCorrect, allowPenalty, effect } = data.payload; if (!scoresBySession[sessionId]) scoresBySession[sessionId] = {}; const prev = scoresBySession[sessionId][userId] || 0; if (isCorrect) { sessions[sessionId].currentTargetEffect = null; if (effect === 'burn') { scoresBySession[sessionId][userId] = Math.max(0, prev - 2); //console.log(`[WSS] Player ${userId} burned, score reduced by 2 from ${prev} to ${scoresBySession[sessionId][userId]}`); } else if (effect === 'grow') { scoresBySession[sessionId][userId] = prev + 2; // console.log(`[WSS] Player ${userId} grew, score increased by 2 from ${prev} to ${scoresBySession[sessionId][userId]}`); } else { scoresBySession[sessionId][userId] = prev + 1; } } else if (allowPenalty) { scoresBySession[sessionId][userId] = Math.max(0, prev - 1); } broadcast(sessionId, { type: 'SCORE_UPDATE_BROADCAST', payload: { scores: scoresBySession[sessionId] } }); if (IS_PROD) { if (isCorrect) { await pool.query('UPDATE game_statistics SET total_correct_eats = total_correct_eats + 1 WHERE id = 1'); } else { await pool.query('UPDATE game_statistics SET total_wrong_eats = total_wrong_eats + 1 WHERE id = 1'); } } } // When a player selects a color, broadcast it to the session if (data.type === 'SELECT_COLOR') { const { sessionId, userId, color } = data.payload; if (sessions[sessionId]) { // Find the client who sent the message and assign them the color for (const client of sessions[sessionId]) { if (client.userId === userId) { client.color = color; break; } } // Collect all taken colors in the session const takenColors = Array.from(sessions[sessionId]) .map(client => client.color) .filter(c => c); broadcast(sessionId, { type: 'COLOR_UPDATE', payload: { takenColors } }); } // If in production, update the database with the color selection if (IS_PROD && color) { await pool.query( `UPDATE game_statistics SET hippo_color_counts = jsonb_set( hippo_color_counts, '{${color}}', (COALESCE(hippo_color_counts->>'${color}', '0')::int + 1)::text::jsonb ) WHERE id = 1` ); } } // When a client requests an update on taken colors, broadcast the current state if (data.type === 'REQUEST_COLOR_UPDATE') { const { sessionId } = data.payload; if (sessions[sessionId]) { const takenColors = Array.from(sessions[sessionId]) .map(client => client.color) .filter(c => c); broadcast(sessionId, { type: 'COLOR_UPDATE', payload: { takenColors } }); } } // When a presenter clicks "End Game", broadcast to all clients in the session if (data.type === 'RESET_GAME') { const { sessionId } = data.payload; console.log('[WSS] RESET_GAME received for session', sessionId); // Reset scores to 0 for Hippo Players if (scoresBySession[sessionId]) { console.log('[WSS] Scores before reset:', scoresBySession[sessionId]); for (const client of sessions[sessionId]) { if (client.role === 'Hippo Player') { console.log(`[WSS] Resetting score for ${client.userId}`); scoresBySession[sessionId][client.userId] = 0; } } console.log('[WSS] Scores after reset:', scoresBySession[sessionId]); } else { console.log('[WSS] No scores found for session', sessionId); } // Send updated scores to all clients broadcast(sessionId, { type: 'SCORE_UPDATE_BROADCAST', payload: { scores: scoresBySession[sessionId] } }); // Notify clients to reset game UI broadcast(sessionId, { type: 'RESET_GAME_BROADCAST', payload: {}, }); } } catch (error) { console.error('WSS Error processing message:', error); sendError(ws, { message: 'Server error' }); } }); ws.on('close', async () => { const { sessionId, userId } = ws; if (!sessionId || !userId) { console.log('WSS Client disconnected without session or user ID'); return; } console.log(`WSS Client ${userId} disconnected from session ${sessionId}`); let remainingPlayers = 0; if (IS_PROD && sessionId && userId) { try { await pool.query('DELETE FROM players WHERE session_id = $1 AND user_id = $2', [sessionId, userId]); const result = await pool.query('SELECT COUNT(*) FROM players WHERE session_id = $1', [sessionId]); remainingPlayers = parseInt(result.rows[0].count, 10); // If no players remain, remove the session from the database if (remainingPlayers === 0) { await pool.query('DELETE FROM sessions WHERE session_id = $1', [sessionId]); console.log(`WSS Session ${sessionId} was empty and has been removed from the database.`); } else { console.log(`WSS Player ${userId} removed from session ${sessionId}. Remaining players: ${remainingPlayers}`); } } catch (err) { console.error('Error removing player from database:', err); } } // Remove the session from the sessions object if it is empty if (sessions[sessionId] && sessions[sessionId].size === 0) { cleanupSession(sessionId); delete sessions[sessionId]; } // If the role is Presenter and the game hasn't started, broadcast SESSION_CLOSED if (userId === 'presenter' && sessions[sessionId] && !sessions[sessionId].gameStarted) { console.log(`[WSS] Presenter for session ${sessionId} disconnected. Starting 5s reconnect timer...`); reconnectionTimers[sessionId] = setTimeout(async () => { // Before closing, double-check if the presenter has rejoined. const sessionClients = sessions[sessionId] ? Array.from(sessions[sessionId]) : []; const isPresenterConnected = sessionClients.some(client => client.role === 'Presenter'); if (!isPresenterConnected) { console.log(`[WSS] Presenter for ${sessionId} did not reconnect in time. Closing session.`); broadcast(sessionId, { type: 'SESSION_CLOSED' }); cleanupSession(sessionId); delete sessions[sessionId]; if (IS_PROD) { try { await pool.query('DELETE FROM sessions WHERE session_id = $1', [sessionId]); console.log(`[WSS] Deleted timed-out session ${sessionId} from database.`); } catch (err) { console.error(`[WSS] Error deleting timed-out session ${sessionId} from DB:`, err); } } } else { console.log(`[WSS] Reconnect timer for ${sessionId} fired, but presenter has returned. Aborting closure.`); } delete reconnectionTimers[sessionId]; }, 5000); } // Remove the client from the ws if (sessions[sessionId]) { sessions[sessionId].delete(ws); // If the session still exists, broadcast the updated user list const usersInSession = Array.from(sessions[sessionId]) .filter(client => client.readyState === WebSocket.OPEN) .map(client => ({ userId: client.userId, role: client.role })); broadcast(sessionId, { type: 'USERS_LIST_UPDATE', payload: { users: usersInSession } }); // After a player leaves, re-calculate the taken colors and notify everyone. const takenColors = Array.from(sessions[sessionId]) .map(client => client.color) .filter(c => c); broadcast(sessionId, { type: 'COLOR_UPDATE', payload: { takenColors } }); console.log(`WSS Player left, broadcasting updated USERS_LIST_UPDATE to ${sessionId}:`, usersInSession); } // Remove the session from the sessions object if it is empty // if (sessions[sessionId].size === 0) { // delete sessions[sessionId]; // } }); }); /** * Helper function to broadcast a message to all clients in a specific session * @param {string} sessionId The ID of the session room * @param {object} data The data to send */ function broadcast(sessionId, data) { if (sessions[sessionId]) { sessions[sessionId].forEach(client => { if (client.readyState === WebSocket.OPEN) { try { client.send(JSON.stringify(data)); } catch (err) { console.error('Error broadcasting to client:', err); } } }); } } /** * Generates a random alphanumeric session ID consisting of uppercase letters and digits. * * @param {number} length - The desired length of the session ID. Defaults to 5. * @returns {string} A randomly generated session ID. */ function generateSessionId(length = 5) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; let id = ''; for (let i = 0; i < length; i++) { id += chars[Math.floor(Math.random() * chars.length)]; } return id; } /** * Generates a unique session ID that does not exist in the given array of existing session IDs. * * @param {string[]} existingSessions - An array of session IDs that are already taken. * @param {number} length - The desired length of the new session ID. Defaults to 5. * @returns {string} A new unique session ID not present in the existingSessions array. * */ function generateUniqueSessionId(existingSessions, length = 5) { let newId; let attempts = 0; const maxAttempts = 1000; do { if (attempts >= maxAttempts) { throw new Error('Max attempts reached'); } newId = generateSessionId(length); attempts++; } while (existingSessions.includes(newId)); return newId; } const PORT = process.env.PORT || 4000; // Start the Express server setupDatabase().then(() => { server.listen(PORT, () => { console.log(`Server listening on ${PORT}`); }); }); function cleanupSession(sessionId) { // Clear fruit interval if present if (fruitIntervals[sessionId]) { clearInterval(fruitIntervals[sessionId]); delete fruitIntervals[sessionId]; } // Remove all per-session state delete activeFoods[sessionId]; delete fruitQueues[sessionId]; delete scoresBySession[sessionId]; delete sessionGameModes[sessionId]; delete lastSpawnAt[sessionId]; } // ---- Queue Management Helpers ---- function enqueueFruit(sessionId, fruit, { front = false } = {}) { if (!fruitQueues[sessionId]) fruitQueues[sessionId] = []; if (front) { fruitQueues[sessionId].unshift(fruit); } else { fruitQueues[sessionId].push(fruit); } // Trim after any mutation if (fruitQueues[sessionId].length > QUEUE_MAX) { fruitQueues[sessionId] = fruitQueues[sessionId].slice(0, QUEUE_MAX); } } function dequeueFruit(sessionId) { if (!fruitQueues[sessionId] || fruitQueues[sessionId].length === 0) return null; return fruitQueues[sessionId].shift(); } function clearFruitQueue(sessionId) { fruitQueues[sessionId] = []; } function sendError(ws, { code = 'SERVER_ERROR', message, ...meta }) { ws.send( JSON.stringify({ type: 'ERROR_MESSAGE', payload: { code, message, ...meta }, }), ); } |