election-game/backend/apps/sim/src/party-balance.ts

69 lines
4.3 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.

/**
* 出身與黨的資源平衡報告(18 第 53 項):node src/party-balance.ts [--seeds 40]
*
* 政治生涯的條件(勢均力敵開局、自創角色、能力照捏人物點數+出身+黨的資源),
* 同一個角色分別掛大黨、民眾黨、小黨跑立委、縣市長兩種關卡,看打得好(greedy)和隨手玩(casual)的勝率。
* 目標:打得好的玩家,各黨勝率差在 10 個百分點以內(小黨另外多拿經驗)。
*/
import { parseArgs } from "node:util";
import { CUSTOM_ID, withCustomCandidate } from "@election2026/content";
import { originAbilities, partyKind, type Background } from "@election2026/protocol";
import { runBot, type BotName } from "@election2026/rogue";
import { buildSetup } from "@election2026/rogue/setup";
import { loadWorld } from "./world.ts";
const SCENARIOS = ["leg2024-68000-01", "leg2024-64000-04", "local2022-66000", "local2022-10007"];
const PARTIES = ["民主進步黨", "台灣民眾黨", "時代力量", null] as const;
const BACKGROUNDS: Background[] = ["律師", "基層里長", "網紅"];
const BOTS: BotName[] = ["casual", "greedy"];
const avatar = { skin: "#eebd98", hair: "bob", hairColor: "#2f2520", outfit: "blouse", suit: "#24324a", shirt: "#f7f5f0", tie: "#b3262d", glasses: true, beard: false, bg: "#dfe8f0" } as const;
const stats = { fame: 5, eloquence: 5, strategy: 5, wealth: 5, network: 5 };
/** 政治生涯升級拿到的技能點(POINTS=4 約等於立委關的 Lv 5):輪流加在口才、組織、媒體 */
function withPoints(a: ReturnType<typeof originAbilities>, n: number): ReturnType<typeof originAbilities> {
const out = { ...a };
const order = ["eloquence", "organization", "media"] as const;
for (let k = 0; k < n; k++) out[order[k % 3]!] = Math.min(5, out[order[k % 3]!] + 1);
return out;
}
const mean = (xs: number[]) => xs.reduce((a, b) => a + b, 0) / (xs.length || 1);
const pct = (x: number) => `${(x * 100).toFixed(0)}%`;
async function main(): Promise<void> {
const { values } = parseArgs({ options: { seeds: { type: "string", default: "40" } } });
const seeds = Array.from({ length: Number(values.seeds) }, (_, i) => 9_000 + i);
const totals: Record<string, Partial<Record<BotName, number[]>>> = {};
const rows: string[] = ["| 劇本 | 政黨 | 隨手玩 | 打得好 |", "|---|---|---:|---:|"];
for (const id of SCENARIOS) {
const base = await loadWorld(id);
for (const party of PARTIES) {
const same = party ? base.scenario.candidates.find((c) => c.party === party) : undefined;
const won: Partial<Record<BotName, number[]>> = { casual: [], greedy: [] } as Partial<Record<BotName, number[]>>;
for (const background of BACKGROUNDS) {
const custom = { name: "測試人", party, background, stats, avatar: { ...avatar }, replace: same?.id ?? null };
const world = { ...base, scenario: withCustomCandidate(base.scenario, custom, new Set()) };
// REAL=1:政治生涯的條件(照真實底盤、最多落後 15 點,有大絕招)
const setup = buildSetup(world, CUSTOM_ID, process.env.REAL ? { even: false, maxGap: Number(process.env.GAP ?? 0.15) } : { even: true });
const origin = { background, party, kind: partyKind(party) };
for (const seed of seeds) {
for (const bot of BOTS) {
const s = runBot(setup, seed, bot, process.env.BASELINE ? { ascension: 2, even: true } : { ascension: 2, even: !process.env.REAL, abilities: withPoints(originAbilities(custom), Number(process.env.POINTS ?? 0)), origin });
won[bot]!.push(s.result!.won ? 1 : 0);
}
}
}
const key = partyKind(party) === "major" ? "大黨" : partyKind(party) === "tpp" ? "民眾黨" : party ? "小黨" : "無黨籍";
totals[key] ??= { casual: [], greedy: [] } as Partial<Record<BotName, number[]>>;
for (const bot of BOTS) totals[key]![bot]!.push(...won[bot]!);
rows.push(`| ${base.scenario.name.replace(/:.*$/, "")} | ${party ?? "無黨籍"} | ${pct(mean(won.casual!))} | ${pct(mean(won.greedy!))} |`);
console.log(`${id} ${party ?? "無黨籍"} 完成`);
}
}
rows.push("", "| 合計 | 隨手玩 | 打得好 |", "|---|---:|---:|");
for (const [k, v] of Object.entries(totals)) rows.push(`| ${k} | ${pct(mean(v.casual!))} | ${pct(mean(v.greedy!))} |`);
console.log(rows.join("\n"));
}
await main();