Guest Login Register
null; let tournamentState = null; let displayedRoundIndex = 0; let currentChatMatchId = null; let chatRefreshInterval = null; let lastKnownChatId = {}; let chatSoundEnabled = localStorage.getItem('chatSound') !== 'false'; function openMatchChat(btn) { const matchId = btn.getAttribute('data-match-id'); const player1 = btn.getAttribute('data-player1') || 'Player 1'; const player2 = btn.getAttribute('data-player2') || 'Player 2'; if (!matchId || matchId == 0) { alert("Invalid match. Chat will be available when the match is created."); return; } currentChatMatchId = parseInt(matchId); document.getElementById('chatModalTitle').innerHTML = `💬 Chat: ${player1} vs ${player2}`; markConversationRead(67, currentChatMatchId); loadChatMessages(); document.getElementById('chatModal').style.display = 'flex'; if (chatRefreshInterval) clearInterval(chatRefreshInterval); chatRefreshInterval = setInterval(loadChatMessages, 5000); } function openAdminChat() { currentChatMatchId = null; document.getElementById('chatModalTitle').innerHTML = `🎧 Admin Support - `; markConversationRead(67, 0); loadChatMessages(); document.getElementById('chatModal').style.display = 'flex'; if (chatRefreshInterval) clearInterval(chatRefreshInterval); chatRefreshInterval = setInterval(loadChatMessages, 5000); } function loadChatMessages() { const matchIdParam = (currentChatMatchId !== null && currentChatMatchId > 0) ? currentChatMatchId : 0; const lastId = lastKnownChatId[matchIdParam] || 0; fetch(`?id=67&action=get_messages&match_id=${matchIdParam}&last_id=${lastId}`) .then(res => res.json()) .then(data => { if (data.error) { console.warn(data.error); return; } if (data.new_messages && lastId > 0 && !data.last_sender_is_self) playBeep(); const container = document.getElementById('chatMessages'); container.innerHTML = ''; data.messages.forEach(msg => { const div = document.createElement('div'); div.className = `chat-message ${msg.is_own ? 'own' : ''}`; let adminTag = msg.is_admin ? 'Admin' : ''; div.innerHTML = `${escapeHtml(msg.username)} ${adminTag}
${escapeHtml(msg.message)}
${new Date(msg.created_at).toLocaleString()}`; container.appendChild(div); }); container.scrollTop = container.scrollHeight; lastKnownChatId[matchIdParam] = data.last_id; }).catch(err => console.error(err)); } function sendChatMessage() { const message = document.getElementById('chatInput').value.trim(); if (!message) return; // match_id must be 0 for admin chat, or positive integer for match chat const matchId = (currentChatMatchId !== null && currentChatMatchId > 0) ? currentChatMatchId : 0; const formData = new URLSearchParams(); formData.append('action', 'send_message'); formData.append('match_id', matchId); formData.append('message', message); formData.append('csrf_token', '58e775ac764384d34cec7b144532fef5a99b5f490edbbbccb3f8bc534047ac3c'); fetch(`?id=67`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest' }, body: formData }).then(res => res.json()).then(data => { if (data.success) { document.getElementById('chatInput').value = ''; loadChatMessages(); } else { alert('Failed to send: ' + (data.error || 'Unknown error')); } }).catch(err => alert('Error sending message')); } function markConversationRead(tournamentId, matchId) { fetch('ajax/mark_conversation_read.php', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `tournament_id=${tournamentId}&match_id=${matchId}&csrf_token=58e775ac764384d34cec7b144532fef5a99b5f490edbbbccb3f8bc534047ac3c` }).then(res=>res.json()).then(data=>{ if(data.success && data.updated>0) refreshUnreadCount(); }).catch(err=>console.error(err)); } // ... rest of JS (bracket render, etc.)