@chrischall/warapp-mcp 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +3 -3
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +28 -17
- package/dist/analysis/accept-surrender.js +56 -0
- package/dist/analysis/calibrate.js +239 -0
- package/dist/analysis/finished-game.js +136 -0
- package/dist/analysis/game-lessons.js +217 -0
- package/dist/analysis/local-deploy.js +37 -17
- package/dist/analysis/opponent-profile.js +6 -5
- package/dist/analysis/opponents.js +10 -2
- package/dist/analysis/outlook.js +63 -1
- package/dist/analysis/plan/shared.js +194 -7
- package/dist/analysis/plan/threats.js +103 -29
- package/dist/analysis/planner.js +10 -5
- package/dist/analysis/stress.js +14 -7
- package/dist/analysis/whereabouts.js +3 -0
- package/dist/bundle.js +12392 -9245
- package/dist/calibration-source.js +75 -0
- package/dist/chat.js +73 -0
- package/dist/client.js +85 -7
- package/dist/finished-store.js +357 -0
- package/dist/index.js +1 -1
- package/dist/lessons-store.js +356 -0
- package/dist/live.js +9 -0
- package/dist/map-details.js +127 -0
- package/dist/map-stats.js +14 -6
- package/dist/map-store.js +44 -6
- package/dist/my-games.js +44 -6
- package/dist/normalize.js +34 -2
- package/dist/notifications.js +143 -0
- package/dist/outlook-log.js +28 -1
- package/dist/player-record-store.js +2 -0
- package/dist/profile-store.js +61 -11
- package/dist/protocol/methods.js +86 -2
- package/dist/replay.js +16 -4
- package/dist/stats.js +26 -4
- package/dist/strategy/notes.js +64 -9
- package/dist/surrender-pending.js +126 -0
- package/dist/tools/accept-surrender.js +311 -0
- package/dist/tools/chat-inbox.js +17 -5
- package/dist/tools/chat-read-mark.js +179 -0
- package/dist/tools/chat.js +50 -8
- package/dist/tools/check-plan.js +1 -0
- package/dist/tools/commander-check.js +1 -0
- package/dist/tools/end-of-game.js +384 -0
- package/dist/tools/game.js +31 -6
- package/dist/tools/index.js +15 -5
- package/dist/tools/live-context.js +1 -1
- package/dist/tools/map-details.js +88 -0
- package/dist/tools/map-stats.js +15 -2
- package/dist/tools/map.js +10 -2
- package/dist/tools/my-games.js +77 -7
- package/dist/tools/notifications.js +484 -0
- package/dist/tools/opponents.js +14 -2
- package/dist/tools/past-games.js +94 -0
- package/dist/tools/picks.js +15 -2
- package/dist/tools/profile-opponents.js +1 -1
- package/dist/tools/profile-reads.js +3 -3
- package/dist/tools/push.js +72 -12
- package/dist/tools/strategy.js +65 -8
- package/dist/tools/stress-opponents.js +1 -1
- package/dist/tools/surrender-pending.js +74 -0
- package/dist/tools/surrender.js +10 -3
- package/dist/tools/team.js +1 -5
- package/dist/tools/turn.js +79 -15
- package/dist/tools/where.js +42 -17
- package/dist/tools/write-kit.js +6 -0
- package/dist/trend.js +145 -20
- package/dist/version.js +1 -1
- package/manifest.json +22 -2
- package/mint.yaml +14 -9
- package/package.json +1 -1
- package/server.json +2 -2
- package/skills/warapp-mcp/SKILL.md +141 -14
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { boardIncome, incomeRules } from './player.js';
|
|
2
|
+
const BY_STATE = {
|
|
3
|
+
Won: 'won',
|
|
4
|
+
Eliminated: 'eliminated',
|
|
5
|
+
SurrenderAccepted: 'surrendered',
|
|
6
|
+
Booted: 'booted',
|
|
7
|
+
EndedByVote: 'ended-by-vote',
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* My outcome from the final player states: my own state when it names an end, else `lost` when
|
|
11
|
+
* another player won, else `unknown` (also when I am not among the players).
|
|
12
|
+
*/
|
|
13
|
+
export function outcomeOf(players, me) {
|
|
14
|
+
const mine = players.find((p) => p.id === me);
|
|
15
|
+
if (!mine)
|
|
16
|
+
return 'unknown';
|
|
17
|
+
const named = BY_STATE[mine.state ?? ''];
|
|
18
|
+
if (named)
|
|
19
|
+
return named;
|
|
20
|
+
return players.some((p) => p.id !== me && p.state === 'Won') ? 'lost' : 'unknown';
|
|
21
|
+
}
|
|
22
|
+
const ownerOf = (t) => t?.owner.kind === 'player' ? t.owner.playerId : undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Each of `players` on `board`: its income by THE income rule (`boardIncome`, src/analysis/player.ts:
|
|
25
|
+
* base or per-territory income, plus every full bonus; the map's values with the game's overrides
|
|
26
|
+
* applied by the caller), the bonuses it holds, its territories and armies. A player with no land
|
|
27
|
+
* earns 0 (it is out). Pure.
|
|
28
|
+
*/
|
|
29
|
+
export function turnIncomes(g, board, players, settings) {
|
|
30
|
+
const held = new Map(players.map((p) => [p, new Set()]));
|
|
31
|
+
const armies = new Map(players.map((p) => [p, 0]));
|
|
32
|
+
for (const t of board.territories) {
|
|
33
|
+
const o = ownerOf(t);
|
|
34
|
+
if (o === undefined || !held.has(o) || !g.territoryById.has(t.territoryId))
|
|
35
|
+
continue;
|
|
36
|
+
held.get(o).add(t.territoryId);
|
|
37
|
+
const a = armies.get(o);
|
|
38
|
+
armies.set(o, a === null || t.armies === null ? null : a + t.armies);
|
|
39
|
+
}
|
|
40
|
+
const rules = incomeRules(settings);
|
|
41
|
+
const out = new Map();
|
|
42
|
+
for (const p of players) {
|
|
43
|
+
const land = held.get(p);
|
|
44
|
+
if (!land.size) {
|
|
45
|
+
out.set(p, { income: 0, territories: 0, armies: 0, bonuses: [] });
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const inc = boardIncome(g, land, rules);
|
|
49
|
+
out.set(p, {
|
|
50
|
+
income: inc.total,
|
|
51
|
+
territories: land.size,
|
|
52
|
+
armies: armies.get(p),
|
|
53
|
+
bonuses: inc.bonuses.map((b) => b.bonusId).sort((a, b) => a - b),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
const ascending = (boards) => [...boards].sort((a, b) => a.turn - b.turn);
|
|
59
|
+
const landOf = (board, p) => board.territories
|
|
60
|
+
.filter((t) => ownerOf(t) === p)
|
|
61
|
+
.map((t) => t.territoryId)
|
|
62
|
+
.sort((a, b) => a - b);
|
|
63
|
+
/** Each player's territories on the first board where it holds any (its start). Pure. */
|
|
64
|
+
export function startsOf(boards, players) {
|
|
65
|
+
const out = new Map();
|
|
66
|
+
for (const { board } of ascending(boards))
|
|
67
|
+
for (const p of players) {
|
|
68
|
+
if (out.has(p))
|
|
69
|
+
continue;
|
|
70
|
+
const land = landOf(board, p);
|
|
71
|
+
if (land.length)
|
|
72
|
+
out.set(p, land);
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
/** The first board, after one where `player` held land, on which it holds none; `null` when never. Pure. */
|
|
77
|
+
export function outTurnOf(boards, player) {
|
|
78
|
+
let held = false;
|
|
79
|
+
for (const { turn, board } of ascending(boards)) {
|
|
80
|
+
const has = board.territories.some((t) => ownerOf(t) === player);
|
|
81
|
+
if (has)
|
|
82
|
+
held = true;
|
|
83
|
+
else if (held)
|
|
84
|
+
return turn;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const side = (t) => ({
|
|
89
|
+
owner: !t ? 'unknown' : t.owner.kind === 'player' ? t.owner.playerId : t.owner.kind === 'neutral' ? 'neutral' : 'unknown',
|
|
90
|
+
armies: t?.armies ?? null,
|
|
91
|
+
});
|
|
92
|
+
/**
|
|
93
|
+
* Every target my cached orders attacked and did not take: for orders of turn T (`orders`, by the
|
|
94
|
+
* turn played), the history boards T − 1 and T; a move into a territory not mine on board T − 1 that
|
|
95
|
+
* is still not mine on board T. Moves into one target in one turn are one entry. Pure.
|
|
96
|
+
*/
|
|
97
|
+
export function failedAttacks(boards, orders, me) {
|
|
98
|
+
const byTurn = new Map(boards.map((b) => [b.turn, b.board]));
|
|
99
|
+
const out = [];
|
|
100
|
+
for (const turn of [...orders.keys()].sort((a, b) => a - b)) {
|
|
101
|
+
const was = byTurn.get(turn - 1);
|
|
102
|
+
const now = byTurn.get(turn);
|
|
103
|
+
if (!was || !now)
|
|
104
|
+
continue;
|
|
105
|
+
const before = new Map(was.territories.map((t) => [t.territoryId, t]));
|
|
106
|
+
const after = new Map(now.territories.map((t) => [t.territoryId, t]));
|
|
107
|
+
const hits = new Map();
|
|
108
|
+
for (const m of orders.get(turn).moves) {
|
|
109
|
+
if (ownerOf(before.get(m.to)) === me || ownerOf(after.get(m.to)) === me)
|
|
110
|
+
continue;
|
|
111
|
+
const h = hits.get(m.to) ?? { from: new Set(), sent: 0 };
|
|
112
|
+
h.from.add(m.from);
|
|
113
|
+
h.sent += Math.max(0, m.armies);
|
|
114
|
+
hits.set(m.to, h);
|
|
115
|
+
}
|
|
116
|
+
for (const [to, h] of [...hits].sort(([a], [b]) => a - b))
|
|
117
|
+
out.push({ turn, to, from: [...h.from].sort((a, b) => a - b), sent: h.sent, before: side(before.get(to)), after: side(after.get(to)) });
|
|
118
|
+
}
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
/** The whole capture of a finished game from its history boards and my orders. Pure. */
|
|
122
|
+
export function captureFinished(input) {
|
|
123
|
+
const ids = input.players.map((p) => p.id).sort((a, b) => a - b);
|
|
124
|
+
const boards = ascending(input.boards);
|
|
125
|
+
return {
|
|
126
|
+
outcome: {
|
|
127
|
+
result: outcomeOf(input.players, input.me),
|
|
128
|
+
turn: input.numberOfTurns ?? null,
|
|
129
|
+
outTurn: outTurnOf(boards, input.me),
|
|
130
|
+
winners: input.players.filter((p) => p.state === 'Won').map((p) => p.id).sort((a, b) => a - b),
|
|
131
|
+
},
|
|
132
|
+
starts: startsOf(boards, ids),
|
|
133
|
+
turns: input.graph ? boards.map(({ turn, board }) => ({ afterTurn: turn, players: turnIncomes(input.graph, board, ids, input.settings) })) : [],
|
|
134
|
+
failedAttacks: failedAttacks(boards, input.orders, input.me),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { turnIncomes } from './finished-game.js';
|
|
2
|
+
import { howLost } from './how-lost.js';
|
|
3
|
+
import { habitsOf, observeTurn } from './opponent-profile.js';
|
|
4
|
+
import { trackOpponents } from './opponents.js';
|
|
5
|
+
const ownsLand = (b, p) => b.territories.some((t) => t.owner.kind === 'player' && t.owner.playerId === p);
|
|
6
|
+
const ascending = (xs) => [...xs].sort((a, b) => a.turn - b.turn);
|
|
7
|
+
/**
|
|
8
|
+
* Each opponent's turns as the history shows them (part C of docs/PLAN-PROFILES.md): every pair of
|
|
9
|
+
* consecutive history boards N, N + 1 is turn N + 1, observed with my orders of that turn when cached
|
|
10
|
+
* (`observeTurn`, the same rules as the live boards; a history board shows everything, so nothing is
|
|
11
|
+
* left out for the fog). Only while I held land on the earlier board: after I am out nothing is
|
|
12
|
+
* between me and anyone. Opponents ascending, turns oldest first; an opponent never observed is left
|
|
13
|
+
* out. Pure.
|
|
14
|
+
*/
|
|
15
|
+
export function observeHistory(input) {
|
|
16
|
+
const boards = ascending(input.boards);
|
|
17
|
+
const out = new Map();
|
|
18
|
+
for (let i = 0; i + 1 < boards.length; i++) {
|
|
19
|
+
const was = boards[i];
|
|
20
|
+
const now = boards[i + 1];
|
|
21
|
+
if (now.turn !== was.turn + 1 || !ownsLand(was.board, input.me))
|
|
22
|
+
continue;
|
|
23
|
+
const turn = was.turn + 1;
|
|
24
|
+
const seen = observeTurn({
|
|
25
|
+
graph: input.graph,
|
|
26
|
+
me: input.me,
|
|
27
|
+
previous: was.board,
|
|
28
|
+
current: now.board,
|
|
29
|
+
orders: input.orders.get(turn),
|
|
30
|
+
settings: input.settings,
|
|
31
|
+
opponents: input.opponents,
|
|
32
|
+
...(input.cards !== undefined ? { cards: input.cards } : {}),
|
|
33
|
+
});
|
|
34
|
+
for (const [o, primitives] of seen) {
|
|
35
|
+
const list = out.get(o) ?? [];
|
|
36
|
+
list.push({ turn, primitives });
|
|
37
|
+
out.set(o, list);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return new Map([...out].sort(([a], [b]) => a - b));
|
|
41
|
+
}
|
|
42
|
+
/** {@link observeHistory} added up into habits per opponent (`habitsOf`). Pure. */
|
|
43
|
+
export function historyHabits(input) {
|
|
44
|
+
return new Map([...observeHistory(input)].map(([o, turns]) => [o, habitsOf(turns)]));
|
|
45
|
+
}
|
|
46
|
+
/** `player`'s curve from the capture's turns (`FinishedCapture.turns`). Pure. */
|
|
47
|
+
export function incomeCurve(turns, player) {
|
|
48
|
+
return turns
|
|
49
|
+
.flatMap((t) => {
|
|
50
|
+
const p = t.players.get(player);
|
|
51
|
+
return p ? [{ afterTurn: t.afterTurn, income: p.income }] : [];
|
|
52
|
+
})
|
|
53
|
+
.sort((a, b) => a.afterTurn - b.afterTurn);
|
|
54
|
+
}
|
|
55
|
+
/** The turns of no growth that make an income flat (#312: "slope ≈ 0 for 3+ turns"). */
|
|
56
|
+
export const FLAT_TURNS = 3;
|
|
57
|
+
/** What counts as no growth over those turns: at most this share of the income (and at least 1 army). */
|
|
58
|
+
export const FLAT_TOLERANCE = 0.1;
|
|
59
|
+
/**
|
|
60
|
+
* The first board (by completed turns, the opening's board 0 left out) from which my income stayed
|
|
61
|
+
* within {@link FLAT_TOLERANCE} of it (at least 1) on each of the next {@link FLAT_TURNS} consecutive
|
|
62
|
+
* boards, while I held land; `null` when it never did. Pure.
|
|
63
|
+
*/
|
|
64
|
+
export function flatTurn(curve) {
|
|
65
|
+
const at = new Map(curve.map((c) => [c.afterTurn, c.income]));
|
|
66
|
+
for (const { afterTurn, income } of [...curve].sort((a, b) => a.afterTurn - b.afterTurn)) {
|
|
67
|
+
if (afterTurn < 1 || income <= 0)
|
|
68
|
+
continue;
|
|
69
|
+
const tol = Math.max(1, FLAT_TOLERANCE * income);
|
|
70
|
+
let flat = true;
|
|
71
|
+
for (let k = 1; k <= FLAT_TURNS && flat; k++) {
|
|
72
|
+
const next = at.get(afterTurn + k);
|
|
73
|
+
flat = next !== undefined && next > 0 && Math.abs(next - income) <= tol;
|
|
74
|
+
}
|
|
75
|
+
if (flat)
|
|
76
|
+
return afterTurn;
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
/** The bonuses with a member territory in `start`, ascending. Pure. */
|
|
81
|
+
export function startRegion(g, start) {
|
|
82
|
+
const set = new Set(start);
|
|
83
|
+
const bonuses = [...g.bonusById.values()].filter((b) => b.territoryIds.some((t) => set.has(t))).map((b) => b.id).sort((a, b) => a - b);
|
|
84
|
+
return { territories: [...start].sort((a, b) => a - b), bonuses };
|
|
85
|
+
}
|
|
86
|
+
/** Income curve points kept per game (the opening and the middle game: the pace). */
|
|
87
|
+
export const MAX_CURVE_POINTS = 60;
|
|
88
|
+
/** The boards the pace is read on: the income after these completed turns. */
|
|
89
|
+
export const PACE_TURNS = [4, 8];
|
|
90
|
+
const incomeAfter = (curve, n) => curve.find((c) => c.afterTurn === n)?.income ?? null;
|
|
91
|
+
/**
|
|
92
|
+
* My start region and income curve (index = completed turns, `null` where no board read, at most
|
|
93
|
+
* {@link MAX_CURVE_POINTS}), and each winner's start region and income after turns 4 and 8 (the pace
|
|
94
|
+
* to beat). Pure.
|
|
95
|
+
*/
|
|
96
|
+
export function mapFacts(g, me, winners, starts, turns) {
|
|
97
|
+
const mine = incomeCurve(turns, me);
|
|
98
|
+
const last = Math.min(MAX_CURVE_POINTS, mine.length ? mine.at(-1).afterTurn + 1 : 0);
|
|
99
|
+
const income = Array.from({ length: last }, (_, i) => incomeAfter(mine, i));
|
|
100
|
+
const pace = (curve) => ({ income4: incomeAfter(curve, PACE_TURNS[0]), income8: incomeAfter(curve, PACE_TURNS[1]) });
|
|
101
|
+
return {
|
|
102
|
+
me: { ...startRegion(g, starts.get(me) ?? []), income, ...pace(mine) },
|
|
103
|
+
winners: [...winners].sort((a, b) => a - b).map((w) => ({ ...startRegion(g, starts.get(w) ?? []), ...pace(incomeCurve(turns, w)) })),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* How I lost, from the history (full boards): eliminated → `howLost` on the history boards (numbered
|
|
108
|
+
* by the turn played on them; a history board carries no special units, so a game with commanders
|
|
109
|
+
* reads `unknown`); surrendered or booted → that, on my out turn; lost while still holding land (a team
|
|
110
|
+
* game my side lost) → `still-holding`. `null` for a game I did not lose. Pure.
|
|
111
|
+
*/
|
|
112
|
+
export function lessonLoss(input) {
|
|
113
|
+
const { result } = input;
|
|
114
|
+
if (result === 'surrendered' || result === 'booted')
|
|
115
|
+
return { cause: result, turn: input.outTurn };
|
|
116
|
+
if (result === 'lost')
|
|
117
|
+
return { cause: 'still-holding', turn: input.numberOfTurns };
|
|
118
|
+
if (result !== 'eliminated')
|
|
119
|
+
return null;
|
|
120
|
+
const s = input.settings;
|
|
121
|
+
const loss = howLost({
|
|
122
|
+
me: input.me,
|
|
123
|
+
boards: input.boards.map((b) => ({ turn: b.turn + 1, board: b.board })),
|
|
124
|
+
orders: input.orders,
|
|
125
|
+
adjacency: input.graph.neighbors,
|
|
126
|
+
rates: {
|
|
127
|
+
...(s?.offensiveKillRate !== undefined ? { offensiveKillRate: s.offensiveKillRate } : {}),
|
|
128
|
+
...(s?.defensiveKillRate !== undefined ? { defensiveKillRate: s.defensiveKillRate } : {}),
|
|
129
|
+
...(s?.luckModifier !== undefined ? { luck: s.luckModifier } : {}),
|
|
130
|
+
...(s?.roundingMode !== undefined ? { rounding: s.roundingMode } : {}),
|
|
131
|
+
},
|
|
132
|
+
...(input.numberOfTurns !== null ? { turnsCompleted: input.numberOfTurns } : {}),
|
|
133
|
+
...(s?.commanders !== undefined ? { commanders: s.commanders } : {}),
|
|
134
|
+
});
|
|
135
|
+
return loss ? { cause: loss.cause, turn: loss.turn } : { cause: 'unknown', turn: input.outTurn };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* For each turn T I cached a board for and the history has the board T was played on: every
|
|
139
|
+
* opponent with land there and a tracked income (`trackOpponents` over my cached boards up to T, as
|
|
140
|
+
* the plan and the track tool read them), the estimate beside the truth and the coverage. Pure.
|
|
141
|
+
*/
|
|
142
|
+
export function calibrationSamples(input) {
|
|
143
|
+
const truthBoard = new Map(input.history.map((b) => [b.turn, b.board]));
|
|
144
|
+
const cached = ascending(input.cached);
|
|
145
|
+
const scored = new Set(input.opponents);
|
|
146
|
+
const out = [];
|
|
147
|
+
for (const { turn } of cached) {
|
|
148
|
+
const truth = truthBoard.get(turn - 1);
|
|
149
|
+
if (!truth)
|
|
150
|
+
continue;
|
|
151
|
+
const upTo = cached.filter((b) => b.turn <= turn);
|
|
152
|
+
if (upTo.length < 2)
|
|
153
|
+
continue;
|
|
154
|
+
const tracks = trackOpponents(input.graph, upTo, input.me, input.plannerSettings, input.game);
|
|
155
|
+
const real = turnIncomes(input.graph, truth, input.opponents, input.incomeSettings);
|
|
156
|
+
for (const track of [...tracks].sort((a, b) => a.playerId - b.playerId)) {
|
|
157
|
+
if (!scored.has(track.playerId) || track.income === null)
|
|
158
|
+
continue;
|
|
159
|
+
const t = real.get(track.playerId);
|
|
160
|
+
if (!t || t.income <= 0)
|
|
161
|
+
continue;
|
|
162
|
+
out.push({ turn, opponent: track.playerId, estimate: track.income, truth: t.income, coverage: track.coverage?.share ?? null });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
/** The coverage buckets a sample is counted in. */
|
|
168
|
+
export const COVERAGE_BUCKETS = ['none', 'under-25', '25-50', '50-75', '75-up'];
|
|
169
|
+
export function coverageBucket(share) {
|
|
170
|
+
if (share === null)
|
|
171
|
+
return 'none';
|
|
172
|
+
if (share < 0.25)
|
|
173
|
+
return 'under-25';
|
|
174
|
+
if (share < 0.5)
|
|
175
|
+
return '25-50';
|
|
176
|
+
if (share < 0.75)
|
|
177
|
+
return '50-75';
|
|
178
|
+
return '75-up';
|
|
179
|
+
}
|
|
180
|
+
/** A ratio under this reads low; over {@link CALIBRATION_HIGH}, high. */
|
|
181
|
+
export const CALIBRATION_LOW = 0.5;
|
|
182
|
+
export const CALIBRATION_HIGH = 1.5;
|
|
183
|
+
const round = (x) => Math.round(x * 1e6) / 1e6;
|
|
184
|
+
/** The samples counted by coverage bucket (aggregates only: no turn, no player). Pure. */
|
|
185
|
+
export function aggregateCalibration(samples) {
|
|
186
|
+
const out = {};
|
|
187
|
+
for (const s of samples) {
|
|
188
|
+
const r = s.estimate / s.truth;
|
|
189
|
+
const b = coverageBucket(s.coverage);
|
|
190
|
+
const x = out[b] ?? { n: 0, sum: 0, sq: 0, logSum: 0, under: 0, over: 0 };
|
|
191
|
+
out[b] = {
|
|
192
|
+
n: x.n + 1,
|
|
193
|
+
sum: round(x.sum + r),
|
|
194
|
+
sq: round(x.sq + r * r),
|
|
195
|
+
logSum: round(x.logSum + Math.log(Math.max(r, 1e-6))),
|
|
196
|
+
under: x.under + (r < CALIBRATION_LOW ? 1 : 0),
|
|
197
|
+
over: x.over + (r > CALIBRATION_HIGH ? 1 : 0),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
/** Two calibrations added, `b` scaled (−1 takes it back out). Pure. */
|
|
203
|
+
export function addCalibration(a, b, scale = 1) {
|
|
204
|
+
const out = {};
|
|
205
|
+
for (const k of COVERAGE_BUCKETS) {
|
|
206
|
+
const x = a[k];
|
|
207
|
+
const y = b[k];
|
|
208
|
+
if (!x && !y)
|
|
209
|
+
continue;
|
|
210
|
+
const z = { n: 0, sum: 0, sq: 0, logSum: 0, under: 0, over: 0 };
|
|
211
|
+
for (const f of Object.keys(z))
|
|
212
|
+
z[f] = round((x?.[f] ?? 0) + scale * (y?.[f] ?? 0));
|
|
213
|
+
if (z.n > 0)
|
|
214
|
+
out[k] = z;
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
@@ -128,32 +128,52 @@ export function chargeDeploys(pools, deploys) {
|
|
|
128
128
|
return { anywhereUsed, used };
|
|
129
129
|
}
|
|
130
130
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* pools that contain one of `on`, never above the estimate. Only what it holds above its free
|
|
135
|
-
* floor — max(`floor`, its base income) — is taken as pooled: the free base always lands anywhere.
|
|
136
|
-
* `pools` are the values and members of the bonuses it holds; with none the whole estimate reaches.
|
|
131
|
+
* What an opponent may deploy anywhere under Local Deployment (#293): its free income — its base
|
|
132
|
+
* income (never below `floor`) plus the Reinforcement card armies its estimate counts. Bonus armies
|
|
133
|
+
* land only in their own bonus.
|
|
137
134
|
*/
|
|
138
|
-
export function
|
|
139
|
-
|
|
140
|
-
|
|
135
|
+
export function freeIncome(base, floor, card = 0) {
|
|
136
|
+
return Math.max(floor, base) + Math.max(0, card);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* An opponent's deploy under Local Deployment on `on` (#180, #293), as the planner bounds it (#128)
|
|
140
|
+
* and warapp_check_plan's stress test does: its free income ({@link freeIncome}) plus the pools of the
|
|
141
|
+
* bonuses it could hold ({@link couldHoldBonusPools}) that contain one of `on`, never above the
|
|
142
|
+
* estimate. A total read above that (a tracked figure, the fog prior) never lands on one border.
|
|
143
|
+
* With per-territory income (`perTerritory`) the income no pool can carry (per-territory income on
|
|
144
|
+
* territories the fog hides) may land anywhere too: what the estimate holds beyond every bonus it
|
|
145
|
+
* could hold together counts as free (with no pools, the whole estimate reaches). Without it, nothing
|
|
146
|
+
* beyond the free income and those pools does.
|
|
147
|
+
*/
|
|
148
|
+
export function enemyDeployReaching(estimate, free, pools, on, perTerritory) {
|
|
141
149
|
const pooled = pools.reduce((n, q) => n + q.value, 0);
|
|
142
150
|
const here = pools.reduce((n, q) => n + (on.some((t) => q.members.has(t)) ? q.value : 0), 0);
|
|
143
|
-
const
|
|
144
|
-
return Math.min(estimate,
|
|
151
|
+
const anywhere = perTerritory ? Math.max(Math.min(estimate, free), estimate - pooled) : free;
|
|
152
|
+
return Math.min(estimate, anywhere + here);
|
|
145
153
|
}
|
|
146
|
-
/**
|
|
147
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Each player's Local Deployment pools as far as the board can tell (#293): the bonuses (value above 0)
|
|
156
|
+
* it could hold — none of their territories shown with another owner (`ownerOf` gives a player id, `null`
|
|
157
|
+
* for an owner shown that is no player, `undefined` when the board hides the owner). For each of
|
|
158
|
+
* `players`, sorted by bonus id. A bonus it visibly holds in full is one of them; so is one the fog hides.
|
|
159
|
+
*/
|
|
160
|
+
export function couldHoldBonusPools(g, players, ownerOf) {
|
|
148
161
|
const out = new Map();
|
|
149
|
-
for (const
|
|
162
|
+
for (const p of players)
|
|
163
|
+
out.set(p, []);
|
|
164
|
+
for (const b of [...g.map.bonuses].sort((x, y) => x.id - y.id)) {
|
|
150
165
|
if (b.value <= 0)
|
|
151
166
|
continue;
|
|
152
167
|
const ts = bonusTerritories(g, b.id);
|
|
153
|
-
|
|
154
|
-
if (p === undefined || !ts.every((t) => ownerOf(t) === p))
|
|
168
|
+
if (!ts.length)
|
|
155
169
|
continue;
|
|
156
|
-
|
|
170
|
+
const members = new Set(ts);
|
|
171
|
+
for (const p of players)
|
|
172
|
+
if (ts.every((t) => {
|
|
173
|
+
const o = ownerOf(t);
|
|
174
|
+
return o === undefined || o === p;
|
|
175
|
+
}))
|
|
176
|
+
out.get(p).push({ bonusId: b.id, value: b.value, members });
|
|
157
177
|
}
|
|
158
178
|
return out;
|
|
159
179
|
}
|
|
@@ -453,9 +453,10 @@ export function estimate(events, n, p0, k = PROFILE_PRIOR_WEIGHT) {
|
|
|
453
453
|
/**
|
|
454
454
|
* `(p0 × K + e) ÷ (K + n)` with `e` and `n` this game's and the past's together. The basis is the
|
|
455
455
|
* largest weight of this game's `n`, the past's `n` and the prior's K; the prior wins a tie, then
|
|
456
|
-
* this game.
|
|
456
|
+
* this game. A past basis reads `history` when `fromHistory` (the part of the past folded from
|
|
457
|
+
* finished games' histories) holds at least half of the past's `n` (#312 E3); the value never changes.
|
|
457
458
|
*/
|
|
458
|
-
function blend(game, past, p0, prior, k = PROFILE_PRIOR_WEIGHT) {
|
|
459
|
+
function blend(game, past, p0, prior, k = PROFILE_PRIOR_WEIGHT, fromHistory) {
|
|
459
460
|
const e = game.e + past.e;
|
|
460
461
|
const n = game.n + past.n;
|
|
461
462
|
let basis = prior;
|
|
@@ -465,7 +466,7 @@ function blend(game, past, p0, prior, k = PROFILE_PRIOR_WEIGHT) {
|
|
|
465
466
|
weight = game.n;
|
|
466
467
|
}
|
|
467
468
|
if (past.n > weight)
|
|
468
|
-
basis = 'past-games';
|
|
469
|
+
basis = fromHistory && fromHistory.n > 0 && 2 * fromHistory.n >= past.n ? 'history' : 'past-games';
|
|
469
470
|
return { value: round2((p0 * k + e) / (k + n)), n: round2(n), confidence: round2(n / (n + k)), basis };
|
|
470
471
|
}
|
|
471
472
|
/** Every habit's estimate from a game's counts, pooled with the past and shrunk toward the prior (B1). */
|
|
@@ -482,7 +483,7 @@ export function estimateHabits(h, priors = {}) {
|
|
|
482
483
|
};
|
|
483
484
|
const rate = (name) => {
|
|
484
485
|
const p = prior(name, (x) => x[name]);
|
|
485
|
-
return blend(h[name], past[name], p.p0, p.from);
|
|
486
|
+
return blend(h[name], past[name], p.p0, p.from, PROFILE_PRIOR_WEIGHT, priors.pastHistory?.[name]);
|
|
486
487
|
};
|
|
487
488
|
const size = (x) => ({ e: x.stacking.sum, n: x.stacking.n });
|
|
488
489
|
const s = prior('stacking', size);
|
|
@@ -490,7 +491,7 @@ export function estimateHabits(h, priors = {}) {
|
|
|
490
491
|
reinforce: rate('reinforce'),
|
|
491
492
|
attack: rate('attack'),
|
|
492
493
|
firstStrike: rate('firstStrike'),
|
|
493
|
-
stacking: blend(size(h), size(past), s.p0, s.from),
|
|
494
|
+
stacking: blend(size(h), size(past), s.p0, s.from, PROFILE_PRIOR_WEIGHT, priors.pastHistory ? size(priors.pastHistory) : undefined),
|
|
494
495
|
retaliate: rate('retaliate'),
|
|
495
496
|
retake: rate('retake'),
|
|
496
497
|
};
|
|
@@ -4,7 +4,7 @@ import { neutralizeSeats, staticSeats } from './static-seats.js';
|
|
|
4
4
|
import { bonusTerritories } from './bonuses.js';
|
|
5
5
|
import { DEFAULT_DEFENSIVE_KILL_RATE, DEFAULT_OFFENSIVE_KILL_RATE, straightKills } from './combat.js';
|
|
6
6
|
import { bfsDistances, neighborsOf } from './graph.js';
|
|
7
|
-
import { bombPossibleOn, enemyDeployEstimates, failedAttackKills, holdLosses, observeSigned, ownerShown, repelledFloor, visibleState, } from './planner.js';
|
|
7
|
+
import { bombPossibleOn, enemyDeployEstimates, failedAttackKills, frontierCoverage, holdLosses, localFreeOn, observeSigned, ownerShown, repelledFloor, coverageText, LOW_COVERAGE, LOW_COVERAGE_FACTOR, TRACKED_MIN_TURNS, trackCoverage, visibleState, } from './planner.js';
|
|
8
8
|
import { boardIncome, DEFAULT_BASE_INCOME } from './player.js';
|
|
9
9
|
/** #164: a stack is large when its armies are at least this many turns of its visible income. */
|
|
10
10
|
const LOST_STACK_INCOMES = 2;
|
|
@@ -832,7 +832,7 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
|
|
|
832
832
|
// about its deploy (every stack would read as a reveal, or there is no estimate at all):
|
|
833
833
|
// skip it with a note rather than observe 0 or throw (#83).
|
|
834
834
|
const e = seesPlayer(before, g, playerId)
|
|
835
|
-
? enemyDeployEstimates(g, after, me, settings, before).find((x) => x.playerId === playerId)
|
|
835
|
+
? enemyDeployEstimates(g, after, me, settings, before, undefined, undefined, { turn: to }).find((x) => x.playerId === playerId)
|
|
836
836
|
: undefined;
|
|
837
837
|
if (!e?.observation) {
|
|
838
838
|
unseen.push(seesPlayer(before, g, playerId) ? to : from);
|
|
@@ -857,6 +857,8 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
|
|
|
857
857
|
guard,
|
|
858
858
|
most: e.observedCap,
|
|
859
859
|
...(expected !== null ? { expected: Math.max(expected, carriedSoFar) } : {}),
|
|
860
|
+
// #293: the same Local Deployment reading the board-only observation used.
|
|
861
|
+
...(settings.localDeployments ? { localFree: localFreeOn(e.base, DEFAULT_BASE_INCOME, settings.opponentReinforcement, from) } : {}),
|
|
860
862
|
};
|
|
861
863
|
const { signed, ...o } = expected !== null && carriedSoFar > expected
|
|
862
864
|
? observeSigned(g, after, before, playerId, settings, context)
|
|
@@ -874,6 +876,7 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
|
|
|
874
876
|
observed.push({
|
|
875
877
|
from,
|
|
876
878
|
to,
|
|
879
|
+
coverage: frontierCoverage(g, after, before, playerId),
|
|
877
880
|
observed: seenObserved,
|
|
878
881
|
raw,
|
|
879
882
|
...(context.expected !== undefined ? { expected: context.expected } : {}),
|
|
@@ -1216,6 +1219,10 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
|
|
|
1216
1219
|
lostStacks.sort((a, b) => b.armies - a.armies || a.territoryId - b.territoryId).splice(MAX_LOST_STACKS);
|
|
1217
1220
|
for (const l of lostStacks)
|
|
1218
1221
|
notes.push(`A stack of ${l.armies} was last seen on territory ${l.territoryId} on turn ${l.turn} and has been out of view since (stacks of it that came into view since, within its reach, are taken as it and offset it): the planner counts it at each border it can reach by now, for ${LOST_STACK_TURNS} turns after it was seen.`);
|
|
1222
|
+
// #294: what its income was read from.
|
|
1223
|
+
const covered = estimate ? trackCoverage(incomeWindow) : null;
|
|
1224
|
+
if (covered && covered.share < 1)
|
|
1225
|
+
notes.push(`Income read from ${coverageText(covered)}: the rest of where it can deploy was out of view, so the figure is a lower bound${covered.share < LOW_COVERAGE && income > visibleIncome && observed.length >= TRACKED_MIN_TURNS ? `, and the planner keeps at least ${LOW_COVERAGE_FACTOR} × it` : ''}.`);
|
|
1219
1226
|
notes.push(...warnings);
|
|
1220
1227
|
return {
|
|
1221
1228
|
playerId,
|
|
@@ -1227,6 +1234,7 @@ export function trackOpponents(g, boards, me, settings = {}, game) {
|
|
|
1227
1234
|
visibleIncome,
|
|
1228
1235
|
income,
|
|
1229
1236
|
incomeTurns: incomeWindow.map((x) => x.from),
|
|
1237
|
+
coverage: estimate ? trackCoverage(incomeWindow) : null,
|
|
1230
1238
|
observedRange: estimate?.observedRange ?? null,
|
|
1231
1239
|
incomeMethod: estimate?.method ?? null,
|
|
1232
1240
|
incomeLowered: carried ? carried.set - carried.income : 0,
|
package/dist/analysis/outlook.js
CHANGED
|
@@ -78,6 +78,10 @@
|
|
|
78
78
|
// then `state` is null and the basis says what each turn read. #228: given the plan's profile
|
|
79
79
|
// `standing`, an `even` or `stalemate` latest turn beside `ahead` / `behind` says why the two differ
|
|
80
80
|
// (the standing compares incomes; this outlook needs a decided ratio). The state never reads it.
|
|
81
|
+
// #304: beside the state, each turn reports what it rests on (`evidence`): the two incomes compared
|
|
82
|
+
// and how much of each opponent's frontier its income was read from (#294's coverage); a share under
|
|
83
|
+
// LOW_COVERAGE, or none measured, under fog is thin evidence, and the basis says "even" is then not safe.
|
|
84
|
+
// Reporting only: the state never reads it.
|
|
81
85
|
//
|
|
82
86
|
// OUTLOOK_RATIO (2.4×) is borrowed from a published bot's "lost" rule and has never been measured
|
|
83
87
|
// for war.app: the outlook is a basis for a decision, not a verdict. The planner acts on it in one
|
|
@@ -94,6 +98,7 @@ import { boardIncome, DEFAULT_BASE_INCOME, territoryIncome } from './player.js';
|
|
|
94
98
|
import { BEHIND_RATIO, STRENGTH_INCOME_TURNS } from './profile.js';
|
|
95
99
|
import { FORECAST_TURNS, forecastBorder, mainFront } from './exchange.js';
|
|
96
100
|
import { specialUnitStrength } from './combat.js';
|
|
101
|
+
import { coverageText, LOW_COVERAGE } from './plan/shared.js';
|
|
97
102
|
/** Turns a state must hold before it is reported. */
|
|
98
103
|
export const OUTLOOK_TURNS = 3;
|
|
99
104
|
/** Winning at this strength ratio or more, losing at its inverse or less (borrowed, unmeasured). */
|
|
@@ -283,6 +288,7 @@ export function gameOutlook(input) {
|
|
|
283
288
|
income: tr.income === null ? null : Math.max(tr.visibleIncome, tr.income),
|
|
284
289
|
visibleIncome: tr.visibleIncome,
|
|
285
290
|
observed: tr.turns.map((o) => ({ to: o.to, observed: o.observed })),
|
|
291
|
+
coverage: tr.coverage ?? null,
|
|
286
292
|
},
|
|
287
293
|
])));
|
|
288
294
|
const trackedOf = (playerId) => trackedAll().get(playerId);
|
|
@@ -531,6 +537,36 @@ export function gameOutlook(input) {
|
|
|
531
537
|
const groundDown = ownOutcome === 'ground-down';
|
|
532
538
|
const incomeBehind = lowLikely && mostTracked() > 0 && myNow <= BEHIND_RATIO * mostTracked();
|
|
533
539
|
const lostOnEstimate = lowLikely && (groundDown || incomeBehind);
|
|
540
|
+
// #304: what the state rests on — the two incomes compared and each opponent's frontier read.
|
|
541
|
+
// Reporting only: nothing above reads it.
|
|
542
|
+
const evidenceRows = rows.map((r) => {
|
|
543
|
+
const tr = trackedOf(r.playerId);
|
|
544
|
+
const tracked = tr?.income ?? null;
|
|
545
|
+
const cov = tr?.coverage ?? null;
|
|
546
|
+
return {
|
|
547
|
+
playerId: r.playerId,
|
|
548
|
+
income: tracked ?? r.seen.income,
|
|
549
|
+
source: tracked !== null ? 'tracked' : 'visible',
|
|
550
|
+
coverage: cov ? round2(cov.share) : null,
|
|
551
|
+
thin: !open && (cov === null || cov.share < LOW_COVERAGE),
|
|
552
|
+
...(cov ? { read: coverageText(cov) } : {}),
|
|
553
|
+
};
|
|
554
|
+
});
|
|
555
|
+
const compared = likely ? sides.find((x) => x.playerId === likely.playerId) : top;
|
|
556
|
+
const comparedRows = compared ? evidenceRows.filter((r) => (compared.players ?? [compared.playerId]).includes(r.playerId)) : [];
|
|
557
|
+
const evidence = {
|
|
558
|
+
incomes: compared
|
|
559
|
+
? {
|
|
560
|
+
mine: myNow,
|
|
561
|
+
theirs: comparedRows.reduce((n, r) => n + r.income, 0),
|
|
562
|
+
playerId: compared.playerId,
|
|
563
|
+
...sideOf(compared),
|
|
564
|
+
source: comparedRows.some((r) => r.source === 'tracked') ? 'tracked' : 'visible',
|
|
565
|
+
}
|
|
566
|
+
: null,
|
|
567
|
+
opponents: evidenceRows,
|
|
568
|
+
thin: evidenceRows.some((r) => r.thin),
|
|
569
|
+
};
|
|
534
570
|
const state = bound || estimated
|
|
535
571
|
? 'winning'
|
|
536
572
|
: lostOnBound || lostOnEstimate
|
|
@@ -551,6 +587,7 @@ export function gameOutlook(input) {
|
|
|
551
587
|
state,
|
|
552
588
|
...(estimated || lostOnEstimate ? { estimated: true } : {}),
|
|
553
589
|
...(lowLikely ? { lowLikely: { myIncome: myNow, mostTracked: mostTracked(), groundDown, losing: lostOnEstimate } } : {}),
|
|
590
|
+
evidence,
|
|
554
591
|
};
|
|
555
592
|
});
|
|
556
593
|
const last = turns.at(-1);
|
|
@@ -594,6 +631,23 @@ export function gameOutlook(input) {
|
|
|
594
631
|
: state
|
|
595
632
|
? `${state[0].toUpperCase()}${state.slice(1)} for ${OUTLOOK_TURNS} turns running; ${figures}.`
|
|
596
633
|
: `${last.state[0].toUpperCase()}${last.state.slice(1)} for ${heldTurns} of the ${OUTLOOK_TURNS} turns (${turns.map((t) => t.state).join(', ')}), so no state yet; ${figures}.`;
|
|
634
|
+
// #304: the incomes compared and each opponent's frontier read, said beside the state, and thin
|
|
635
|
+
// evidence named so that "even" does not read as safe.
|
|
636
|
+
const ev = last.evidence;
|
|
637
|
+
if (ev?.incomes) {
|
|
638
|
+
const i = ev.incomes;
|
|
639
|
+
reading += ` Incomes compared: ${teamGame ? "my team's" : 'mine'} ${i.mine}, ${sideName(i)}'s ${i.theirs} (${i.source === 'tracked' ? 'tracked off my boards' : 'in view, a lower bound under fog'}).`;
|
|
640
|
+
}
|
|
641
|
+
if (ev && !fullyVisible(g, byTurn.get(last.turn).board) && ev.opponents.length)
|
|
642
|
+
reading += ` Frontier read: ${ev.opponents.map((r) => `player ${r.playerId} ${r.read ?? 'not measured'}`).join('; ')}.`;
|
|
643
|
+
if (ev?.thin) {
|
|
644
|
+
const pct = Math.round(LOW_COVERAGE * 100);
|
|
645
|
+
const which = ev.opponents
|
|
646
|
+
.filter((r) => r.thin)
|
|
647
|
+
.map((r) => `player ${r.playerId} (${r.coverage === null ? 'not measured' : `${Math.round(r.coverage * 100)}%`})`)
|
|
648
|
+
.join(', ');
|
|
649
|
+
reading += ` Thin evidence: the income of ${which} was read from under ${pct}% of its frontier, so "${last.state}" is not a safe reading: an opponent may be stronger than the fog shows.`;
|
|
650
|
+
}
|
|
597
651
|
reading += est;
|
|
598
652
|
// #228: a likely ratio far below parity, said with what it took (or lacked) to read losing.
|
|
599
653
|
const low = last.lowLikely;
|
|
@@ -629,5 +683,13 @@ export function gameOutlook(input) {
|
|
|
629
683
|
reading += ` The main front (the biggest enemy stack next to me against my biggest beside it), each side deploying its whole income there every turn (mine ${fr.myIncome}, player ${fr.player}'s ${fr.theirIncome}: ${theirs}): my ${fr.mine} on #${fr.from} against its ${fr.theirs} on #${fr.at}, ${outcome}.`;
|
|
630
684
|
}
|
|
631
685
|
}
|
|
632
|
-
return {
|
|
686
|
+
return {
|
|
687
|
+
state,
|
|
688
|
+
heldTurns,
|
|
689
|
+
turns,
|
|
690
|
+
k,
|
|
691
|
+
threshold: OUTLOOK_RATIO,
|
|
692
|
+
basis: `${reading} ${measure} ${thresholdsText}`,
|
|
693
|
+
...(last?.evidence ? { evidence: last.evidence } : {}),
|
|
694
|
+
};
|
|
633
695
|
}
|