forked from WikiDeck/wikideck
209 lines
9.0 KiB
TypeScript
209 lines
9.0 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, Category, 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;
|
|
/** Current power of a card in play (engine's cardPower). Optional so tests can build contexts by hand. */
|
|
power?(card: CardInPlay): number;
|
|
}
|
|
|
|
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;
|
|
/** Runs at the end of each turn while in play (after all reveals). May mutate ctx.state. */
|
|
endOfTurn?(ctx: EffectContext, self: CardInPlay): void;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/** Faction of a lane (its Topic), or null. v1: an active `portal` lane modifier names one. */
|
|
export function laneFaction(state: MatchState, lane: number): Category | null {
|
|
const l = state.lanes[lane];
|
|
if (!l || l.modifier !== 'portal') return null;
|
|
const revealTurn = state.ruleset.laneModifiers.revealTurns[lane] ?? 1;
|
|
if (state.turn < revealTurn) return null;
|
|
return CATEGORY_ORDER[state.ruleset.laneModifiers.params.portal?.categoryIndex ?? 0] ?? null;
|
|
}
|
|
|
|
/** Ongoing power of a card, excluding effects that need the full engine (base + bonus + ongoing keywords here). */
|
|
function sidePower(ctx: EffectContext, lane: number, owner: PlayerId, exclude?: CardInPlay): number {
|
|
return ctx.state.lanes[lane].cards[owner].filter(card => card !== exclude)
|
|
.reduce((sum, card) => sum + (ctx.power ? ctx.power(card) : card.card.power + card.bonus), 0);
|
|
}
|
|
|
|
/** True when `card` should be shown face down to `viewer` (Obscure). viewFor uses this. */
|
|
export function hiddenFrom(state: MatchState, card: CardInPlay, viewer: PlayerId): boolean {
|
|
return card.owner !== viewer && state.phase !== 'over' && card.card.keyword === 'obscure'
|
|
&& Object.hasOwn(state.ruleset.keywords, 'obscure');
|
|
}
|
|
|
|
export const MENTION_TOKEN_PREFIX = 'token:mention:';
|
|
|
|
export const KEYWORDS: Record<string, KeywordDef> = {
|
|
// engine-a's original examples, kept for the placeholder starter pool and modes.
|
|
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;
|
|
},
|
|
},
|
|
|
|
// DESIGN.md (designer-b draft) §7.
|
|
edition: {
|
|
id: 'edition', name: 'Edition', text: 'Ongoing: +{amount} power for each other copy of this article on your side.',
|
|
ongoing(ctx, self, target) {
|
|
if (target !== self || self.card.pageId === null) return 0;
|
|
let copies = 0;
|
|
for (const lane of ctx.state.lanes) for (const card of lane.cards[self.owner])
|
|
if (card !== self && card.card.pageId === self.card.pageId) copies++;
|
|
return copies * (ctx.params.amount ?? 2);
|
|
},
|
|
},
|
|
translated: {
|
|
id: 'translated', name: 'Translated', text: 'On reveal: +{amount} power to your first card in each other Topic.',
|
|
onReveal(ctx, self) {
|
|
const amount = ctx.params.amount ?? 1;
|
|
ctx.state.lanes.forEach((lane, index) => {
|
|
const first = lane.cards[self.owner][0];
|
|
if (index !== self.lane && first) {
|
|
first.bonus += amount;
|
|
ctx.log(`Translated +${amount} to ${first.card.name}`);
|
|
}
|
|
});
|
|
},
|
|
},
|
|
viral: {
|
|
id: 'viral', name: 'Viral', text: 'On reveal: add a {amount}-power Mention to a random other Topic with space.',
|
|
onReveal(ctx, self) {
|
|
const cap = ctx.state.ruleset.laneCapacity;
|
|
const open = ctx.state.lanes.map((lane, index) => ({ lane, index }))
|
|
.filter(({ lane, index }) => index !== self.lane && lane.cards[self.owner].length < cap)
|
|
.map(({ index }) => index);
|
|
if (!open.length) return;
|
|
const lane = open[ctx.rng.int(open.length)];
|
|
const token: GameCard = {
|
|
id: `${MENTION_TOKEN_PREFIX}${self.card.id}:${ctx.state.turn}`, name: 'Mention', pageId: null,
|
|
category: self.card.category, cost: 0, power: ctx.params.amount ?? 1, keyword: null, source: 'loaner',
|
|
meta: { token: true },
|
|
};
|
|
ctx.state.lanes[lane].cards[self.owner].push({ card: token, owner: self.owner, lane, turn: ctx.state.turn, bonus: 0 });
|
|
ctx.log(`Viral: Mention in lane ${lane + 1}`);
|
|
},
|
|
},
|
|
rising: {
|
|
id: 'rising', name: 'Rising', text: 'End of turn: +{amount} power (up to +{max}).',
|
|
endOfTurn(ctx, self) {
|
|
const amount = ctx.params.amount ?? 1, max = ctx.params.max ?? 3;
|
|
const risen = Number(self.card.meta?.risen ?? 0);
|
|
const gain = Math.max(0, Math.min(amount, max - risen));
|
|
if (!gain) return;
|
|
self.bonus += gain;
|
|
self.card.meta = { ...self.card.meta, risen: risen + gain };
|
|
ctx.log(`Rising +${gain}`);
|
|
},
|
|
},
|
|
nostalgic: {
|
|
id: 'nostalgic', name: 'Nostalgic', text: "On reveal: if you're losing here, +{amount} power.",
|
|
onReveal(ctx, self) {
|
|
const mine = sidePower(ctx, self.lane, self.owner, self);
|
|
const theirs = sidePower(ctx, self.lane, self.owner === 0 ? 1 : 0);
|
|
if (mine < theirs) {
|
|
self.bonus += ctx.params.amount ?? 3;
|
|
ctx.log(`Nostalgic +${ctx.params.amount ?? 3}`);
|
|
}
|
|
},
|
|
},
|
|
encyclopedic: {
|
|
id: 'encyclopedic', name: 'Encyclopedic', text: "Can't have its power reduced. On reveal: if it's your only card here, +{amount} power.",
|
|
onReveal(ctx, self) {
|
|
if (allies(ctx.state, self).length === 0) {
|
|
self.bonus += ctx.params.amount ?? 2;
|
|
ctx.log(`Encyclopedic +${ctx.params.amount ?? 2}`);
|
|
}
|
|
},
|
|
// Floor: cancel any negative bonus on itself (v1 has no negative effects; kept for modes).
|
|
ongoing(_ctx, self, target) { return target === self && self.bonus < 0 ? -self.bonus : 0; },
|
|
},
|
|
obscure: {
|
|
id: 'obscure', name: 'Obscure', text: 'The opponent sees this card face down until the match ends.',
|
|
},
|
|
local: {
|
|
id: 'local', name: 'Local', text: "Ongoing: +{amount} power in a Topic of this card's faction.",
|
|
ongoing(ctx, self, target) {
|
|
return target === self && laneFaction(ctx.state, self.lane) === self.card.category ? ctx.params.amount ?? 2 : 0;
|
|
},
|
|
},
|
|
wildcard: {
|
|
id: 'wildcard', name: 'Wildcard', text: "On reveal: this card's faction becomes its Topic's faction.",
|
|
onReveal(ctx, self) {
|
|
const faction = laneFaction(ctx.state, self.lane);
|
|
if (faction && faction !== self.card.category) {
|
|
self.card.category = faction;
|
|
ctx.log(`Wildcard becomes ${faction}`);
|
|
}
|
|
},
|
|
},
|
|
};
|
|
|
|
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;
|