import type { CandidatePublic, CareerAbilities, MiniGameKind, PartyKind, RogueArchetype, RogueIntentKind, RogueLog, RogueResult, RogueTrait } from "@election2026/protocol"; /** 一區的起始資料:真實人口、投票率、各陣營的底盤(由選民模型算出,見 setup.ts) */ export interface TownSetup { code: string; name: string; voters: number; /** 起始支持比例(你、對手、其他人,加總 1) */ lean: { you: number; rival: number; others: number }; traits: RogueTrait[]; /** 相鄰的區(地圖拓撲算出;沒有拓撲時是空的) */ neighbors?: string[]; } export type RivalStyle = "balanced" | "ground" | "air"; export interface RogueSetup { scenarioId: string; scenarioName: string; packId: string; countyCode: string; countyName: string; /** 選區名稱(例如「高雄市第 4 選舉區」);舊存檔沒有,用縣市名稱 */ area?: string; office: string; you: CandidatePublic; rival: CandidatePublic; others: CandidatePublic[]; towns: TownSetup[]; /** 起始資金(萬元) */ money: number; /** 對手 AI 的風格(劇本設定) */ rivalStyle: RivalStyle; /** 對手是真實人物(行動名稱要中性,docs/22) */ rivalReal: boolean; /** 「其他候選人」裡最強的那一位佔其他人票數的比例(開票判斷當選用;沒寫是 1) */ othersTop?: number; /** 多席次(議員):選幾席;贏過最後一席(對手)就當選 */ seats?: number; /** 多席次:最後一席的得票率(真實資料,畫面說明用) */ threshold?: number; /** 議員選區把村里分群時:村里 → 遊戲裡的區代碼(前端畫地圖用) */ groups?: Record; } export interface RunOptions { /** 逆風難度 0–10 */ ascension: number; /** 一局幾週(新手教學 3 週) */ weeks: number; /** 新手教學:固定牌組、沒有辯論和黑函 */ tutorial: boolean; /** 每日一戰(只是標記,給成就和畫面用) */ daily: string | null; /** 勢均力敵(開局已經在 setup 拉平;這裡記下來給「再選一次」用) */ even: boolean; /** 玩真實候選人時,開局能力的依據(公開選舉紀錄) */ abilitiesBasis?: string | null; /** 政治生涯的第幾關 */ career: { stage: number; total: number } | null; /** 政治生涯的角色能力(沒有是 null) */ abilities: CareerAbilities | null; /** 政策島的建築等級(id → 1–3) */ perks: Record; /** 自創角色的出身與政黨(18 第 53 項:專屬起始牌、黨的資源);真實候選人、舊存檔是 null */ origin?: RunOrigin | null; /** 關掉大絕招(只給模擬對照用) */ noUltimate?: boolean; /** 候選人類型(docs/25):決定技能與弱點;新手教學、舊存檔沒有 */ archetype?: RogueArchetype | null; /** 類型的依據(選舉紀錄或出身) */ archetypeBasis?: string | null; /** 對手的類型與依據(真實人物才有;rival.ts RIVAL_TYPES) */ rivalArchetype?: RogueArchetype | null; rivalArchetypeBasis?: string | null; /** 玩家的聲望(unlocks.ts):決定哪些核心牌、幕僚已經解鎖;null 是全部解鎖 */ reputation?: number | null; } export interface RunOrigin { background: string; party: string | null; kind: PartyKind; } export type Owner = "you" | "rival" | null; export interface TownState { code: string; name: string; voters: number; you: number; rival: number; others: number; undecided: number; traits: RogueTrait[]; neighbors: string[]; shield: number; rivalShield: number; office: number; played: string[]; /** 區的大小係數:人越多,同一個行程接觸到的人越多(見 townReach) */ reach: number; /** 插旗:已表態的票裡領先 FLAG_LEAD 以上 */ flag: Owner; /** 上一個插旗的人(從對手手上拿過來才算翻轉,中間掉成沒人插旗也算) */ was: Owner; /** 本命區(docs/25):誰的本命區;舊存檔沒有 */ home?: Owner; } export interface CardInst { uid: string; id: string; /** 升級過(「+」版) */ up?: boolean; } export interface Intent { id: string; kind: RogueIntentKind; town: string | null; amount: number; cut: number; /** 預告過的大型行動 */ big?: boolean; } /** 電視辯論的三種論點:拿數據、講故事、反問對手 */ export type DebateMove = "policy" | "emotion" | "question"; /** 你手上的一張論點;policy 是你自己寫的政策名字 */ export interface DebateArg { kind: DebateMove; text: string; policy?: string; } export interface DebateRound { arg: DebateArg; result: "win" | "lose" | "tie"; combo: number; votes: number; } export type Scene = | { kind: "press"; id: string } | { kind: "debate"; round: number; topics: string[]; /** 每回合的質疑和最好的回應 */ challenges: string[]; needs: DebateMove[]; hand: DebateArg[]; history: DebateRound[]; wins: number; losses: number; /** 連續答對幾回合 */ combo: number; } | { kind: "crisis"; amount: number } | { kind: "poll"; you: number; rival: number; lead: boolean }; export type StopKind = "card" | "advisor" | "upgrade" | "remove" | "shop" | "encounter" | "event" | "minigame" | "elite"; export interface ShopItem { kind: "card" | "advisor" | "remove"; id: string; price: number; sold: boolean; } export type Stop = | { kind: "card"; options: string[] } | { kind: "advisor"; options: string[] } | { kind: "upgrade" } | { kind: "remove" } | { kind: "shop"; items: ShopItem[] } | { kind: "encounter"; town: string; encounters: string[]; step: number; score: number; notes: string[] } | { kind: "event"; id: string } | { kind: "minigame"; game: MiniGameKind; town: string | null; level: number; seed: number } /** 地方派系談判(精英站,docs/25 第 3 節):選了就接下挑戰,不用進門 */ | { kind: "elite"; town: string; goal: number }; export interface RunStats { played: number; attacks: number; flips: number; maxCombo: number; bigHit: number; debateWins: number; /** 玩了幾次選戰小遊戲(舊存檔沒有) */ minigames?: number; /** 放了幾次大絕招(舊存檔沒有) */ ultimates?: number; crisisLoss: number; /** 第 4 週民調落後幾個百分點(逆轉勝用) */ pollGap: number; bought: number; removed: number; upgraded: number; } export interface RunState { version: 2; runId: string; seed: number; /** 亂數狀態(每次抽亂數都往前推,存檔重開也接得上) */ rng: number; setup: Omit; options: RunOptions; /** 數字的比例尺:全市投票人數 / 100 萬(小縣市的牌面數字跟著變小) */ scale: number; week: number; totalWeeks: number; phase: "scene" | "plan" | "route" | "stop" | "finished"; energy: number; energyMax: number; /** 下週體力的調整(地方抉擇、黑函危機) */ energyNext: number; money: number; momentum: number; rivalMomentum: number; leverage: number; towns: TownState[]; draw: CardInst[]; hand: CardInst[]; discard: CardInst[]; /** 這局用掉的「消耗」牌 */ exhausted: CardInst[]; nextUid: number; advisors: string[]; /** 打過的能力牌(整局有效) */ powers: string[]; /** 這週的加成(政策白皮書) */ buffs: { tag: string; mult: number }[]; intents: Intent[]; /** 對手預告的大型行動(下週執行) */ nextBig: Intent | null; event: string; /** 這週已經打過的牌、網路牌張數 */ playedThisWeek: number; onlinePlayed: number; /** 正在結算的這張牌花了幾點體力(夜貓子用;結算完歸零) */ paying?: number; /** 這週打過的牌(每週一次的牌用) */ weekCards?: string[]; /** 政策卡的名字(玩家自己取的;id → 名字) */ policyNames?: Record; /** 這週的戰果(結算畫面、狀態列用) */ tally?: { gained: number; lost: number; blocked: number; startYou: number; startRival: number }; scene: Scene | null; route: StopKind[] | null; /** 下一站每扇門裡面的內容(開門前就備好,畫面直接攤開;舊存檔沒有) */ routeStops?: Stop[] | null; /** 下下週的門(docs/25:看得到前方,好規劃);只先決定種類,到時候不能用的門會重抽 */ nextRoute?: StopKind[] | null; /** 最近一次民調(第 4 週自動公布;舊存檔沒有) */ poll?: { week: number; you: number; rival: number; lead: boolean } | null; stop: Stop | null; log: RogueLog[]; history: { week: number; you: number; rival: number }[]; /** 上週你出牌最多的區(對手會跟過去) */ lastFocus: string[]; /** 這週發生的事(下週堵麥出題用) */ lastWeek: { attacked: boolean; rallied: boolean; flips: number }; /** 出現過的堵麥題目、地方抉擇(不重複) */ seen: string[]; /** 黑函危機發生過 */ crisisDone: boolean; /** 大黨的包袱:「黨中央風波」在第幾週(沒有是 null;舊存檔沒有這個欄位) */ partyStorm?: number | null; /** 大絕招(ultimate.ts):集氣 0–100、放過幾次、這局最多幾次(舊存檔沒有,用到時補上) */ ult?: { charge: number; used: number; max: number }; /** 開局時全市已表態的票裡你領先幾個百分點(以小搏大的成就用) */ startGap: number; /** 本命區的依據文字(雙方) */ homeBasis?: { you: string; rival: string }; /** 媒體型:這週爆紅過了(每週一次)、下一張空戰牌加倍 */ viral?: { used: boolean; boost: boolean }; /** 你翻走對手的本命區:對手下週少幾個行動 */ rivalStun?: number; /** 這局的行事曆(docs/25 第 4 節):辯論、民調、黑函在第幾週、地方事件週;舊存檔、短局沒有,用固定週次 */ schedule?: Schedule; /** 這局對手的規則(docs/25 第 3 節,rival.ts 的 RIVAL_RULES);舊存檔沒有 */ rivalRules?: string[]; /** 這週翻區已經抽了幾張(每週最多 FLIP_DRAW_MAX) */ flipDraws?: number; /** 地方派系談判:第 week 週在 town 淨增 goal 票(start 是那週開始時的差距) */ contract?: { town: string; goal: number; week: number; start?: number }; /** 流派核心牌(docs/25 第 2 節):聲量(整局累積)、這週打過幾張地面牌、反將一軍、懶人包小編用過了 */ buzz?: number; groundPlayed?: number; turnaround?: boolean; memeUsed?: boolean; stats: RunStats; result: RogueResult | null; } export interface Schedule { debate: number; poll: number; crisis: number; /** 地方事件週:第幾週、哪個事件(data.ts EVENTS 裡有 local 的);舊存檔沒有 */ local?: { week: number; event: string }[]; } export const SAVE_VERSION = 2; export const TOTAL_WEEKS = 8; export const HAND_SIZE = 5; /** 手牌上限:抽牌抽到這麼多張就停 */ export const HAND_MAX = 10; export const BASE_ENERGY = 3; export const MAX_ADVISORS = 5; export const MOMENTUM_MAX = 5; /** 開局時還沒表態的比例 */ export const UNDECIDED_START = 0.3; /** 每週固定收入(萬元:小額捐款、政黨補助) */ export const WEEKLY_INCOME = 100; /** 插旗:已表態的票裡領先幾個百分點 */ export const FLAG_LEAD = 0.08; /** 每面旗每週的小額捐款(萬元) */ export const FLAG_INCOME = 20; /** 對手規則「地方大老」:翻對手的旗要領先這麼多 */ export const ELDER_LEAD = 0.12; /** 翻區獎勵:每翻轉一區抽 1 張,每週最多幾張(docs/25 第 5 節) */ export const FLIP_DRAW_MAX = 2; /** 可重現的亂數(mulberry32):回傳 [0, 1) 與新的狀態 */ export function nextRandom(rng: number): [number, number] { const next = (rng + 0x6d2b79f5) | 0; let t = next; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return [((t ^ (t >>> 14)) >>> 0) / 4294967296, next]; } /** 在這份狀態副本上抽一個亂數(只在規則函式內部、對副本使用) */ export function rand(s: RunState): number { const [x, next] = nextRandom(s.rng); s.rng = next; return x; } export function shuffle(s: RunState, items: readonly T[]): T[] { const out = [...items]; for (let i = out.length - 1; i > 0; i--) { const j = Math.floor(rand(s) * (i + 1)); [out[i], out[j]] = [out[j]!, out[i]!]; } return out; } export function pickWeighted(s: RunState, items: readonly T[], weight: (x: T) => number): T | undefined { const total = items.reduce((a, x) => a + Math.max(0, weight(x)), 0); if (total <= 0) return undefined; let r = rand(s) * total; for (const x of items) { r -= Math.max(0, weight(x)); if (r <= 0) return x; } return items[items.length - 1]; } /** 規則函式不改輸入:先複製一份再改 */ export const cloneState = (s: RunState): RunState => structuredClone(s); export const totals = (towns: readonly TownState[]) => towns.reduce((a, t) => ({ you: a.you + t.you, rival: a.rival + t.rival, others: a.others + t.others, undecided: a.undecided + t.undecided, voters: a.voters + t.voters }), { you: 0, rival: 0, others: 0, undecided: 0, voters: 0, }); /** 區的大小係數:(這區投票人數 / 各區平均)^0.3,夾在 0.6–1.4;小區約 ×0.7、大區約 ×1.3 */ export const townReach = (voters: number, meanVoters: number): number => Math.min(1.4, Math.max(0.6, (voters / (meanVoters || 1)) ** 0.3)); /** 把票數四捨五入到十位,牌面和畫面上的數字比較好讀 */ /** 票數取整:大的取到十位;里長選舉這種幾千人的小選舉,數字小,取到個位(不然都變成 0) */ export const round10 = (n: number): number => (Math.abs(n) < 200 ? Math.round(n) : Math.round(n / 10) * 10); /** 已表態的票裡,你領先對手幾個百分點(負的是落後) */ export const townLead = (t: Pick): number => (t.you - t.rival) / (t.you + t.rival + t.others || 1); export const newCard = (s: RunState, id: string, up = false): CardInst => ({ uid: `c${s.nextUid++}`, id, ...(up ? { up: true } : {}) }); /** 把這些紀錄算進這週的戰果:正的是你拉到、負的是被拉走、blocked 是擋下 */ export function addTally(s: RunState, entries: readonly { votes: number; blocked?: number }[]): void { const t = s.tally ?? { gained: 0, lost: 0, blocked: 0, startYou: 0, startRival: 0 }; for (const e of entries) { if (e.votes > 0) t.gained += e.votes; else t.lost -= e.votes; t.blocked += e.blocked ?? 0; } s.tally = t; } /** 拿到新牌(下一站、物資行、地方抉擇):放在抽牌堆最上面,下週一定抽到(狀態牌、詛咒牌照樣進棄牌堆) */ export function gainCard(s: RunState, card: CardInst): void { s.draw.unshift(card); } export const emptyStats = (): RunStats => ({ played: 0, attacks: 0, flips: 0, maxCombo: 0, bigHit: 0, debateWins: 0, crisisLoss: 0, pollGap: 0, bought: 0, removed: 0, upgraded: 0 });