Files
wikideck/shared/game/keywords.ts
T
claudeandClaude Opus 5.5 7c86ed35f5 Game engine scaffold: seeded RNG, types, Ruleset schema, core loop
Pure deterministic off-chain engine in shared/game: createMatch, legalActions,
applyAction, isOver, result, viewFor, serialize/deserialize, replay. Placeholder
Snap-style lanes rules until DESIGN.md v1; keyword hook registry for engine-b.

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

86 lines
3.4 KiB
TypeScript

/**
* Keyword and lane-modifier registry. Effects are code here, but which ones a
* mode uses and their numbers are data (Ruleset.keywords / laneModifiers.params).
* Owner: engine-b (claude/wikideck#3). engine-a owns the hook contract below.
*
* Hook contract:
* - onReveal: runs once when the card is revealed, after it is placed in its lane.
* May mutate ctx.state (e.g. add `bonus` to cards, draw cards) but must use ctx.rng
* for any randomness and must not throw.
* - ongoing: pure; returns a power delta for `target` while `source` is in play.
* Called for every (source, target) pair in play, including source === target.
* - Lane modifiers have the same shape; `source` is null and `lane` is the modifier's lane.
* canPlay lets a lane reject a card (checked when staging).
*/
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/modifier from the ruleset. */
params: Record<string, number>;
lane: number;
log(text: string): void;
}
export interface KeywordDef {
id: string;
name: string;
/** Short rules text with {param} placeholders, for card faces/tooltips. */
text: string;
onReveal?(ctx: EffectContext, self: CardInPlay): void;
ongoing?(ctx: EffectContext, self: CardInPlay, target: CardInPlay): number;
}
export interface LaneModifierDef {
id: string;
name: string;
text: string;
/** Runs when any card is revealed in this lane (after its own onReveal). */
onReveal?(ctx: EffectContext, card: CardInPlay): void;
ongoing?(ctx: EffectContext, target: CardInPlay): number;
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);
}
export const KEYWORDS: Record<string, KeywordDef> = {
// Examples so the hook contract is exercised; engine-b replaces/extends per DESIGN.md.
rally: {
id: 'rally', name: 'Rally', text: 'On reveal: +{amount} power 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(`Rally +${amount}`);
},
},
anchor: {
id: 'anchor', name: 'Anchor', text: 'Ongoing: +{amount} power if this is your only card here.',
ongoing(ctx, self, target) {
return target === self && allies(ctx.state, self).length === 0 ? ctx.params.amount ?? 2 : 0;
},
},
};
export const LANE_MODIFIERS: Record<string, LaneModifierDef> = {
plain: { id: 'plain', name: 'Plain', text: 'No effect.' },
portal: {
id: 'portal', name: 'Portal', text: 'Cards of category {category} get +{amount} power here.',
// params.categoryIndex picks from CATEGORY_ORDER so params stay numeric.
ongoing(ctx, target) {
const category = CATEGORY_ORDER[ctx.params.categoryIndex ?? 0];
return target.card.category === category ? ctx.params.amount ?? 2 : 0;
},
},
narrow: {
id: 'narrow', name: 'Narrow', text: 'Only cards costing {maxCost} or less can be played here.',
canPlay(ctx, card) { return card.cost <= (ctx.params.maxCost ?? 3); },
},
};
export const CATEGORY_ORDER = ['Space', 'Nature', 'People', 'Science', 'History', 'Culture'] as const;