forked from WikiDeck/wikideck
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:
@@ -24,6 +24,7 @@ export function OnlinePlay({ mode, playerName, onExit, onFinish }: {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const inFlight = useRef(false);
|
||||
const finished = useRef(false);
|
||||
const done = status?.phase === 'finished' || status?.phase === 'expired';
|
||||
|
||||
const refreshLobbies = useCallback(() => {
|
||||
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; }
|
||||
}, [seat]);
|
||||
useEffect(() => {
|
||||
if (!seat) return;
|
||||
if (!seat || done) return;
|
||||
void poll();
|
||||
const t = setInterval(() => { void poll(); }, POLL_MS);
|
||||
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(() => {
|
||||
const st = status?.state;
|
||||
if (finished.current || !status || !seat || status.phase !== 'finished' || !st?.result) return;
|
||||
finished.current = true;
|
||||
const names = st.lanes.map(l => l.topic.name);
|
||||
const t = setTimeout(() => onFinish(resultView(st.result!, seat.seat, names)), 900);
|
||||
return () => clearTimeout(t);
|
||||
finishTimer.current = setTimeout(() => onFinish(resultView(st.result!, seat.seat, names)), 900);
|
||||
}, [status, seat, onFinish]);
|
||||
useEffect(() => () => clearTimeout(finishTimer.current), []);
|
||||
|
||||
const enter = async (fn: () => Promise<LobbySeat>) => {
|
||||
setBusy(true); setError('');
|
||||
|
||||
+4
-3
@@ -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 {
|
||||
const opp: PlayerId = me === 0 ? 1 : 0;
|
||||
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) => {
|
||||
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
@@ -67,8 +67,9 @@ export type UIAction = (
|
||||
| { type: 'concede' }
|
||||
) & { engine?: Action };
|
||||
|
||||
/** Always from the viewer's side: 0 = you, 1 = your opponent, null = draw. */
|
||||
export type ResultView = {
|
||||
winner: PlayerIndex | null; // null = draw
|
||||
winner: PlayerIndex | null;
|
||||
lanes: { name: string; myPower: number; theirPower: number; winner: PlayerIndex | null }[];
|
||||
turns: number;
|
||||
reason: MatchResult['reason'];
|
||||
|
||||
@@ -45,7 +45,9 @@ test('UI-driven random play always finishes within the turn cap and is determini
|
||||
const ra = engine.result(a, 0);
|
||||
assert.ok(ra.turns <= DEFAULT_RULESET.turns, `match ${i}: ${JSON.stringify(ra)}`);
|
||||
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]]);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user