forked from WikiDeck/wikideck
62 lines
2.7 KiB
TypeScript
62 lines
2.7 KiB
TypeScript
/** Hand-built match states for keyword unit tests. */
|
||
import {
|
||
DEFAULT_RULESET, Rng, createMatch, starterDeck, KEYWORDS, cardPower, lanePower,
|
||
type CardInPlay, type Category, type EffectContext, type GameCard, type MatchState, type PlayerId, type Ruleset,
|
||
} from '../../shared/game/index.ts';
|
||
|
||
export const ALL_KEYWORDS: Ruleset['keywords'] = {
|
||
rally: { amount: 1 }, anchor: { amount: 2 }, edition: { amount: 2 }, translated: { amount: 1 },
|
||
viral: { amount: 1 }, rising: { amount: 1, max: 3 }, nostalgic: { amount: 3 }, encyclopedic: { amount: 2 },
|
||
obscure: {}, local: { amount: 2 }, wildcard: {},
|
||
};
|
||
|
||
/** A ruleset with every keyword on and lane 0 a revealed Science portal (lanes 1–2 plain). */
|
||
export function testRules(overrides: Partial<Ruleset> = {}): Ruleset {
|
||
return {
|
||
...DEFAULT_RULESET, keywords: ALL_KEYWORDS,
|
||
laneModifiers: { pool: [], revealTurns: [1, 1, 1], params: { portal: { categoryIndex: 3, amount: 0 } } },
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
/** An empty board on turn 1 with lane 0 set to the Science portal. */
|
||
export function emptyBoard(rules = testRules()): MatchState {
|
||
const state = createMatch(rules, [starterDeck('a', rules), starterDeck('b', rules)], 'keyword-test');
|
||
for (const lane of state.lanes) lane.cards = [[], []];
|
||
state.lanes[0].modifier = 'portal';
|
||
state.lanes[1].modifier = 'plain';
|
||
state.lanes[2].modifier = 'plain';
|
||
return state;
|
||
}
|
||
|
||
let serial = 0;
|
||
export function card(overrides: Partial<GameCard> = {}): GameCard {
|
||
serial++;
|
||
return { id: `t${serial}`, name: `Card ${serial}`, pageId: 1000 + serial, category: 'Culture' as Category,
|
||
cost: 1, power: 2, keyword: null, source: 'loaner', ...overrides };
|
||
}
|
||
|
||
export function place(state: MatchState, owner: PlayerId, lane: number, c: GameCard): CardInPlay {
|
||
const inPlay: CardInPlay = { card: c, owner, lane, turn: state.turn, bonus: 0 };
|
||
state.lanes[lane].cards[owner].push(inPlay);
|
||
return inPlay;
|
||
}
|
||
|
||
export function ctxFor(state: MatchState, keyword: string, lane: number, rng = new Rng([1, 2, 3, 4])): EffectContext {
|
||
return { state, rng, params: state.ruleset.keywords[keyword] ?? {}, lane, log: () => {}, power: c => cardPower(state, c) };
|
||
}
|
||
|
||
/** Place a card and fire its onReveal, as the engine does at reveal. */
|
||
export function reveal(state: MatchState, owner: PlayerId, lane: number, c: GameCard, rng?: Rng): CardInPlay {
|
||
const inPlay = place(state, owner, lane, c);
|
||
if (c.keyword) KEYWORDS[c.keyword]?.onReveal?.(ctxFor(state, c.keyword, lane, rng), inPlay);
|
||
return inPlay;
|
||
}
|
||
|
||
export function endOfTurn(state: MatchState, self: CardInPlay): void {
|
||
const keyword = self.card.keyword;
|
||
if (keyword) KEYWORDS[keyword]?.endOfTurn?.(ctxFor(state, keyword, self.lane), self);
|
||
}
|
||
|
||
export { cardPower, lanePower };
|