297 lines
14 KiB
TypeScript
297 lines
14 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import { cardDef } from "../src/data.ts";
|
||
import { updateFlags } from "../src/mechanics.ts";
|
||
import type { RogueArchetype } from "@election2026/protocol";
|
||
import { applyRivalRules, executeIntent, planIntents, RIVAL_RULES, RIVAL_TYPES } 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");
|
||
const { local, ...fixed } = when;
|
||
expect(toView(s).schedule).toMatchObject({ ...fixed, bigRally: 7, local: (local ?? []).map((l) => ({ week: l.week, id: l.event })) });
|
||
});
|
||
});
|
||
|
||
describe("地方事件週", () => {
|
||
/** 甲區宮廟多、乙區客家庄 */
|
||
const LOCAL: RogueSetup = {
|
||
...LEANED,
|
||
towns: LEANED.towns.map((t, i) => ({ ...t, traits: i === 0 ? ["temples"] : i === 1 ? ["hakka"] : [] })),
|
||
};
|
||
|
||
it("依縣市特色排 1–2 個,在第 2–6 週、不和辯論/民調/黑函、對手大型造勢同週;沒有符合的特色就沒有", () => {
|
||
const kinds = new Set<string>();
|
||
for (let seed = 1; seed <= 30; seed++) {
|
||
const { debate, poll, crisis, local } = createRun(LOCAL, "r", seed).schedule!;
|
||
expect(local!.length).toBeGreaterThanOrEqual(1);
|
||
expect(local!.length).toBeLessThanOrEqual(2);
|
||
for (const l of local!) {
|
||
expect(l.week).toBeGreaterThanOrEqual(2);
|
||
expect(l.week).toBeLessThanOrEqual(6);
|
||
expect([debate, poll, crisis]).not.toContain(l.week);
|
||
kinds.add(l.event);
|
||
}
|
||
}
|
||
expect([...kinds].sort()).toEqual(["procession", "yimin"]);
|
||
const plain: RogueSetup = { ...LEANED, towns: LEANED.towns.map((t) => ({ ...t, traits: [] })) };
|
||
expect(createRun(plain, "r", 1).schedule!.local).toEqual([]);
|
||
});
|
||
|
||
it("到了那週就是那個事件;只加成有那個特色的區(雙方都算)", () => {
|
||
let s = createRun(LOCAL, "r", 4);
|
||
s.schedule = { ...s.schedule!, local: [{ week: 2, event: "procession" }] };
|
||
s = must(endWeek(toPlan(s)));
|
||
s = toPlan(must(choose(s, "skip")));
|
||
expect(s.week).toBe(2);
|
||
expect(s.event).toBe("procession");
|
||
const view = toView(s);
|
||
expect(view.event.text).toContain("甲區");
|
||
const t = bare(s, [], ["temple", "temple"]);
|
||
const at = (code: string) => {
|
||
const after = ok(playCard(t, "t0", code));
|
||
return town(after, code).you - town(t, code).you;
|
||
};
|
||
// 宮廟參拜在宮廟多的區本來就 ×2,遶境週再 ×2;別區不受影響
|
||
const calm = { ...t, event: "calm" };
|
||
const calmAt = (code: string) => town(ok(playCard(calm, "t0", code)), code).you - town(calm, code).you;
|
||
expect(at("A") / calmAt("A")).toBeCloseTo(2, 1);
|
||
expect(at("C") / calmAt("C")).toBeCloseTo(1, 1);
|
||
});
|
||
});
|
||
|
||
describe("對手的類型", () => {
|
||
const withRival = (arch: RogueArchetype) => createRun(LEANED, "r", 7, { rivalArchetype: arch });
|
||
|
||
it("畫面看得到對手的類型、強項、弱點;沒有類型是 null", () => {
|
||
expect(toView(withRival("media")).rivalType).toMatchObject({ archetype: "media", name: "媒體型", skill: RIVAL_TYPES.media.skill });
|
||
expect(toView(createRun(LEANED, "r", 7)).rivalType).toBeNull();
|
||
});
|
||
|
||
it("組織型對手的樁腳 +40%、空戰 −10%", () => {
|
||
const base = { ...createRun(LEANED, "r", 1), week: 4 };
|
||
const amount = (arch: RogueArchetype | null, kind: "stake" | "tv") => {
|
||
const s = { ...structuredClone(base), options: { ...base.options, rivalArchetype: arch }, rivalRules: [] };
|
||
for (let rng = 1; rng < 300; rng++) {
|
||
const i = planIntents({ ...structuredClone(s), rng }).find((x) => x.kind === kind && !x.town);
|
||
if (i) return i.amount;
|
||
const j = planIntents({ ...structuredClone(s), rng }).find((x) => x.kind === kind && x.town === "A");
|
||
if (j) return j.amount;
|
||
}
|
||
throw new Error(`沒有 ${kind}`);
|
||
};
|
||
expect(amount("organization", "stake") / amount(null, "stake")).toBeCloseTo(1.4, 1);
|
||
expect(amount("organization", "tv") / amount(null, "tv")).toBeCloseTo(0.9, 1);
|
||
});
|
||
|
||
it("媒體型對手被比較文宣多傷 20%,清廉型少 20%;類型相近的規則比較常抽到", () => {
|
||
const hurt = (arch: RogueArchetype | null) => {
|
||
const s = bare({ ...createRun(LEANED, "r", 1), options: { ...createRun(LEANED, "r", 1).options, rivalArchetype: arch } }, [], ["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("media") / hurt(null)).toBeCloseTo(1.2, 1);
|
||
expect(hurt("integrity") / hurt(null)).toBeCloseTo(0.8, 1);
|
||
const count = (arch: RogueArchetype | null) => {
|
||
let n = 0;
|
||
for (let seed = 1; seed <= 200; seed++) if (createRun(LEANED, "r", seed, { rivalArchetype: arch }).rivalRules!.some((r) => RIVAL_TYPES.organization.rules.includes(r))) n++;
|
||
return n;
|
||
};
|
||
expect(count("organization")).toBeGreaterThan(count(null));
|
||
});
|
||
});
|
||
|
||
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(100, 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);
|
||
});
|
||
});
|