import { describe, expect, it } from "vitest"; import { BATTLE_PUSH, battleTown, settleBattle } from "../src/battle.ts"; import { createRun, endWeek, rush, RUSH_PRICE } from "../src/rules.ts"; import { PRESIDENT_POWER, PUSH_GAP, executeIntent, planIntents, rivalCatchUp, rivalPower, rivalSize } from "../src/rival.ts"; import { BUZZ_OTHER_STEP, BUZZ_STEP, boost, spillShare } from "../src/cards.ts"; import { cardDef } from "../src/data.ts"; import { cityVotes } from "../src/mechanics.ts"; import { MAP_CALIBRATION, mapFactor } from "../src/calibration.ts"; import { MONEY_BASE, MONEY_SPREAD, startingMoney } from "../src/setup.ts"; import { HAND_SIZE, type RunState } from "../src/state.ts"; import { tutorialSetup } from "../src/tutorial.ts"; import { toView } from "../src/view.ts"; import { SETUP, advance, must, toPlan } from "./fixtures.ts"; /** docs/26:本週戰場、對手急起直追、加開行程、起始資金 */ describe("本週戰場", () => { it("每週標一個大區,對手一定在那裡全力搶攻", () => { for (let seed = 1; seed <= 20; seed++) { const s = createRun(SETUP, "r", seed); const t = battleTown(s)!; expect(t).toBeTruthy(); // 太小的區不會被挑(丙區只佔 7%,還是夠大;四區都有可能) const push = s.intents.filter((i) => i.battle); expect(push).toHaveLength(1); expect(push[0]!.town).toBe(t.code); expect(push[0]!.kind).not.toBe("stake"); expect(toView(s).battle?.town).toBe(t.code); expect(toView(s).intents.find((i) => i.battle)?.label).toContain("搶戰場"); } }); it("不會連續兩週同一區", () => { let s = createRun(SETUP, "r", 3); for (let w = 1; w < 5; w++) { const before = battleTown(s)!.code; s = advance(s, s.week + 1); expect(battleTown(s)!.code).not.toBe(before); } }); it("淨增比對手多:聲勢 +1、下週多抽 1 張;比對手少:對手聲勢 +1", () => { const s = toPlan(createRun(SETUP, "r", 7)); const t = battleTown(s)!; const win = structuredClone(s) as RunState; win.towns.find((x) => x.code === t.code)!.you += 50_000; const m = win.momentum; settleBattle(win); expect(win.momentum).toBe(m + 1); expect(win.battleDraw).toBe(1); expect(win.stats.battles).toBe(1); const lose = structuredClone(s) as RunState; lose.towns.find((x) => x.code === t.code)!.rival += 50_000; const r = lose.rivalMomentum; settleBattle(lose); expect(lose.rivalMomentum).toBe(r + 1); expect(lose.battleDraw ?? 0).toBe(0); }); it("贏了戰場,下週手牌多一張", () => { let s = toPlan(createRun(SETUP, "r", 9)); s.towns.find((x) => x.code === battleTown(s)!.code)!.you += 80_000; s = toPlan(must(endWeek(s))); expect(s.week).toBe(2); expect(s.hand.length).toBeGreaterThanOrEqual(HAND_SIZE + 1); }); it("對手在戰場的行動比一般大", () => { const s = createRun(SETUP, "r", 11); const push = s.intents.find((i) => i.battle)!; const same = s.intents.find((i) => !i.battle && i.kind === push.kind && i.town); if (same) expect(push.amount).toBeGreaterThan(same.amount); expect(BATTLE_PUSH).toBeGreaterThan(1); }); it("新手教學沒有戰場、沒有加開行程", () => { const s = createRun(tutorialSetup(), "t", 1, { tutorial: true, weeks: 3 }); expect(battleTown(s)).toBeNull(); expect(s.intents.some((i) => i.battle)).toBe(false); expect(toView(s).battle).toBeNull(); expect(toView(s).rush).toBeNull(); expect("error" in rush(s)).toBe(true); }); }); describe("對手急起直追", () => { it(`第 3 週起落後 ${PUSH_GAP} 個百分點以上多 1 個行動,兩倍多 2 個`, () => { let s = advance(createRun(SETUP, "r", 5), 3); const base = s.intents.length; const lead = (pp: number) => { const x = structuredClone(s) as RunState; // 每一區都拉成你領先 pp(已表態的票裡) for (const t of x.towns) { const both = t.you + t.rival; const cast = both + t.others; t.you = both / 2 + (cast * pp) / 2; t.rival = both / 2 - (cast * pp) / 2; } rivalCatchUp(x); return x.rivalPush; }; expect(lead(0)).toBeNull(); expect(lead(0.04)?.extra).toBe(1); expect(lead(0.08)?.extra).toBe(2); s = toPlan(s); expect(s.intents.length).toBeGreaterThanOrEqual(base); }); it("前兩週不會", () => { const s = createRun(SETUP, "r", 5); for (const t of s.towns) { t.you += t.rival * 0.5; t.rival *= 0.5; } rivalCatchUp(s); expect(s.rivalPush).toBeNull(); }); }); describe("加開行程", () => { it("花資金換 1 點體力,每週一次", () => { const s = toPlan(createRun(SETUP, "r", 2)); const r = must(rush(s)); expect(r.energy).toBe(s.energy + 1); expect(r.money).toBe(s.money - RUSH_PRICE); expect(rush(r)).toEqual({ error: "這週已經加開過了" }); expect(toView(r).rush?.blocked).toBe("這週已經加開過了"); // 下週又可以 const next = advance(must(endWeek(r)), 2); expect(next.rushed).toBe(false); }); it("錢不夠不行", () => { const s = toPlan(createRun(SETUP, "r", 2)); s.money = RUSH_PRICE - 1; expect(rush(s)).toEqual({ error: "資金不夠" }); }); }); describe("起始資金", () => { it("真實經費壓縮到固定範圍,有錢的人還是比較多", () => { expect(startingMoney(0)).toBe(MONEY_BASE); expect(startingMoney(3_000_000)).toBe(MONEY_BASE); expect(startingMoney(12_000_000)).toBeGreaterThan(startingMoney(6_000_000)); expect(startingMoney(2_000_000_000)).toBe(MONEY_BASE + MONEY_SPREAD); }); }); describe("其他 docs/26 的規則", () => { it("媒體焦點:全市加票時,本週戰場分到的比例加倍", () => { const s = toPlan(createRun(SETUP, "r", 4)); const t = battleTown(s)!; const share = (x: RunState) => { const before = x.towns.map((y) => y.you); cityVotes(x, 10_000); return (x.towns.find((y) => y.code === t.code)!.you - before[x.towns.findIndex((y) => y.code === t.code)]!) / 10_000; }; const focus = share(structuredClone(s) as RunState); const plain = share({ ...(structuredClone(s) as RunState), battle: null }); expect(focus).toBeGreaterThan(plain * 1.5); }); it("造勢被打對台壓到一半以下:對手聲勢不漲", () => { const s = toPlan(createRun(SETUP, "r", 6)); const rally = { id: "x", kind: "rally" as const, town: "A", amount: 3000, cut: 1 }; const full = structuredClone(s) as RunState; executeIntent(full, rally); expect(full.rivalMomentum).toBe(s.rivalMomentum + 2); const cold = structuredClone(s) as RunState; executeIntent(cold, { ...rally, cut: 0.4 }); expect(cold.rivalMomentum).toBe(s.rivalMomentum); }); it("聲量滾雪球:空戰牌每點 +5%,其他牌每點 +3%", () => { const s = toPlan(createRun(SETUP, "r", 6)); const boosted = { ...(structuredClone(s) as RunState), powers: ["buzz"], buzz: 4 }; const canvass = cardDef("canvass")!; const tv = cardDef("tv")!; const town = s.towns[0]!; expect(boost(boosted, canvass, town).mult / boost(s, canvass, town).mult).toBeCloseTo(1 + 4 * BUZZ_OTHER_STEP); expect(boost(boosted, tv, null).mult / boost(s, tv, null).mult).toBeCloseTo(1 + 4 * BUZZ_STEP); }); it("地方大老:對手插旗的區,對手的行動 +15%", () => { const s = toPlan(createRun(SETUP, "r", 6)); for (const t of s.towns) t.flag = "rival"; const plain = planIntents({ ...(structuredClone(s) as RunState), rivalRules: [] }); const elder = planIntents({ ...(structuredClone(s) as RunState), rivalRules: ["elder"] }); const a = plain.find((i) => i.town && i.kind === "canvass" && !i.battle); const b = elder.find((i) => i.id === a?.id); if (a && b && b.kind === a.kind && b.town === a.town) expect(b.amount / a.amount).toBeCloseTo(1.15, 1); }); }); describe("擴散不看鄰區數(docs/26)", () => { it("擴散出去的合計固定;隔壁區不夠的留在本區", () => { expect(spillShare(0, 0.25)).toEqual({ each: 0, rest: 0.5 }); expect(spillShare(1, 0.25)).toEqual({ each: 0.25, rest: 0.25 }); expect(spillShare(2, 0.25)).toEqual({ each: 0.25, rest: 0 }); const four = spillShare(4, 0.25); expect(four.each * 4 + four.rest).toBeCloseTo(0.5); }); }); describe("地圖大小(docs/26)", () => { it("只有一區的選區:沒有本命區、沒有戰場", () => { const one = { ...SETUP, towns: [SETUP.towns[0]!] }; const s = createRun(one, "r", 3); expect(s.towns.every((t) => !t.home)).toBe(true); expect(battleTown(s)).toBeNull(); expect(s.intents.some((i) => i.battle)).toBe(false); }); it("對手規模依區數調整,總統大選全國動員", () => { expect(rivalSize(12)).toBe(1); expect(rivalSize(1)).toBeLessThan(rivalSize(3)); expect(rivalSize(37)).toBeGreaterThan(rivalSize(22)); expect(rivalSize(1)).toBeGreaterThanOrEqual(0.88); expect(rivalSize(500)).toBeLessThanOrEqual(1.08); // 測試用的假城市代碼剛好是 00000(全國),改成一般縣市比較 const city = createRun({ ...SETUP, countyCode: "10007" }, "r", 3); const nation = createRun({ ...SETUP, countyCode: "00000" }, "r", 3); expect(rivalPower(nation) / rivalPower(city)).toBeCloseTo(PRESIDENT_POWER); }); it("區的大小係數:依人口加權的平均是 1", () => { const s = createRun(SETUP, "r", 3); const v = s.towns.reduce((a, t) => a + t.voters, 0); expect(s.towns.reduce((a, t) => a + t.voters * t.reach, 0) / v).toBeCloseTo(1); }); }); describe("逐張地圖校準(docs/26)", () => { it("沒有校準的地圖是 1;勢均力敵和真實底盤共用同一個係數", () => { expect(mapFactor("test")).toBe(1); for (const [id, f] of Object.entries(MAP_CALIBRATION)) { expect(mapFactor(`${id}-even`)).toBe(f); expect(mapFactor(id)).toBe(f); expect(f).toBeGreaterThanOrEqual(0.85); expect(f).toBeLessThanOrEqual(1.2); } }); });