72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
import type { CandidatePublic } from "@election2026/protocol";
|
||
import { choose, endWeek } from "../src/rules.ts";
|
||
import type { RogueSetup, RunState } from "../src/state.ts";
|
||
|
||
/** 測試共用:一個四區的假城市、往前推進的小工具 */
|
||
|
||
export const person = (id: string, number: number, isPlayer = false): CandidatePublic => ({
|
||
id,
|
||
name: id,
|
||
party: null,
|
||
slot: "other",
|
||
number,
|
||
isPlayer,
|
||
description: "",
|
||
issueIds: [],
|
||
tags: [],
|
||
avatar: null,
|
||
runningMate: null,
|
||
gender: null,
|
||
slogan: null,
|
||
sloganSource: null,
|
||
});
|
||
|
||
export const lean = { you: 0.45, rival: 0.45, others: 0.1 };
|
||
export const SETUP: RogueSetup = {
|
||
scenarioId: "test",
|
||
scenarioName: "測試市長",
|
||
packId: "test",
|
||
countyCode: "00000",
|
||
countyName: "測試市",
|
||
office: "市長",
|
||
you: person("me", 1, true),
|
||
rival: person("rival", 2),
|
||
others: [person("minor", 3)],
|
||
towns: [
|
||
{ code: "A", name: "甲區", voters: 300_000, lean, traits: ["dense", "young"], neighbors: ["B"] },
|
||
{ code: "B", name: "乙區", voters: 200_000, lean, traits: ["old", "temples"], neighbors: ["A", "C"] },
|
||
{ code: "C", name: "丙區", voters: 50_000, lean, traits: ["rural"], neighbors: ["B"] },
|
||
{ code: "D", name: "丁區", voters: 150_000, lean, traits: ["hakka"], neighbors: [] },
|
||
],
|
||
money: 1000,
|
||
rivalStyle: "balanced",
|
||
rivalReal: true,
|
||
};
|
||
|
||
export const must = (r: RunState | { error: string }): RunState => {
|
||
if ("error" in r) throw new Error(r.error);
|
||
return r;
|
||
};
|
||
|
||
/** 一週一週往前(不出牌、下一站都不去),到第 week 週開場 */
|
||
export function advance(s: RunState, week: number): RunState {
|
||
while (s.week < week) s = must(choose(must(endWeek(toPlan(s))), "skip"));
|
||
return s;
|
||
}
|
||
|
||
/** 一路選第一個選項,直到可以出牌 */
|
||
export function toPlan(s: RunState): RunState {
|
||
// 記者堵麥在出牌階段也要先回答(s.scene 還在)
|
||
for (let k = 0; k < 20 && (s.phase !== "plan" || s.scene) && s.phase !== "finished"; k++) {
|
||
if (s.phase === "stop" && (s.stop?.kind === "upgrade" || s.stop?.kind === "remove")) s = must(choose(s, "skip"));
|
||
else if (s.phase === "stop" && s.stop?.kind === "shop") s = must(choose(s, "skip"));
|
||
// 開跑記者會不開支票:其他測試照沒有支票的情境(支票在 pledges.test.ts)
|
||
else if (s.scene?.kind === "pledge") s = must(choose(s, "skip"));
|
||
else {
|
||
const r = choose(s, 0);
|
||
s = "error" in r ? must(choose(s, 1)) : r;
|
||
}
|
||
}
|
||
return s;
|
||
}
|