// ── Poll Debate Status ── let pollInterval = null; async function pollDebateStatus() { if (!debateId) return; pollInterval = setInterval(async () => { try { const res = await fetch(`api.php/debate/${debateId}`); const data = await res.json(); if (!data) return; // 更新状态显示 if (data.status === 'running') { document.getElementById('phaseTag').textContent = '进行中...'; } else if (data.status === 'paused') { document.getElementById('phaseTag').textContent = '⏸ 已暂停'; } else if (data.status === 'finished' || data.status === 'stopped') { clearInterval(pollInterval); document.getElementById('phaseTag').textContent = '已结束'; } // 更新消息 const messages = data.messages || []; const phase = data.phase || ''; const currentRound = data.currentRound || 0; if (phase !== '') { const labels = { opening: '🎤 开场陈词', rebuttal: `⚔️ 第 ${currentRound} 轮`, closing: '📝 总结陈词', judge: '⚖️ 最终评判' }; const label = labels[phase] || phase; addPhaseTag(label); } // 去重:只处理新增的消息 for (let i = lastMsgIdx; i < messages.length; i++) { const msg = messages[i]; if (msg.t === 'msg') { addSysMsg(msg.text); } else if (msg.t === 'judge_round') { removeThinking('judge'); addJudgeRound(msg.data); } else if (msg.t === 'judge_final') { removeThinking('judge'); addJudgeFinal(msg.data); document.getElementById('btnExport').style.display = ''; } else if (msg.t === 'judge_text') { removeThinking('judge'); addJudgeComment(msg.text, msg.round); } } lastMsgIdx = messages.length; // 如果有结束横幅 if (data.status === 'finished' || data.status === 'stopped') { const reason = data.status === 'finished' ? '辩论结束' : '辩论已终止'; addEndBanner(reason); if (data.finalResult) { addJudgeFinal(data.finalResult); } } } catch (e) { console.error('Poll failed:', e); } }, 2000); // 每 2 秒轮询一次 }