[ui-pvp] follow host-pvp #77: POST /invites/:code/join, drafting phase, record decks as strength

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 22:52:40 -05:00
co-authored by Claude Opus 5.5
parent 53736e12a0
commit a032c6e5d6
4 changed files with 17 additions and 11 deletions
+1 -1
View File
@@ -219,7 +219,7 @@ export function DuelOnlinePlay({ modeId, modeName, deck, entry, accountToken, on
return <section className="play-panel" aria-labelledby="wait-title"> return <section className="play-panel" aria-labelledby="wait-title">
<div className="section-eyebrow">{eyebrow}</div> <div className="section-eyebrow">{eyebrow}</div>
<h2 id="wait-title">{status?.phase === 'expired' ? 'This lobby expired' : !seat ? (error ? 'Could not start' : 'Connecting…') : status?.phase === 'starting' ? 'Starting…' : entry.kind === 'quick' ? 'Finding an opponent' : 'Waiting for your opponent'}</h2> <h2 id="wait-title">{status?.phase === 'expired' ? 'This lobby expired' : !seat ? (error ? 'Could not start' : 'Connecting…') : status?.phase === 'starting' ? 'Starting…' : status?.phase === 'drafting' ? 'Drafting…' : entry.kind === 'quick' ? 'Finding an opponent' : 'Waiting for your opponent'}</h2>
{seat && link && status?.phase !== 'expired' && <> {seat && link && status?.phase !== 'expired' && <>
<p className="muted">Send this link to the person you want to play. It opens this lobby, and they bring their own {deck.length}-card deck.</p> <p className="muted">Send this link to the person you want to play. It opens this lobby, and they bring their own {deck.length}-card deck.</p>
<div className="invite-box"><code aria-label="Invite link">{link}</code><button className="button outline" onClick={copy}><Copy size={14} /> {'share' in navigator ? 'Share' : 'Copy'}</button></div> <div className="invite-box"><code aria-label="Invite link">{link}</code><button className="button outline" onClick={copy}><Copy size={14} /> {'share' in navigator ? 'Share' : 'Copy'}</button></div>
+5 -9
View File
@@ -81,7 +81,7 @@ export const online = {
export type DuelSeat = { lobbyId: string; seat: 0 | 1; token: string; invite?: string }; export type DuelSeat = { lobbyId: string; seat: 0 | 1; token: string; invite?: string };
export type DuelStatus = { export type DuelStatus = {
lobbyId: string; version: number; lobbyId: string; version: number;
phase: 'waiting' | 'starting' | 'playing' | 'finished' | 'expired'; phase: 'waiting' | 'starting' | 'drafting' | 'playing' | 'finished' | 'expired';
seat: 0 | 1 | null; players: { name: string; rating?: number }[]; seat: 0 | 1 | null; players: { name: string; rating?: number }[];
outcome: { winner: 0 | 1 | null; reason: string } | null; outcome: { winner: 0 | 1 | null; reason: string } | null;
matchId?: string; invite?: string; matchId?: string; invite?: string;
@@ -92,7 +92,7 @@ export type LadderEntry = { name: string; rating: number | null; wins: number; l
const sid = (v: unknown, max = 128) => (typeof v === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(v) ? v.slice(0, max) : ''); const sid = (v: unknown, max = 128) => (typeof v === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(v) ? v.slice(0, max) : '');
const seatNo = (v: unknown): 0 | 1 | null => (v === 0 || v === 1 ? v : null); const seatNo = (v: unknown): 0 | 1 | null => (v === 0 || v === 1 ? v : null);
const PHASES = new Set(['waiting', 'starting', 'playing', 'finished', 'expired']); const PHASES = new Set(['waiting', 'starting', 'drafting', 'playing', 'finished', 'expired']);
export function parseSeat(v: unknown): DuelSeat { export function parseSeat(v: unknown): DuelSeat {
const o = (v && typeof v === 'object' ? v : {}) as Record<string, unknown>; const o = (v && typeof v === 'object' ? v : {}) as Record<string, unknown>;
@@ -162,13 +162,9 @@ async function duelCall(path: string, init: DuelInit = {}): Promise<{ status: nu
export const pvp = { export const pvp = {
createPrivate: (modeId: string, deck: string[], idToken: string) => createPrivate: (modeId: string, deck: string[], idToken: string) =>
duelCall(`modes/${id(modeId)}/lobbies`, { body: { deck, private: true, idToken } }).then(r => parseSeat(r.data)), duelCall(`modes/${id(modeId)}/lobbies`, { body: { deck, private: true, idToken } }).then(r => parseSeat(r.data)),
/** GET /invites/:code resolves the private lobby, then a normal join with our deck. */ /** POST /invites/:code/join (host-pvp, docs/game/HOSTING.md "Seats, invites, quick match and polling"). */
joinInvite: async (code: string, deck: string[], idToken: string) => { joinInvite: (code: string, deck: string[], idToken: string) =>
const lobby = (await duelCall(`invites/${id(code)}`)).data as Record<string, unknown> | null; duelCall(`invites/${id(code)}/join`, { body: { deck, idToken } }).then(r => parseSeat(r.data)),
const lobbyId = sid(lobby?.lobbyId);
if (!lobbyId) throw new Error('This invite link has expired or was already used.');
return parseSeat((await duelCall(`lobbies/${id(lobbyId)}/join`, { body: { deck, idToken } })).data);
},
quick: (modeId: string, deck: string[], idToken: string) => quick: (modeId: string, deck: string[], idToken: string) =>
duelCall(`modes/${id(modeId)}/quick`, { body: { deck, idToken } }).then(r => parseSeat(r.data)), duelCall(`modes/${id(modeId)}/quick`, { body: { deck, idToken } }).then(r => parseSeat(r.data)),
/** Returns null when the host answers 204 (nothing changed since `version`). */ /** Returns null when the host answers 204 (nothing changed since `version`). */
+2 -1
View File
@@ -85,7 +85,8 @@ export function parseMatchRecord(v: unknown): MatchRecord {
matchId: str(o.matchId ?? o.id, 128), modeId: str(o.modeId, 64) || undefined, modeName: str(o.modeName, 64) || undefined, matchId: str(o.matchId ?? o.id, 128), modeId: str(o.modeId, 64) || undefined, modeName: str(o.modeName, 64) || undefined,
players: [names[0], names[1]], startedAt: int(o.startedAt), endedAt: int(o.endedAt ?? o.finishedAt), winner, players: [names[0], names[1]], startedAt: int(o.startedAt), endedAt: int(o.endedAt ?? o.finishedAt), winner,
reason: str(r.reason, 60) || undefined, turns: int(o.turns ?? r.turns), reason: str(r.reason, 60) || undefined, turns: int(o.turns ?? r.turns),
strength: pair(o.strength, x => int(x) ?? 0), played: pair(o.played, refs), lost: pair(o.lost, refs), log, raw: v, // host-pvp records carry `decks` (engine cards per seat); their sizes are each side's strength.
strength: pair(o.strength, x => int(x) ?? 0) ?? pair(o.decks, x => (Array.isArray(x) ? x.length : 0)), played: pair(o.played, refs), lost: pair(o.lost, refs), log, raw: v,
}; };
} }
+9
View File
@@ -94,3 +94,12 @@ test('story markup becomes text runs; card titles cannot inject markup', () => {
const seen = [doc.lead, ...doc.sections.flatMap(s => s.paragraphs)].flatMap(runs); const seen = [doc.lead, ...doc.sections.flatMap(s => s.paragraphs)].flatMap(runs);
assert.ok(!seen.some(r => r.cite === 9)); assert.ok(!seen.some(r => r.cite === 9));
}); });
test('a host-pvp match record (winner/reason at top level, decks, no replay yet) still yields a plain recap', () => {
const doc = fallbackRecap(parseMatchRecord({ matchId: 'a'.repeat(32), modeId: 'classic', seed: 's', rules: {}, players: [{ id: 'x', name: 'alice' }, { id: 'y', name: 'bob' }],
winner: 1, reason: 'concede', startedAt: Date.UTC(2026, 10, 18, 1), endedAt: Date.UTC(2026, 10, 18, 1, 20), decks: [new Array(40).fill({}), new Array(40).fill({})], actions: [] }));
assert.equal(doc.title, 'Match of 18 November 2026');
assert.equal(doc.infobox.result, 'Victory for bob');
assert.deepEqual(doc.infobox.strength, ['40 cards', '40 cards']);
assert.match(doc.sections.flatMap(s => s.paragraphs).join(' '), /alice withdrew from the engagement\./);
});