// Battle screen — Street Fighter HUD style
const { useState: useBtState, useEffect: useBtEffect, useRef: useBtRef } = React;
function BattleScreen({ tournamentId, round, matchIdx }) {
const [tournament, setTournament] = useBtState(null);
const [loading, setLoading] = useBtState(true);
const [winsA, setWinsA] = useBtState(0);
const [winsB, setWinsB] = useBtState(0);
const [winner, setWinner] = useBtState(null); // 'a' | 'b'
const [submitting, setSubmitting] = useBtState(false);
const [phase, setPhase] = useBtState('intro'); // 'intro' | 'fight' | 'done'
const [flashSide, setFlashSide] = useBtState(null);
const flashRef = useBtRef(null);
const phaseRef = useBtRef(null);
useBtEffect(() => {
fetch(`/api/tournaments/${tournamentId}`)
.then(r => r.json())
.then(data => {
setTournament(backendToReact(data));
setLoading(false);
phaseRef.current = setTimeout(() => setPhase('fight'), 1600);
})
.catch(() => setLoading(false));
return () => { clearTimeout(flashRef.current); clearTimeout(phaseRef.current); };
}, []);
function triggerFlash(side) {
setFlashSide(side);
clearTimeout(flashRef.current);
flashRef.current = setTimeout(() => setFlashSide(null), 500);
}
async function handleWin(side) {
if (winner || submitting || phase !== 'fight') return;
const newA = side === 'a' ? winsA + 1 : winsA;
const newB = side === 'b' ? winsB + 1 : winsB;
if (side === 'a') setWinsA(newA); else setWinsB(newB);
triggerFlash(side);
const needed = tournament?.winsNeeded || 2;
if (newA >= needed || newB >= needed) {
const ws = newA >= needed ? 'a' : 'b';
setWinner(ws);
setPhase('done');
setTimeout(async () => {
setSubmitting(true);
try {
await fetch(`/api/tournaments/${tournamentId}/draw`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ round, match: matchIdx, side: ws }),
});
window.location.href = `/champion/${tournamentId}`;
} catch {
setSubmitting(false);
setWinner(null);
setPhase('fight');
}
}, 2600);
}
}
// ── Loading / error ──────────────────────────────────────────
if (loading) {
return (
);
}
const m = tournament?.bracket?.rounds[round]?.matches[matchIdx];
if (!m) {
return (
);
}
// ── Derived values ───────────────────────────────────────────
const fighterA = m.a;
const fighterB = m.b;
const needed = tournament?.winsNeeded || 2;
const bestOf = needed * 2 - 1;
const colorA = fighterA && !fighterA.bye ? fighterColor(fighterA.seed, fighterA.name) : { hex: '#888888' };
const colorB = fighterB && !fighterB.bye ? fighterColor(fighterB.seed, fighterB.name) : { hex: '#888888' };
const roundLabel = tournament.bracket.rounds[round]?.name || `Rodada ${round + 1}`;
const matchLabel = `${roundLabel.charAt(0)}${round + 1}.${String(matchIdx + 1).padStart(2, '0')}`;
const bgColor = tournament.bgColor || '#05080e';
const progA = Math.min(winsA / needed, 1) * 100;
const progB = Math.min(winsB / needed, 1) * 100;
const winnerFighter = winner ? (winner === 'a' ? fighterA : fighterB) : null;
const seedA = String(fighterA?.seed || 0).padStart(2, '0');
const seedB = String(fighterB?.seed || 0).padStart(2, '0');
return (
{/* ── Colored background glows ── */}
{/* ── HUD (Street Fighter top bar) ── */}
{/* P1 — clicável para pontuar */}
{/* Center */}
{roundLabel}
{winsA}
–
{winsB}
{matchLabel}
{/* P2 — clicável para pontuar */}
{/* ── Arena ── */}
{/* FIGHT! intro overlay */}
{phase === 'intro' && (
FIGHT!
)}
{/* Winner overlay */}
{phase === 'done' && winnerFighter && (
{winnerFighter.name}
VENCEU!
{submitting &&
registrando resultado...
}
)}
{/* ── Footer ── */}
MELHOR DE {bestOf}
);
}
window.BattleScreen = BattleScreen;