import { ENCOUNTERS } from "@election2026/content"; import type { EncounterDef } from "@election2026/engine"; import { ADVISORS, type AdvisorDef } from "./advisors.ts"; import { AFFINITY_WEIGHT, ARCHETYPES } from "./archetypes.ts"; import { STOP_EVENTS, STOP_EVENT_BY_ID } from "./content.ts"; import { REWARD_CARDS, cardDef, upgradable } from "./data.ts"; import { addMomentum, asc, cityVotes, has, isArch, moveVotes } from "./mechanics.ts"; import { applyOutcome, choiceBlocked, rollChoice } from "./scenes.ts"; import type { MiniGameKind } from "@election2026/protocol"; import { MAX_ADVISORS, TOTAL_WEEKS, gainCard, newCard, pickWeighted, rand, round10, shuffle, type RunState, type ShopItem, type Stop, type StopKind } from "./state.ts"; /** * 每週結束後的「下一站」(docs/22,像《殺戮尖塔》的地圖):三選一,再進那一站做選擇。 * 也可以都不選,改拿 SKIP_MONEY 萬。 */ export const SKIP_MONEY = 150; /** 商店價格(萬元) */ export const PRICES = { common: 120, rare: 220, advisor: 300, remove: 100 } as const; /** 深入基層:三個遭遇的總分(−6 到 +6)決定那區加多少票 */ export const ENCOUNTER_BASE = 1380; export const ENCOUNTER_STEP = 0.3; /** * 選戰小遊戲(docs/23):玩完回報 0–100 分,70 分算「玩得不錯」。 * 掃街握手:那區加票;造勢:聲勢 +0/1/2 加全市少量票;看板卡位:那區每週多一點票(據點)。 */ export const MINIGAME = { handshake: 2300, handshakeHot: 90, rallyVotes: 600, rallyStep: [40, 80], billboardBase: 250, billboardStep: 6, good: 70 } as const; export const MINIGAME_INFO: Record = { handshake: { title: "掃街握手", text: "人群裡有人舉手,時間內握越多手越好;那一區加票" }, rally: { title: "造勢喊凍蒜", text: "跟著鼓點喊凍蒜,拍子越準聲勢越高" }, billboard: { title: "看板卡位", text: "把看板推到人流最多的路口;那一區之後每週多一點票" }, }; export const STOP_LABELS: Record = { card: { title: "拜會地方", text: "三選一,加一張新牌" }, advisor: { title: "招募幕僚", text: "三選一,加一位幕僚" }, upgrade: { title: "黨部研習", text: "升級一張牌(+版)" }, remove: { title: "整頓行程", text: "從牌組拿掉一張牌" }, shop: { title: "競選物資行", text: "用資金買牌、幕僚、刪牌" }, encounter: { title: "深入基層", text: "親自拜票,連遇三個人" }, event: { title: "地方抉擇", text: "有代價、有風險的選擇" }, minigame: { title: "選戰小遊戲", text: "掃街、造勢或看板卡位,玩得好就加成" }, elite: { title: "地方派系談判", text: "接下挑戰:下週在指定的區淨增票數,達成拿一位稀有幕僚;沒達成聲勢 −1" }, }; /** 地方派系談判的目標(以全市 100 萬人為基準,乘上區的大小) */ export const ELITE_GOAL = 3000; const deckOf = (s: RunState) => [...s.draw, ...s.discard, ...s.hand]; const optionCount = (s: RunState) => (asc(s, 4) ? 2 : 3); const advisorOpen = (s: RunState) => s.advisors.length < MAX_ADVISORS; /** 這週結束後可以去的下一站 */ export function makeRoute(s: RunState): StopKind[] { const out: StopKind[] = []; const must: StopKind = s.week % 2 === 0 && advisorOpen(s) ? "advisor" : "card"; out.push(must); const weights: Record = { card: must === "card" ? 0 : 2.5, advisor: must === "advisor" || !advisorOpen(s) ? 0 : 0.6, upgrade: 1.5, remove: deckOf(s).length > 8 ? 1 : 0, shop: s.money >= 200 ? 1.3 : 0, encounter: 1.2, event: STOP_EVENTS.some((e) => !s.seen.includes(e.id)) ? 1.3 : 0, minigame: s.options.tutorial ? 0 : 1.1, // 精英站:第 2、4 週結束後可能出現(docs/25 第 3 節) elite: !s.options.tutorial && s.totalWeeks === TOTAL_WEEKS && (s.week === 2 || s.week === 4) ? 1.4 : 0, }; while (out.length < optionCount(s)) { const kinds = (Object.keys(weights) as StopKind[]).filter((k) => !out.includes(k)); const k = pickWeighted(s, kinds, (x) => weights[x]); if (!k) break; out.push(k); } return shuffle(s, out); } /** 和你的類型搭配(docs/25 第 1c 節):比較常出現 */ const affinity = (s: RunState, tags: readonly string[]): number => { const arch = s.options.archetype ? ARCHETYPES[s.options.archetype] : null; return arch && arch.tags.some((t) => tags.includes(t)) ? AFFINITY_WEIGHT : 1; }; const advisorAffinity = (s: RunState, a: AdvisorDef): number => { const arch = s.options.archetype ? ARCHETYPES[s.options.archetype] : null; return arch && arch.school === a.school ? AFFINITY_WEIGHT : 1; }; /** 幕僚選項:不重複,和類型同一派的比較常出現 */ function advisorOptions(s: RunState, n: number): string[] { const out: string[] = []; const pool = ADVISORS.filter((a) => !has(s, a.id)); while (out.length < n) { const a = pickWeighted( s, pool.filter((x) => !out.includes(x.id)), (x) => advisorAffinity(s, x), ); if (!a) break; out.push(a.id); } return out; } /** 物資行的價格:財務型八折 */ const price = (s: RunState, base: number): number => Math.round(base * (isArch(s, "finance") ? 0.8 : 1)); function cardOptions(s: RunState, n: number): string[] { const options: string[] = []; const pool = REWARD_CARDS.filter((c) => c.effect.kind !== "kneel" || s.week >= s.totalWeeks - 3); while (options.length < n) { const c = pickWeighted( s, pool.filter((x) => !options.includes(x.id)), (x) => (x.rarity === "rare" ? 1 : 3) * affinity(s, x.tags), ); if (!c) break; options.push(c.id); } return options; } /** 選情最接近的大區(深入基層、小遊戲去那裡) */ const contestedTown = (s: RunState) => pickWeighted(s, s.towns, (t) => t.voters * (1 - Math.abs(t.you - t.rival) / (t.you + t.rival || 1)) ** 3) ?? s.towns[0]!; const encounterPool = (): readonly EncounterDef[] => ENCOUNTERS.filter((e) => e.activities.some((a) => ["*", "market_visit", "night_market", "street_interview", "door_to_door", "pilgrimage"].includes(a))); function makeStop(s: RunState, kind: StopKind): Stop { switch (kind) { case "elite": { const t = contestedTown(s); return { kind, town: t.code, goal: round10(ELITE_GOAL * s.scale * t.reach) }; } case "card": return { kind, options: cardOptions(s, optionCount(s)) }; case "advisor": return { kind, options: advisorOptions(s, optionCount(s)) }; case "upgrade": case "remove": return { kind }; case "shop": { const items: ShopItem[] = cardOptions(s, 3).map((id) => ({ kind: "card", id, price: price(s, cardDef(id)!.rarity === "rare" ? PRICES.rare : PRICES.common), sold: false })); const advisor = advisorOpen(s) ? advisorOptions(s, 1)[0] : undefined; if (advisor) items.push({ kind: "advisor", id: advisor, price: price(s, PRICES.advisor), sold: false }); items.push({ kind: "remove", id: "remove", price: price(s, PRICES.remove), sold: false }); return { kind, items }; } case "encounter": { const town = contestedTown(s); return { kind, town: town.code, encounters: shuffle(s, encounterPool()).slice(0, 3).map((e) => e.id), step: 0, score: 0, notes: [] }; } case "minigame": { const game = (["handshake", "rally", "billboard"] as const)[Math.floor(rand(s) * 3)]!; // 難度:第 1 週 0.1,越後面越難,最高 0.9 const level = Math.min(0.9, 0.1 + (0.8 * (s.week - 1)) / Math.max(1, s.totalWeeks - 2)); return { kind, game, town: game === "rally" ? null : contestedTown(s).code, level: Math.round(level * 100) / 100, seed: Math.floor(rand(s) * 2 ** 31) }; } case "event": { // 選了這扇門才算看過(pickRoute),沒選的之後還可能出現 const pool = STOP_EVENTS.filter((e) => !s.seen.includes(e.id)); const e = pool[Math.floor(rand(s) * pool.length)] ?? STOP_EVENTS[0]!; return { kind, id: e.id }; } } } /** 這週結束:排好三扇門,門裡的內容也先備好(畫面直接攤開,一次點選就拿到) */ /** 先抽好的門到時候還能不能用(幕僚滿了、牌組太小、地方抉擇都看過了、精英站只在第 2、4 週) */ function stillValid(s: RunState, kinds: readonly StopKind[]): boolean { return kinds.every((k) => { if (k === "advisor") return advisorOpen(s); if (k === "remove") return deckOf(s).length > 8; if (k === "event") return STOP_EVENTS.some((e) => !s.seen.includes(e.id)); if (k === "elite") return s.week === 2 || s.week === 4; return true; }); } export function prepareRoute(s: RunState): void { // 上週已經預告的門照用(還能用的話) s.route = s.nextRoute && stillValid(s, s.nextRoute) ? s.nextRoute : makeRoute(s); s.routeStops = s.route.map((k) => makeStop(s, k)); // 下下週的門:照下週的狀況先抽好種類,畫面灰色顯示 if (s.week + 1 < s.totalWeeks) { s.week += 1; s.nextRoute = makeRoute(s); s.week -= 1; } else s.nextRoute = null; } /** 拜會地方、招募幕僚、地方抉擇:在門上直接選,不用再進門 */ export const pickInDoor = (kind: StopKind): boolean => kind === "card" || kind === "advisor" || kind === "event"; /** * 選下一站;skip 是不去,改拿資金。option:門上直接選的選項(拜會地方、招募幕僚、地方抉擇),一次完成。 * done 之後由呼叫的人開始下一週 */ export function pickRoute(s: RunState, pick: number | "skip", option?: number): { error: string } | { done: boolean } { if (!s.route) return { error: "現在沒有下一站可以選" }; if (pick === "skip") { s.money += SKIP_MONEY; s.log.push({ side: "system", text: `回總部休息:資金 +${SKIP_MONEY} 萬`, town: null, votes: 0 }); s.route = null; s.routeStops = null; return { done: true }; } const kind = s.route[pick]; if (!kind) return { error: "沒有這一站" }; const stop = s.routeStops?.[pick] ?? makeStop(s, kind); s.route = null; s.routeStops = null; // 地方派系談判:選了就接下挑戰,下週結算 if (stop.kind === "elite") { s.contract = { town: stop.town, goal: stop.goal, week: s.week + 1 }; const name = s.towns.find((t) => t.code === stop.town)?.name ?? stop.town; s.log.push({ side: "system", text: `接下地方派系談判:下週在${name}淨增 ${stop.goal.toLocaleString("en-US")} 票`, town: stop.town, votes: 0 }); return { done: true }; } if (stop.kind === "event") s.seen.push(stop.id); s.stop = stop; s.phase = "stop"; if (option !== undefined && pickInDoor(kind)) return pickStop(s, option, null); return { done: false }; } const removeCard = (s: RunState, uid: string): boolean => { for (const pile of [s.draw, s.discard, s.hand]) { const i = pile.findIndex((c) => c.uid === uid); if (i >= 0) { pile.splice(i, 1); s.stats.removed += 1; return true; } } return false; }; export const encounterById = (id: string): EncounterDef | undefined => ENCOUNTERS.find((e) => e.id === id); /** 在這一站做選擇。pick 是第幾個選項;target 是要升級、刪掉的牌(uid)。done 之後開始下一週 */ export function pickStop(s: RunState, pick: number | "skip", target: string | null): { error: string } | { done: boolean } { const stop = s.stop; if (!stop) return { error: "現在不在任何一站" }; if (pick === "skip" && stop.kind !== "encounter") { s.stop = null; return { done: true }; } switch (stop.kind) { case "card": { const id = stop.options[pick as number]; if (!id) return { error: "沒有這個選項" }; gainCard(s, newCard(s, id)); s.log.push({ side: "you", text: `牌組加入「${cardDef(id)!.name}」,下週一定會抽到`, town: null, votes: 0 }); break; } case "advisor": { const id = stop.options[pick as number]; if (!id || !advisorOpen(s)) return { error: "沒有這個選項" }; s.advisors.push(id); break; } case "upgrade": { const c = deckOf(s).find((x) => x.uid === target); if (!c || !upgradable(c.id, c.up)) return { error: "選一張可以升級的牌" }; c.up = true; s.stats.upgraded += 1; s.log.push({ side: "you", text: `「${cardDef(c.id)!.name}」升級了`, town: null, votes: 0 }); break; } case "remove": { if (!target || !removeCard(s, target)) return { error: "選一張要拿掉的牌" }; break; } case "shop": { const item = stop.items[pick as number]; if (!item || item.sold) return { error: "沒有這個商品" }; if (s.money < item.price) return { error: "資金不夠" }; if (item.kind === "remove" && (!target || !removeCard(s, target))) return { error: "選一張要拿掉的牌" }; if (item.kind === "advisor" && !advisorOpen(s)) return { error: "幕僚已經滿了" }; s.money -= item.price; item.sold = true; s.stats.bought += 1; if (item.kind === "card") gainCard(s, newCard(s, item.id)); if (item.kind === "advisor") s.advisors.push(item.id); // 商店可以一直買,按「離開」才結束 return { done: false }; } case "encounter": { const town = s.towns.find((t) => t.code === stop.town)!; if (stop.step >= stop.encounters.length) { s.stop = null; return { done: true }; } const e = encounterById(stop.encounters[stop.step]!); const option = e?.options[pick === "skip" ? -1 : pick]; if (!e || !option) return { error: "沒有這個選項" }; const result = option.roll ? (rand(s) < option.roll.chance ? option.roll.success : option.roll.failure) : option; stop.score += result.score; stop.notes.push(fillVars(result.text, s, town.name)); stop.step += 1; if (stop.step >= stop.encounters.length) { const mult = Math.max(0, 1 + ENCOUNTER_STEP * stop.score); const net = moveVotes(town, ENCOUNTER_BASE * s.scale * town.reach * mult * (1 + 0.08 * s.momentum), "you"); s.log.push({ side: "you", text: `深入基層・${town.name}(應對 ${stop.score >= 0 ? "+" : ""}${stop.score})`, town: town.code, votes: Math.round(net) }); if (stop.score >= 4) { addMomentum(s, 1); s.log.push({ side: "you", text: "鄉親口耳相傳:聲勢 +1", town: town.code, votes: 0 }); } } return { done: false }; } case "minigame": { if (typeof pick !== "number" || pick < 0 || pick > 100) return { error: "小遊戲分數要在 0–100" }; playMinigame(s, stop, Math.round(pick)); break; } case "event": { const def = STOP_EVENT_BY_ID.get(stop.id); const choice = def?.choices[pick as number]; if (!def || !choice) return { error: "沒有這個選項" }; const blocked = choiceBlocked(s, choice); if (blocked) return { error: blocked }; applyOutcome(s, rollChoice(s, choice), "between"); break; } } s.stop = null; return { done: true }; } /** 小遊戲的分數換成效果 */ function playMinigame(s: RunState, stop: Extract, score: number): void { const info = MINIGAME_INFO[stop.game]; const town = s.towns.find((t) => t.code === stop.town) ?? null; s.stats.minigames = (s.stats.minigames ?? 0) + 1; if (stop.game === "handshake" && town) { const net = moveVotes(town, ((MINIGAME.handshake * s.scale * town.reach * score) / MINIGAME.good) * (1 + 0.08 * s.momentum), "you"); s.log.push({ side: "you", text: `${info.title}・${town.name}(${score} 分)`, town: town.code, votes: Math.round(net) }); if (score >= MINIGAME.handshakeHot) { addMomentum(s, 1); s.log.push({ side: "you", text: "握手握到手痠,照片上了地方新聞:聲勢 +1", town: town.code, votes: 0 }); } } else if (stop.game === "rally") { const gain = MINIGAME.rallyStep.filter((x) => score >= x).length; if (gain) addMomentum(s, gain); const net = cityVotes(s, (MINIGAME.rallyVotes * s.scale * score) / 100); s.log.push({ side: "you", text: `${info.title}(${score} 分)${gain ? `:聲勢 +${gain}` : ":台下反應普通"}`, town: null, votes: Math.round(net) }); } else if (town) { const perWeek = MINIGAME.billboardBase + MINIGAME.billboardStep * score; town.office += perWeek; s.log.push({ side: "you", text: `${info.title}・${town.name}(${score} 分):看板據點每週 +${Math.round(perWeek * s.scale * town.reach)} 票`, town: town.code, votes: 0 }); } } export const fillVars = (text: string, s: RunState, town: string): string => text.replace(/\{town\}/g, town).replace(/\{temple\}/g, "地方大廟").replace(/\{name\}/g, s.setup.you.name).replace(/\{opponent\}/g, s.setup.rival.name);