From 9330b3b943cfa066d310b99d39050d97907b9f08 Mon Sep 17 00:00:00 2001 From: jwinterm Date: Tue, 13 Jan 2026 16:04:50 -0500 Subject: [PATCH] Fix direct payment game start, reduce prod logging, QR improvements - Fix game start failure in direct payment mode when payment confirms before mempool detection (add player to queue if not already there) - Fix debug logging in production: correct indentation in debugRegisteredUsers, swap console_logging config (false in prod, true in debug) - Add close button to QR code container - Auto-hide QR on payment detected, confirmed, and game start --- html/js/network/socketHandlers.js | 24 +++++++++++++++++++-- src/index.js | 8 +++---- src/network/paymentHandlers.js | 36 ++++++++++++++++++++++++++++--- src/package.json | 8 +++---- 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/html/js/network/socketHandlers.js b/html/js/network/socketHandlers.js index 490354e..4512fe4 100644 --- a/html/js/network/socketHandlers.js +++ b/html/js/network/socketHandlers.js @@ -370,6 +370,9 @@ const SocketHandlers = { SocketHandlers._isQueued = false; SocketHandlers._updateEarlyEntryButton(); + // Hide any lingering QR code + SocketHandlers.hidePaymentQR(); + $('#messages').append($('
  • ').text("Starting game...")); // Display provably fair commitment if present @@ -683,12 +686,14 @@ const SocketHandlers = { if (statusDiv) { qrHolder = document.createElement('div'); qrHolder.id = 'paymentQRContainer'; - qrHolder.style.cssText = 'margin-top:6px;padding:8px;border:1px solid #0f0;background:#000;display:block;width:calc(100% - 18px);text-align:center;'; + qrHolder.style.cssText = 'position:relative;margin-top:6px;padding:8px;border:1px solid #0f0;background:#000;display:block;width:calc(100% - 18px);text-align:center;'; statusDiv.appendChild(qrHolder); } } if (qrHolder) { - qrHolder.innerHTML = 'Payment QR'; + // Create close button + const closeBtn = '
    ✕
    '; + qrHolder.innerHTML = closeBtn + 'Payment QR'; } } else { console.warn('Payment created but no QR data supplied by server.'); @@ -707,6 +712,14 @@ const SocketHandlers = { } } }, + + // Helper to hide/remove the payment QR code container + hidePaymentQR: function() { + const qrHolder = document.getElementById('paymentQRContainer'); + if (qrHolder) { + qrHolder.remove(); + } + }, onShowPaymentOptions: function(data) { console.log('Show payment options:', data); @@ -727,6 +740,9 @@ const SocketHandlers = { SocketHandlers._lastDisplayedConfirmation = data.paymentId; SocketHandlers._confirmationTimestamps[data.paymentId] = Date.now(); + // Hide the QR code now that payment is confirmed + SocketHandlers.hidePaymentQR(); + if (typeof Game !== 'undefined' && Game.drawWaitingScreen) { Game.drawWaitingScreen(); } @@ -751,6 +767,10 @@ const SocketHandlers = { if (SocketHandlers._mempoolShownForPayment.has(data.paymentId)) return; SocketHandlers._mempoolShownForPayment.add(data.paymentId); SocketHandlers._mempoolTimestamps[data.paymentId] = Date.now(); + + // Hide the QR code now that payment was detected + SocketHandlers.hidePaymentQR(); + if (typeof Game !== 'undefined') { Game._pendingPaymentDetected(data); if (Game.drawWaitingScreen) Game.drawWaitingScreen(); diff --git a/src/index.js b/src/index.js index 3ef2975..448161a 100644 --- a/src/index.js +++ b/src/index.js @@ -913,10 +913,10 @@ function debugRegisteredUsers() { console.log("📊 REGISTERED USERS DEBUG:"); const allUsers = require('./db/user').getAllUsers(); console.log(`Total registered users: ${allUsers.length}`); - allUsers.forEach(u => { - // u.id is the socket/server id; there is no separate socketId property on User - console.log(` - User ID: ${u.id}, Socket: ${u.id}, Client: ${u.clientId}`); - }); + allUsers.forEach(u => { + // u.id is the socket/server id; there is no separate socketId property on User + console.log(` - User ID: ${u.id}, Socket: ${u.id}, Client: ${u.clientId}`); + }); console.log(`Active games: ${activeGames.size}`); } } diff --git a/src/network/paymentHandlers.js b/src/network/paymentHandlers.js index 13df6ba..639ea54 100644 --- a/src/network/paymentHandlers.js +++ b/src/network/paymentHandlers.js @@ -310,14 +310,44 @@ class PaymentHandlers { } socket.emit('payment_confirmed', { paymentId: paymentRequest.id, message: 'Payment confirmed in block!', confirmations: status.confirmations }); - this.queueManager.markConfirmed(socket.id); + + // IMPORTANT: If payment confirmed before mempool detection (fast blocks), + // the player may not be in the queue yet. Add them now with confirmed=true. + const existingIdx = this.queueManager.getPlayerIndex(socket.id); + if (existingIdx === -1) { + console.log(`[PaymentHandlers] Payment confirmed but player not in queue - adding now (socket: ${socket.id})`); + // Try to get DB userId from session + let userId = null; + if (this.sessionManager?.sessions?.has(socket.id)) { + userId = this.sessionManager.sessions.get(socket.id).id; + } + this.queueManager.addPlayer({ + serverId: socket.id, + clientId: currentUser ? currentUser.clientId : null, + userId: userId, + paymentId: paymentRequest.id, + requiresConfirmation: false, // Already confirmed + confirmed: true + }); + } else { + // Player was in queue from mempool detection, just mark confirmed + this.queueManager.markConfirmed(socket.id); + } + // Attempt immediate game start so user doesn't wait another full block const currentBlock = this.debugManager.getCurrentBlockHeight ? this.debugManager.getCurrentBlockHeight() : null; if (currentBlock !== null) { const started = await this.queueManager.startGameImmediately(socket.id, currentBlock); - if (!started && this.debugManager.CONSOLE_LOGGING) { - console.log(`[PaymentHandlers] Immediate start skipped (not queued or still unconfirmed) for ${socket.id}`); + if (!started) { + console.log(`[PaymentHandlers] Immediate start failed for ${socket.id} - player will start on next block`); + if (this.debugManager.CONSOLE_LOGGING) { + this.queueManager.debugDumpQueue(); + } + } else { + console.log(`[PaymentHandlers] ✅ Immediate game start successful for ${socket.id}`); } + } else { + console.log(`[PaymentHandlers] No block height available - player will start on next block`); } } } diff --git a/src/package.json b/src/package.json index e43b322..4429f91 100644 --- a/src/package.json +++ b/src/package.json @@ -14,14 +14,14 @@ }, "config": { "debug": { - "enabled": false, - "console_logging": false, - "debug_hotkeys": false, + "enabled": true, + "console_logging": true, + "debug_hotkeys": true, "simulated_blocks": false }, "production": { "enabled": true, - "console_logging": true, + "console_logging": false, "debug_hotkeys": false, "simulated_blocks": false }