diff --git a/server/game/collection.ts b/server/game/collection.ts index 74044b2..e000c91 100644 --- a/server/game/collection.ts +++ b/server/game/collection.ts @@ -24,7 +24,8 @@ export type CollectionResponse = { needed: number; /** playable >= deckSize. */ eligible: boolean; - cards: ChainCard[]; + /** `duel` is present when the server has a frozen profile for the card (see collectionRouter's `duelCard`). */ + cards: (ChainCard & { duel?: unknown })[]; }; export function collectionResponse(deps: Pick, sub: string, @@ -37,7 +38,16 @@ export function collectionResponse(deps: Pick= r.deckSize, cards }; } -export function collectionRouter(deps: OwnershipDeps & { rules?: Partial }): Router { +export type CollectionRouterDeps = OwnershipDeps & { + rules?: Partial; + /** + * Optional: the same chain card → duel card function the PvP host uses, so the browser shows and + * plays exactly what PvP would. Return undefined while a card's profile is pending; it must not fetch. + */ + duelCard?: (card: ChainCard) => unknown; +}; + +export function collectionRouter(deps: CollectionRouterDeps): Router { const router = Router(); router.get('/', async (req: Request, res: Response) => { res.setHeader('Cache-Control', 'no-store'); @@ -46,7 +56,16 @@ export function collectionRouter(deps: OwnershipDeps & { rules?: Partial { + let duel: unknown; + try { duel = toDuel(c); } catch { duel = undefined; } + return duel === undefined ? c : { ...c, duel }; + }); + } + res.json(body); } catch (e) { const err = e as Error & { status?: number }; const status = e instanceof DeckError ? err.status! : err.status === 503 ? 503 : 500; diff --git a/tests/ownership-deck.test.ts b/tests/ownership-deck.test.ts index 309801c..073525e 100644 --- a/tests/ownership-deck.test.ts +++ b/tests/ownership-deck.test.ts @@ -273,3 +273,19 @@ test('GET /api/game/collection requires a verified eom.dev token (local loopback assert.equal((await res.json()).owned, 0); } finally { await close(); } }); + +test('GET /api/game/collection attaches the injected duel card when one is ready, and omits it otherwise', async () => { + const { ledger, deps } = fixture(); + mintMany(ledger, ALICE, 3); + const duelCard = (c: { pageId: number }) => { + if (c.pageId === 2) return undefined; // profile pending + if (c.pageId === 3) throw new Error('boom'); // never breaks the collection + return { kind: 'Figure', pageId: c.pageId }; + }; + const { base, close } = await serve(collectionRouter({ ...deps, duelCard })); + try { + const body = await (await fetch(base, { headers: { authorization: 'Bearer good.token.alice' } })).json(); + assert.deepEqual(body.cards.map((c: { duel?: unknown }) => c.duel), [{ kind: 'Figure', pageId: 1 }, undefined, undefined]); + assert.ok(!('duel' in body.cards[1])); + } finally { await close(); } +});