@chrischall/warapp-mcp 0.1.0 → 0.3.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 +49 -20
- 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 +13214 -9759
- package/dist/calibration-source.js +75 -0
- package/dist/chat.js +73 -0
- package/dist/client.js +85 -7
- package/dist/direct/link.js +9 -5
- package/dist/direct/session.js +60 -6
- package/dist/direct/sign-in.js +296 -0
- package/dist/direct/transport.js +59 -24
- package/dist/finished-store.js +357 -0
- package/dist/hosted.js +6 -4
- 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/healthcheck.js +7 -1
- package/dist/tools/index.js +24 -8
- package/dist/tools/join-game.js +1 -1
- package/dist/tools/live-context.js +3 -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 +78 -8
- package/dist/tools/notifications.js +484 -0
- package/dist/tools/open-games.js +5 -2
- 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/session.js +50 -18
- package/dist/tools/sign-in.js +56 -0
- 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-brief.js +1 -1
- package/dist/tools/team.js +2 -6
- package/dist/tools/turn.js +79 -15
- package/dist/tools/where.js +42 -17
- package/dist/tools/write-kit.js +16 -3
- package/dist/trend.js +145 -20
- package/dist/version.js +1 -1
- package/docs/examples/warapp-auth-flow.json +31 -0
- package/manifest.json +22 -2
- package/mint.yaml +44 -22
- package/package.json +3 -2
- package/server.json +2 -2
- package/skills/warapp-mcp/SKILL.md +141 -14
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { symmetricStart, trackOpponents, unevenStart } from './opponents.js';
|
|
2
|
+
import { turnIncomes } from './finished-game.js';
|
|
3
|
+
import { aggregateCalibration, calibrationSamples, } from './game-lessons.js';
|
|
4
|
+
import { planTurn } from './planner.js';
|
|
5
|
+
import { planEnemyEstimates } from './plan/estimates.js';
|
|
6
|
+
import { boardIncome, incomeRules } from './player.js';
|
|
7
|
+
import { simulateTurn } from './turn-sim.js';
|
|
8
|
+
const landOf = (b, p) => new Set(b.territories.flatMap((t) => (t.owner.kind === 'player' && t.owner.playerId === p ? [t.territoryId] : [])));
|
|
9
|
+
// ---- (a) calibration ----
|
|
10
|
+
/** The figures scored. */
|
|
11
|
+
export const ESTIMATORS = ['tracker', 'prior', 'plan'];
|
|
12
|
+
/**
|
|
13
|
+
* For each turn T I cached a board for whose history board T − 1 the history has, every opponent with
|
|
14
|
+
* land on that board: the tracker (E2's `calibrationSamples`), the fog prior and the plan's estimate
|
|
15
|
+
* (`planEnemyEstimates` as warapp_plan_turn calls it: my income on my board, the previous cached board
|
|
16
|
+
* and my orders on it, the tracks, the seats with a player holding no land on the truth board read as
|
|
17
|
+
* out). A turn on which none of the three has a figure is left out. Pure.
|
|
18
|
+
*/
|
|
19
|
+
export function estimateSamples(input) {
|
|
20
|
+
const tracker = new Map(calibrationSamples(input).map((s) => [`${s.turn}:${s.opponent}`, s]));
|
|
21
|
+
const truthBoard = new Map(input.history.map((b) => [b.turn, b.board]));
|
|
22
|
+
const cached = [...input.cached].sort((a, b) => a.turn - b.turn);
|
|
23
|
+
const rules = incomeRules(input.incomeSettings);
|
|
24
|
+
const out = [];
|
|
25
|
+
for (const cur of cached) {
|
|
26
|
+
const truth = truthBoard.get(cur.turn - 1);
|
|
27
|
+
if (!truth)
|
|
28
|
+
continue;
|
|
29
|
+
const upTo = cached.filter((b) => b.turn <= cur.turn);
|
|
30
|
+
const tracks = upTo.length >= 2 ? trackOpponents(input.graph, upTo, input.me, input.plannerSettings, input.game) : [];
|
|
31
|
+
const previous = cached.find((b) => b.turn === cur.turn - 1);
|
|
32
|
+
const real = turnIncomes(input.graph, truth, input.opponents, input.incomeSettings);
|
|
33
|
+
const uneven = unevenStart(input.settings);
|
|
34
|
+
const { estimates } = planEnemyEstimates({
|
|
35
|
+
graph: input.graph,
|
|
36
|
+
board: cur.board,
|
|
37
|
+
me: input.me,
|
|
38
|
+
income: boardIncome(input.graph, landOf(cur.board, input.me), rules).total,
|
|
39
|
+
settings: input.plannerSettings,
|
|
40
|
+
...(previous ? { previousBoard: previous.board } : {}),
|
|
41
|
+
...(previous?.orders ? { previousOrders: previous.orders } : {}),
|
|
42
|
+
tracks,
|
|
43
|
+
game: {
|
|
44
|
+
players: input.players.map((p) => ({ ...p, state: p.id === input.me || (real.get(p.id)?.territories ?? 1) > 0 ? 'Playing' : 'Eliminated' })),
|
|
45
|
+
turn: cur.turn,
|
|
46
|
+
symmetricStart: symmetricStart(input.settings),
|
|
47
|
+
...(uneven ? { unevenStart: uneven } : {}),
|
|
48
|
+
...(input.settings?.fog !== undefined ? { fog: input.settings.fog } : {}),
|
|
49
|
+
},
|
|
50
|
+
turn: cur.turn,
|
|
51
|
+
});
|
|
52
|
+
for (const o of [...input.opponents].sort((a, b) => a - b)) {
|
|
53
|
+
const t = real.get(o);
|
|
54
|
+
if (!t || t.income <= 0)
|
|
55
|
+
continue;
|
|
56
|
+
const tr = tracker.get(`${cur.turn}:${o}`);
|
|
57
|
+
const e = estimates.find((x) => x.playerId === o);
|
|
58
|
+
const coverage = tr?.coverage ?? tracks.find((x) => x.playerId === o)?.coverage?.share ?? null;
|
|
59
|
+
const sample = {
|
|
60
|
+
turn: cur.turn,
|
|
61
|
+
opponent: o,
|
|
62
|
+
truth: t.income,
|
|
63
|
+
coverage,
|
|
64
|
+
tracker: tr?.estimate ?? null,
|
|
65
|
+
prior: e?.prior ?? null,
|
|
66
|
+
plan: e?.estimate ?? null,
|
|
67
|
+
};
|
|
68
|
+
if (sample.tracker !== null || sample.prior !== null || sample.plan !== null)
|
|
69
|
+
out.push(sample);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
/** Each estimator's samples counted by coverage band (E2's `aggregateCalibration`): ratios only. Pure. */
|
|
75
|
+
export function aggregateEstimates(samples) {
|
|
76
|
+
const out = {};
|
|
77
|
+
for (const k of ESTIMATORS) {
|
|
78
|
+
const of = samples.flatMap((s) => (s[k] === null ? [] : [{ turn: s.turn, opponent: s.opponent, estimate: s[k], truth: s.truth, coverage: s.coverage }]));
|
|
79
|
+
if (of.length)
|
|
80
|
+
out[k] = aggregateCalibration(of);
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
const r3 = (x) => Math.round(x * 1000) / 1000;
|
|
85
|
+
/** {@link RatioStats} read out; `null` without samples. Pure. */
|
|
86
|
+
export function ratioSummary(s) {
|
|
87
|
+
if (!s || s.n <= 0)
|
|
88
|
+
return null;
|
|
89
|
+
const mean = s.sum / s.n;
|
|
90
|
+
return {
|
|
91
|
+
n: s.n,
|
|
92
|
+
mean: r3(mean),
|
|
93
|
+
geoMean: r3(Math.exp(s.logSum / s.n)),
|
|
94
|
+
sd: r3(Math.sqrt(Math.max(0, s.sq / s.n - mean * mean))),
|
|
95
|
+
under: r3(s.under / s.n),
|
|
96
|
+
over: r3(s.over / s.n),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// ---- (b) the opening benchmark ----
|
|
100
|
+
/** The turns the opening benchmark replays. */
|
|
101
|
+
export const BENCHMARK_TURNS = 8;
|
|
102
|
+
/** "Land without bonuses" (#318 finding 2): this many territories or more on at most {@link LAND_POOR_INCOME}, after {@link GAP_TURN}. */
|
|
103
|
+
export const LAND_POOR_TERRITORIES = 11;
|
|
104
|
+
export const LAND_POOR_INCOME = 8;
|
|
105
|
+
/** The board the opening is judged on, and the gap that lost it (#318 finding 1: an opponent at 1.5× my income). */
|
|
106
|
+
export const GAP_TURN = 4;
|
|
107
|
+
export const GAP_FACTOR = 1.5;
|
|
108
|
+
const simSettingsOf = (s) => ({
|
|
109
|
+
...(s.offensiveKillRate !== undefined ? { offensiveKillRate: s.offensiveKillRate } : {}),
|
|
110
|
+
...(s.defensiveKillRate !== undefined ? { defensiveKillRate: s.defensiveKillRate } : {}),
|
|
111
|
+
...(s.luckModifier !== undefined ? { luckModifier: s.luckModifier } : {}),
|
|
112
|
+
...(s.roundingMode !== undefined ? { roundingMode: s.roundingMode } : {}),
|
|
113
|
+
...(s.oneArmyStandsGuard !== undefined ? { oneArmyStandsGuard: s.oneArmyStandsGuard } : {}),
|
|
114
|
+
...(s.multiAttack !== undefined ? { multiAttack: s.multiAttack } : {}),
|
|
115
|
+
});
|
|
116
|
+
const territory = (territoryId, owner, armies) => ({
|
|
117
|
+
territoryId,
|
|
118
|
+
owner: typeof owner === 'number' ? { kind: 'player', playerId: owner } : owner === 'neutral' ? { kind: 'neutral' } : { kind: 'unknown' },
|
|
119
|
+
armies: owner === 'unknown' ? null : armies,
|
|
120
|
+
});
|
|
121
|
+
/**
|
|
122
|
+
* The next replayed board: land I hold after my replayed orders stays mine with its armies; any other
|
|
123
|
+
* territory an opponent holds on the history board is the history's (owner and armies); the rest keeps
|
|
124
|
+
* its replayed state (a neutral my attack wore down, or one I really took but did not take here). Pure.
|
|
125
|
+
*/
|
|
126
|
+
export function overlayHistory(g, sim, history, me) {
|
|
127
|
+
const real = new Map(history.territories.map((t) => [t.territoryId, t]));
|
|
128
|
+
const territories = [];
|
|
129
|
+
for (const id of [...g.territoryById.keys()].sort((a, b) => a - b)) {
|
|
130
|
+
const r = sim.get(id);
|
|
131
|
+
const h = real.get(id);
|
|
132
|
+
if (r?.owner === me)
|
|
133
|
+
territories.push(territory(id, me, r.armies));
|
|
134
|
+
else if (h && h.owner.kind === 'player' && h.owner.playerId !== me)
|
|
135
|
+
territories.push({ territoryId: id, owner: h.owner, armies: h.armies });
|
|
136
|
+
else if (r)
|
|
137
|
+
territories.push(territory(id, r.owner, r.armies));
|
|
138
|
+
else if (h)
|
|
139
|
+
territories.push({ territoryId: id, owner: h.owner.kind === 'player' ? { kind: 'neutral' } : h.owner, armies: h.armies });
|
|
140
|
+
}
|
|
141
|
+
return { source: 'gamefeed', verified: false, territories };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* My opening replayed from my real start (see the header for what is approximated): each turn the
|
|
145
|
+
* planner plans on the replayed board, the turn simulator resolves my deploys and moves alone, and the
|
|
146
|
+
* opponents are put back where the history shows them ({@link overlayHistory}). After each turn, my
|
|
147
|
+
* replayed income and territories beside the real ones, the winner's and the best opponent's. Pure.
|
|
148
|
+
*/
|
|
149
|
+
export function openingBenchmark(input) {
|
|
150
|
+
const history = new Map(input.history.map((b) => [b.turn, b.board]));
|
|
151
|
+
const start = history.get(0);
|
|
152
|
+
if (!start || !landOf(start, input.me).size)
|
|
153
|
+
return { turns: [], exact: true, stopped: 'no-start' };
|
|
154
|
+
const plan = input.plan ?? planTurn;
|
|
155
|
+
const rules = incomeRules(input.incomeSettings);
|
|
156
|
+
const ids = [...new Set([...input.players.map((p) => p.id), input.me])].sort((a, b) => a - b);
|
|
157
|
+
const teams = new Map(input.players.flatMap((p) => (typeof p.team === 'number' && p.team >= 0 ? [[p.id, p.team]] : [])));
|
|
158
|
+
const sim = simSettingsOf(input.plannerSettings);
|
|
159
|
+
const turns = [];
|
|
160
|
+
let exact = true;
|
|
161
|
+
let board = start;
|
|
162
|
+
for (let n = 1; n <= (input.turns ?? BENCHMARK_TURNS); n++) {
|
|
163
|
+
const next = history.get(n);
|
|
164
|
+
if (!next)
|
|
165
|
+
return { turns, exact, stopped: 'history-ends' };
|
|
166
|
+
const income = boardIncome(input.graph, landOf(board, input.me), rules).total;
|
|
167
|
+
const held = new Map(ids.map((p) => [p, landOf(board, p).size]));
|
|
168
|
+
const p = plan({
|
|
169
|
+
graph: input.graph,
|
|
170
|
+
board,
|
|
171
|
+
me: input.me,
|
|
172
|
+
income,
|
|
173
|
+
settings: input.plannerSettings,
|
|
174
|
+
turn: n,
|
|
175
|
+
game: {
|
|
176
|
+
players: input.players.map((x) => ({ ...x, state: (held.get(x.id) ?? 0) > 0 ? 'Playing' : 'Eliminated' })),
|
|
177
|
+
turn: n,
|
|
178
|
+
fog: 'NoFog',
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
const r = simulateTurn({
|
|
182
|
+
graph: input.graph,
|
|
183
|
+
board,
|
|
184
|
+
deploys: p.deploys.map((d) => ({ player: input.me, territoryId: d.territoryId, armies: d.armies })),
|
|
185
|
+
orders: [{ player: input.me, orders: p.moves.map((m) => ({ from: m.from, to: m.to, armies: m.armies })) }],
|
|
186
|
+
moveOrder: [input.me],
|
|
187
|
+
settings: sim,
|
|
188
|
+
...(teams.size ? { teams } : {}),
|
|
189
|
+
});
|
|
190
|
+
exact &&= r.exact;
|
|
191
|
+
board = overlayHistory(input.graph, r.territories, next, input.me);
|
|
192
|
+
const truth = turnIncomes(input.graph, next, ids, input.incomeSettings);
|
|
193
|
+
const real = truth.get(input.me);
|
|
194
|
+
const winners = input.winners.map((w) => truth.get(w)?.income ?? 0);
|
|
195
|
+
const mineNow = landOf(board, input.me);
|
|
196
|
+
turns.push({
|
|
197
|
+
afterTurn: n,
|
|
198
|
+
replay: { income: boardIncome(input.graph, mineNow, rules).total, territories: mineNow.size },
|
|
199
|
+
real: real && real.territories > 0 ? { income: real.income, territories: real.territories } : null,
|
|
200
|
+
winner: winners.length ? Math.max(...winners) : null,
|
|
201
|
+
bestOpponent: Math.max(0, ...ids.filter((id) => id !== input.me).map((id) => truth.get(id)?.income ?? 0)),
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return { turns, exact };
|
|
205
|
+
}
|
|
206
|
+
const mean = (xs) => r3(xs.reduce((a, b) => a + b, 0) / xs.length);
|
|
207
|
+
/** Many {@link Benchmark}s aggregated. A turn where I held nothing in the real game counts the real income as 0. Pure. */
|
|
208
|
+
export function aggregateBenchmarks(results) {
|
|
209
|
+
const byTurn = new Map();
|
|
210
|
+
for (const r of results)
|
|
211
|
+
for (const t of r.turns)
|
|
212
|
+
byTurn.set(t.afterTurn, [...(byTurn.get(t.afterTurn) ?? []), t]);
|
|
213
|
+
const poor = (x) => x !== null && x.territories >= LAND_POOR_TERRITORIES && x.income <= LAND_POOR_INCOME;
|
|
214
|
+
const behind = (mine, best) => best >= GAP_FACTOR * mine;
|
|
215
|
+
const at = results.flatMap((r) => r.turns.filter((t) => t.afterTurn === GAP_TURN));
|
|
216
|
+
return {
|
|
217
|
+
games: results.length,
|
|
218
|
+
inexact: results.filter((r) => !r.exact).length,
|
|
219
|
+
byTurn: [...byTurn]
|
|
220
|
+
.sort(([a], [b]) => a - b)
|
|
221
|
+
.map(([afterTurn, ts]) => {
|
|
222
|
+
const winners = ts.flatMap((t) => (t.winner === null ? [] : [t.winner]));
|
|
223
|
+
return {
|
|
224
|
+
afterTurn,
|
|
225
|
+
n: ts.length,
|
|
226
|
+
replay: mean(ts.map((t) => t.replay.income)),
|
|
227
|
+
real: mean(ts.map((t) => t.real?.income ?? 0)),
|
|
228
|
+
winner: winners.length ? mean(winners) : null,
|
|
229
|
+
replayAhead: ts.filter((t) => t.replay.income > (t.real?.income ?? 0)).length,
|
|
230
|
+
replayBehind: ts.filter((t) => t.replay.income < (t.real?.income ?? 0)).length,
|
|
231
|
+
};
|
|
232
|
+
}),
|
|
233
|
+
landWithoutBonuses: { replay: at.filter((t) => poor(t.replay)).length, real: at.filter((t) => poor(t.real)).length },
|
|
234
|
+
outpaced: {
|
|
235
|
+
replay: at.filter((t) => behind(t.replay.income, t.bestOpponent)).length,
|
|
236
|
+
real: at.filter((t) => behind(t.real?.income ?? 0, t.bestOpponent)).length,
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
@@ -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
|
}
|