/** * Payment UI Manager * Handles the game mode selection and payment modal interactions */ const PaymentUI = { config: null, userCredits: 0, hasPayoutAddress: false, init: function() { console.log("Initializing PaymentUI..."); // Bind close button $('#close-payment').on('click', function() { $('#payment-ui').hide(); }); // Bind mode options (use delegation for dynamic content) $('#payment-ui').on('click', '.mode-option', function() { const mode = $(this).data('mode'); const action = $(this).data('action'); PaymentUI.handleModeSelection(mode, action); }); // Bind check payment button $('#check-payment').on('click', function() { const paymentId = $(this).data('payment-id'); if (paymentId && window.socket) { window.socket.emit('check_payment_status', { paymentId }); $(this).text('Checking...'); setTimeout(() => $('#check-payment').text('🔄 Check Payment'), 1000); } }); // Bind copy button $('#copy-address').on('click', function() { const addr = $('#payment-address').val(); if(addr) { // Try modern clipboard API first if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(addr).then(() => { $('#copy-address').text('Copied!'); setTimeout(() => $('#copy-address').html('📋 Copy Address'), 2000); }); } else { // Fallback const $temp = $(""); $("body").append($temp); $temp.val(addr).select(); document.execCommand("copy"); $temp.remove(); $('#copy-address').text('Copied!'); setTimeout(() => $('#copy-address').html('📋 Copy Address'), 2000); } } }); // Bind Shop Button (will be added to index.html) $('#shopButton').on('click', function() { PaymentUI.show(); }); // Listen for credits updates if (window.socket) { window.socket.on('credits_update', function(data) { PaymentUI.updateCredits(data.balance); }); } }, updateConfig: function(config) { console.log("PaymentUI config updated:", config); this.config = config; this.render(); }, updateCredits: function(balance) { this.userCredits = balance || 0; this.render(); // Update credit display if visible $('#user-credits-display').text(this.userCredits); }, render: function() { if (!this.config) return; const currency = this.config.cryptoType || 'WOW'; const decimals = currency === 'XMR' ? 12 : 11; const divisor = Math.pow(10, decimals); const bothEnabled = this.config.directModeEnabled && this.config.creditsModeEnabled; const hasCredits = this.userCredits >= (this.config.creditsPerGame || 1); // Build dynamic options container const $container = $('.game-modes'); $container.empty(); // Add credits balance display if credits mode is enabled if (this.config.creditsModeEnabled) { $container.append(`
Your Credits: ${this.userCredits}
`); } // If user has credits and credits mode is enabled, show "Use Credit" option first if (hasCredits && this.config.creditsModeEnabled) { const creditsPayoutsEnabled = this.config.creditsPayoutsEnabled; const payoutNote = creditsPayoutsEnabled ? '💰 Payouts enabled' : '⚡ Fast play • No payouts'; $container.append(` `); } // Show direct payment option if (this.config.directModeEnabled) { const price = (this.config.singleGamePrice / divisor).toFixed(decimals === 12 ? 4 : 2); const recommended = !hasCredits ? 'recommended' : ''; const directPayoutsEnabled = this.config.directPayoutsEnabled !== false; // Default true const payoutInfo = directPayoutsEnabled ? '💰 2x payout on escape, 3x with treasure' : '⚡ Play mode • No payouts'; $container.append(` `); } // Show buy credits option if credits mode is enabled if (this.config.creditsModeEnabled) { const price = (this.config.creditsPackagePrice / divisor).toFixed(decimals === 12 ? 4 : 2); const pkgCredits = this.config.creditsPackageCount || 10; $container.append(` `); } // Show FREE option only if payments are disabled if (!this.config.paymentsEnabled) { $container.append(` `); } }, handleModeSelection: function(mode, action) { console.log("Mode selected:", mode, "Action:", action); if (mode === 'FREE') { $('#payment-ui').hide(); // Emit to start game directly if (window.socket) { window.socket.emit('enter_game'); } return; } // If using existing credits, just start the game if (action === 'use_credit' && this.userCredits >= 1) { $('#payment-ui').hide(); if (window.socket) { window.socket.emit('enter_game'); // Will use credits automatically } return; } // Otherwise, request payment let paymentType = 'single_game'; if (action === 'buy_credits') { paymentType = 'credits_package'; } if (window.socket) { window.socket.emit('request_payment', { type: paymentType }); // Show loading state $('.game-modes').hide(); $('#payment-details').show(); $('#payment-status').text('Requesting payment address...'); $('#payment-amount').text('...'); $('#payment-address').val('Loading...'); } }, showPaymentRequest: function(data) { $('#payment-ui').show(); $('#payment-details').show(); $('.game-modes').hide(); // Hide selection, show payment const isCreditsPurchase = data.paymentType === 'credits_package'; const headerText = isCreditsPurchase ? '🎫 Buy Credits' : '💳 Payment Required'; $('.payment-header strong').text(headerText); const displayAmount = data.humanAmount || data.amountFormatted || data.amount; $('#payment-amount').text(displayAmount + ' ' + (data.currency || '')); $('#payment-address').val(data.address); $('#check-payment').data('payment-id', data.id); if (data.reused) { $('#payment-status').html('♻️ Using existing pending payment'); } else { const statusText = isCreditsPurchase ? 'Send payment to receive credits...' : 'Waiting for payment...'; $('#payment-status').text(statusText); } }, show: function() { $('#payment-ui').show(); $('.game-modes').show(); $('#payment-details').hide(); $('.payment-header strong').text('🎮 Game Mode Selection'); } }; // Expose globally window.PaymentUI = PaymentUI; // Initialize when DOM is ready $(document).ready(function() { PaymentUI.init(); });