Play: online results from each player's side; finish timer survives polls

Found with a two-browser test against the modes host: the seat-1 player
saw Defeat after winning (ResultView is now viewer-relative), and a poll
could cancel the move to the results screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 23:43:00 -05:00
co-authored by Claude Opus 5.5
parent 60af45262d
commit efea1b70c7
5 changed files with 316 additions and 308 deletions
+8 -4
View File
@@ -24,6 +24,7 @@ export function OnlinePlay({ mode, playerName, onExit, onFinish }: {
const [busy, setBusy] = useState(false); const [busy, setBusy] = useState(false);
const inFlight = useRef(false); const inFlight = useRef(false);
const finished = useRef(false); const finished = useRef(false);
const done = status?.phase === 'finished' || status?.phase === 'expired';
const refreshLobbies = useCallback(() => { const refreshLobbies = useCallback(() => {
online.openLobbies(modeId).then(l => { setLobbies(l); setError(''); }, e => setError(message(e))); online.openLobbies(modeId).then(l => { setLobbies(l); setError(''); }, e => setError(message(e)));
@@ -43,20 +44,23 @@ export function OnlinePlay({ mode, playerName, onExit, onFinish }: {
finally { inFlight.current = false; } finally { inFlight.current = false; }
}, [seat]); }, [seat]);
useEffect(() => { useEffect(() => {
if (!seat) return; if (!seat || done) return;
void poll(); void poll();
const t = setInterval(() => { void poll(); }, POLL_MS); const t = setInterval(() => { void poll(); }, POLL_MS);
return () => clearInterval(t); return () => clearInterval(t);
}, [seat, poll]); }, [seat, poll, done]);
// Once finished, stop polling and show results after a beat. The timer must survive later
// re-renders, so it is only cleared on unmount.
const finishTimer = useRef<ReturnType<typeof setTimeout>>(undefined);
useEffect(() => { useEffect(() => {
const st = status?.state; const st = status?.state;
if (finished.current || !status || !seat || status.phase !== 'finished' || !st?.result) return; if (finished.current || !status || !seat || status.phase !== 'finished' || !st?.result) return;
finished.current = true; finished.current = true;
const names = st.lanes.map(l => l.topic.name); const names = st.lanes.map(l => l.topic.name);
const t = setTimeout(() => onFinish(resultView(st.result!, seat.seat, names)), 900); finishTimer.current = setTimeout(() => onFinish(resultView(st.result!, seat.seat, names)), 900);
return () => clearTimeout(t);
}, [status, seat, onFinish]); }, [status, seat, onFinish]);
useEffect(() => () => clearTimeout(finishTimer.current), []);
const enter = async (fn: () => Promise<LobbySeat>) => { const enter = async (fn: () => Promise<LobbySeat>) => {
setBusy(true); setError(''); setBusy(true); setError('');
+4 -3
View File
@@ -115,14 +115,15 @@ export function viewFromPlayerView(v: PlayerView, opts: { log?: string[]; capaci
}; };
} }
/** Results from `me`'s point of view (winner stays an absolute seat; the screen compares it to `me`). */ /** Results from `me`'s point of view: in a ResultView, 0 always means "you" and 1 "your opponent". */
export function resultView(r: MatchResult, me: PlayerId, laneNames: string[]): ResultView { export function resultView(r: MatchResult, me: PlayerId, laneNames: string[]): ResultView {
const opp: PlayerId = me === 0 ? 1 : 0; const opp: PlayerId = me === 0 ? 1 : 0;
return { return {
winner: r.winner, turns: r.turns, reason: r.reason, totalPower: [r.totalPower[me], r.totalPower[opp]], winner: r.winner === null ? null : r.winner === me ? 0 : 1, turns: r.turns, reason: r.reason,
totalPower: [r.totalPower[me], r.totalPower[opp]],
lanes: r.lanePower.map((pw, i) => { lanes: r.lanePower.map((pw, i) => {
const a = pw[me], b = pw[opp]; const a = pw[me], b = pw[opp];
return { name: laneNames[i] ?? `Topic ${i + 1}`, myPower: a, theirPower: b, winner: a === b ? null : a > b ? me : opp }; return { name: laneNames[i] ?? `Topic ${i + 1}`, myPower: a, theirPower: b, winner: a === b ? null : a > b ? 0 : 1 };
}), }),
}; };
} }
+2 -1
View File
@@ -67,8 +67,9 @@ export type UIAction = (
| { type: 'concede' } | { type: 'concede' }
) & { engine?: Action }; ) & { engine?: Action };
/** Always from the viewer's side: 0 = you, 1 = your opponent, null = draw. */
export type ResultView = { export type ResultView = {
winner: PlayerIndex | null; // null = draw winner: PlayerIndex | null;
lanes: { name: string; myPower: number; theirPower: number; winner: PlayerIndex | null }[]; lanes: { name: string; myPower: number; theirPower: number; winner: PlayerIndex | null }[];
turns: number; turns: number;
reason: MatchResult['reason']; reason: MatchResult['reason'];
+3 -1
View File
@@ -45,7 +45,9 @@ test('UI-driven random play always finishes within the turn cap and is determini
const ra = engine.result(a, 0); const ra = engine.result(a, 0);
assert.ok(ra.turns <= DEFAULT_RULESET.turns, `match ${i}: ${JSON.stringify(ra)}`); assert.ok(ra.turns <= DEFAULT_RULESET.turns, `match ${i}: ${JSON.stringify(ra)}`);
assert.deepEqual(ra, engine.result(b, 0)); assert.deepEqual(ra, engine.result(b, 0));
assert.equal(engine.result(a, 1).winner, ra.winner); const rb = engine.result(a, 1);
assert.equal(rb.winner, ra.winner === null ? null : 1 - ra.winner, 'each side sees the other side of the same result');
assert.deepEqual(rb.totalPower, [ra.totalPower[1], ra.totalPower[0]]);
} }
}); });