fix chat and payments and add spectator mode

This commit is contained in:
jwinterm
2025-12-22 10:36:46 -05:00
parent fbf5bd5b17
commit 24f46fa35e
26 changed files with 2742 additions and 7302 deletions
+289
View File
@@ -0,0 +1,289 @@
/**
* Configuration Persistence
*
* Provides a mechanism to persist and reload payment configuration changes
* without requiring a full server restart. Configuration is stored in the
* database and merged with environment variables on load.
*
* Priority order (highest to lowest):
* 1. Database overrides (hot-reloadable)
* 2. Environment variables (.env)
* 3. Default values
*/
const { normalizeError } = require('../utils/errors');
// Allowed configuration keys that can be persisted
const ALLOWED_KEYS = new Set([
// Direct mode
'DIRECT_GAME_PRICE',
'DIRECT_PAYOUT_ESCAPE',
'DIRECT_PAYOUT_TREASURE',
'DIRECT_PAYOUTS_ENABLED',
// Credits mode
'CREDITS_PER_GAME',
'CREDITS_PACKAGES',
'CREDITS_PAYOUT_BASE',
'CREDITS_PAYOUT_ESCAPE',
'CREDITS_PAYOUT_TREASURE',
'CREDITS_PAYOUTS_ENABLED',
// Limits
'MAX_GAMES_PER_HOUR',
'MAX_PAYOUTS_PER_DAY',
'GAME_COOLDOWN_SECONDS',
'PAYOUT_MIN_AMOUNT',
'PAYOUT_MAX_PER_GAME',
// Difficulty
'DIFFICULTY_PRESET'
]);
class ConfigPersistence {
constructor({ db, debugManager }) {
this.db = db;
this.debugManager = debugManager;
this._cache = new Map();
this._initialized = false;
this._listeners = new Set();
}
/**
* Initialize the persistence layer, creating table if needed
*/
async initialize() {
if (this._initialized) return;
try {
// Create config table if it doesn't exist
await this.db.query(`
CREATE TABLE IF NOT EXISTS config_overrides (
key VARCHAR(100) PRIMARY KEY,
value TEXT NOT NULL,
updated_at TIMESTAMP DEFAULT NOW(),
updated_by VARCHAR(100) DEFAULT 'system'
)
`);
// Load existing overrides into cache
await this._loadFromDatabase();
this._initialized = true;
if (this.debugManager?.CONSOLE_LOGGING) {
console.log(`[ConfigPersistence] Initialized with ${this._cache.size} overrides`);
}
} catch (error) {
const normalized = normalizeError(error, 'Failed to initialize config persistence');
console.error('[ConfigPersistence] Initialization error:', normalized.message);
// Don't throw - system should work without persistence
}
}
/**
* Load all overrides from database into cache
*/
async _loadFromDatabase() {
try {
const result = await this.db.query('SELECT key, value FROM config_overrides');
this._cache.clear();
for (const row of result.rows) {
if (ALLOWED_KEYS.has(row.key)) {
this._cache.set(row.key, row.value);
}
}
} catch (error) {
const normalized = normalizeError(error, 'Failed to load config from database');
console.error('[ConfigPersistence] Load error:', normalized.message);
}
}
/**
* Get a configuration value with priority: DB override > env > default
* @param {string} key - Configuration key
* @param {*} defaultValue - Default value if not found
* @returns {string|null} Configuration value
*/
get(key, defaultValue = null) {
// Check database override first
if (this._cache.has(key)) {
return this._cache.get(key);
}
// Fall back to environment variable
if (process.env[key] !== undefined) {
return process.env[key];
}
return defaultValue;
}
/**
* Get a numeric configuration value
* @param {string} key - Configuration key
* @param {number} defaultValue - Default value
* @returns {number} Parsed numeric value
*/
getNumber(key, defaultValue = 0) {
const value = this.get(key);
if (value === null) return defaultValue;
const parsed = parseFloat(value);
return isNaN(parsed) ? defaultValue : parsed;
}
/**
* Get a boolean configuration value
* @param {string} key - Configuration key
* @param {boolean} defaultValue - Default value
* @returns {boolean} Parsed boolean value
*/
getBoolean(key, defaultValue = false) {
const value = this.get(key);
if (value === null) return defaultValue;
return value === 'true' || value === '1';
}
/**
* Get a JSON configuration value
* @param {string} key - Configuration key
* @param {*} defaultValue - Default value
* @returns {*} Parsed JSON value
*/
getJSON(key, defaultValue = null) {
const value = this.get(key);
if (value === null) return defaultValue;
try {
return JSON.parse(value);
} catch {
return defaultValue;
}
}
/**
* Set a configuration override (persisted to database)
* @param {string} key - Configuration key
* @param {*} value - Value to set
* @param {string} updatedBy - Who made the change
* @returns {boolean} Success
*/
async set(key, value, updatedBy = 'admin') {
if (!ALLOWED_KEYS.has(key)) {
console.warn(`[ConfigPersistence] Attempted to set non-allowed key: ${key}`);
return false;
}
try {
const stringValue = typeof value === 'object' ? JSON.stringify(value) : String(value);
await this.db.query(`
INSERT INTO config_overrides (key, value, updated_at, updated_by)
VALUES ($1, $2, NOW(), $3)
ON CONFLICT (key) DO UPDATE SET
value = EXCLUDED.value,
updated_at = NOW(),
updated_by = EXCLUDED.updated_by
`, [key, stringValue, updatedBy]);
// Update cache
this._cache.set(key, stringValue);
// Notify listeners
this._notifyListeners(key, stringValue);
if (this.debugManager?.CONSOLE_LOGGING) {
console.log(`[ConfigPersistence] Set ${key} = ${stringValue.substring(0, 50)}... by ${updatedBy}`);
}
return true;
} catch (error) {
const normalized = normalizeError(error, 'Failed to set config value');
console.error('[ConfigPersistence] Set error:', normalized.message);
return false;
}
}
/**
* Remove a configuration override (reverts to env/default)
* @param {string} key - Configuration key
* @returns {boolean} Success
*/
async remove(key) {
try {
await this.db.query('DELETE FROM config_overrides WHERE key = $1', [key]);
this._cache.delete(key);
this._notifyListeners(key, null);
if (this.debugManager?.CONSOLE_LOGGING) {
console.log(`[ConfigPersistence] Removed override for ${key}`);
}
return true;
} catch (error) {
const normalized = normalizeError(error, 'Failed to remove config value');
console.error('[ConfigPersistence] Remove error:', normalized.message);
return false;
}
}
/**
* Get all current overrides
* @returns {Object} Key-value pairs of all overrides
*/
getAllOverrides() {
return Object.fromEntries(this._cache);
}
/**
* Reload configuration from database
* Call this to pick up external changes
*/
async reload() {
await this._loadFromDatabase();
if (this.debugManager?.CONSOLE_LOGGING) {
console.log(`[ConfigPersistence] Reloaded ${this._cache.size} overrides`);
}
}
/**
* Register a listener for configuration changes
* @param {Function} callback - (key, value) => void
* @returns {Function} Unsubscribe function
*/
onConfigChange(callback) {
this._listeners.add(callback);
return () => this._listeners.delete(callback);
}
/**
* Notify all listeners of a configuration change
*/
_notifyListeners(key, value) {
for (const listener of this._listeners) {
try {
listener(key, value);
} catch (e) {
console.error('[ConfigPersistence] Listener error:', e);
}
}
}
/**
* Get configuration snapshot for display/API
* @returns {Object} Current effective configuration
*/
getSnapshot() {
const snapshot = {};
for (const key of ALLOWED_KEYS) {
snapshot[key] = {
value: this.get(key),
source: this._cache.has(key) ? 'database' : (process.env[key] ? 'environment' : 'default'),
hasOverride: this._cache.has(key)
};
}
return snapshot;
}
}
module.exports = ConfigPersistence;
+4
View File
@@ -37,6 +37,7 @@ const DIFFICULTY_PRESETS = {
startDistanceFromPlayer: 0.9, // 90% of max distance (far from player)
movesPerPlayerMove: 0.5, // Monster moves every other player move
chaseAggressiveness: 0.6, // 60% chance to chase, 40% random
visionRange: 6, // Short vision - easy to evade
respawnOnDeath: false
},
treasure: {
@@ -60,6 +61,7 @@ const DIFFICULTY_PRESETS = {
startDistanceFromPlayer: 0.6, // 60% of max distance
movesPerPlayerMove: 1.0, // Monster moves every player move
chaseAggressiveness: 0.8, // 80% chase, 20% random
visionRange: 10, // Medium vision
respawnOnDeath: false
},
treasure: {
@@ -83,6 +85,7 @@ const DIFFICULTY_PRESETS = {
startDistanceFromPlayer: 0.4, // Closer to player
movesPerPlayerMove: 1.0,
chaseAggressiveness: 0.9, // Very aggressive
visionRange: 14, // Good vision
respawnOnDeath: false
},
treasure: {
@@ -106,6 +109,7 @@ const DIFFICULTY_PRESETS = {
startDistanceFromPlayer: 0.35, // Close to player
movesPerPlayerMove: 1.0,
chaseAggressiveness: 0.95, // Nearly always chases
visionRange: 18, // Excellent vision
respawnOnDeath: false
},
treasure: {
+3 -1
View File
@@ -108,7 +108,9 @@ class Game {
this.gameState = 'waiting';
this.startBlock = null;
this.player = new Player();
this.monster = new Monster();
this.monster = new Monster(0, 0, {
visionRange: difficultyConfig.monster.visionRange || 12
});
this.monsterMoveAccumulator = 0; // For fractional monster moves
this.fee = 0;
+28 -3
View File
@@ -6,12 +6,21 @@
const Game = require('../game/game');
class GameManager {
constructor({ activeGames, io, broadcastManager, debugManager, gameModeManager }) {
constructor({ activeGames, io, broadcastManager, debugManager, gameModeManager, spectatorManager = null }) {
this.activeGames = activeGames;
this.io = io;
this.broadcastManager = broadcastManager;
this.debugManager = debugManager;
this.gameModeManager = gameModeManager;
this.spectatorManager = spectatorManager;
}
/**
* Set the spectator manager (allows late binding after initialization)
* @param {SpectatorManager} spectatorManager
*/
setSpectatorManager(spectatorManager) {
this.spectatorManager = spectatorManager;
}
/**
@@ -88,7 +97,9 @@ class GameManager {
}
}
// Emit game over event
// Emit game over event with provably fair reveal
const proofReveal = game.getProofReveal ? game.getProofReveal() : null;
this.io.to(socketId).emit('game_over', {
status: status,
reason: reason,
@@ -97,9 +108,23 @@ class GameManager {
moves,
durationSeconds,
payout: payoutInfo,
treasure: game.player.hasTreasure || false
treasure: game.player.hasTreasure || false,
proof: proofReveal
});
// Notify spectators that the game has ended
if (this.spectatorManager) {
this.spectatorManager.notifyGameEnded(game.id, {
status,
reason,
message,
score: finalScore,
moves,
durationSeconds,
treasure: game.player.hasTreasure || false
});
}
// Persist completion details if DB available
await this._updateGameRecord(game, socketId, status, reason, moves, durationSeconds);
+177 -26
View File
@@ -1,12 +1,25 @@
const ROT = require('./rot.js');
/**
* Monster class - Manages monster state and AI
* Monster class - Manages monster state and AI with improved pathfinding
*
* AI Behavior:
* - Uses A* pathfinding when player is visible (line of sight)
* - Patrols toward last known player position when player not visible
* - Falls back to wandering when no target
* - Vision limited to configurable range (not omniscient)
*/
class Monster {
constructor(x = 0, y = 0) {
constructor(x = 0, y = 0, options = {}) {
this.x = x;
this.y = y;
// AI state
this.lastKnownPlayerX = null;
this.lastKnownPlayerY = null;
this.visionRange = options.visionRange || 12; // tiles the monster can "see"
this.pathCache = null;
this.pathCacheTarget = null;
}
// Move monster to new position
@@ -19,38 +32,161 @@ class Monster {
isAt(x, y) {
return this.x === x && this.y === y;
}
/**
* Check if monster has line of sight to a position
* Uses Bresenham's line algorithm
*/
hasLineOfSight(targetX, targetY, dungeon, isPassable) {
const dx = Math.abs(targetX - this.x);
const dy = Math.abs(targetY - this.y);
const sx = this.x < targetX ? 1 : -1;
const sy = this.y < targetY ? 1 : -1;
let err = dx - dy;
let x = this.x;
let y = this.y;
while (x !== targetX || y !== targetY) {
const e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x += sx;
}
if (e2 < dx) {
err += dx;
y += sy;
}
// Check if we hit the target
if (x === targetX && y === targetY) return true;
// Check if blocked by wall
if (!dungeon.map[y] || !isPassable(dungeon.map[y][x])) {
return false;
}
}
return true;
}
/**
* Calculate distance to a point
*/
distanceTo(x, y) {
return Math.sqrt((x - this.x) ** 2 + (y - this.y) ** 2);
}
/**
* Find path using A* pathfinding
* Returns array of [x, y] positions or null if no path
*/
findPath(targetX, targetY, dungeon, isPassable) {
// Use ROT.js A* pathfinding
const passableCallback = (x, y) => {
if (!dungeon.map[y]) return false;
return isPassable(dungeon.map[y][x]);
};
const path = [];
const astar = new ROT.Path.AStar(targetX, targetY, passableCallback, { topology: 4 });
astar.compute(this.x, this.y, (x, y) => {
path.push([x, y]);
});
// Remove the starting position from path
if (path.length > 0 && path[0][0] === this.x && path[0][1] === this.y) {
path.shift();
}
return path.length > 0 ? path : null;
}
// Simple AI to move toward player (or randomly if player is null)
/**
* Improved AI to move toward player using A* pathfinding with limited vision
*/
moveTowardPlayer(player, dungeon) {
if (!dungeon || !dungeon.map) return;
// Check if a tile is passable (floor)
// Check if a tile is passable (floor, stairs, treasure)
const isPassable = (tile) => {
return tile === "'1" || tile === "'2" || tile === 0; // Support both new and legacy floor types
return tile === "'1" || tile === "'2" || tile === 0 ||
tile === '>' || tile === '$M'; // Can walk on stairs and treasure
};
// If no player target, move randomly
// If no player target, wander randomly
if (!player) {
const dirs = [[0,1],[1,0],[0,-1],[-1,0]];
const shuffledDirs = ROT.RNG.shuffle(dirs.slice());
for (const [dx, dy] of shuffledDirs) {
const nx = this.x + dx;
const ny = this.y + dy;
if (dungeon.map[ny] && isPassable(dungeon.map[ny][nx])) {
this.x = nx;
this.y = ny;
return;
}
}
this._wander(dungeon, isPassable);
return;
}
const dx = Math.sign(player.x - this.x);
const dy = Math.sign(player.y - this.y);
// Try horizontal move first
if (dx !== 0) {
const distToPlayer = this.distanceTo(player.x, player.y);
let canSeePlayer = false;
// Check if player is within vision range AND line of sight
if (distToPlayer <= this.visionRange) {
canSeePlayer = this.hasLineOfSight(player.x, player.y, dungeon, isPassable);
}
if (canSeePlayer) {
// Player visible - update last known position and use A* pathfinding
this.lastKnownPlayerX = player.x;
this.lastKnownPlayerY = player.y;
// Find path to player
const path = this.findPath(player.x, player.y, dungeon, isPassable);
if (path && path.length > 0) {
// Move to next position in path
const [nextX, nextY] = path[0];
this.x = nextX;
this.y = nextY;
return;
}
// Fallback: direct move toward player
this._moveDirectToward(player.x, player.y, dungeon, isPassable);
} else if (this.lastKnownPlayerX !== null) {
// Player not visible but we have last known position - patrol toward it
const distToLastKnown = this.distanceTo(this.lastKnownPlayerX, this.lastKnownPlayerY);
if (distToLastKnown < 1) {
// Reached last known position, clear it and wander
this.lastKnownPlayerX = null;
this.lastKnownPlayerY = null;
this._wander(dungeon, isPassable);
} else {
// Path toward last known position
const path = this.findPath(this.lastKnownPlayerX, this.lastKnownPlayerY, dungeon, isPassable);
if (path && path.length > 0) {
const [nextX, nextY] = path[0];
this.x = nextX;
this.y = nextY;
} else {
// Can't reach last known position, clear and wander
this.lastKnownPlayerX = null;
this.lastKnownPlayerY = null;
this._wander(dungeon, isPassable);
}
}
} else {
// No player info - wander randomly
this._wander(dungeon, isPassable);
}
}
/**
* Simple direct movement toward a target (fallback)
*/
_moveDirectToward(targetX, targetY, dungeon, isPassable) {
const dx = Math.sign(targetX - this.x);
const dy = Math.sign(targetY - this.y);
// Try both directions, prioritize the one with more distance to cover
const xDist = Math.abs(targetX - this.x);
const yDist = Math.abs(targetY - this.y);
if (xDist >= yDist && dx !== 0) {
// Try horizontal first
const newX = this.x + dx;
if (dungeon.map[this.y] && isPassable(dungeon.map[this.y][newX])) {
this.x = newX;
@@ -58,7 +194,6 @@ class Monster {
}
}
// Try vertical move
if (dy !== 0) {
const newY = this.y + dy;
if (dungeon.map[newY] && isPassable(dungeon.map[newY][this.x])) {
@@ -67,7 +202,23 @@ class Monster {
}
}
// Try random direction if direct path is blocked
// Try horizontal if we haven't yet
if (xDist < yDist && dx !== 0) {
const newX = this.x + dx;
if (dungeon.map[this.y] && isPassable(dungeon.map[this.y][newX])) {
this.x = newX;
return;
}
}
// Blocked in preferred directions, try diagonals (A* backup)
this._wander(dungeon, isPassable);
}
/**
* Random wandering behavior
*/
_wander(dungeon, isPassable) {
const dirs = [[0,1],[1,0],[0,-1],[-1,0]];
const shuffledDirs = ROT.RNG.shuffle(dirs.slice());
@@ -77,7 +228,7 @@ class Monster {
if (dungeon.map[ny] && isPassable(dungeon.map[ny][nx])) {
this.x = nx;
this.y = ny;
break;
return;
}
}
}
+12 -1
View File
@@ -3,13 +3,14 @@
* Handles rate limiting and applying player movement to games.
*/
class MovementManager {
constructor({ activeGames, io, debugManager, moveCooldown = 100, postMoveHook = null }) {
constructor({ activeGames, io, debugManager, moveCooldown = 100, postMoveHook = null, spectatorManager = null }) {
this.activeGames = activeGames; // Map socketId -> Game
this.io = io;
this.debugManager = debugManager;
this.moveCooldown = moveCooldown;
this._lastMove = new Map(); // socketId -> timestamp
this.postMoveHook = typeof postMoveHook === 'function' ? postMoveHook : null;
this.spectatorManager = spectatorManager; // For broadcasting to spectators
}
handleMove(socketId, moveData) {
@@ -58,6 +59,11 @@ class MovementManager {
}
this.io.to(socketId).emit('game_update', state);
// Broadcast to spectators
if (this.spectatorManager && game.id) {
this.spectatorManager.broadcastToSpectators(game.id, state);
}
// Handle special events from moveResult (escape / treasure)
if (moveResult && moveResult.event) {
@@ -80,6 +86,11 @@ class MovementManager {
state.blockHeight = this.debugManager.getCurrentBlockHeight();
}
this.io.to(socketId).emit('game_update', state);
// Broadcast to spectators
if (this.spectatorManager && game.id) {
this.spectatorManager.broadcastToSpectators(game.id, state);
}
}
}
+187 -2
View File
@@ -22,6 +22,7 @@ const { AppError, ValidationError, NotFoundError } = require('./utils/errors');
const BroadcastManager = require('./network/broadcastManager');
const DebugManager = require('./debug/debugManager');
const SocketHandlers = require('./network/socketHandlers');
const { verifyGame, hashSeed } = require('./game/provablyFair');
// Initialize payment configuration
const paymentConfigManager = new PaymentConfigManager({ logger: console });
@@ -191,13 +192,58 @@ app.post('/api/user/:socketId/address', asyncHandler(async (req, res) => {
}
}));
// Health check endpoint
// Health check endpoint - enhanced for monitoring dashboard
app.get('/health', (req, res) => {
const memUsage = process.memoryUsage();
const health = {
status: 'ok',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memoryUsage: process.memoryUsage(),
nodeVersion: process.version,
environment: process.env.NODE_ENV || 'development',
// Memory details
memory: {
heapUsed: memUsage.heapUsed,
heapTotal: memUsage.heapTotal,
rss: memUsage.rss,
external: memUsage.external || 0
},
// Game statistics
games: {
active: activeGames.size,
queued: socketHandlers?.queueManager?.getQueueLength?.() || 0,
connected: io.sockets.sockets.size || 0,
mode: gameModeManager.gameMode
},
// Blockchain
blockHeight: debugManager.getCurrentBlockHeight(),
blockSource: process.env.BLOCK_SOURCE || 'daemon',
network: process.env.MONERO_NETWORK || 'mainnet',
// Wallet status
wallet: {
available: gameModeManager.paymentsEnabled,
status: walletRPCService.isHealthy ? 'connected' : 'disconnected',
endpoint: process.env.PRIMARY_WALLET_ENDPOINT ? '...' + process.env.PRIMARY_WALLET_ENDPOINT.slice(-15) : null,
pendingPayouts: 0 // Could track this in gameModeManager
},
// Rate limiter stats (if available)
rateLimiter: socketHandlers?.rateLimiter ? {
trackedEntries: socketHandlers.rateLimiter.getTrackedCount?.() || 0,
blockedCount: socketHandlers.rateLimiter.getBlockedCount?.() || 0
} : null,
// Limits
limits: {
maxGamesPerHour: parseInt(process.env.MAX_GAMES_PER_HOUR) || 60,
maxPayoutsPerDay: parseInt(process.env.MAX_PAYOUTS_PER_DAY) || 100
},
// Legacy fields for compatibility
gameMode: gameModeManager.gameMode,
paymentsEnabled: gameModeManager.paymentsEnabled,
directModeEnabled: gameModeManager.directModeEnabled,
@@ -270,6 +316,145 @@ app.get('/api/game-modes', (req, res) => {
});
});
// ============================================================================
// Provably Fair Verification Endpoint
// ============================================================================
/**
* Verify a game's fairness by checking if the seed matches the commitment hash
*
* GET /verify/:gameId - Returns HTML verification page
* GET /api/verify - Verify seed and commitment (query params: seed, commitment)
*/
app.get('/verify/:gameId', asyncHandler(async (req, res) => {
const { gameId } = req.params;
// Try to look up the game in the database
let gameRecord = null;
try {
const result = await databaseManager.query(
'SELECT dungeon_seed, status, treasure_found, moves_made, duration_seconds, created_at FROM games WHERE dungeon_seed = $1',
[gameId]
);
if (result.rows.length > 0) {
gameRecord = result.rows[0];
}
} catch (err) {
// Database lookup failed, continue without game record
}
// Return a simple HTML verification page
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Wownerogue - Game Verification</title>
<style>
body { background: #0a0a0a; color: #0f0; font-family: monospace; padding: 20px; max-width: 800px; margin: 0 auto; }
h1 { color: #0ff; }
.box { background: #001a00; border: 1px solid #0f0; padding: 15px; margin: 10px 0; border-radius: 5px; }
input, button { font-family: monospace; padding: 8px; margin: 5px 0; }
input { background: #001a00; color: #0f0; border: 1px solid #0f0; width: 100%; box-sizing: border-box; }
button { background: #0a5c0a; color: #ff0; border: 2px solid #0f0; cursor: pointer; }
button:hover { background: #0f0; color: #000; }
.success { color: #4ade80; }
.error { color: #f00; }
#result { margin-top: 15px; padding: 10px; }
code { background: #002200; padding: 2px 6px; border-radius: 3px; }
.info { color: #888; font-size: 12px; }
</style>
</head>
<body>
<h1>🔐 Wownerogue Provably Fair Verification</h1>
<div class="box">
<h3>How it works:</h3>
<ol>
<li>Before game start, you received a <strong>commitment hash</strong> (SHA-256)</li>
<li>After the game, you received the <strong>seed</strong></li>
<li>This page verifies: <code>SHA256(seed) === commitment</code></li>
<li>The seed deterministically generated your dungeon layout</li>
</ol>
</div>
<div class="box">
<h3>Verify Game</h3>
<label>Game Seed (revealed after game):</label>
<input type="text" id="seed" placeholder="64 character hex string">
<label>Commitment Hash (shown before game):</label>
<input type="text" id="commitment" placeholder="64 character hex string">
<button onclick="verify()">🔍 Verify</button>
<div id="result"></div>
</div>
${gameRecord ? `
<div class="box">
<h3>Game Record</h3>
<p><strong>Game ID:</strong> ${gameId}</p>
<p><strong>Status:</strong> ${gameRecord.status}</p>
<p><strong>Treasure Found:</strong> ${gameRecord.treasure_found ? 'Yes' : 'No'}</p>
<p><strong>Moves:</strong> ${gameRecord.moves_made || 'N/A'}</p>
<p><strong>Duration:</strong> ${gameRecord.duration_seconds ? gameRecord.duration_seconds + 's' : 'N/A'}</p>
<p><strong>Created:</strong> ${gameRecord.created_at}</p>
</div>
` : ''}
<div class="box info">
<p>For technical verification, you can also use the API:</p>
<code>GET /api/verify?seed=YOUR_SEED&commitment=YOUR_COMMITMENT</code>
</div>
<script>
async function verify() {
const seed = document.getElementById('seed').value.trim();
const commitment = document.getElementById('commitment').value.trim();
const result = document.getElementById('result');
if (!seed || !commitment) {
result.innerHTML = '<p class="error">Please enter both seed and commitment.</p>';
return;
}
try {
const response = await fetch('/api/verify?seed=' + encodeURIComponent(seed) + '&commitment=' + encodeURIComponent(commitment));
const data = await response.json();
if (data.valid) {
result.innerHTML = '<p class="success">✅ ' + data.message + '</p>' +
'<p>Computed hash: <code>' + data.computedHash + '</code></p>';
} else {
result.innerHTML = '<p class="error">❌ ' + data.message + '</p>' +
'<p>Expected: <code>' + data.expectedCommitment + '</code></p>' +
'<p>Got: <code>' + data.computedHash + '</code></p>';
}
} catch (err) {
result.innerHTML = '<p class="error">Error: ' + err.message + '</p>';
}
}
</script>
</body>
</html>
`);
}));
// API endpoint for programmatic verification
app.get('/api/verify', (req, res) => {
const { seed, commitment } = req.query;
if (!seed || !commitment) {
return res.status(400).json({
valid: false,
error: 'Missing required parameters: seed and commitment'
});
}
const result = verifyGame(seed, commitment);
res.json(result);
});
// Initialize debug manager with new block callbacks
debugManager.onNewBlockCallback((blockHeight) => {
// Start games for waiting players when new block detected
+22
View File
@@ -0,0 +1,22 @@
-- Chat History Migration
-- Persistent storage for chat messages
-- Create chat_messages table
CREATE TABLE IF NOT EXISTS chat_messages (
id SERIAL PRIMARY KEY,
socket_id VARCHAR(255),
username VARCHAR(50),
message TEXT NOT NULL,
message_type VARCHAR(20) DEFAULT 'chat', -- 'chat', 'system', 'event'
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Index for efficient retrieval of recent messages
CREATE INDEX IF NOT EXISTS idx_chat_messages_created_at ON chat_messages(created_at DESC);
-- Optional: Index for user-specific history lookups
CREATE INDEX IF NOT EXISTS idx_chat_messages_socket_id ON chat_messages(socket_id);
-- Comment on table
COMMENT ON TABLE chat_messages IS 'Stores chat message history for display to new users';
COMMENT ON COLUMN chat_messages.message_type IS 'Type of message: chat (user), system (server), event (game events)';
+56 -4
View File
@@ -3,8 +3,10 @@
* Handles chat messages, commands, and chat-related rate limiting
*/
const ChatHistoryManager = require('./chatHistoryManager');
class ChatHandler {
constructor({ io, broadcastManager, debugManager, addressManager, paymentHandlers, queueManager, gameModeManager, rateLimiter }) {
constructor({ io, broadcastManager, debugManager, addressManager, paymentHandlers, queueManager, gameModeManager, rateLimiter, db }) {
this.io = io;
this.broadcastManager = broadcastManager;
this.debugManager = debugManager;
@@ -14,6 +16,13 @@ class ChatHandler {
this.gameModeManager = gameModeManager;
this.rateLimiter = rateLimiter;
// Initialize chat history manager for persistent message storage
this.chatHistory = new ChatHistoryManager({
db: db || (gameModeManager?.db),
debugManager: debugManager,
maxHistoryMessages: 50
});
// Memory leak prevention - cleanup old timestamps
this._chatLastSent = new Map();
this.chatCleanupInterval = setInterval(() => this.cleanupChatTimestamps(), 300000); // 5 minutes
@@ -21,6 +30,32 @@ class ChatHandler {
this._awaitingAddressFor = new Set(); // Track users waiting for address confirmation
}
/**
* Initialize the chat handler (must be called after construction)
*/
async initialize() {
await this.chatHistory.initialize();
}
/**
* Send chat history to a newly connected user
* @param {string} socketId - Socket ID of the new user
* @param {number} messageCount - Number of messages to send (default 50)
*/
async sendChatHistoryToUser(socketId, messageCount = 50) {
try {
const messages = await this.chatHistory.getRecentMessages(messageCount);
if (messages.length > 0) {
this.io.to(socketId).emit('chat_history', { messages });
if (this.debugManager?.CONSOLE_LOGGING) {
console.log(`📜 Sent ${messages.length} chat history messages to ${socketId}`);
}
}
} catch (error) {
console.error('Failed to send chat history:', error.message);
}
}
/**
* Handle incoming chat messages with rate limiting
*/
@@ -69,10 +104,12 @@ class ChatHandler {
/**
* Get chat statistics
*/
getStats() {
async getStats() {
const historyStats = await this.chatHistory.getStats();
return {
chatTimestamps: this._chatLastSent.size,
awaitingAddress: this._awaitingAddressFor.size
awaitingAddress: this._awaitingAddressFor.size,
chatHistory: historyStats
};
}
@@ -84,6 +121,9 @@ class ChatHandler {
clearInterval(this.chatCleanupInterval);
this.chatCleanupInterval = null;
}
if (this.chatHistory) {
this.chatHistory.shutdown();
}
}
/**
@@ -198,7 +238,19 @@ class ChatHandler {
// Very light sanitization (escape < >)
const safe = trimmed.replace(/[<>]/g, c => c === '<' ? '&lt;' : '&gt;').slice(0, 300);
this.broadcastManager.broadcastChatMessage(socket.id.substring(0,6), safe, now, socket.id);
const username = socket.id.substring(0, 6);
// Save message to history (async, don't block broadcast)
this.chatHistory.saveMessage({
socketId: socket.id,
username: username,
message: safe,
type: 'chat'
}).catch(err => {
console.error('Failed to save chat message:', err.message);
});
this.broadcastManager.broadcastChatMessage(username, safe, now, socket.id);
}
/**
+274
View File
@@ -0,0 +1,274 @@
/**
* Chat History Manager
* Handles persistent storage and retrieval of chat messages
*/
class ChatHistoryManager {
constructor({ db, debugManager, maxHistoryMessages = 50, inMemoryFallbackSize = 100 }) {
this.db = db;
this.debugManager = debugManager;
this.maxHistoryMessages = maxHistoryMessages;
// In-memory fallback for when DB is not available
this._inMemoryHistory = [];
this._inMemoryFallbackSize = inMemoryFallbackSize;
// Cache recent messages in memory for fast access
this._recentMessagesCache = [];
this._cacheSize = maxHistoryMessages;
this._cacheInitialized = false;
// Cleanup old messages periodically (once per day)
this._cleanupInterval = null;
this._maxMessageAge = 30 * 24 * 60 * 60 * 1000; // 30 days in ms
}
/**
* Initialize the manager - load recent messages into cache
*/
async initialize() {
try {
if (this.db) {
// Ensure table exists
await this._ensureTable();
// Load recent messages into cache
await this._refreshCache();
// Start cleanup interval (run once per day)
this._cleanupInterval = setInterval(() => {
this.cleanupOldMessages().catch(err => {
console.error('Chat history cleanup error:', err.message);
});
}, 24 * 60 * 60 * 1000);
if (this.debugManager?.CONSOLE_LOGGING) {
console.log(`💬 ChatHistoryManager initialized with ${this._recentMessagesCache.length} cached messages`);
}
} else {
console.warn('ChatHistoryManager: No database available, using in-memory fallback');
}
this._cacheInitialized = true;
} catch (error) {
console.error('ChatHistoryManager initialization error:', error.message);
// Fall back to in-memory mode
this._cacheInitialized = true;
}
}
/**
* Ensure the chat_messages table exists
*/
async _ensureTable() {
if (!this.db) return;
try {
await this.db.query(`
CREATE TABLE IF NOT EXISTS chat_messages (
id SERIAL PRIMARY KEY,
socket_id VARCHAR(255),
username VARCHAR(50),
message TEXT NOT NULL,
message_type VARCHAR(20) DEFAULT 'chat',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
// Create index if it doesn't exist
await this.db.query(`
CREATE INDEX IF NOT EXISTS idx_chat_messages_created_at
ON chat_messages(created_at DESC)
`);
} catch (error) {
// Table might already exist, that's fine
if (this.debugManager?.CONSOLE_LOGGING) {
console.log('Chat table check:', error.message);
}
}
}
/**
* Refresh the in-memory cache from database
*/
async _refreshCache() {
if (!this.db) return;
try {
const result = await this.db.query(`
SELECT id, socket_id, username, message, message_type, created_at
FROM chat_messages
ORDER BY created_at DESC
LIMIT $1
`, [this._cacheSize]);
// Reverse to get chronological order (oldest first)
this._recentMessagesCache = result.rows.reverse().map(row => ({
id: row.id,
socketId: row.socket_id,
username: row.username,
message: row.message,
type: row.message_type,
timestamp: new Date(row.created_at).getTime()
}));
} catch (error) {
console.error('Failed to refresh chat cache:', error.message);
}
}
/**
* Save a chat message
* @param {Object} messageData - Message data
* @param {string} messageData.socketId - Socket ID of sender
* @param {string} messageData.username - Display username
* @param {string} messageData.message - Message content
* @param {string} messageData.type - Message type (chat, system, event)
* @returns {Object} Saved message with id and timestamp
*/
async saveMessage({ socketId, username, message, type = 'chat' }) {
const timestamp = Date.now();
const messageObj = {
socketId,
username,
message,
type,
timestamp
};
// Add to in-memory cache immediately
this._recentMessagesCache.push(messageObj);
if (this._recentMessagesCache.length > this._cacheSize) {
this._recentMessagesCache.shift();
}
// Also add to fallback array
this._inMemoryHistory.push(messageObj);
if (this._inMemoryHistory.length > this._inMemoryFallbackSize) {
this._inMemoryHistory.shift();
}
// Persist to database asynchronously (best effort)
if (this.db) {
try {
const result = await this.db.query(`
INSERT INTO chat_messages (socket_id, username, message, message_type, created_at)
VALUES ($1, $2, $3, $4, NOW())
RETURNING id
`, [socketId, username, message, type]);
messageObj.id = result.rows[0]?.id;
} catch (error) {
console.error('Failed to persist chat message:', error.message);
// Message is still in memory, so users will see it
}
}
return messageObj;
}
/**
* Get recent messages for a new user
* @param {number} count - Number of messages to retrieve (default: 50)
* @returns {Array} Array of recent messages
*/
async getRecentMessages(count = 50) {
const limit = Math.min(count, this._cacheSize);
// If cache is initialized, use it
if (this._cacheInitialized && this._recentMessagesCache.length > 0) {
return this._recentMessagesCache.slice(-limit);
}
// If cache is empty, try in-memory fallback
if (this._inMemoryHistory.length > 0) {
return this._inMemoryHistory.slice(-limit);
}
// If nothing in memory, try database
if (this.db) {
try {
const result = await this.db.query(`
SELECT id, socket_id, username, message, message_type, created_at
FROM chat_messages
ORDER BY created_at DESC
LIMIT $1
`, [limit]);
return result.rows.reverse().map(row => ({
id: row.id,
socketId: row.socket_id,
username: row.username,
message: row.message,
type: row.message_type,
timestamp: new Date(row.created_at).getTime()
}));
} catch (error) {
console.error('Failed to get recent messages from DB:', error.message);
}
}
return [];
}
/**
* Clean up old messages (older than 30 days by default)
* @param {number} maxAgeMs - Maximum message age in milliseconds
* @returns {number} Number of messages deleted
*/
async cleanupOldMessages(maxAgeMs = this._maxMessageAge) {
if (!this.db) return 0;
try {
const cutoffDate = new Date(Date.now() - maxAgeMs);
const result = await this.db.query(`
DELETE FROM chat_messages
WHERE created_at < $1
`, [cutoffDate]);
const deletedCount = result.rowCount || 0;
if (this.debugManager?.CONSOLE_LOGGING && deletedCount > 0) {
console.log(`🧹 Cleaned up ${deletedCount} old chat messages`);
}
return deletedCount;
} catch (error) {
console.error('Chat cleanup error:', error.message);
return 0;
}
}
/**
* Get message count for statistics
* @returns {Object} Statistics about chat messages
*/
async getStats() {
const stats = {
cachedMessages: this._recentMessagesCache.length,
inMemoryFallback: this._inMemoryHistory.length,
totalInDb: null
};
if (this.db) {
try {
const result = await this.db.query('SELECT COUNT(*) as count FROM chat_messages');
stats.totalInDb = parseInt(result.rows[0]?.count || 0);
} catch (error) {
// Ignore errors for stats
}
}
return stats;
}
/**
* Shutdown - cleanup intervals
*/
shutdown() {
if (this._cleanupInterval) {
clearInterval(this._cleanupInterval);
this._cleanupInterval = null;
}
}
}
module.exports = ChatHistoryManager;
+5 -2
View File
@@ -3,6 +3,8 @@
* Handles game queue operations, validation, and queue management
*/
const { normalizeError } = require('../utils/errors');
class QueueHandler {
constructor({ queueManager, gameModeManager, paymentHandlers, activeGames, broadcastManager, debugManager, rateLimiter }) {
this.queueManager = queueManager;
@@ -87,8 +89,9 @@ class QueueHandler {
console.log(`🕒 QUEUE ENTRY: Player ${socket.id} queued for block ${nextBlock}. Queue length: ${this.queueManager.getQueueLength()}`);
}
} catch (error) {
console.error('handleGameQueue error:', error);
this.broadcastManager.sendStatusUpdate(socket.id, 'error', 'Failed to join queue. Please try again.');
const normalized = normalizeError(error, 'Failed to join queue');
console.error('handleGameQueue error:', normalized.message);
this.broadcastManager.sendStatusUpdate(socket.id, 'error', normalized.message);
}
}
+17 -4
View File
@@ -6,6 +6,9 @@
* - Starting games when a new block arrives
* - Preserving unconfirmed (mempool-only) entries across blocks
*/
const { normalizeError } = require('../utils/errors');
class QueueManager {
constructor({ debugManager, broadcastManager, io, createGameForUser, getUserBySocket, activeGames, gameModeManager, consoleLogging }) {
this.debugManager = debugManager;
@@ -84,11 +87,16 @@ class QueueManager {
const game = this.createGameForUser(currentUser, 'standard');
const gameState = game.getState();
gameState.blockHeight = blockHeight;
// Include provably fair commitment
if (game.getProofCommitment) {
gameState.proof = game.getProofCommitment();
}
this.io.to(serverId).emit('game_start', gameState);
if (this.CONSOLE_LOGGING) console.log(`[QueueManager] Game started for ${serverId}`);
} catch (error) {
console.error('[QueueManager] Error creating game:', error);
this.io.to(serverId).emit('message', 'Error starting game: ' + error.message);
const normalized = normalizeError(error, 'Failed to start game');
console.error('[QueueManager] Error creating game:', normalized.message);
this.io.to(serverId).emit('message', 'Error starting game: ' + normalized.message);
}
}
if (remainingQueue.length > 0) {
@@ -132,12 +140,17 @@ class QueueManager {
const game = this.createGameForUser(currentUser, 'standard');
const gameState = game.getState();
gameState.blockHeight = blockHeight;
// Include provably fair commitment
if (game.getProofCommitment) {
gameState.proof = game.getProofCommitment();
}
this.io.to(serverId).emit('game_start', gameState);
if (this.CONSOLE_LOGGING) console.log(`[QueueManager] (immediate) Game started for ${serverId} at block ${blockHeight}`);
return true;
} catch (err) {
console.error('[QueueManager] Error starting immediate game:', err);
this.io.to(serverId).emit('message', 'Error starting game: ' + err.message);
const normalized = normalizeError(err, 'Failed to start immediate game');
console.error('[QueueManager] Error starting immediate game:', normalized.message);
this.io.to(serverId).emit('message', 'Error starting game: ' + normalized.message);
return false;
}
}
+22
View File
@@ -236,6 +236,28 @@ class RateLimiter {
};
}
/**
* Get count of tracked entries (for dashboard)
*/
getTrackedCount() {
return this.storage.size + this.ipStorage.size;
}
/**
* Get count of currently blocked users (rough estimate)
*/
getBlockedCount() {
const now = Date.now();
let blocked = 0;
for (const [key, data] of this.storage.entries()) {
const limit = this._getLimitFromKey(key);
if (limit && data.count >= limit.max && (now - data.firstAttempt) < limit.window) {
blocked++;
}
}
return blocked;
}
/**
* Shutdown the rate limiter (cleanup interval)
*/
+6 -3
View File
@@ -3,6 +3,7 @@
* Issues and resumes anonymous session tokens to persist credits & payout address across reconnects.
*/
const crypto = require('crypto');
const { normalizeError } = require('../utils/errors');
class SessionManager {
constructor({ db, debugManager, gameModeManager }) {
@@ -72,8 +73,9 @@ class SessionManager {
user: newUser
};
} catch (error) {
console.error('[SessionManager] Error in resumeOrCreate:', error);
throw error;
const normalized = normalizeError(error, 'Failed to resume or create session');
console.error('[SessionManager] Error in resumeOrCreate:', normalized.message);
throw normalized;
}
}
@@ -146,7 +148,8 @@ class SessionManager {
console.log(`[SessionManager] Cleaned up ${result.rowCount} expired sessions`);
}
} catch (error) {
console.error('[SessionManager] Error cleaning up sessions:', error);
const normalized = normalizeError(error, 'Failed to cleanup expired sessions');
console.error('[SessionManager] Error cleaning up sessions:', normalized.message);
}
}
+139 -4
View File
@@ -14,6 +14,7 @@ const RateLimiter = require('./rateLimiter');
const ConnectionHandler = require('./connectionHandler');
const ChatHandler = require('./chatHandler');
const QueueHandler = require('./queueHandler');
const SpectatorManager = require('./spectatorManager');
const MemoryManager = require('../utils/memoryManager');
const { normalizeError } = require('../utils/errors');
@@ -120,7 +121,7 @@ class SocketHandlers {
}
});
// Initialize chat handler
// Initialize chat handler with database for persistent history
this.chatHandler = new ChatHandler({
io: this.io,
broadcastManager: this.broadcastManager,
@@ -129,7 +130,13 @@ class SocketHandlers {
paymentHandlers: this.paymentHandlers,
queueManager: this.queueManager,
gameModeManager: this.gameModeManager,
rateLimiter: this.rateLimiter
rateLimiter: this.rateLimiter,
db: this.gameModeManager?.db
});
// Initialize chat history (async, non-blocking)
this.chatHandler.initialize().catch(err => {
console.error('Failed to initialize chat handler:', err.message);
});
// Initialize queue handler
@@ -143,6 +150,18 @@ class SocketHandlers {
rateLimiter: this.rateLimiter
});
// Initialize spectator manager for live game viewing
this.spectatorManager = new SpectatorManager({
io: this.io,
activeGames: this.activeGames,
broadcastManager: this.broadcastManager,
debugManager: this.debugManager
});
this.spectatorManager.initialize();
// Wire spectator manager to game manager (for game over notifications)
this.gameManager.setSpectatorManager(this.spectatorManager);
// Movement manager abstraction with memory cleanup
this.playerMoveTimestamps = new Map();
this.movementManager = new MovementManager({
@@ -150,7 +169,8 @@ class SocketHandlers {
io: this.io,
debugManager: this.debugManager,
moveCooldown: this.MOVE_COOLDOWN,
postMoveHook: ({ socketId, game, moveResult }) => this.afterPlayerMove(socketId, game, moveResult)
postMoveHook: ({ socketId, game, moveResult }) => this.afterPlayerMove(socketId, game, moveResult),
spectatorManager: this.spectatorManager // Pass for spectator broadcasts
});
// Register memory cleanup functions
@@ -318,6 +338,21 @@ class SocketHandlers {
socket.emit('game_mode_info', this.gameModeManager.getGameModeInfo());
}
// Send chat history to new user
if (this.chatHandler && typeof this.chatHandler.sendChatHistoryToUser === 'function') {
this.chatHandler.sendChatHistoryToUser(socket.id).catch(err => {
console.error('Failed to send chat history:', err.message);
});
}
// Join the lobby room for game list broadcasts
if (this.spectatorManager) {
this.spectatorManager.joinLobby(socket.id);
// Send initial game list
const gameList = this.spectatorManager.getActiveGamesList({ page: 1, pageSize: 20 });
socket.emit('active_games', gameList);
}
// Register event handlers
socket.on('chat message', (msg) => this.chatHandler.handleChatMessage(socket, msg, {
handleGameQueue: (socket) => this.queueHandler.handleGameQueue(socket, (socketId) => this.connectionHandler.getUserBySocket(socketId)),
@@ -336,6 +371,11 @@ class SocketHandlers {
socket.on('check_payment_status', (data) => this.handleCheckPaymentStatus(socket, data));
socket.on('get_user_credits', () => this.handleGetUserCredits(socket));
socket.on('address:update', (data) => this.handleAddressUpdate(socket, data));
// Spectator handlers
socket.on('get_active_games', (options) => this.handleGetActiveGames(socket, options));
socket.on('spectate_game', (data) => this.handleSpectateGame(socket, data));
socket.on('leave_spectate', () => this.handleLeaveSpectate(socket));
}
/**
@@ -411,6 +451,11 @@ class SocketHandlers {
const game = this.gameManager.createGameForUser(memUser, 'standard');
const state = game.getState();
state.blockHeight = blockHeight;
// Include provably fair commitment
if (game.getProofCommitment) {
state.proof = game.getProofCommitment();
}
// Process start (credits deduction / payment link)
if (this.gameModeManager) {
@@ -570,6 +615,11 @@ class SocketHandlers {
if (this.chatHandler) {
this.chatHandler.clearAddressConfirmation(socket.id);
}
// Clean up spectator state
if (this.spectatorManager) {
this.spectatorManager.handleDisconnect(socket.id);
}
});
}
@@ -661,6 +711,86 @@ class SocketHandlers {
}
}
// ====== SPECTATOR HANDLERS ======
/**
* Handle request for active games list
*/
handleGetActiveGames(socket, options = {}) {
try {
const gameList = this.spectatorManager.getActiveGamesList(options);
socket.emit('active_games', gameList);
} catch (err) {
console.error('handleGetActiveGames error:', err);
socket.emit('error', { message: 'Failed to get games list' });
}
}
/**
* Handle request to spectate a specific game
*/
handleSpectateGame(socket, data) {
try {
if (!data || !data.gameId) {
socket.emit('spectate_error', { message: 'Game ID is required' });
return;
}
// Check if user is currently in a game (can't spectate while playing)
if (this.activeGames.has(socket.id)) {
socket.emit('spectate_error', { message: 'Cannot spectate while in a game' });
return;
}
// Leave lobby when starting to spectate
this.spectatorManager.leaveLobby(socket.id);
const result = this.spectatorManager.addSpectator(socket.id, data.gameId);
if (result.success) {
socket.emit('spectate_start', {
gameId: result.gameId,
playerId: result.playerSocketId?.substring(0, 6),
initialState: result.initialState,
spectatorCount: result.spectatorCount
});
this.broadcastManager.sendStatusUpdate(socket.id, 'info',
`👁️ Now spectating game by player ${result.playerSocketId?.substring(0, 6)}`);
} else {
socket.emit('spectate_error', { message: result.reason });
// Rejoin lobby if spectating failed
this.spectatorManager.joinLobby(socket.id);
}
} catch (err) {
console.error('handleSpectateGame error:', err);
socket.emit('spectate_error', { message: 'Failed to join game as spectator' });
}
}
/**
* Handle request to leave spectating
*/
handleLeaveSpectate(socket) {
try {
const wasSpectating = this.spectatorManager.removeSpectator(socket.id);
if (wasSpectating) {
socket.emit('spectate_ended', { reason: 'user_left' });
this.broadcastManager.sendStatusUpdate(socket.id, 'info', '👁️ Left spectator mode');
}
// Rejoin lobby
this.spectatorManager.joinLobby(socket.id);
// Send updated games list
const gameList = this.spectatorManager.getActiveGamesList({ page: 1, pageSize: 20 });
socket.emit('active_games', gameList);
} catch (err) {
console.error('handleLeaveSpectate error:', err);
}
}
/**
* Get comprehensive statistics
*/
@@ -672,7 +802,8 @@ class SocketHandlers {
connections: this.connectionHandler.getStats(),
chat: this.chatHandler.getStats(),
games: this.gameManager.getStats(),
queue: this.queueHandler.getStats()
queue: this.queueHandler.getStats(),
spectators: this.spectatorManager ? this.spectatorManager.getStats() : null
};
}
@@ -685,6 +816,10 @@ class SocketHandlers {
}
// Shutdown components in reverse order of initialization
if (this.spectatorManager) {
this.spectatorManager.shutdown();
}
if (this.chatHandler) {
this.chatHandler.shutdown();
}
+447
View File
@@ -0,0 +1,447 @@
/**
* Spectator Manager
* Manages spectator connections and game broadcasts
*
* Design considerations for scalability:
* - Efficient tracking of spectators per game
* - Periodic game list broadcasts instead of per-update
* - Room-based broadcasting using Socket.IO rooms
* - Memory-efficient cleanup of stale spectators
*/
class SpectatorManager {
constructor({ io, activeGames, broadcastManager, debugManager }) {
this.io = io;
this.activeGames = activeGames;
this.broadcastManager = broadcastManager;
this.debugManager = debugManager;
// Map of gameId -> Set of spectator socket IDs
this._spectatorsByGame = new Map();
// Map of socketId -> gameId (for quick lookup of what a spectator is watching)
this._spectatorWatching = new Map();
// Cached game list for efficient broadcasting
this._cachedGameList = [];
this._gameListLastUpdate = 0;
this._gameListCacheTimeout = 2000; // 2 second cache
// Periodic game list broadcast interval
this._broadcastInterval = null;
this._broadcastIntervalMs = 3000; // Broadcast every 3 seconds
}
/**
* Initialize the manager - start periodic broadcasts
*/
initialize() {
// Start periodic game list broadcasting
this._broadcastInterval = setInterval(() => {
this._broadcastGameListToLobby();
}, this._broadcastIntervalMs);
if (this.debugManager?.CONSOLE_LOGGING) {
console.log('👁️ SpectatorManager initialized');
}
}
/**
* Get list of active games with spectator-safe info
* @param {Object} options - Filtering/pagination options
* @returns {Array} List of active games with metadata
*/
getActiveGamesList(options = {}) {
const { page = 1, pageSize = 20, sortBy = 'newest' } = options;
// Check cache
const now = Date.now();
if (now - this._gameListLastUpdate < this._gameListCacheTimeout && this._cachedGameList.length > 0) {
return this._applyPagination(this._cachedGameList, page, pageSize);
}
// Build fresh list
const games = [];
for (const [socketId, game] of this.activeGames.entries()) {
if (game && game.gameState === 'active') {
games.push(this._buildGameSummary(socketId, game));
}
}
// Sort games
this._sortGames(games, sortBy);
// Cache the list
this._cachedGameList = games;
this._gameListLastUpdate = now;
return this._applyPagination(games, page, pageSize);
}
/**
* Build a spectator-safe game summary
* @private
*/
_buildGameSummary(socketId, game) {
const spectatorCount = this._spectatorsByGame.get(game.id)?.size || 0;
return {
gameId: game.id,
playerId: socketId.substring(0, 6), // Anonymized player ID
startedAt: game.startedAt,
moveCount: game.moveCount || 0,
hasTreasure: game.player?.hasTreasure || false,
spectatorCount: spectatorCount,
// Don't expose exact positions - let spectators get that when they join
dungeonSize: {
width: game.width || 25,
height: game.height || 19
},
difficulty: game.difficultyConfig?.presetName || 'normal',
durationSeconds: game.startedAt ? Math.floor((Date.now() - game.startedAt) / 1000) : 0
};
}
/**
* Sort games array by criteria
* @private
*/
_sortGames(games, sortBy) {
switch (sortBy) {
case 'newest':
games.sort((a, b) => b.startedAt - a.startedAt);
break;
case 'oldest':
games.sort((a, b) => a.startedAt - b.startedAt);
break;
case 'spectators':
games.sort((a, b) => b.spectatorCount - a.spectatorCount);
break;
case 'moves':
games.sort((a, b) => b.moveCount - a.moveCount);
break;
default:
games.sort((a, b) => b.startedAt - a.startedAt);
}
}
/**
* Apply pagination to games list
* @private
*/
_applyPagination(games, page, pageSize) {
const totalGames = games.length;
const totalPages = Math.ceil(totalGames / pageSize);
const startIndex = (page - 1) * pageSize;
const endIndex = startIndex + pageSize;
return {
games: games.slice(startIndex, endIndex),
pagination: {
page,
pageSize,
totalGames,
totalPages,
hasNext: page < totalPages,
hasPrev: page > 1
}
};
}
/**
* Add a spectator to a game
* @param {string} spectatorSocketId - Socket ID of the spectator
* @param {string} gameId - ID of the game to spectate
* @returns {Object} Result with success status and initial game state
*/
addSpectator(spectatorSocketId, gameId) {
// Find the game by gameId
let targetGame = null;
let playerSocketId = null;
for (const [socketId, game] of this.activeGames.entries()) {
if (game && game.id === gameId) {
targetGame = game;
playerSocketId = socketId;
break;
}
}
if (!targetGame) {
return { success: false, reason: 'Game not found or has ended' };
}
if (targetGame.gameState !== 'active') {
return { success: false, reason: 'Game is no longer active' };
}
// Check if already spectating this game
const currentlyWatching = this._spectatorWatching.get(spectatorSocketId);
if (currentlyWatching === gameId) {
return { success: false, reason: 'Already spectating this game' };
}
// If spectating another game, leave it first
if (currentlyWatching) {
this.removeSpectator(spectatorSocketId);
}
// Add to spectators set for this game
if (!this._spectatorsByGame.has(gameId)) {
this._spectatorsByGame.set(gameId, new Set());
}
this._spectatorsByGame.get(gameId).add(spectatorSocketId);
// Track what this spectator is watching
this._spectatorWatching.set(spectatorSocketId, gameId);
// Join Socket.IO room for this game
const spectatorSocket = this._getSocket(spectatorSocketId);
if (spectatorSocket) {
spectatorSocket.join(`spectate:${gameId}`);
}
if (this.debugManager?.CONSOLE_LOGGING) {
console.log(`👁️ Spectator ${spectatorSocketId.substring(0,6)} joined game ${gameId.substring(0,8)}`);
}
// Get initial game state for spectator
const initialState = this._getSpectatorGameState(targetGame, playerSocketId);
return {
success: true,
gameId: gameId,
playerSocketId: playerSocketId,
initialState: initialState,
spectatorCount: this._spectatorsByGame.get(gameId).size
};
}
/**
* Remove a spectator from their current game
* @param {string} spectatorSocketId - Socket ID of the spectator
* @returns {boolean} Whether removal was successful
*/
removeSpectator(spectatorSocketId) {
const gameId = this._spectatorWatching.get(spectatorSocketId);
if (!gameId) return false;
// Remove from game's spectator set
const spectators = this._spectatorsByGame.get(gameId);
if (spectators) {
spectators.delete(spectatorSocketId);
if (spectators.size === 0) {
this._spectatorsByGame.delete(gameId);
}
}
// Remove from watching map
this._spectatorWatching.delete(spectatorSocketId);
// Leave Socket.IO room
const spectatorSocket = this._getSocket(spectatorSocketId);
if (spectatorSocket) {
spectatorSocket.leave(`spectate:${gameId}`);
}
if (this.debugManager?.CONSOLE_LOGGING) {
console.log(`👁️ Spectator ${spectatorSocketId.substring(0,6)} left game ${gameId.substring(0,8)}`);
}
return true;
}
/**
* Get what game a socket is currently spectating
* @param {string} socketId - Socket ID to check
* @returns {string|null} Game ID or null
*/
getSpectatingGame(socketId) {
return this._spectatorWatching.get(socketId) || null;
}
/**
* Check if a socket is currently spectating any game
* @param {string} socketId - Socket ID to check
* @returns {boolean}
*/
isSpectating(socketId) {
return this._spectatorWatching.has(socketId);
}
/**
* Get spectator count for a game
* @param {string} gameId - Game ID
* @returns {number}
*/
getSpectatorCount(gameId) {
return this._spectatorsByGame.get(gameId)?.size || 0;
}
/**
* Broadcast game update to all spectators of a game
* @param {string} gameId - Game ID
* @param {Object} gameState - Updated game state
*/
broadcastToSpectators(gameId, gameState) {
const spectators = this._spectatorsByGame.get(gameId);
if (!spectators || spectators.size === 0) return;
// Use Socket.IO room for efficient broadcasting
this.io.to(`spectate:${gameId}`).emit('spectator_update', {
gameId: gameId,
gameState: gameState,
timestamp: Date.now()
});
}
/**
* Notify spectators that a game has ended
* @param {string} gameId - Game ID
* @param {Object} gameOverData - Game over information
*/
notifyGameEnded(gameId, gameOverData) {
const spectators = this._spectatorsByGame.get(gameId);
if (!spectators || spectators.size === 0) return;
// Emit game over to spectators
this.io.to(`spectate:${gameId}`).emit('spectate_ended', {
gameId: gameId,
reason: 'game_over',
gameOverData: gameOverData
});
// Remove all spectators from this game's tracking
for (const spectatorId of spectators) {
this._spectatorWatching.delete(spectatorId);
const sock = this._getSocket(spectatorId);
if (sock) sock.leave(`spectate:${gameId}`);
}
this._spectatorsByGame.delete(gameId);
// Invalidate cache
this._gameListLastUpdate = 0;
}
/**
* Get spectator-safe game state
* @private
*/
_getSpectatorGameState(game, playerSocketId) {
// Return a sanitized version of the game state
if (typeof game.getState === 'function') {
const state = game.getState();
return {
...state,
playerId: playerSocketId.substring(0, 6),
isSpectating: true
};
}
// Fallback for legacy game structure
return {
player: game.player ? {
x: game.player.x,
y: game.player.y,
hasTreasure: game.player.hasTreasure
} : null,
monster: game.monster ? {
x: game.monster.x,
y: game.monster.y
} : null,
visibleTiles: game.visibleTiles,
lighting: game.lightingAndFov?.getLightingData ? game.lightingAndFov.getLightingData() : null,
torches: game.dungeon?.torches || [],
playerId: playerSocketId.substring(0, 6),
isSpectating: true
};
}
/**
* Broadcast game list to users in the lobby (not in a game, not spectating)
* @private
*/
_broadcastGameListToLobby() {
// Get fresh game list
const gameListData = this.getActiveGamesList({ page: 1, pageSize: 50 });
// Only broadcast if there are active games
if (gameListData.pagination.totalGames === 0) return;
// Broadcast to a "lobby" room - users join this room when not in game
this.io.to('lobby').emit('active_games', gameListData);
}
/**
* Add a socket to the lobby room
* @param {string} socketId - Socket ID
*/
joinLobby(socketId) {
const sock = this._getSocket(socketId);
if (sock) {
sock.join('lobby');
}
}
/**
* Remove a socket from the lobby room
* @param {string} socketId - Socket ID
*/
leaveLobby(socketId) {
const sock = this._getSocket(socketId);
if (sock) {
sock.leave('lobby');
}
}
/**
* Get a socket by ID
* @private
*/
_getSocket(socketId) {
return this.io.sockets.sockets.get(socketId);
}
/**
* Get statistics about spectator system
* @returns {Object}
*/
getStats() {
return {
totalSpectators: this._spectatorWatching.size,
gamesBeingWatched: this._spectatorsByGame.size,
cachedGameListSize: this._cachedGameList.length,
spectatorsByGame: Object.fromEntries(
Array.from(this._spectatorsByGame.entries()).map(([gameId, set]) => [
gameId.substring(0, 8),
set.size
])
)
};
}
/**
* Cleanup when a socket disconnects
* @param {string} socketId - Socket ID that disconnected
*/
handleDisconnect(socketId) {
// Remove from spectating if applicable
this.removeSpectator(socketId);
// Leave lobby is automatic when socket disconnects
}
/**
* Shutdown the manager
*/
shutdown() {
if (this._broadcastInterval) {
clearInterval(this._broadcastInterval);
this._broadcastInterval = null;
}
this._spectatorsByGame.clear();
this._spectatorWatching.clear();
this._cachedGameList = [];
}
}
module.exports = SpectatorManager;