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

234 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from "vitest";
import { BUZZ_STEP, EXPLAINER_STEP } from "../src/cards.ts";
import { cardDef, REWARD_CARDS } from "../src/data.ts";
import { executeIntent } from "../src/rival.ts";
import { prepareRoute } from "../src/route.ts";
import { createRun, endWeek, FINANCE_CAP, playCard } from "../src/rules.ts";
import type { Stop } from "../src/state.ts";
import { FLAG_LEAD, type Intent, type RunState, type TownState } from "../src/state.ts";
import { isLocked, UNLOCKS } from "../src/unlocks.ts";
import { catalog } from "../src/view.ts";
import { SETUP, must, toPlan } from "./fixtures.ts";
/** 流派核心牌與改規則的幕僚(docs/25 第 2 節) */
const town = (s: RunState, code: string): TownState => s.towns.find((t) => t.code === code)!;
/** 乾淨的局面:指定手牌、幕僚、能力,清掉意圖、樁腳、本命區、對手規則 */
function bare(hand: string[], opts: { advisors?: string[]; powers?: string[]; intents?: Intent[] } = {}): RunState {
const s = structuredClone(createRun(SETUP, "r", 1));
return {
...s,
phase: "plan",
scene: null,
rivalRules: [],
towns: s.towns.map((t) => ({ ...t, shield: 0, rivalShield: 0, home: null })),
hand: hand.map((id, i) => ({ uid: `t${i}`, id })),
intents: opts.intents ?? [],
advisors: opts.advisors ?? [],
powers: opts.powers ?? [],
energy: 9,
money: 1000,
momentum: 0,
};
}
const ok = (r: ReturnType<typeof playCard>): RunState => {
if ("error" in r) throw new Error(r.error);
return r.state;
};
const gained = (a: RunState, b: RunState) => b.towns.reduce((x, t, i) => x + t.you - a.towns[i]!.you, 0);
describe("流派核心牌", () => {
it("十張新牌都在獎勵池、圖鑑裡", () => {
const ids = ["legwork", "doorstep", "network", "buzz", "explainer", "turntable", "wargame", "carpetads", "microdonate"];
for (const id of ids) expect(REWARD_CARDS.some((c) => c.id === id)).toBe(true);
const listed = new Set(catalog().cards.map((c) => c.id));
for (const id of ids) expect(listed.has(id)).toBe(true);
for (const a of ["iron_legs", "faction_broker", "meme_editor", "crisis_flip", "actuary"]) expect(catalog().advisors.some((x) => x.id === a)).toBe(true);
});
it("走透透:這週每打過一張地面牌,之後的地面牌 +8%", () => {
const s = bare(["canvass", "canvass", "canvass"], { powers: ["legwork"] });
const plain = bare(["canvass"]);
const one = ok(playCard(s, "t0", "D"));
const two = ok(playCard(one, "t1", "C"));
const three = ok(playCard(two, "t2", "D"));
const base = gained(plain, ok(playCard(plain, "t0", "D")));
// 第三張打在丁區:前面打過 2 張地面牌 ×1.16,同一區第二種牌不算組合拳(都是掃街)
expect(gained(two, three)).toBeCloseTo(base * 1.16, 0);
});
it("遍地開花:這週出過牌的區越多越強(最多 5 區)", () => {
const s = bare(["canvass", "canvass", "doorstep"]);
const after2 = ok(playCard(ok(playCard(s, "t0", "A")), "t1", "B"));
const first = bare(["doorstep"]);
const alone = gained(first, ok(playCard(first, "t0", "D")));
expect(gained(after2, ok(playCard(after2, "t2", "D")))).toBeCloseTo(alone * 3, 0);
});
it("樁腳網:週末還沒被用掉的樁腳,三成變成你在那區的票", () => {
const s = bare([], { powers: ["network"] });
town(s, "A").shield = 1000;
const after = must(endWeek(s));
expect(after.log.some((l) => l.text === "樁腳網・甲區" && l.votes === 300)).toBe(true);
});
it("聲量滾雪球:空戰牌累積聲量,每點 +5%;懶人包每點再 +6%", () => {
const s = bare(["tv", "tv"], { powers: ["buzz"] });
const one = ok(playCard(s, "t0", null));
expect(one.buzz).toBe(1);
const plain = bare(["tv"]);
const base = gained(plain, ok(playCard(plain, "t0", null)));
expect(gained(one, ok(playCard(one, "t1", null)))).toBeCloseTo(base * (1 + BUZZ_STEP), -1);
const ex = { ...bare(["explainer"]), buzz: 4 };
const exPlain = bare(["explainer"]);
expect(gained(ex, ok(playCard(ex, "t0", null)))).toBeCloseTo(gained(exPlain, ok(playCard(exPlain, "t0", null))) * (1 + 4 * EXPLAINER_STEP), -1);
});
it("反將一軍:批評剩一半,擋下的一半變成你的全市票", () => {
const attack: Intent = { id: "a", kind: "attack", town: null, amount: 1000, cut: 1 };
const s = ok(playCard(bare(["turntable"], { intents: [attack] }), "t0", null));
expect(s.intents[0]!.cut).toBe(0.5);
const before = s.towns.reduce((a, t) => a + t.you, 0);
executeIntent(s, s.intents[0]!);
const back = s.log.find((l) => l.text === "反將一軍:擋下的行動變成你的票")!;
expect(back.votes).toBe(250);
expect(before).toBeGreaterThan(0);
});
it("沙盤推演:每週前兩張擋對手的牌不花體力", () => {
const attack: Intent = { id: "a", kind: "attack", town: null, amount: 1000, cut: 1 };
const s = bare(["clarify", "clarify", "clarify"], { powers: ["wargame"], intents: [attack] });
const two = ok(playCard(ok(playCard(s, "t0", null)), "t1", null));
expect(two.energy).toBe(9);
expect(ok(playCard(two, "t2", null)).energy).toBe(8);
});
it("聲量滾雪球:空戰牌的花費 −30%", () => {
const s = bare(["tv"], { powers: ["buzz"] });
const plain = bare(["tv"]);
expect(s.money - ok(playCard(s, "t0", null)).money).toBe(Math.round((plain.money - ok(playCard(plain, "t0", null)).money) * 0.7));
});
it("地毯式廣告:花光資金,每 100 萬全市 +票;小額募款:每面旗多 60 萬", () => {
const s = bare(["carpetads"]);
const after = ok(playCard(s, "t0", null));
expect(after.money).toBe(0);
expect(gained(s, after)).toBeGreaterThan(0);
const m = bare(["microdonate"]);
for (const code of ["A", "B"]) {
const t = town(m, code);
t.you = t.rival + (t.you + t.rival + t.others) * (FLAG_LEAD + 0.05) * 2;
t.flag = "you";
}
expect(ok(playCard(m, "t0", null)).money - m.money).toBe(120 + 60 * 2);
});
});
describe("流派幕僚", () => {
it("鐵腿:一週跑 3 區以上,週末聲勢 +2", () => {
let s = bare(["canvass", "canvass", "canvass"], { advisors: ["iron_legs"] });
for (const [uid, code] of [["t0", "A"], ["t1", "B"], ["t2", "C"]] as const) s = ok(playCard(s, uid, code));
const before = s.momentum;
expect(must(endWeek(s)).log.some((l) => l.text.startsWith("鐵腿"))).toBe(true);
expect(before).toBeGreaterThanOrEqual(0);
});
it("派系協調人:樁腳擋下的票,一半變成你的", () => {
const s = bare([], { advisors: ["faction_broker"] });
town(s, "A").shield = 1000;
executeIntent(s, { id: "c", kind: "canvass", town: "A", amount: 800, cut: 1 });
expect(s.log.find((l) => l.text === "派系協調人・甲區")!.votes).toBe(400);
});
it("懶人包小編:爆紅之後那張牌回到手上(每週一次)", () => {
// 找一個會爆紅的亂數狀態
for (let rng = 1; rng < 200; rng++) {
const s = { ...bare(["live"], { advisors: ["meme_editor"] }), rng };
const after = ok(playCard(s, "t0", null));
if (!after.memeUsed) continue;
expect(after.hand.map((c) => c.id)).toEqual(["live"]);
return;
}
throw new Error("找不到爆紅的亂數");
});
it("危機變轉機:擋下幾次批評,週末聲勢就 +幾;精算師:存款利息最多 50 萬", () => {
const s = bare(["clarify"], { advisors: ["crisis_flip", "actuary"], intents: [{ id: "a", kind: "attack", town: null, amount: 1000, cut: 1 }] });
const after = must(endWeek(ok(playCard(s, "t0", null))));
expect(after.log.some((l) => l.text.startsWith("危機變轉機"))).toBe(true);
expect(after.log.find((l) => l.text.startsWith("精算師"))!.text).toContain("50 萬");
});
});
describe("聲望解鎖", () => {
it("核心牌、新幕僚都要聲望解鎖;圖鑑寫出解鎖條件", () => {
expect(Object.keys(UNLOCKS)).toHaveLength(14);
const c = catalog();
expect(c.cards.find((x) => x.id === "legwork")).toMatchObject({ unlock: 1 });
expect(c.cards.find((x) => x.id === "legwork")!.how).toContain("聲望 1 解鎖");
expect(c.advisors.find((x) => x.id === "actuary")).toMatchObject({ unlock: 14 });
expect(c.cards.find((x) => x.id === "canvass")!.unlock).toBeNull();
});
it("聲望不夠的不會出現在下一站;沒帶聲望(模擬、舊存檔)、每日一戰全部解鎖", () => {
const offered = new Set<string>();
for (let seed = 1; seed <= 60; seed++) {
const s = must(endWeek(toPlan(createRun(SETUP, "r", seed, { reputation: 0 }))));
for (const stop of (s.routeStops ?? []) as Stop[]) {
if (stop.kind === "card" || stop.kind === "advisor") for (const id of stop.options) offered.add(id);
if (stop.kind === "shop") for (const i of stop.items) offered.add(i.id);
}
}
expect(offered.size).toBeGreaterThan(10);
for (const id of Object.keys(UNLOCKS)) expect(offered.has(id)).toBe(false);
const fresh = createRun(SETUP, "r", 1, { reputation: 5 });
expect(isLocked(fresh, "network")).toBe(false);
expect(isLocked(fresh, "faction_broker")).toBe(true);
expect(isLocked(createRun(SETUP, "r", 1), "microdonate")).toBe(false);
expect(isLocked(createRun(SETUP, "r", 1, { reputation: 0, daily: "2026-10-29" }), "microdonate")).toBe(false);
});
});
describe("流派傾向", () => {
/** 很多局的下一站裡出現的牌(拜會地方、物資行) */
const offered = (extra: string[]) => {
const ids: string[] = [];
for (let seed = 1; seed <= 150; seed++) {
const s = toPlan(createRun(SETUP, "r", seed));
s.draw.push(...extra.map((id, i) => ({ uid: `x${i}`, id })));
prepareRoute(s);
for (const stop of (s.routeStops ?? []) as Stop[]) {
if (stop.kind === "card") ids.push(...stop.options);
if (stop.kind === "shop") ids.push(...stop.items.filter((i) => i.kind === "card").map((i) => i.id));
}
}
return ids;
};
const share = (ids: string[], ok: (id: string) => boolean) => ids.filter(ok).length / ids.length;
it("牌組裡地面牌多,下一站的地面牌、地面核心牌比較常出現", () => {
const plain = offered([]);
const ground = offered(["door", "door", "night", "park"]);
const isGround = (id: string) => cardDef(id)!.tags.includes("ground");
const isCore = (id: string) => id === "legwork" || id === "doorstep";
expect(share(ground, isGround)).toBeGreaterThan(share(plain, isGround) + 0.1);
expect(share(ground, isCore)).toBeGreaterThan(share(plain, isCore) * 1.8);
});
});
describe("財務型:錢多好辦事", () => {
it("週末依手上的資金加票,最多算 1,000 萬", () => {
const votes = (money: number) => {
const s = { ...bare([]), money, options: { ...bare([]).options, archetype: "finance" as const } };
return must(endWeek(s)).log.find((l) => l.text === "財務型:手上的資金換成廣告曝光")?.votes ?? 0;
};
// 收入進帳之後才算(收入也算進去);不是財務型沒有
expect(must(endWeek({ ...bare([]), money: 1000 })).log.some((l) => l.text.startsWith("財務型"))).toBe(false);
expect(votes(500)).toBeGreaterThan(0);
expect(votes(FINANCE_CAP * 3)).toBeCloseTo(votes(FINANCE_CAP), -1);
expect(votes(FINANCE_CAP)).toBeGreaterThan(votes(100) * 1.5);
});
});