forked from WikiDeck/wikideck
ui-duel: accept untyped duel profiles from the collection and shape-check them (#58)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+10
-2
@@ -2,7 +2,15 @@
|
||||
import type { DuelAction, DuelCard, PlayerView } from '../../../shared/duel/types.ts';
|
||||
|
||||
/** A collection card as ui-pvp passes it (ownership's ChainCard satisfies this). `duel` is the server-derived frozen profile. */
|
||||
export type BotMatchCard = { tokenId: string; title: string; url?: string; imageUrl?: string; extract?: string; duel?: DuelCard };
|
||||
export type BotMatchCard = { tokenId: string; title: string; url?: string; imageUrl?: string; extract?: string; duel?: unknown };
|
||||
|
||||
const TYPES = ['Figure', 'Location', 'Event', 'Institution', 'Doctrine'];
|
||||
/** Shape check for a profile that arrives untyped over the wire (the engine's validateDeck does the rest). */
|
||||
export function isDuelCard(v: unknown): v is DuelCard {
|
||||
const c = v as Partial<DuelCard> | null;
|
||||
return !!c && typeof c === 'object' && typeof c.title === 'string' && TYPES.includes(c.type as string) && typeof c.cost === 'number'
|
||||
&& Array.isArray(c.keywords) && Array.isArray(c.effects) && typeof c.rulesText === 'string';
|
||||
}
|
||||
/** The bot sees only its own seat's view (Night 1 rule, #5). */
|
||||
export type DuelBot = { name: string; choose(view: PlayerView, legal: DuelAction[], rng: () => number): DuelAction };
|
||||
|
||||
@@ -25,7 +33,7 @@ export function playerDeck(deck: string[], cards: BotMatchCard[], deckSize: numb
|
||||
const out: DuelCard[] = [], pending: string[] = [], missing: string[] = [];
|
||||
for (const id of deck) {
|
||||
const c = byId.get(id);
|
||||
if (!c) missing.push(id); else if (!c.duel) pending.push(c.title); else out.push({ ...c.duel, cardId: id });
|
||||
if (!c) missing.push(id); else if (!isDuelCard(c.duel)) pending.push(c.title); else out.push({ ...c.duel, cardId: id });
|
||||
}
|
||||
return { cards: out.length === deckSize && deck.length === deckSize ? out : [], pending, missing };
|
||||
}
|
||||
|
||||
@@ -114,3 +114,12 @@ test('bot match decks: exactly deckSize owned, profiled cards; the bot deck is i
|
||||
assert.ok(a.every(c => c.cardId.startsWith('bot:')));
|
||||
assert.notDeepEqual(botDeck(pool, 40, 'other').map(c => c.cardId), a.map(c => c.cardId));
|
||||
});
|
||||
|
||||
test('bot match: untyped profiles from the wire are shape-checked', async () => {
|
||||
const { playerDeck, isDuelCard } = await import('../src/game/duel/decks.ts');
|
||||
assert.equal(isDuelCard(card('x')), true);
|
||||
assert.equal(isDuelCard({ title: 'x' }), false);
|
||||
assert.equal(isDuelCard(null), false);
|
||||
const cards = Array.from({ length: 40 }, (_, i) => ({ tokenId: `t${i}`, title: `C${i}`, duel: i === 5 ? { junk: true } : card(`d${i}`) }));
|
||||
assert.deepEqual(playerDeck(cards.map(c => c.tokenId), cards, 40).pending, ['C5']);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user