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

61 lines
3.8 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.

/**
* 政治生涯地方選舉(里長、議員)平衡報告:node src/career-balance.ts [--seeds 120]
*
* 幾個縣市各挑幾個里,用自創角色(民進黨、國民黨、無黨籍)跑里長、議員兩關,
* 看打得好的玩家(greedy)和隨手玩(casual)的勝率。逆風照政治生涯:里長 0 級、議員 1 級。
*/
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { parseArgs } from "node:util";
import { CUSTOM_ID, withCustomCandidate } from "@election2026/content";
import { DEFAULT_PACKS_ROOT } from "@election2026/pack-loader";
import { runBot, type BotName } from "@election2026/rogue";
import { COUNCIL_WEEKS, VILLAGE_WEEKS, councilSetup, districtOfVillage, villageSetup, type CouncilDistrict } from "@election2026/rogue/local";
import type { TopologyLike } from "@election2026/rogue/setup";
import { loadWorld } from "./world.ts";
const COUNTIES = ["local2022-63000", "local2022-64000", "local2022-10007", "local2022-10015"];
const PARTIES = ["民主進步黨", "中國國民黨", null] as const;
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 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: "120" } } });
const seeds = Array.from({ length: Number(values.seeds) }, (_, i) => 7_000 + i);
const council = (JSON.parse(await readFile(new URL("../../../data/build/council/2022.json", import.meta.url), "utf-8")) as { districts: CouncilDistrict[] }).districts;
const rows: string[] = ["| 縣市 | 政黨 | 關卡 | 隨手玩 | 打得好 |", "|---|---|---|---:|---:|"];
for (const id of COUNTIES) {
const base = await loadWorld(id);
const topology = JSON.parse(await readFile(join(DEFAULT_PACKS_ROOT, base.scenario.packId, "topology.json"), "utf-8")) as TopologyLike;
for (const party of PARTIES) {
const scenario = withCustomCandidate(base.scenario, { name: "測試人", party, background: "基層里長", stats: { fame: 5, eloquence: 5, strategy: 5, wealth: 5, network: 5 }, avatar: { ...avatar }, replace: null }, new Set());
const world = { ...base, scenario };
const empty = (): Partial<Record<BotName, number[]>> => Object.fromEntries(BOTS.map((b) => [b, []]));
const results: Record<string, Partial<Record<BotName, number[]>>> = { 里長: empty(), 議員: empty() };
for (const seed of seeds.slice(0, Math.ceil(seeds.length / 4))) {
// 每個種子挑一個里:里長、議員各跑幾局
const village = villageSetup(world, CUSTOM_ID, null, seed);
const code = village.scenarioId.slice("village:".length);
const district = districtOfVillage(council, code);
for (let k = 0; k < 4; k++) {
for (const bot of BOTS) {
results.里長![bot]!.push(runBot(village, seed * 10 + k, bot, { ascension: 0, weeks: VILLAGE_WEEKS }).result!.won ? 1 : 0);
if (district) {
const c = councilSetup(world, CUSTOM_ID, district, seed, topology).setup;
results.議員![bot]!.push(runBot(c, seed * 10 + k, bot, { ascension: 1, weeks: COUNCIL_WEEKS }).result!.won ? 1 : 0);
}
}
}
}
for (const stage of ["里長", "議員"]) rows.push(`| ${base.scenario.region.countyName} | ${party ?? "無黨籍"} | ${stage} | ${pct(mean(results[stage]!.casual!))} | ${pct(mean(results[stage]!.greedy!))} |`);
console.log(`${base.scenario.region.countyName} ${party ?? "無黨籍"} 完成`);
}
}
console.log(rows.join("\n"));
}
await main();