67 lines
3.4 KiB
TypeScript
67 lines
3.4 KiB
TypeScript
/**
|
|
* Test helpers for the beginner help (docs/07 §5): real views from the rule modules, so help text
|
|
* is checked against the actual view shape. Only imported by *.test.ts files.
|
|
*/
|
|
import { GAMES } from '@bg/games';
|
|
import type { PublicUser } from '@bg/shared/lite';
|
|
import { SeededRng, type RngState } from '../../../../packages/engine/src/rng';
|
|
|
|
export const NICKS = ['호랑이', '고양이', '토끼', '거북이', '여우', '사슴', '곰', '부엉이', '다람쥐', '오리'];
|
|
|
|
/** Player ids are 'A', 'B', ...; nicknames from NICKS. */
|
|
export function seatIds(n: number): string[] {
|
|
return Array.from({ length: n }, (_, i) => String.fromCharCode(65 + i));
|
|
}
|
|
|
|
export function usersOf(ids: string[]): Record<string, PublicUser> {
|
|
return Object.fromEntries(ids.map((id, i) => [id, { id, nickname: NICKS[i % NICKS.length]!, avatar: null, kind: 'guest' as const }]));
|
|
}
|
|
|
|
/** Fresh state for a game with `n` players and options merged over the defaults. */
|
|
export function setupGame(id: string, n: number, options: Record<string, unknown> = {}, seed: RngState = [1, 2, 3, 4], now = 1_700_000_000_000) {
|
|
const def = GAMES[id]!;
|
|
const players = seatIds(n);
|
|
const opts = def.optionsSchema.parse({ ...(def.defaultOptions as object), ...options });
|
|
const rng = SeededRng.fromSeed(seed);
|
|
const state = def.setup({ players, options: opts, rng, now });
|
|
return { def, players, rng, state: state as any, now };
|
|
}
|
|
|
|
/** Apply an action (validated) and return the new state. */
|
|
export function applyAction(id: string, state: any, actor: string, action: unknown, rng = SeededRng.fromSeed([9, 9, 9, 9]), now = 1_700_000_100_000): any {
|
|
const def = GAMES[id]!;
|
|
const v = def.validate(state, actor, action, { now });
|
|
if (!v.ok) throw new Error(`${id}: rejected ${JSON.stringify(action)}: ${v.reason}`);
|
|
return def.apply(state, actor, action, { rng, now }).state;
|
|
}
|
|
|
|
/**
|
|
* Random playout that hands every intermediate view (each player + spectator) to `visit`.
|
|
* Uses legal actions when the module lists them, otherwise the timeout action.
|
|
*/
|
|
export function eachView(id: string, n: number, visit: (view: any, viewer: string | null, state: any) => void, { steps = 400, seed = [5, 6, 7, 8] as RngState, options = {} as Record<string, unknown> } = {}) {
|
|
const { def, players, rng, state: s0 } = setupGame(id, n, options, seed);
|
|
const chooser = SeededRng.fromSeed([seed[3], seed[2], seed[1], seed[0] ^ 0x9e3779b9]);
|
|
let state = s0;
|
|
let now = 1_700_000_000_000;
|
|
for (let step = 0; step <= steps; step++) {
|
|
for (const p of [...players, null]) visit(def.view(state, p), p, state);
|
|
if (def.result(state)) return;
|
|
const actives = def.activePlayers(state);
|
|
if (actives.length === 0) return;
|
|
const actor = actives[chooser.int(actives.length)]!;
|
|
const legal = def.legalActions?.(state, actor) ?? [];
|
|
const action = legal.length > 0 ? legal[chooser.int(legal.length)] : def.onTimeout(state, actor);
|
|
now += 1000;
|
|
if (!def.validate(state, actor, action, { now }).ok) return;
|
|
state = def.apply(state, actor, action, { rng, now }).state;
|
|
}
|
|
}
|
|
|
|
/** Help text must read as finished Korean: no leaked placeholders. */
|
|
export function assertCleanText(text: string, where: string) {
|
|
if (typeof text !== 'string' || text.trim().length === 0) throw new Error(`${where}: empty help`);
|
|
const bad = /undefined|NaN|null|\[object|\$\{|님님/.exec(text);
|
|
if (bad) throw new Error(`${where}: "${bad[0]}" in help: ${text}`);
|
|
}
|