Files
wikideck/shared/game/keywords.ts
T
claudeandClaude Opus 5.5 f3b6097640 Engine core: align with DESIGN.md draft (Edit War)
mulberry32 uint32 seed with the §4 draw order, Topics with stats and Portals,
staggered reveal, initiative and underdog rules, queue/undo/commit/timeout
actions, §9 flat RulesConfig with zod bounds, viewFor hidden info, replay.
cards.ts left to engine-b; placeholder loaners in starter.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 22:55:51 -05:00

88 lines
3.7 KiB
TypeScript

/**
* Keyword and Portal registry (DESIGN §10, §10.3). Effects are code here; which
* ones a card/topic uses and their numbers are data (GameCard.keyword,
* TopicDef.portal, Ruleset.keywordParams / portalParams).
* Owner of the effects: engine-b (claude/wikideck#3). engine-a owns the hook contract:
*
* - Hooks get an EffectContext over the engine's *working copy* of the state and may
* mutate it (e.g. `bonus` on cards). Mutating is safe: applyAction already cloned.
* Any randomness must come from ctx.rng (the match RNG, in resolution order, §4.2.5).
* - onReveal: once, right after the card enters its lane (§5.4).
* - endOfTurn: every card in play, at §5.5, initiative player's cards first, lanes left→right,
* in play order.
* - ongoing: PURE. Returns a power delta for `target` while `self` is in play. Called for every
* (self, target) pair in play, including self === target. ctx.rng is a throwaway copy.
* - Portals: same shape; ongoing/canPlay/onReveal only apply while the Topic is revealed.
* onTopicReveal runs when the Topic flips face-up (§5.1).
* - Hooks must not throw, must not move/remove cards (§10 v1 constraint).
*/
import type { Rng } from './rng.ts';
import type { CardInPlay, GameCard, MatchState, PlayerId } from './types.ts';
export interface EffectContext {
state: MatchState;
rng: Rng;
/** Tunable numbers for this keyword/portal from Ruleset.keywordParams / portalParams. */
params: Record<string, number>;
/** Lane the effect lives in. */
lane: number;
/** Add a line to the match log (attributed to this keyword/portal). */
log(text: string): void;
}
export interface KeywordDef {
id: string;
name: string;
/** One short line of reminder text with {param} placeholders. */
text: string;
onReveal?(ctx: EffectContext, self: CardInPlay): void;
endOfTurn?(ctx: EffectContext, self: CardInPlay): void;
ongoing?(ctx: EffectContext, self: CardInPlay, target: CardInPlay): number;
}
export interface PortalDef {
id: string;
name: string;
text: string;
onTopicReveal?(ctx: EffectContext): void;
/** Runs when any card is revealed in this Topic (after the card's own onReveal). */
onReveal?(ctx: EffectContext, card: CardInPlay): void;
ongoing?(ctx: EffectContext, target: CardInPlay): number;
/** Lets a Topic reject a card; checked when queuing and again at reveal. */
canPlay?(ctx: EffectContext, card: GameCard, player: PlayerId): boolean;
}
/** Cards on the same side of the same lane, excluding `self`. */
export function allies(state: MatchState, self: CardInPlay): CardInPlay[] {
return state.lanes[self.lane].cards[self.owner].filter(card => card !== self);
}
/** PLACEHOLDERS so the hook contract is exercised; engine-b replaces these per DESIGN §10. */
export const KEYWORDS: Record<string, KeywordDef> = {
rally: {
id: 'rally', name: 'Rally', text: 'On reveal: +{amount} to your other cards here.',
onReveal(ctx, self) {
const amount = ctx.params.amount ?? 1;
for (const ally of allies(ctx.state, self)) ally.bonus += amount;
ctx.log(`${self.card.title}: Rally +${amount}`);
},
},
anchor: {
id: 'anchor', name: 'Anchor', text: 'Ongoing: +{amount} if alone here.',
ongoing(ctx, self, target) {
return target === self && allies(ctx.state, self).length === 0 ? ctx.params.amount ?? 2 : 0;
},
},
};
export const PORTALS: Record<string, PortalDef> = {
spotlight: {
id: 'spotlight', name: 'Spotlight', text: 'Cards here get +{amount}.',
ongoing(ctx) { return ctx.params.amount ?? 1; },
},
paywall: {
id: 'paywall', name: 'Paywall', text: 'Only cards costing {maxCost} or less.',
canPlay(ctx, card) { return card.cost <= (ctx.params.maxCost ?? 3); },
},
};