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

264 lines
12 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import { cardDef, findCard } from "../src/data.ts";
import { policyCardDef, policyCardId, policySlots } from "../src/meta.ts";
import { MINIGAME } from "../src/route.ts";
import { choose, createRun, endWeek, playCard, ultimate } from "../src/rules.ts";
import { ultReady } from "../src/ultimate.ts";
import { CRISIS_BASE } from "../src/scenes.ts";
import { totals, type RunState } from "../src/state.ts";
import { toView } from "../src/view.ts";
import { SETUP, advance, must, toPlan } from "./fixtures.ts";
/** 跨局成長(docs/23):政策島的建築、政治生涯的能力、政策工坊的政策、選戰小遊戲 */
const deck = (s: RunState) => [...s.draw, ...s.discard, ...s.hand];
describe("政策島的建築", () => {
it("競選金庫、觀光夜市加開局資金;競選總部加聲勢;競選學院升級開局牌", () => {
const base = createRun(SETUP, "r", 5);
const s = createRun(SETUP, "r", 5, {
perks: [
{ id: "vault", level: 2 },
{ id: "nightmarket", level: 1 },
{ id: "hq", level: 1 },
{ id: "academy", level: 2 },
],
});
expect(s.money - base.money).toBe(150 * 2 + 50);
expect(s.momentum).toBe(1);
expect(deck(s).filter((c) => c.up).length).toBe(2);
});
it("不認得的建築不算、等級夾在 1–3", () => {
const s = createRun(SETUP, "r", 5, { perks: [{ id: "casino", level: 3 }, { id: "vault", level: 9 }] });
expect(s.options.perks).toEqual({ vault: 3 });
});
it("交通樞紐前幾週體力 +1;志工之家第 1 週多抽牌", () => {
const s = createRun(SETUP, "r", 5, { perks: [{ id: "hub", level: 2 }, { id: "volunteer", level: 1 }] });
expect(s.energyMax).toBe(4);
expect(s.hand).toHaveLength(6);
const w3 = advance(s, 3);
expect(w3.energyMax).toBe(3);
});
it("長照中心讓長輩多的區更有效", () => {
const plain = createRun(SETUP, "r", 5);
const care = createRun(SETUP, "r", 5, { perks: [{ id: "care", level: 2 }] });
const play = (s: RunState) => {
const t = structuredClone(s);
t.hand = [{ uid: "x", id: "canvass" }];
t.energy = 3;
const r = playCard(t, "x", "B");
if ("error" in r) throw new Error(r.error);
return r.state.towns.find((x) => x.code === "B")!.you - s.towns.find((x) => x.code === "B")!.you;
};
expect(play(care) / play(plain)).toBeCloseTo(1.16, 1);
});
it("政策研究院:每局可以帶的政策份數", () => {
expect(policySlots({})).toBe(1);
expect(policySlots({ library: 1 })).toBe(2);
expect(policySlots({ library: 3 })).toBe(3);
});
});
describe("政治生涯的能力", () => {
it("財務:開局資金、每週收入都變多", () => {
const base = createRun(SETUP, "r", 5);
const rich = createRun(SETUP, "r", 5, { abilities: { eloquence: 0, organization: 0, media: 0, finance: 2, integrity: 0 } });
expect(rich.money - base.money).toBe(160);
const income = (s: RunState) => must(endWeek(toPlan(s))).money - s.money;
expect(income(rich) - income(base)).toBe(30);
});
it("清廉:匿名黑函的損失變小", () => {
const clean = advance(createRun(SETUP, "r", 33, { abilities: { eloquence: 0, organization: 0, media: 0, finance: 0, integrity: 5 } }), 6);
expect(clean.scene!.kind).toBe("crisis");
expect((clean.scene as { amount: number }).amount).toBeCloseTo(CRISIS_BASE * clean.scale * 0.4);
});
it("口才 3 級辯論多一張論點、5 級看得出最好的論點", () => {
const talk = (eloquence: number) => advance(createRun(SETUP, "r", 21, { abilities: { eloquence, organization: 0, media: 0, finance: 0, integrity: 0 } }), 3);
const d0 = talk(0).scene as Extract<RunState["scene"], { kind: "debate" }>;
const d3 = talk(3).scene as Extract<RunState["scene"], { kind: "debate" }>;
expect(d3.hand.length).toBe(d0.hand.length + 1);
expect((toView(talk(4)).scene as { best: number | null }).best).toBeNull();
expect((toView(talk(5)).scene as { best: number | null }).best).not.toBeNull();
});
});
describe("政策工坊", () => {
const policy = { name: "銀髮安心卡", target: "old", method: "subsidy", funding: "tax" } as const;
it("政策變成一張牌:第 1 週就在手上、用玩家取的名字", () => {
const s = createRun(SETUP, "r", 5, { policies: [policy] });
const id = policyCardId(0, policy);
expect(s.hand.some((c) => c.id === id)).toBe(true);
const view = toView(s);
expect(view.hand.find((c) => c.id === id)!.name).toBe("銀髮安心卡");
expect(findCard(id)!.cost).toBe(1);
});
it("沒有政策研究院只能帶 1 份", () => {
const s = createRun(SETUP, "r", 5, { policies: [policy, { ...policy, name: "第二份" }] });
expect(Object.keys(s.policyNames ?? {})).toHaveLength(1);
});
it("財源:加稅花費少一半但多一張記者追問;招商加資金;刪預算全市掉一點票", () => {
const play = (funding: "tax" | "invest" | "cut") => {
const p = { ...policy, funding };
const s = createRun(SETUP, "r", 5, { policies: [p] });
const uid = s.hand.find((c) => c.id === policyCardId(0, p))!.uid;
const r = playCard(s, uid, "B");
if ("error" in r) throw new Error(r.error);
return { before: s, after: r.state };
};
const tax = play("tax");
expect(tax.after.discard.filter((c) => c.id === "reporter").length).toBe(1);
expect(policyCardDef(policyCardId(0, { ...policy, funding: "tax" }))!.money).toBe(Math.round(policyCardDef(policyCardId(0, { ...policy, funding: "cut" }))!.money * 0.5));
const invest = play("invest");
expect(invest.after.money - invest.before.money).toBe(100 - policyCardDef(policyCardId(0, { ...policy, funding: "invest" }))!.money);
const cut = play("cut");
expect(cut.after.log.some((l) => l.text.includes("被刪預算"))).toBe(true);
});
it("政策落地:這份政策的「家」(上班族→交通樞紐)每級 +10%", () => {
const p = { name: "通勤補助", target: "workers", method: "subsidy", funding: "tax" } as const;
const gain = (perks: { id: string; level: number }[]) => {
const s = createRun(SETUP, "r", 5, { policies: [p], perks });
const uid = s.hand.find((c) => c.id === policyCardId(0, p))!.uid;
const r = playCard(s, uid, "C");
if ("error" in r) throw new Error(r.error);
return { votes: r.state.towns.find((t) => t.code === "C")!.you - s.towns.find((t) => t.code === "C")!.you, notes: r.notes };
};
const plain = gain([]);
const landed = gain([{ id: "hub", level: 2 }]);
expect(landed.votes / plain.votes).toBeCloseTo(1.2, 1);
expect(landed.notes.some((n) => n.includes("交通樞紐落地"))).toBe(true);
});
it("格式不對的政策牌 id 找不到", () => {
expect(policyCardDef("policy:0:aliens:subsidy:tax")).toBeUndefined();
expect(policyCardDef("policy:x")).toBeUndefined();
expect(cardDef("policy:0:old:subsidy:tax")).toBeDefined();
});
});
describe("選戰小遊戲", () => {
/** 直接把下一站換成小遊戲 */
function atGame(game: "handshake" | "rally" | "billboard"): RunState {
const s = must(endWeek(toPlan(createRun(SETUP, "r", 8))));
s.route = null;
s.phase = "stop";
s.stop = { kind: "minigame", game, town: game === "rally" ? null : "A", level: 0.3, seed: 1 };
return s;
}
it("掃街握手:分數越高那一區加越多票,90 分以上聲勢 +1", () => {
const a = atGame("handshake");
const low = must(choose(a, 30));
const high = must(choose(atGame("handshake"), 95));
const gain = (x: RunState) => x.towns.find((t) => t.code === "A")!.you - a.towns.find((t) => t.code === "A")!.you;
expect(gain(high)).toBeGreaterThan(gain(low) * 2);
expect(high.momentum).toBeGreaterThan(low.momentum);
});
it("造勢喊凍蒜:40、80 分各一個門檻", () => {
const m = (score: number) => {
const s = atGame("rally");
return must(choose(s, score)).momentum - s.momentum;
};
expect([m(20), m(50), m(85)]).toEqual([0, 1, 2]);
});
it("看板卡位:那一區之後每週多票", () => {
const s = must(choose(atGame("billboard"), 70));
expect(s.towns.find((t) => t.code === "A")!.office).toBe(MINIGAME.billboardBase + MINIGAME.billboardStep * 70);
});
it("不玩不給分;分數超過 100 不行", () => {
const s = atGame("handshake");
const skipped = must(choose(s, "skip"));
expect(totals(skipped.towns).you).toBeCloseTo(totals(s.towns).you);
expect("error" in choose(atGame("rally"), 101)).toBe(true);
});
it("小遊戲的畫面資料:遊戲、區、難度、種子", () => {
const v = toView(atGame("billboard")).stop as { kind: string; game: string; townName: string; seed: number };
expect(v).toMatchObject({ kind: "minigame", game: "billboard", townName: "甲區", seed: 1 });
});
});
describe("出身與黨的資源(18 第 53 項)", () => {
const origin = (party: string | null, background = "律師") => ({ background, party, kind: (party === "民主進步黨" || party === "中國國民黨" ? "major" : party === "台灣民眾黨" ? "tpp" : "minor") as "major" | "tpp" | "minor" });
it("出身專屬牌第一週就在手上,不會出現在獎勵裡", () => {
const s = createRun(SETUP, "r", 5, { origin: origin(null, "律師") });
expect(s.hand.some((c) => c.id === "origin_legal")).toBe(true);
expect(cardDef("origin_legal")?.tags).toContain("origin");
});
it("大黨:第 3 或第 4 週黨中央風波(手上多一張),開局多一張黨部動員掃街", () => {
const s = createRun(SETUP, "r", 5, { origin: origin("中國國民黨") });
expect([3, 4]).toContain(s.partyStorm);
expect([...s.hand, ...s.draw].some((c) => c.id === "walkx")).toBe(true);
const w = advance(s, s.partyStorm!);
expect(w.hand.some((c) => c.id === "storm")).toBe(true);
expect(w.log.some((l) => l.text.startsWith("黨中央風波"))).toBe(true);
});
it("小黨、無黨籍:起步多 0.2 個百分點,沒有黨中央風波", () => {
const base = createRun(SETUP, "r", 5);
const s = createRun(SETUP, "r", 5, { origin: origin("時代力量") });
expect(s.partyStorm).toBeNull();
const committed = (x: RunState) => x.towns.reduce((a, t) => a + t.you + t.rival + t.others, 0);
const gain = (totals(s.towns).you - totals(base.towns).you) / committed(base);
expect(gain).toBeCloseTo(0.002, 3);
});
});
describe("大絕招(18 第 57 項)", () => {
/** 開局硬調成落後 gap 個百分點 */
const behind = (gap: number) => ({
...SETUP,
towns: SETUP.towns.map((t) => ({ ...t, lean: { you: t.lean.you - gap / 200, rival: t.lean.rival + gap / 200, others: t.lean.others } })),
});
it("落後越多集越快;集滿可以放,玩得好票加得多,放完就沒了", () => {
let s = createRun(behind(14), "r", 5);
expect(ultReady(s)).toBe(false);
s = advance(s, 2);
expect(s.ult!.charge).toBeGreaterThanOrEqual(100);
expect(ultReady(s)).toBe(true);
s = toPlan(s);
const good = must(ultimate(s, "rally", 95));
const bad = must(ultimate(s, "rally", 20));
const gain = (x: RunState) => totals(x.towns).you - totals(s.towns).you;
expect(gain(good)).toBeGreaterThan(gain(bad) * 3);
expect(ultReady(good)).toBe(false);
expect("error" in ultimate(good, "rally", 90)).toBe(true);
});
it("領先也集得到(一局的六成時間集滿),只是比落後慢(docs/26)", () => {
const ahead = advance(createRun(behind(-10), "r", 5), 3);
const behindRun = advance(createRun(behind(10), "r", 5), 3);
expect(ahead.ult!.charge).toBeGreaterThan(30);
expect(ahead.ult!.charge).toBeLessThan(100);
expect(behindRun.ult!.charge).toBeGreaterThan(ahead.ult!.charge);
// 領先的人大約一局的六成就集滿
const later = advance(createRun(behind(-10), "r", 5), Math.ceil(0.6 * 8) + 1);
expect(later.ult!.charge).toBeGreaterThanOrEqual(100);
});
it("掃街馬拉松只加挑的區;新手教學沒有大絕招", () => {
let s = toPlan(advance(createRun(behind(14), "r", 5), 2));
const code = s.towns[0]!.code;
const r = must(ultimate(s, "handshake", 80, [code]));
expect(r.towns[0]!.you).toBeGreaterThan(s.towns[0]!.you);
expect(r.towns[1]!.you).toBe(s.towns[1]!.you);
s = createRun(SETUP, "t", 5, { tutorial: true });
expect(toView(s).ultimate).toBeNull();
});
});