61 lines
3.3 KiB
TypeScript
61 lines
3.3 KiB
TypeScript
import { addMomentum, addRivalMomentum } from "./mechanics.ts";
|
||
import { pickWeighted, townLead, type RunState, type TownState } from "./state.ts";
|
||
|
||
/**
|
||
* 本週戰場(docs/26,參考《冷戰熱鬥》的關鍵國家、《1960》的搖擺州):每週開始標出一個選情接近的大區,
|
||
* 對手一定在那裡全力搶攻(掃街、到你的票倉拉票 ×BATTLE_PUSH;造勢本來就大,照原本的)。週末對手出手之後,這週在那區淨增的票誰多誰贏:
|
||
* 你贏了聲勢 +1、下週多抽 1 張;對手贏了對手聲勢 +1。讓地圖上每週都有一塊要搶的地,不只是比總票數。
|
||
*/
|
||
|
||
/** 對手在戰場的行動倍率 */
|
||
export const BATTLE_PUSH = 1.5;
|
||
/** 贏下戰場:聲勢 */
|
||
export const BATTLE_MOMENTUM = 1;
|
||
/** 你贏下戰場:下週多抽幾張 */
|
||
export const BATTLE_DRAW = 1;
|
||
/** 只挑選舉人數至少佔全區這個比例的區(太小的區搶了也沒感覺) */
|
||
const BATTLE_MIN_SHARE = 0.03;
|
||
/** 差距在這個範圍內算「選情接近」,越接近越容易被挑中 */
|
||
const BATTLE_RANGE = 0.12;
|
||
|
||
export const battleTown = (s: RunState): TownState | null => (s.battle && s.battle.week === s.week ? (s.towns.find((t) => t.code === s.battle!.town) ?? null) : null);
|
||
|
||
/** 這週開始:挑戰場(不連續兩週同一區),記下雙方在那區的票。新手教學沒有 */
|
||
export function startBattle(s: RunState): void {
|
||
// 只有一區的選區:整張地圖就是同一區,沒有要搶的戰場(docs/26)
|
||
if (s.options.tutorial || s.towns.length < 2) {
|
||
s.battle = null;
|
||
return;
|
||
}
|
||
const voters = s.towns.reduce((a, t) => a + t.voters, 0);
|
||
const prev = s.battle?.town;
|
||
const big = s.towns.filter((t) => t.voters >= voters * BATTLE_MIN_SHARE && t.code !== prev);
|
||
const pool = big.length ? big : s.towns.filter((t) => t.code !== prev);
|
||
const t = pickWeighted(s, pool.length ? pool : s.towns, (x) => x.voters * Math.max(0.02, 1 - Math.abs(townLead(x)) / BATTLE_RANGE) ** 3);
|
||
s.battle = t ? { town: t.code, week: s.week, startYou: t.you, startRival: t.rival } : null;
|
||
}
|
||
|
||
/** 這週到目前為止雙方在戰場淨增的票 */
|
||
export function battleProgress(s: RunState): { town: TownState; you: number; rival: number } | null {
|
||
const t = battleTown(s);
|
||
if (!t || !s.battle) return null;
|
||
return { town: t, you: t.you - s.battle.startYou, rival: t.rival - s.battle.startRival };
|
||
}
|
||
|
||
/** 週末(對手出手之後、聲勢消退之後):淨增多的一方贏下戰場 */
|
||
export function settleBattle(s: RunState): void {
|
||
const p = battleProgress(s);
|
||
if (!p) return;
|
||
const fmt = (n: number) => Math.round(n).toLocaleString("en-US");
|
||
const score = `你淨增 ${fmt(p.you)}、${s.setup.rival.name} ${fmt(p.rival)}`;
|
||
if (p.you > p.rival) {
|
||
addMomentum(s, BATTLE_MOMENTUM);
|
||
s.battleDraw = BATTLE_DRAW;
|
||
s.stats.battles = (s.stats.battles ?? 0) + 1;
|
||
s.log.push({ side: "you", text: `本週戰場${p.town.name}:你贏了(${score})!聲勢 +${BATTLE_MOMENTUM},下週多抽 ${BATTLE_DRAW} 張`, town: p.town.code, votes: 0 });
|
||
} else if (p.rival > p.you) {
|
||
addRivalMomentum(s, BATTLE_MOMENTUM);
|
||
s.log.push({ side: "rival", text: `本週戰場${p.town.name}:被${s.setup.rival.name}拿下(${score}),對手聲勢 +${BATTLE_MOMENTUM}`, town: p.town.code, votes: 0 });
|
||
}
|
||
}
|