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

187 lines
9.4 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 { ARCHETYPES, HOME_MULT, homeTowns } from "../src/archetypes.ts";
import { cardDef, STARTER_DECK } from "../src/data.ts";
import { updateFlags } from "../src/mechanics.ts";
import { planIntents } from "../src/rival.ts";
import { createRun, endWeek, playCard } from "../src/rules.ts";
import { FLAG_LEAD, 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 第 1 節) */
/** 甲區是你的票倉、乙區是對手的票倉,其他兩區平均 */
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 ok = (r: ReturnType<typeof playCard>): RunState => {
if ("error" in r) throw new Error(r.error);
return r.state;
};
/** 指定手牌,清掉對手的意圖與樁腳,方便算數字 */
function withHand(s: RunState, ids: string[]): RunState {
const copy = structuredClone(s);
return { ...copy, phase: "plan", scene: null, towns: copy.towns.map((t) => ({ ...t, shield: 0, rivalShield: 0 })), hand: ids.map((id, i) => ({ uid: `t${i}`, id })), intents: [], energy: 9, money: 5000, momentum: 0 };
}
const town = (s: RunState, code: string): TownState => s.towns.find((t) => t.code === code)!;
/** 讓 side 在這區領先 12 個百分點(超過插旗門檻),原本是另一方插旗 */
function takeLead(t: TownState, side: "you" | "rival"): void {
const other = side === "you" ? "rival" : "you";
t.was = other;
const lead = FLAG_LEAD + 0.04;
t[side] = (t[other] + lead * (t[other] + t.others)) / (1 - lead);
}
const deckIds = (s: RunState): string[] => [...s.draw, ...s.hand, ...s.discard].map((c) => c.id).sort();
describe("本命區", () => {
it("底盤比全區平均高最多、而且夠大的區;對手用同樣的算法,不會挑到同一區", () => {
// 丙區的底盤最高,但只佔 7%(夠大);甲區次高但人多
expect(homeTowns(LEANED.towns, "you")).toEqual(["C"]);
expect(homeTowns(LEANED.towns, "rival")).toEqual(["B"]);
// 太小的區不算:丙區只剩全區 2%
const tiny = { ...LEANED, towns: LEANED.towns.map((t) => (t.code === "C" ? { ...t, voters: 14_000 } : t)) };
expect(homeTowns(tiny.towns, "you")).toEqual(["A"]);
const s = createRun(LEANED, "r", 1);
expect(town(s, "C").home).toBe("you");
expect(town(s, "B").home).toBe("rival");
expect(s.towns.filter((t) => t.home).length).toBe(2);
expect(toView(s).identity).toBeNull(); // 沒有類型就沒有特色面板
expect(toView(s).towns.find((t) => t.code === "C")!.home).toBe("you");
});
it("你的牌打在本命區 ×1.2;回鄉造勢只能打在本命區,開局就在牌組裡", () => {
const s = createRun(LEANED, "r", 1);
expect(deckIds(s)).toContain("homecoming");
const h = withHand(s, ["canvass", "canvass"]);
const home = ok(playCard(h, "t0", "C"));
const plain = ok(playCard({ ...h, towns: h.towns.map((t) => ({ ...t, home: null })) }, "t0", "C"));
expect(town(home, "C").you - town(h, "C").you).toBeCloseTo((town(plain, "C").you - town(h, "C").you) * HOME_MULT);
const hc = withHand(s, ["homecoming"]);
expect("error" in playCard(hc, "t0", "A")).toBe(true);
const back = ok(playCard(hc, "t0", "C"));
expect(back.momentum).toBe(1);
expect(town(back, "C").you).toBeGreaterThan(town(hc, "C").you);
});
it("翻走對手的本命區:聲勢 +2、對手下週少一個行動;本命區失守:聲勢 −2", () => {
const s = withHand(createRun(LEANED, "r", 1), []);
// 乙區(對手本命區)原本對手插旗:把你的票拉到領先
takeLead(town(s, "B"), "you");
updateFlags(s);
expect(s.momentum).toBe(2);
expect(s.rivalStun).toBe(1);
const fewer = planIntents({ ...s, week: 3 });
const normal = planIntents({ ...s, week: 3, rivalStun: 0 });
expect(fewer.length).toBe(normal.length - 1);
// 丙區(你的本命區)被翻走
const lost = withHand(createRun(LEANED, "r", 1), []);
lost.momentum = 3;
takeLead(town(lost, "C"), "rival");
updateFlags(lost);
expect(lost.momentum).toBe(1);
});
it("新手教學沒有本命區、沒有回鄉造勢", () => {
const s = createRun(LEANED, "r", 1, { tutorial: true, archetype: "media" });
expect(s.towns.every((t) => !t.home)).toBe(true);
expect(deckIds(s)).not.toContain("homecoming");
expect(s.options.archetype).toBeNull();
});
});
describe("候選人類型", () => {
it("每種類型:專屬開局牌取代一張掃街(財務型多一張募款餐會),畫面有技能、弱點與依據", () => {
for (const [id, def] of Object.entries(ARCHETYPES)) {
const s = createRun(LEANED, "r", 1, { archetype: id as keyof typeof ARCHETYPES, archetypeBasis: "測試" });
const deck = deckIds(s);
const expected = [...STARTER_DECK];
if (def.replaces) expected[expected.indexOf(def.replaces)] = def.card;
else expected.push(def.card);
expected.push("homecoming");
expect(deck).toEqual(expected.sort());
expect(cardDef(def.card)).toBeDefined();
const view = toView(s).identity!;
expect(view).toMatchObject({ archetype: id, name: def.name, skill: def.skill, weakness: def.weakness, basis: "測試" });
expect(view.home).toContain("丙區");
}
});
it("同一個種子、同一個類型,開局一模一樣", () => {
expect(createRun(LEANED, "r", 7, { archetype: "media" })).toEqual(createRun(LEANED, "r", 7, { archetype: "media" }));
});
it("媒體型:空戰牌 ×1.15;爆紅之後下一張空戰牌加倍;被批評 +20%", () => {
const plain = withHand(createRun(LEANED, "r", 1), ["tv"]);
const media = withHand(createRun(LEANED, "r", 1, { archetype: "media" }), ["tv", "tv"]);
const gain = (a: RunState, b: RunState) => b.towns.reduce((x, t, i) => x + t.you - a.towns[i]!.you, 0);
const base = gain(plain, ok(playCard(plain, "t0", null)));
expect(gain(media, ok(playCard(media, "t0", null)))).toBeCloseTo(base * 1.15, 0);
const boosted = { ...media, viral: { used: true, boost: true } };
const one = ok(playCard(boosted, "t0", null));
expect(gain(boosted, one)).toBeCloseTo(base * 1.15 * 2, 0);
expect(one.viral!.boost).toBe(false); // 用過就沒了
const s = { ...createRun(LEANED, "r", 1, { archetype: "media" }), week: 3 };
const n = { ...createRun(LEANED, "r", 1), week: 3 };
const attack = (x: RunState) => planIntents({ ...x, rng: 99 }).find((i) => i.kind === "attack");
const a = attack(s);
const b = attack(n);
if (a && b) expect(a.amount / b.amount).toBeCloseTo(1.2, 1);
});
it("組織型:本命區每週自動有樁腳;每週第一張佈樁不花體力;空戰牌 ×0.85", () => {
const s = createRun(LEANED, "r", 1, { archetype: "organization" });
expect(town(s, "C").shield).toBeGreaterThan(0);
const h = withHand(s, ["stake", "stake"]);
const one = ok(playCard(h, "t0", "A"));
expect(one.energy).toBe(9);
const two = ok(playCard(one, "t1", "A"));
expect(two.energy).toBe(8);
});
it("資深型:辯論多一張論點、回應記者追問不花體力;年輕人多的區 ×0.9", () => {
const s = withHand(createRun(LEANED, "r", 1, { archetype: "eloquence" }), ["reporter", "canvass"]);
expect(ok(playCard(s, "t0", null)).energy).toBe(9);
const plain = withHand(createRun(LEANED, "r", 1), ["canvass"]);
const young = (x: RunState) => town(ok(playCard(x, x.hand.find((c) => c.id === "canvass")!.uid, "A")), "A").you - town(x, "A").you;
expect(young(s)).toBeCloseTo(young(plain) * 0.9);
});
it("財務型:每週多 150 萬;清廉型:收入 −20%、記者追問不會被塞進牌組", () => {
const income = (arch?: "finance" | "integrity") => {
const s = toPlan(createRun(LEANED, "r", 1, arch ? { archetype: arch } : {}));
const after = must(endWeek({ ...s, intents: [] }));
return after.money - s.money;
};
const base = income();
expect(income("finance") - base).toBe(150);
expect(income("integrity")).toBe(Math.round(base * 0.8));
const clean = withHand(createRun(LEANED, "r", 1, { archetype: "integrity" }), []);
const attacked = must(endWeek({ ...clean, intents: [{ id: "x", kind: "attack", town: null, amount: 500, cut: 1 }] }));
expect([...attacked.draw, ...attacked.discard, ...attacked.hand].some((c) => c.id === "reporter")).toBe(false);
});
it("黑馬型:起始資金 −30%、每點聲勢 +12%、翻區多抽一張", () => {
expect(createRun(LEANED, "r", 1, { archetype: "newcomer" }).money).toBe(Math.round(createRun(LEANED, "r", 1).money * 0.7));
const gain = (arch?: "newcomer") => {
const s = { ...withHand(createRun(LEANED, "r", 1, arch ? { archetype: arch } : {}), ["canvass"]), momentum: 2 };
return town(ok(playCard(s, "t0", "D")), "D").you - town(s, "D").you;
};
expect(gain("newcomer") / gain()).toBeCloseTo(1.24 / 1.16, 2);
const s = withHand(createRun(LEANED, "r", 1, { archetype: "newcomer" }), []);
s.draw = [{ uid: "x1", id: "canvass" }];
takeLead(town(s, "B"), "you");
updateFlags(s);
expect(s.hand.map((c) => c.id)).toEqual(["canvass"]);
});
});