election-game/backend/packages/rogue/test/variety.test.ts

207 lines
9.6 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import { cardDef } from "../src/data.ts";
import { updateFlags } from "../src/mechanics.ts";
import { applyRivalRules, executeIntent, planIntents, RIVAL_RULES } from "../src/rival.ts";
import { choose, createRun, endWeek, playCard } from "../src/rules.ts";
import { sceneFor } from "../src/scenes.ts";
import { ELDER_LEAD, FLAG_LEAD, FLIP_DRAW_MAX, type RogueSetup, type RunState, type TownState } from "../src/state.ts";
import { toView } from "../src/view.ts";
import { SETUP, must, toPlan } from "./fixtures.ts";
/** 每局不一樣(docs/25 第 3、4 節)與局中的小勝利(第 5 節) */
const LEANED: RogueSetup = {
...SETUP,
towns: [
{ ...SETUP.towns[0]!, lean: { you: 0.55, rival: 0.35, others: 0.1 } },
{ ...SETUP.towns[1]!, lean: { you: 0.35, rival: 0.55, others: 0.1 } },
{ ...SETUP.towns[2]!, lean: { you: 0.6, rival: 0.3, others: 0.1 } },
{ ...SETUP.towns[3]!, lean: { you: 0.45, rival: 0.45, others: 0.1 } },
],
};
const town = (s: RunState, code: string): TownState => s.towns.find((t) => t.code === code)!;
/** 指定手牌與對手規則,清掉意圖與樁腳 */
function bare(s: RunState, rules: string[], hand: string[] = []): RunState {
const copy = structuredClone(s);
return { ...copy, phase: "plan", scene: null, rivalRules: rules, towns: copy.towns.map((t) => ({ ...t, shield: 0, rivalShield: 0 })), hand: hand.map((id, i) => ({ uid: `t${i}`, id })), intents: [], energy: 9, money: 5000, momentum: 0 };
}
const ok = (r: ReturnType<typeof playCard>): RunState => {
if ("error" in r) throw new Error(r.error);
return r.state;
};
function takeLead(t: TownState, side: "you" | "rival", lead: number): void {
const other = side === "you" ? "rival" : "you";
t.was = other;
t.flag = other;
t[side] = (t[other] + lead * (t[other] + t.others)) / (1 - lead);
}
describe("行事曆每局不同", () => {
it("8 週的局:辯論 3–5、民調 4–5、黑函 5–6 週,不重疊;同一個種子一樣;換種子會變", () => {
const seen = new Set<string>();
for (let seed = 1; seed <= 40; seed++) {
const s = createRun(LEANED, "r", seed);
const { debate, poll, crisis } = s.schedule!;
expect([3, 4, 5]).toContain(debate);
expect([4, 5]).toContain(poll);
expect([5, 6]).toContain(crisis);
expect(new Set([debate, poll, crisis]).size).toBe(3);
seen.add(`${debate}${poll}${crisis}`);
}
expect(seen.size).toBeGreaterThan(2);
expect(createRun(LEANED, "r", 9).schedule).toEqual(createRun(LEANED, "r", 9).schedule);
expect(createRun(LEANED, "r", 9, { tutorial: true }).schedule).toBeUndefined();
expect(createRun(LEANED, "r", 9, { weeks: 5 }).schedule).toBeUndefined();
});
it("辯論照行事曆的週次開場;畫面拿得到行事曆", () => {
const s = createRun(LEANED, "r", 3);
const when = s.schedule!;
expect(sceneFor({ ...structuredClone(s), week: when.debate }, s.lastWeek)?.kind).toBe("debate");
expect(sceneFor({ ...structuredClone(s), week: when.crisis }, s.lastWeek)?.kind).toBe("crisis");
expect(toView(s).schedule).toMatchObject({ ...when, bigRally: 7 });
});
});
describe("對手規則", () => {
it("開局抽 1 條(逆風 5 起 2 條),同一個種子一樣,新手教學沒有;畫面列出名稱與說明", () => {
const s = createRun(LEANED, "r", 5);
expect(s.rivalRules).toHaveLength(1);
expect(createRun(LEANED, "r", 5, { ascension: 5 }).rivalRules).toHaveLength(2);
expect(createRun(LEANED, "r", 5).rivalRules).toEqual(s.rivalRules);
expect(createRun(LEANED, "r", 5, { tutorial: true }).rivalRules).toEqual([]);
const id = s.rivalRules![0]!;
expect(toView(s).rivalRules).toEqual([{ id, name: RIVAL_RULES[id]!.name, text: RIVAL_RULES[id]!.text }]);
});
it("名嘴加持:批評 ×1.5;你澄清時對手聲勢 −1", () => {
const base = { ...createRun(LEANED, "r", 1), week: 3, rng: 77 };
const attack = (rules: string[]) => planIntents({ ...structuredClone(base), rivalRules: rules }).find((i) => i.kind === "attack");
const a = attack(["pundit"]);
const b = attack([]);
if (a && b) expect(a.amount / b.amount).toBeCloseTo(1.5, 1);
const s = { ...bare(createRun(LEANED, "r", 1), ["pundit"], ["clarify"]), rivalMomentum: 2 };
expect(ok(playCard(s, "t0", null)).rivalMomentum).toBe(1);
});
it("地方大老:對手插旗的區要領先 12 個百分點才翻得過來", () => {
const s = bare(createRun(LEANED, "r", 1), ["elder"]);
takeLead(town(s, "B"), "you", FLAG_LEAD + 0.01);
updateFlags(s);
expect(town(s, "B").flag).toBe("rival");
takeLead(town(s, "B"), "you", ELDER_LEAD + 0.01);
updateFlags(s);
expect(town(s, "B").flag).toBe("you");
});
it("大打空戰:每週多一個電視或網路廣告", () => {
const base = { ...createRun(LEANED, "r", 1), week: 3, rng: 5 };
const air = planIntents({ ...structuredClone(base), rivalRules: ["airwar"] });
const plain = planIntents({ ...structuredClone(base), rivalRules: [] });
expect(air.length).toBe(plain.length + (plain.some((i) => i.kind === "tv") && plain.some((i) => i.kind === "online") ? 0 : 1));
});
it("鐵票區:對手本命區每週自動有樁腳", () => {
const s = bare(createRun(LEANED, "r", 1), ["stronghold"]);
applyRivalRules(s);
expect(town(s, "B").rivalShield).toBeGreaterThan(0);
expect(town(s, "A").rivalShield).toBe(0);
});
it("網路謠言:週末有一區被拉走票;那週打探過就沒事", () => {
const run = (played: string[]) => {
const s = { ...bare(createRun(LEANED, "r", 1), ["rumor"]), weekCards: played };
const after = must(endWeek(s));
return after.log.find((l) => l.text.includes("網路謠言"))!;
};
expect(run([]).votes).toBeLessThan(0);
expect(run(["probe"]).votes).toBe(0);
});
it("地毯式掃街:對手掃街擴散到隔壁區兩成", () => {
const s = bare(createRun(LEANED, "r", 1), ["carpet"]);
const before = town(s, "B").rival;
executeIntent(s, { id: "x", kind: "canvass", town: "A", amount: 1000, cut: 1 });
expect(town(s, "B").rival - before).toBeCloseTo(200, 0);
});
it("形象牌:你的比較文宣只剩一半", () => {
const hurt = (rules: string[]) => {
const s = bare(createRun(LEANED, "r", 1), rules, ["contrast"]);
const after = ok(playCard(s, "t0", null));
return s.towns.reduce((a, t, i) => a + t.rival - after.towns[i]!.rival, 0);
};
expect(hurt(["image"])).toBeCloseTo(hurt([]) / 2, 0);
});
});
describe("局中的小勝利", () => {
it("翻區:每翻轉一區抽 1 張,每週最多 2 張", () => {
const s = bare(createRun(LEANED, "r", 1), []);
s.draw = Array.from({ length: 6 }, (_, i) => ({ uid: `d${i}`, id: "canvass" }));
for (const code of ["B", "D", "C"]) takeLead(town(s, code), "you", FLAG_LEAD + 0.03);
updateFlags(s);
expect(s.hand).toHaveLength(FLIP_DRAW_MAX);
expect(s.flipDraws).toBe(FLIP_DRAW_MAX);
});
it("民調週:領先聲勢 +1、多一張稀有牌;落後多一張悲情催票、下週體力 +1", () => {
const s = createRun(LEANED, "r", 2);
const poll = s.schedule!.poll;
const lead: RunState = { ...structuredClone(s), week: poll, momentum: 0, hand: [] };
lead.towns = lead.towns.map((t) => ({ ...t, you: t.you * 2 }));
sceneFor(lead, s.lastWeek);
expect(lead.momentum).toBe(1);
expect(lead.hand.map((c) => cardDef(c.id)!.rarity)).toEqual(["rare"]);
const behind: RunState = { ...structuredClone(s), week: poll, momentum: 0, hand: [], energyNext: 0 };
behind.towns = behind.towns.map((t) => ({ ...t, rival: t.rival * 2 }));
sceneFor(behind, s.lastWeek);
expect(behind.hand.map((c) => c.id)).toEqual(["sympathy"]);
expect(behind.energyNext).toBe(1);
});
});
describe("精英站:地方派系談判", () => {
/** 停在下一站、只有一扇派系談判的門 */
const atDoor = (): RunState => {
const s = must(endWeek(toPlan(createRun(LEANED, "r", 4))));
return { ...s, rivalRules: [], route: ["elite"], routeStops: [{ kind: "elite", town: "D", goal: 1000 }] };
};
it("選了就接下挑戰、直接進下一週;畫面看得到進度", () => {
const s = must(choose(atDoor(), 0));
expect(s.phase === "plan" || s.phase === "scene").toBe(true);
expect(s.contract).toMatchObject({ town: "D", goal: 1000, week: s.week });
expect(toView(toPlan(s)).contract).toMatchObject({ town: "D", name: "丁區", goal: 1000 });
});
it("達成:拿一位幕僚;沒達成:聲勢 −1", () => {
const won = toPlan(must(choose(atDoor(), 0)));
const d = town(won, "D");
d.you += 5000;
d.undecided = Math.max(0, d.undecided - 5000);
const after = must(endWeek({ ...won, intents: [], advisors: [] }));
expect(after.advisors).toHaveLength(1);
expect(after.contract).toBeUndefined();
const lost = toPlan(must(choose(atDoor(), 0)));
const before = { ...lost, intents: [], momentum: 3 };
expect(must(endWeek(before)).log.some((l) => l.text.startsWith("地方派系談判沒談成"))).toBe(true);
});
});
describe("看得到前方:下下週的門", () => {
it("選下一站時看得到下下週的門;到時候還能用就照預告的出現", () => {
let s = must(endWeek(toPlan(createRun(LEANED, "r", 6))));
expect(s.nextRoute?.length).toBeGreaterThan(0);
expect(toView(s).nextRoute?.map((d) => d.kind)).toEqual(s.nextRoute);
const promised = s.nextRoute!;
s = must(endWeek(toPlan(must(choose(s, "skip")))));
// 第 2 週結束時可能被換掉的只有「不能用」的門;三扇都還能用就一模一樣
if (!promised.includes("elite") && !promised.includes("advisor")) expect(s.route).toEqual(promised);
});
});