@chrischall/warapp-mcp 0.0.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 +4 -4
- package/.claude-plugin/plugin.json +2 -2
- package/README.md +211 -9
- package/dist/analysis/accept-surrender.js +56 -0
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/calibrate.js +239 -0
- package/dist/analysis/card-support.js +329 -0
- package/dist/analysis/card-threats.js +234 -0
- package/dist/analysis/cards-in-effect.js +130 -0
- package/dist/analysis/chains.js +59 -0
- package/dist/analysis/chokepoints.js +140 -0
- package/dist/analysis/combat.js +231 -0
- package/dist/analysis/commander-safety.js +149 -0
- package/dist/analysis/commitments.js +338 -0
- package/dist/analysis/exchange.js +155 -0
- package/dist/analysis/finished-game.js +136 -0
- package/dist/analysis/game-lessons.js +217 -0
- package/dist/analysis/graph.js +82 -0
- package/dist/analysis/guard-off.js +54 -0
- package/dist/analysis/guess.js +351 -0
- package/dist/analysis/headroom.js +241 -0
- package/dist/analysis/hold-diff.js +64 -0
- package/dist/analysis/how-lost.js +130 -0
- package/dist/analysis/index.js +14 -0
- package/dist/analysis/local-deploy.js +179 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +552 -0
- package/dist/analysis/opponents.js +1253 -0
- package/dist/analysis/outlook.js +695 -0
- package/dist/analysis/overrides.js +60 -0
- package/dist/analysis/picks.js +1879 -0
- package/dist/analysis/plan/after-leftover.js +140 -0
- package/dist/analysis/plan/cap-spend.js +242 -0
- package/dist/analysis/plan/captures.js +1195 -0
- package/dist/analysis/plan/commander.js +236 -0
- package/dist/analysis/plan/completed.js +29 -0
- package/dist/analysis/plan/conceded-out.js +131 -0
- package/dist/analysis/plan/estimates.js +715 -0
- package/dist/analysis/plan/guard-off.js +232 -0
- package/dist/analysis/plan/holds.js +1631 -0
- package/dist/analysis/plan/leftover.js +760 -0
- package/dist/analysis/plan/move-order.js +319 -0
- package/dist/analysis/plan/orders.js +317 -0
- package/dist/analysis/plan/relations.js +702 -0
- package/dist/analysis/plan/report.js +672 -0
- package/dist/analysis/plan/shared.js +1405 -0
- package/dist/analysis/plan/stand-still.js +823 -0
- package/dist/analysis/plan/state.js +44 -0
- package/dist/analysis/plan/strike.js +1416 -0
- package/dist/analysis/plan/targets.js +651 -0
- package/dist/analysis/plan/team.js +299 -0
- package/dist/analysis/plan/threats.js +1001 -0
- package/dist/analysis/plan/transfers.js +328 -0
- package/dist/analysis/plan/util.js +5 -0
- package/dist/analysis/plan/vacated.js +285 -0
- package/dist/analysis/planner.js +2927 -0
- package/dist/analysis/player-record.js +144 -0
- package/dist/analysis/player.js +268 -0
- package/dist/analysis/profile.js +347 -0
- package/dist/analysis/relations.js +1030 -0
- package/dist/analysis/schedule.js +88 -0
- package/dist/analysis/since-last.js +50 -0
- package/dist/analysis/static-seats.js +71 -0
- package/dist/analysis/stress.js +584 -0
- package/dist/analysis/team-brief.js +151 -0
- package/dist/analysis/tripwire.js +644 -0
- package/dist/analysis/turn-sim.js +259 -0
- package/dist/analysis/whereabouts.js +1112 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +121082 -36148
- package/dist/calibration-source.js +75 -0
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +265 -0
- package/dist/client.js +439 -0
- package/dist/config.js +62 -0
- package/dist/direct/link.js +102 -0
- package/dist/direct/session.js +267 -0
- package/dist/direct/transport.js +424 -0
- package/dist/finished-store.js +357 -0
- package/dist/guess-salt.js +65 -0
- package/dist/hosted.js +24 -0
- package/dist/index.js +33 -0
- package/dist/join-color.js +140 -0
- package/dist/lessons-store.js +356 -0
- package/dist/live.js +287 -0
- package/dist/map-details.js +127 -0
- package/dist/map-stats.js +160 -0
- package/dist/map-store.js +242 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +218 -0
- package/dist/normalize.js +443 -0
- package/dist/notifications.js +143 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +165 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +231 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +727 -0
- package/dist/protocol/bundle.js +375 -0
- package/dist/protocol/codec.js +556 -0
- package/dist/protocol/decoded.js +12 -0
- package/dist/protocol/interp.js +1904 -0
- package/dist/protocol/methods.js +615 -0
- package/dist/protocol/rpc.js +202 -0
- package/dist/push/listener.js +393 -0
- package/dist/push/packets.js +66 -0
- package/dist/push/socketio.js +333 -0
- package/dist/push/websocket.js +179 -0
- package/dist/redact.js +62 -0
- package/dist/relations-store.js +242 -0
- package/dist/replay.js +227 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +291 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2223 -0
- package/dist/surrender-pending.js +126 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/accept-surrender.js +311 -0
- package/dist/tools/award.js +206 -0
- package/dist/tools/begin-turn.js +162 -0
- package/dist/tools/board.js +77 -0
- package/dist/tools/card-advice.js +256 -0
- package/dist/tools/card-effects.js +49 -0
- package/dist/tools/card-plays.js +434 -0
- package/dist/tools/card-support-advice.js +140 -0
- package/dist/tools/chat-inbox.js +158 -0
- package/dist/tools/chat-read-mark.js +179 -0
- package/dist/tools/chat.js +525 -0
- package/dist/tools/check-plan.js +689 -0
- package/dist/tools/commander-check.js +103 -0
- package/dist/tools/commitments.js +216 -0
- package/dist/tools/common.js +143 -0
- package/dist/tools/diplomacy.js +342 -0
- package/dist/tools/end-of-game.js +384 -0
- package/dist/tools/game.js +151 -0
- package/dist/tools/guess-view.js +56 -0
- package/dist/tools/healthcheck.js +90 -0
- package/dist/tools/hints.js +34 -0
- package/dist/tools/how-lost.js +154 -0
- package/dist/tools/index.js +152 -0
- package/dist/tools/join-game.js +327 -0
- package/dist/tools/leave-game.js +181 -0
- package/dist/tools/live-context.js +339 -0
- package/dist/tools/live.js +404 -0
- package/dist/tools/map-details.js +88 -0
- package/dist/tools/map-stats.js +130 -0
- package/dist/tools/map.js +141 -0
- package/dist/tools/my-games.js +193 -0
- package/dist/tools/notifications.js +484 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +163 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/past-games.js +94 -0
- package/dist/tools/picks.js +305 -0
- package/dist/tools/profile-hook.js +156 -0
- package/dist/tools/profile-opponents.js +121 -0
- package/dist/tools/profile-reads.js +99 -0
- package/dist/tools/push.js +445 -0
- package/dist/tools/relations.js +611 -0
- package/dist/tools/resolve.js +45 -0
- package/dist/tools/route.js +36 -0
- package/dist/tools/scout.js +87 -0
- package/dist/tools/session.js +163 -0
- package/dist/tools/strategy.js +257 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender-pending.js +74 -0
- package/dist/tools/surrender.js +247 -0
- package/dist/tools/survival.js +189 -0
- package/dist/tools/team-brief.js +266 -0
- package/dist/tools/team-source.js +222 -0
- package/dist/tools/team.js +178 -0
- package/dist/tools/turn.js +2825 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +300 -0
- package/dist/tools/write-kit.js +164 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +577 -0
- package/dist/version.js +4 -0
- package/manifest.json +194 -4
- package/mint.yaml +106 -14
- package/package.json +11 -6
- package/server.json +40 -3
- package/skills/warapp-api/SKILL.md +95 -4
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +69 -8
- package/skills/warapp-mcp/SKILL.md +1096 -21
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
// What finished games teach, kept across games (#312 E2; owner decisions P2–P4 of 2026-11-16), name-free:
|
|
2
|
+
//
|
|
3
|
+
// - `<dataDir>/maps-record/<mapId>.json` (version 1), one per map I finished a game on: the games
|
|
4
|
+
// kept and their outcomes, and per game (at most `MAX_MAP_ENTRIES`, the oldest dropped first, its
|
|
5
|
+
// counts kept) the format, seats, my result, my start region (territories and the bonuses they
|
|
6
|
+
// touch) and income curve, and each winner's start region and income after turns 4 and 8 (the pace
|
|
7
|
+
// to beat). At most `MAX_MAP_RECORDS` maps, the least recently written dropped first.
|
|
8
|
+
// - `<dataDir>/lessons.json` (version 1): per format the games, outcomes, how each lost game was lost
|
|
9
|
+
// (a cause, never who), the turn my income first went flat, and the tracker's estimates scored
|
|
10
|
+
// against the true incomes by frontier coverage (#294): aggregates only — a game's entry (at most
|
|
11
|
+
// `MAX_LESSON_ENTRIES`) holds the same counts for that game alone, so processing it again replaces
|
|
12
|
+
// its share, never adds it twice; a dropped entry's counts stay in the totals.
|
|
13
|
+
//
|
|
14
|
+
// Every game is keyed by `finishedGameKey` (an HMAC of my account and the game under the one shared
|
|
15
|
+
// salt): no game or player id, no name, no clock. Territory, bonus and map ids are the map's own,
|
|
16
|
+
// public data. 0600 files in 0700 folders, atomic writes under the in-process path lock; a file that
|
|
17
|
+
// exists but does not parse (not JSON, not an object, another version, a malformed total) is never
|
|
18
|
+
// overwritten (`failed`). Off with `WARAPP_PLAYER_PROFILE=off` (the end-of-game pass does not run).
|
|
19
|
+
// Profiles and lessons INFORM ONLY (P1): no planner decision reads any of this.
|
|
20
|
+
import { readdir, readFile, stat, unlink } from 'node:fs/promises';
|
|
21
|
+
import { join } from 'node:path';
|
|
22
|
+
import { addCalibration, COVERAGE_BUCKETS, } from './analysis/game-lessons.js';
|
|
23
|
+
import { PROFILE_FORMATS } from './analysis/opponent-profile.js';
|
|
24
|
+
import { writeJsonAtomic } from './map-store.js';
|
|
25
|
+
import { withPathLock } from './path-lock.js';
|
|
26
|
+
export const MAX_MAP_ENTRIES = 50;
|
|
27
|
+
export const MAX_MAP_RECORDS = 200;
|
|
28
|
+
export const MAX_LESSON_ENTRIES = 500;
|
|
29
|
+
export const mapRecordDir = (dir) => join(dir, 'maps-record');
|
|
30
|
+
export const mapRecordPath = (dir, mapId) => join(mapRecordDir(dir), `${mapId}.json`);
|
|
31
|
+
export const lessonsPath = (dir) => join(dir, 'lessons.json');
|
|
32
|
+
const isRec = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
33
|
+
const isInt = (v) => typeof v === 'number' && Number.isInteger(v);
|
|
34
|
+
const num = (v) => typeof v === 'number' && Number.isFinite(v);
|
|
35
|
+
const ints = (v) => Array.isArray(v) && v.every(isInt);
|
|
36
|
+
const intOrNull = (v) => v === null || isInt(v);
|
|
37
|
+
const HASH = /^[0-9a-f]{32}$/;
|
|
38
|
+
const isFormat = (v) => PROFILE_FORMATS.includes(v);
|
|
39
|
+
const RESULTS = ['won', 'lost', 'surrendered', 'eliminated', 'booted', 'ended-by-vote', 'unknown'];
|
|
40
|
+
const isResult = (v) => RESULTS.includes(v);
|
|
41
|
+
const CAUSES = ['commander', 'last-territories', 'unknown', 'surrendered', 'booted', 'still-holding'];
|
|
42
|
+
/** Read a store: `missing`, the parsed object, or `unreadable` (exists, not JSON or not an object). */
|
|
43
|
+
async function readStore(path) {
|
|
44
|
+
let text;
|
|
45
|
+
try {
|
|
46
|
+
text = await readFile(path, 'utf8');
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
return e.code === 'ENOENT' ? 'missing' : 'unreadable';
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const v = JSON.parse(text);
|
|
53
|
+
return isRec(v) ? v : 'unreadable';
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return 'unreadable';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const countsOf = (v) => isRec(v) && Object.values(v).every(isInt) ? Object.fromEntries(Object.entries(v)) : undefined;
|
|
60
|
+
const bumpCount = (c, k, by) => {
|
|
61
|
+
const n = (c[k] ?? 0) + by;
|
|
62
|
+
if (n)
|
|
63
|
+
c[k] = n;
|
|
64
|
+
else
|
|
65
|
+
delete c[k];
|
|
66
|
+
};
|
|
67
|
+
const regionOf = (v) => (isRec(v) && ints(v.territories) && ints(v.bonuses) ? { territories: v.territories, bonuses: v.bonuses } : undefined);
|
|
68
|
+
function mapEntryOf(v) {
|
|
69
|
+
if (!isRec(v) || typeof v.key !== 'string' || !HASH.test(v.key) || !isFormat(v.format) || !isInt(v.seats) || !isResult(v.result) || !intOrNull(v.turns))
|
|
70
|
+
return undefined;
|
|
71
|
+
const m = v.me;
|
|
72
|
+
const mine = regionOf(m);
|
|
73
|
+
if (!mine || !isRec(m) || !Array.isArray(m.income) || !m.income.every(intOrNull) || !intOrNull(m.income4) || !intOrNull(m.income8))
|
|
74
|
+
return undefined;
|
|
75
|
+
if (!Array.isArray(v.winners))
|
|
76
|
+
return undefined;
|
|
77
|
+
const winners = [];
|
|
78
|
+
for (const w of v.winners) {
|
|
79
|
+
const r = regionOf(w);
|
|
80
|
+
if (!r || !isRec(w) || !intOrNull(w.income4) || !intOrNull(w.income8))
|
|
81
|
+
return undefined;
|
|
82
|
+
winners.push({ ...r, income4: w.income4, income8: w.income8 });
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
key: v.key,
|
|
86
|
+
format: v.format,
|
|
87
|
+
seats: v.seats,
|
|
88
|
+
result: v.result,
|
|
89
|
+
turns: v.turns,
|
|
90
|
+
me: { ...mine, income: m.income, income4: m.income4, income8: m.income8 },
|
|
91
|
+
winners,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** The record of `mapId`: an empty one when missing, `unreadable` when it cannot be parsed. Never throws. */
|
|
95
|
+
export async function readMapRecord(dir, mapId) {
|
|
96
|
+
try {
|
|
97
|
+
const v = await readStore(mapRecordPath(dir, mapId));
|
|
98
|
+
if (v === 'unreadable')
|
|
99
|
+
return v;
|
|
100
|
+
if (v === 'missing')
|
|
101
|
+
return { version: 1, mapId, games: 0, outcomes: {}, entries: [] };
|
|
102
|
+
const outcomes = countsOf(v.outcomes);
|
|
103
|
+
if (v.version !== 1 || v.mapId !== mapId || !isInt(v.games) || !outcomes)
|
|
104
|
+
return 'unreadable';
|
|
105
|
+
const entries = (Array.isArray(v.entries) ? v.entries : []).map(mapEntryOf).filter((e) => e !== undefined);
|
|
106
|
+
return { version: 1, mapId, games: v.games, outcomes, entries };
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return 'unreadable';
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Keep one finished game in its map's record: a game already there (by key) is replaced, its outcome
|
|
114
|
+
* moved; a new one counted. `kept`, or `failed` (an unreadable record is never overwritten; a failed
|
|
115
|
+
* write). Never throws.
|
|
116
|
+
*/
|
|
117
|
+
export async function recordMapGame(dir, mapId, entry, keep = MAX_MAP_RECORDS) {
|
|
118
|
+
const path = mapRecordPath(dir, mapId);
|
|
119
|
+
try {
|
|
120
|
+
const r = await withPathLock(path, async () => {
|
|
121
|
+
const rec = await readMapRecord(dir, mapId);
|
|
122
|
+
if (rec === 'unreadable')
|
|
123
|
+
return 'failed';
|
|
124
|
+
const old = rec.entries.find((e) => e.key === entry.key);
|
|
125
|
+
if (old)
|
|
126
|
+
bumpCount(rec.outcomes, old.result, -1);
|
|
127
|
+
else
|
|
128
|
+
rec.games++;
|
|
129
|
+
bumpCount(rec.outcomes, entry.result, 1);
|
|
130
|
+
rec.entries = [...rec.entries.filter((e) => e.key !== entry.key), entry].slice(-MAX_MAP_ENTRIES);
|
|
131
|
+
await writeJsonAtomic(path, mapRecordDir(dir), rec);
|
|
132
|
+
return 'kept';
|
|
133
|
+
});
|
|
134
|
+
if (r === 'kept')
|
|
135
|
+
await pruneMapRecords(dir, keep, path).catch(() => undefined);
|
|
136
|
+
return r;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return 'failed';
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async function pruneMapRecords(dir, keep, protect) {
|
|
143
|
+
const folder = mapRecordDir(dir);
|
|
144
|
+
const files = [];
|
|
145
|
+
for (const name of await readdir(folder)) {
|
|
146
|
+
if (!/^\d+\.json$/.test(name))
|
|
147
|
+
continue;
|
|
148
|
+
const path = join(folder, name);
|
|
149
|
+
try {
|
|
150
|
+
files.push({ path, at: (await stat(path)).mtimeMs });
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// Gone already.
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (files.length <= keep)
|
|
157
|
+
return;
|
|
158
|
+
files.sort((a, b) => a.at - b.at || a.path.localeCompare(b.path));
|
|
159
|
+
let over = files.length - keep;
|
|
160
|
+
for (const f of files) {
|
|
161
|
+
if (over <= 0)
|
|
162
|
+
break;
|
|
163
|
+
if (f.path === protect)
|
|
164
|
+
continue;
|
|
165
|
+
await unlink(f.path).catch(() => undefined);
|
|
166
|
+
over--;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const median = (xs) => {
|
|
170
|
+
if (!xs.length)
|
|
171
|
+
return null;
|
|
172
|
+
const s = [...xs].sort((a, b) => a - b);
|
|
173
|
+
const m = Math.floor(s.length / 2);
|
|
174
|
+
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
175
|
+
};
|
|
176
|
+
/** What a map's record says in short: games, my results, and the winners' pace beside mine (medians over the kept games). Pure. */
|
|
177
|
+
export function mapRecordSummary(rec) {
|
|
178
|
+
const of = (xs) => median(xs.filter((x) => x !== null));
|
|
179
|
+
const winners = rec.entries.flatMap((e) => e.winners);
|
|
180
|
+
return {
|
|
181
|
+
games: rec.games,
|
|
182
|
+
outcomes: rec.outcomes,
|
|
183
|
+
pace: {
|
|
184
|
+
winners: { income4: of(winners.map((w) => w.income4)), income8: of(winners.map((w) => w.income8)) },
|
|
185
|
+
mine: { income4: of(rec.entries.map((e) => e.me.income4)), income8: of(rec.entries.map((e) => e.me.income8)) },
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
/** My results per bonus my start touched, and how often each bonus held a winner's start (#312 E3), most games first then by id. Pure. */
|
|
190
|
+
export function mapRecordStarts(rec) {
|
|
191
|
+
const mine = new Map();
|
|
192
|
+
const winners = new Map();
|
|
193
|
+
for (const e of rec.entries) {
|
|
194
|
+
for (const b of e.me.bonuses) {
|
|
195
|
+
const x = mine.get(b) ?? { games: 0, outcomes: {} };
|
|
196
|
+
x.games++;
|
|
197
|
+
bumpCount(x.outcomes, e.result, 1);
|
|
198
|
+
mine.set(b, x);
|
|
199
|
+
}
|
|
200
|
+
// A bonus counts once per game, however many winners started in it.
|
|
201
|
+
for (const b of new Set(e.winners.flatMap((w) => w.bonuses)))
|
|
202
|
+
winners.set(b, (winners.get(b) ?? 0) + 1);
|
|
203
|
+
}
|
|
204
|
+
const order = (a, b) => b.games - a.games || a.bonusId - b.bonusId;
|
|
205
|
+
return {
|
|
206
|
+
mine: [...mine].map(([bonusId, x]) => ({ bonusId, ...x })).sort(order),
|
|
207
|
+
winners: [...winners].map(([bonusId, games]) => ({ bonusId, games })).sort(order),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function statsOf(v) {
|
|
211
|
+
if (!isRec(v) || !isInt(v.n) || !num(v.sum) || !num(v.sq) || !num(v.logSum) || !isInt(v.under) || !isInt(v.over))
|
|
212
|
+
return undefined;
|
|
213
|
+
return { n: v.n, sum: v.sum, sq: v.sq, logSum: v.logSum, under: v.under, over: v.over };
|
|
214
|
+
}
|
|
215
|
+
function calibrationOf(v) {
|
|
216
|
+
if (!isRec(v))
|
|
217
|
+
return undefined;
|
|
218
|
+
const out = {};
|
|
219
|
+
for (const [k, s] of Object.entries(v)) {
|
|
220
|
+
if (!COVERAGE_BUCKETS.includes(k))
|
|
221
|
+
return undefined;
|
|
222
|
+
const x = statsOf(s);
|
|
223
|
+
if (!x)
|
|
224
|
+
return undefined;
|
|
225
|
+
out[k] = x;
|
|
226
|
+
}
|
|
227
|
+
return out;
|
|
228
|
+
}
|
|
229
|
+
function lossOf(v) {
|
|
230
|
+
if (v === null)
|
|
231
|
+
return null;
|
|
232
|
+
if (!isRec(v) || !CAUSES.includes(v.cause) || !intOrNull(v.turn))
|
|
233
|
+
return undefined;
|
|
234
|
+
return { cause: v.cause, turn: v.turn };
|
|
235
|
+
}
|
|
236
|
+
function lessonEntryOf(v) {
|
|
237
|
+
if (!isRec(v) || typeof v.key !== 'string' || !HASH.test(v.key) || !isFormat(v.format) || !isInt(v.seats) || !isResult(v.result) || !intOrNull(v.flatAfterTurn))
|
|
238
|
+
return undefined;
|
|
239
|
+
const loss = lossOf(v.howLost);
|
|
240
|
+
const calibration = calibrationOf(v.calibration);
|
|
241
|
+
if (loss === undefined || !calibration)
|
|
242
|
+
return undefined;
|
|
243
|
+
return { key: v.key, format: v.format, seats: v.seats, result: v.result, flatAfterTurn: v.flatAfterTurn, howLost: loss, calibration };
|
|
244
|
+
}
|
|
245
|
+
function totalsOf(v) {
|
|
246
|
+
if (!isRec(v) || !isInt(v.games))
|
|
247
|
+
return undefined;
|
|
248
|
+
const outcomes = countsOf(v.outcomes);
|
|
249
|
+
const lost = countsOf(v.howLost);
|
|
250
|
+
const calibration = calibrationOf(v.calibration);
|
|
251
|
+
const f = v.flat;
|
|
252
|
+
if (!outcomes || !lost || !calibration || !isRec(f) || !isInt(f.n) || !num(f.sum))
|
|
253
|
+
return undefined;
|
|
254
|
+
return { games: v.games, outcomes, howLost: lost, flat: { n: f.n, sum: f.sum }, calibration };
|
|
255
|
+
}
|
|
256
|
+
/** The lessons store: empty when missing, `unreadable` when it cannot be parsed (a malformed total too). Never throws. */
|
|
257
|
+
export async function readLessons(dir) {
|
|
258
|
+
try {
|
|
259
|
+
const v = await readStore(lessonsPath(dir));
|
|
260
|
+
if (v === 'unreadable')
|
|
261
|
+
return v;
|
|
262
|
+
if (v === 'missing')
|
|
263
|
+
return { version: 1, byFormat: {}, entries: [] };
|
|
264
|
+
if (v.version !== 1 || !isRec(v.byFormat))
|
|
265
|
+
return 'unreadable';
|
|
266
|
+
const byFormat = {};
|
|
267
|
+
for (const [k, t] of Object.entries(v.byFormat)) {
|
|
268
|
+
const totals = totalsOf(t);
|
|
269
|
+
if (!isFormat(k) || !totals)
|
|
270
|
+
return 'unreadable';
|
|
271
|
+
byFormat[k] = totals;
|
|
272
|
+
}
|
|
273
|
+
const entries = (Array.isArray(v.entries) ? v.entries : []).map(lessonEntryOf).filter((e) => e !== undefined);
|
|
274
|
+
return { version: 1, byFormat, entries };
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
return 'unreadable';
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const emptyTotals = () => ({ games: 0, outcomes: {}, howLost: {}, flat: { n: 0, sum: 0 }, calibration: {} });
|
|
281
|
+
/** Move `t` by one entry's share, `scale` +1 (add) or −1 (take back). */
|
|
282
|
+
function applyEntry(t, e, scale) {
|
|
283
|
+
t.games += scale;
|
|
284
|
+
bumpCount(t.outcomes, e.result, scale);
|
|
285
|
+
if (e.howLost)
|
|
286
|
+
bumpCount(t.howLost, e.howLost.cause, scale);
|
|
287
|
+
if (e.flatAfterTurn !== null)
|
|
288
|
+
t.flat = { n: t.flat.n + scale, sum: t.flat.sum + scale * e.flatAfterTurn };
|
|
289
|
+
t.calibration = addCalibration(t.calibration, e.calibration, scale);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Keep one finished game's lessons: its totals moved by the difference from the entry stored for it
|
|
293
|
+
* (so processing a game again replaces its share), the entry kept (at most {@link MAX_LESSON_ENTRIES}).
|
|
294
|
+
* `kept`, or `failed` (an unreadable store is never overwritten; a failed write). Never throws.
|
|
295
|
+
*/
|
|
296
|
+
export async function recordLessons(dir, entry) {
|
|
297
|
+
const path = lessonsPath(dir);
|
|
298
|
+
try {
|
|
299
|
+
return await withPathLock(path, async () => {
|
|
300
|
+
const file = await readLessons(dir);
|
|
301
|
+
if (file === 'unreadable')
|
|
302
|
+
return 'failed';
|
|
303
|
+
const old = file.entries.find((e) => e.key === entry.key);
|
|
304
|
+
if (old) {
|
|
305
|
+
const t = file.byFormat[old.format];
|
|
306
|
+
if (t) {
|
|
307
|
+
applyEntry(t, old, -1);
|
|
308
|
+
if (t.games <= 0)
|
|
309
|
+
delete file.byFormat[old.format];
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const t = file.byFormat[entry.format] ?? emptyTotals();
|
|
313
|
+
applyEntry(t, entry, 1);
|
|
314
|
+
file.byFormat[entry.format] = t;
|
|
315
|
+
file.entries = [...file.entries.filter((e) => e.key !== entry.key), entry].slice(-MAX_LESSON_ENTRIES);
|
|
316
|
+
await writeJsonAtomic(path, dir, file);
|
|
317
|
+
return 'kept';
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
catch {
|
|
321
|
+
return 'failed';
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const r2 = (x) => Math.round(x * 100) / 100;
|
|
325
|
+
/** A calibration's bands summarized ({@link RatioSummary}); a band with no sample is left out. Pure. */
|
|
326
|
+
export function calibrationSummary(c) {
|
|
327
|
+
const out = {};
|
|
328
|
+
for (const k of COVERAGE_BUCKETS) {
|
|
329
|
+
const x = c[k];
|
|
330
|
+
if (!x || x.n <= 0)
|
|
331
|
+
continue;
|
|
332
|
+
out[k] = { n: x.n, mean: r2(x.sum / x.n), geoMean: r2(Math.exp(x.logSum / x.n)), low: r2(x.under / x.n), high: r2(x.over / x.n) };
|
|
333
|
+
}
|
|
334
|
+
return out;
|
|
335
|
+
}
|
|
336
|
+
/** The lessons store summarized (#312 E3): per format, and the calibration of every format together. Aggregates only. Pure. */
|
|
337
|
+
export function lessonsSummary(file) {
|
|
338
|
+
const byFormat = {};
|
|
339
|
+
let games = 0;
|
|
340
|
+
let all = {};
|
|
341
|
+
for (const f of PROFILE_FORMATS) {
|
|
342
|
+
const t = file.byFormat[f];
|
|
343
|
+
if (!t || t.games <= 0)
|
|
344
|
+
continue;
|
|
345
|
+
games += t.games;
|
|
346
|
+
all = addCalibration(all, t.calibration);
|
|
347
|
+
byFormat[f] = {
|
|
348
|
+
games: t.games,
|
|
349
|
+
outcomes: t.outcomes,
|
|
350
|
+
howLost: t.howLost,
|
|
351
|
+
flatAfterTurn: t.flat.n > 0 ? { games: t.flat.n, mean: r2(t.flat.sum / t.flat.n) } : null,
|
|
352
|
+
calibration: calibrationSummary(t.calibration),
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
return { games, byFormat, calibration: calibrationSummary(all) };
|
|
356
|
+
}
|
package/dist/live.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
// The live board: a GetLatestGameInfo reply, decoded by the client bundle's own
|
|
2
|
+
// readers, turned into territory standings — and from there into the
|
|
3
|
+
// source-agnostic BoardState the analysis core takes.
|
|
4
|
+
//
|
|
5
|
+
// Field names in the bundle are minified and change between releases, so
|
|
6
|
+
// nothing here keys on them. Classes are found by their stable registered type
|
|
7
|
+
// name (`$type`), and a TerritoryStanding's fields by read order — verified
|
|
8
|
+
// through the bridge (docs/WEB-PROTOCOL.md): fog level, territory id, Armies,
|
|
9
|
+
// owner. The territory id also keys the standing's dict, which is checked, so a
|
|
10
|
+
// reordered layout is an error rather than a silently wrong board. An Armies
|
|
11
|
+
// record carries its special units (commanders, bosses) as a list; every unit
|
|
12
|
+
// class ends with the shared fields GUID, owner (read from the bundle, #61).
|
|
13
|
+
import { McpToolError } from '@chrischall/mcp-utils';
|
|
14
|
+
import { fields, isObj } from './protocol/decoded.js';
|
|
15
|
+
const LATEST_GAME_INFO = 'warlight.shared.LatestGameInfo';
|
|
16
|
+
const GAME_STANDING = 'warlight.shared.GameStanding';
|
|
17
|
+
const TERRITORY_STANDING = 'warlight.shared.TerritoryStanding';
|
|
18
|
+
const ARMIES = 'warlight.shared.Armies';
|
|
19
|
+
const COMMANDER = 'warlight.shared.Commander';
|
|
20
|
+
const UNIT_KINDS = {
|
|
21
|
+
[COMMANDER]: 'commander',
|
|
22
|
+
'warlight.shared.Boss1': 'boss',
|
|
23
|
+
'warlight.shared.Boss2': 'boss',
|
|
24
|
+
'warlight.shared.Boss3': 'boss',
|
|
25
|
+
'warlight.shared.Boss4': 'boss',
|
|
26
|
+
'warlight.shared.CustomSpecialUnit': 'custom',
|
|
27
|
+
};
|
|
28
|
+
/** TerritoryStanding owner values that are not players. */
|
|
29
|
+
export const OWNER_NEUTRAL = 0;
|
|
30
|
+
export const OWNER_PICKABLE = -2;
|
|
31
|
+
/**
|
|
32
|
+
* StandingFogLevel, as the client bundle names it (verified 2026-11-12): 1 You (my own
|
|
33
|
+
* territory), 2 Visible, 3 OwnerOnly (owner shown, armies hidden), 4 Fogged. The client
|
|
34
|
+
* refuses a deploy on 3 or 4 as "on fogged territory"; a fully fogged standing is
|
|
35
|
+
* fog level 4, owner -1 and `Armies(0, fogged)`.
|
|
36
|
+
*/
|
|
37
|
+
export const FOG_LEVEL_NAMES = { 1: 'You', 2: 'Visible', 3: 'OwnerOnly', 4: 'Fogged' };
|
|
38
|
+
const FOG_LEVEL_YOU = 1;
|
|
39
|
+
export const FOG_LEVEL_VISIBLE = 2;
|
|
40
|
+
export const FOG_LEVEL_OWNER_ONLY = 3;
|
|
41
|
+
const FOG_LEVEL_FOGGED = 4;
|
|
42
|
+
const KNOWN_FOG_LEVELS = new Set([FOG_LEVEL_YOU, FOG_LEVEL_VISIBLE, FOG_LEVEL_OWNER_ONLY, FOG_LEVEL_FOGGED]);
|
|
43
|
+
/** On a decoded LatestGameInfo: the name of its CURRENT standing's field, from the bundle's reader. */
|
|
44
|
+
const CURRENT_STANDING_FIELD = Symbol('currentStandingField');
|
|
45
|
+
/** The live standing arrived in a shape this server does not understand. */
|
|
46
|
+
export class LiveBoardFormatError extends McpToolError {
|
|
47
|
+
constructor(detail) {
|
|
48
|
+
super(`war.app's live board arrived in an unexpected shape (${detail}).`, {
|
|
49
|
+
kind: 'unknown',
|
|
50
|
+
hint: 'The war.app client may have changed. warapp_get_map and warapp_plan_route work without a board.',
|
|
51
|
+
});
|
|
52
|
+
this.name = 'LiveBoardFormatError';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const isType = (v, type) => isObj(v) && v.$type === type;
|
|
56
|
+
/** `[key, value]` pairs of a decoded dict, or the items of a list. */
|
|
57
|
+
function entries(v) {
|
|
58
|
+
if (!Array.isArray(v))
|
|
59
|
+
return undefined;
|
|
60
|
+
return v.map((item) => Array.isArray(item) && item.length === 2 ? { key: item[0], value: item[1] } : { value: item });
|
|
61
|
+
}
|
|
62
|
+
const GUID = /^[0-9a-f]{32}$/i;
|
|
63
|
+
/** One decoded special unit: its class names the kind; GUID and owner are its last two fields. */
|
|
64
|
+
function specialUnitOf(u) {
|
|
65
|
+
if (!isObj(u) || typeof u.$type !== 'string')
|
|
66
|
+
throw new LiveBoardFormatError('a special unit is not a unit');
|
|
67
|
+
// A constructor the interpreter could not run leaves a half-built record (#131, #133).
|
|
68
|
+
if (typeof u.$ctorError === 'string') {
|
|
69
|
+
throw new LiveBoardFormatError(`a special unit (${u.$type}): the client's constructor failed (${u.$ctorError})`);
|
|
70
|
+
}
|
|
71
|
+
const values = fields(u);
|
|
72
|
+
const [id, owner] = values.slice(-2);
|
|
73
|
+
if (typeof id !== 'string' || !GUID.test(id) || typeof owner !== 'number') {
|
|
74
|
+
throw new LiveBoardFormatError(`a special unit (${u.$type}) lacks its GUID and owner`);
|
|
75
|
+
}
|
|
76
|
+
const kind = UNIT_KINDS[u.$type] ?? 'unknown';
|
|
77
|
+
return kind === 'unknown' ? { kind, type: u.$type, id, owner } : { kind, id, owner };
|
|
78
|
+
}
|
|
79
|
+
function territoryOf(key, ts) {
|
|
80
|
+
const values = fields(ts);
|
|
81
|
+
const ints = values.filter((v) => typeof v === 'number');
|
|
82
|
+
const armies = values.find((v) => isType(v, ARMIES));
|
|
83
|
+
if (ints.length < 3 || !armies)
|
|
84
|
+
throw new LiveBoardFormatError('a territory standing lacks its fields');
|
|
85
|
+
const [fogLevel, territoryId, owner] = ints;
|
|
86
|
+
if (typeof key === 'number' && key !== territoryId) {
|
|
87
|
+
throw new LiveBoardFormatError(`territory standing ${key} reads as territory ${territoryId}`);
|
|
88
|
+
}
|
|
89
|
+
// A constructor the interpreter could not run (the client changed how it builds Armies) leaves
|
|
90
|
+
// a half-built record: its count would be a default, not the territory's armies (#131).
|
|
91
|
+
if (typeof armies.$ctorError === 'string') {
|
|
92
|
+
throw new LiveBoardFormatError(`territory ${territoryId}'s armies: the client's Armies constructor failed (${armies.$ctorError})`);
|
|
93
|
+
}
|
|
94
|
+
const armyFields = fields(armies);
|
|
95
|
+
const armyFogged = armyFields.find((v) => typeof v === 'boolean') === true;
|
|
96
|
+
// The count is the record's one number (verified 2026-11-13: fogged flag, count, units). A second
|
|
97
|
+
// number means the client added a field, and nothing here can tell which is the count: refuse,
|
|
98
|
+
// rather than read the wrong one as every territory's armies (#148).
|
|
99
|
+
const numbers = armyFields.filter((v) => typeof v === 'number');
|
|
100
|
+
if (numbers.length > 1) {
|
|
101
|
+
throw new LiveBoardFormatError(`territory ${territoryId}'s Armies record carries ${numbers.length} numbers, so its army count cannot be told`);
|
|
102
|
+
}
|
|
103
|
+
const count = numbers[0];
|
|
104
|
+
// The fog level alone says whether the owner shows: Light Fog's owner-only standings (3) carry a
|
|
105
|
+
// real owner under an Armies record marked fogged (#131). A level this server does not know
|
|
106
|
+
// falls back to the Armies' flag.
|
|
107
|
+
const fogged = KNOWN_FOG_LEVELS.has(fogLevel) ? fogLevel === FOG_LEVEL_FOGGED : armyFogged;
|
|
108
|
+
// Owner-only: the owner shows, any army count is a placeholder (#69).
|
|
109
|
+
const armiesHidden = fogged || armyFogged || fogLevel === FOG_LEVEL_OWNER_ONLY;
|
|
110
|
+
if (!armiesHidden && typeof count !== 'number') {
|
|
111
|
+
throw new LiveBoardFormatError(`visible territory ${territoryId} has no army count`);
|
|
112
|
+
}
|
|
113
|
+
const out = {
|
|
114
|
+
territoryId,
|
|
115
|
+
fogLevel,
|
|
116
|
+
fogged,
|
|
117
|
+
owner,
|
|
118
|
+
armies: armiesHidden || typeof count !== 'number' ? null : count,
|
|
119
|
+
};
|
|
120
|
+
const units = armyFields.find((v) => Array.isArray(v));
|
|
121
|
+
if (!armiesHidden && units?.length)
|
|
122
|
+
out.specialUnits = units.map(specialUnitOf);
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Read a GetLatestGameInfo result (the body positioned at the result-present
|
|
127
|
+
* flag). Returns the territories, or `null` when war.app sent no standing (no
|
|
128
|
+
* result, or a game with no board yet).
|
|
129
|
+
*/
|
|
130
|
+
export function readLiveStanding(bundle, body) {
|
|
131
|
+
return standingFromInfo(readLatestGameInfo(bundle, body));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Decode a GetLatestGameInfo result (the body positioned at the result-present
|
|
135
|
+
* flag) into the bundle's LatestGameInfo object; `null` when war.app sent no result.
|
|
136
|
+
*/
|
|
137
|
+
export function readLatestGameInfo(bundle, body) {
|
|
138
|
+
if (!body.bool())
|
|
139
|
+
return null;
|
|
140
|
+
const info = bundle.readClass(LATEST_GAME_INFO, body);
|
|
141
|
+
if (!isObj(info))
|
|
142
|
+
throw new LiveBoardFormatError('no LatestGameInfo');
|
|
143
|
+
// The result is the whole body: bytes left over mean the readers and the reply disagree
|
|
144
|
+
// on the layout, so what was read cannot be trusted (#131).
|
|
145
|
+
if (body.remaining !== 0)
|
|
146
|
+
throw new LiveBoardFormatError(`${body.remaining} bytes left unread after the LatestGameInfo`);
|
|
147
|
+
if (bundle.fieldsReadAs) {
|
|
148
|
+
const current = bundle.fieldsReadAs(LATEST_GAME_INFO, GAME_STANDING)[0];
|
|
149
|
+
// The info carries the current standing and the previous turn's. When the reader's source does
|
|
150
|
+
// not show which field is the current one (read in another class, through a closure or an
|
|
151
|
+
// own-class helper this check does not follow), the first one present could be last turn's:
|
|
152
|
+
// refuse rather than guess (#148).
|
|
153
|
+
if (current === undefined) {
|
|
154
|
+
throw new LiveBoardFormatError("the client's reader does not show which field holds the current standing, so last turn's could be read in its place");
|
|
155
|
+
}
|
|
156
|
+
Object.defineProperty(info, CURRENT_STANDING_FIELD, { value: current });
|
|
157
|
+
}
|
|
158
|
+
return info;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The CURRENT GameStanding of a decoded LatestGameInfo: the first field its reader fills
|
|
162
|
+
* from GameStanding (verified 2026-11-12, #20 — the info also carries the previous turn's
|
|
163
|
+
* standing, later in field order). When the bundle named that field, a withheld (null)
|
|
164
|
+
* current standing is `undefined` — never last turn's standing in its place (#131);
|
|
165
|
+
* otherwise the first GameStanding present. `undefined` when there is none; never throws.
|
|
166
|
+
*/
|
|
167
|
+
export function currentStanding(info) {
|
|
168
|
+
if (!isObj(info))
|
|
169
|
+
return undefined;
|
|
170
|
+
const field = info[CURRENT_STANDING_FIELD];
|
|
171
|
+
if (field !== undefined) {
|
|
172
|
+
const v = info[field];
|
|
173
|
+
return isType(v, GAME_STANDING) ? v : undefined;
|
|
174
|
+
}
|
|
175
|
+
return fields(info).find((v) => isType(v, GAME_STANDING));
|
|
176
|
+
}
|
|
177
|
+
/** The territories of a decoded LatestGameInfo; `null` when it carries no standing. */
|
|
178
|
+
export function standingFromInfo(info) {
|
|
179
|
+
const standing = currentStanding(info);
|
|
180
|
+
if (!standing)
|
|
181
|
+
return null;
|
|
182
|
+
for (const v of fields(standing)) {
|
|
183
|
+
const list = entries(v);
|
|
184
|
+
if (!list?.length || !list.some((e) => isType(e.value, TERRITORY_STANDING)))
|
|
185
|
+
continue;
|
|
186
|
+
const territories = list
|
|
187
|
+
.filter((e) => isType(e.value, TERRITORY_STANDING))
|
|
188
|
+
.map((e) => territoryOf(e.key, e.value));
|
|
189
|
+
checkArmies(territories);
|
|
190
|
+
return territories;
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* A board-wide sanity check on the army counts, one that a misread count cannot pass by accident
|
|
196
|
+
* (#148): when every territory of mine (fog level You) reads 0 armies, the count was read from the
|
|
197
|
+
* wrong field. A single empty territory of mine is possible (a commander standing alone, or a game
|
|
198
|
+
* without "one army stands guard"), so it proves nothing: a territory carrying a special unit is left
|
|
199
|
+
* out, and the check needs at least two of mine. Two or more all empty is not a board war.app sends.
|
|
200
|
+
*/
|
|
201
|
+
function checkArmies(territories) {
|
|
202
|
+
const mine = territories.filter((t) => t.fogLevel === FOG_LEVEL_YOU && t.armies !== null && !t.specialUnits?.length);
|
|
203
|
+
if (mine.length >= 2 && mine.every((t) => t.armies === 0)) {
|
|
204
|
+
throw new LiveBoardFormatError(`every territory of yours reads 0 armies (${mine.length} of them), so the army count was not read from the right field (if the board in war.app really shows 0 on all of them, e.g. with one-army-stands-guard off, report it: this check assumes that cannot happen)`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* A player's in-game id: GameFeed (and invite tokens) wrap it in two leading
|
|
209
|
+
* and two trailing check digits that vary per player — `id[2:-2]`.
|
|
210
|
+
*/
|
|
211
|
+
export function inGameId(player) {
|
|
212
|
+
const text = player.feedId ?? String(player.id);
|
|
213
|
+
if (!/^\d{5,}$/.test(text)) {
|
|
214
|
+
throw new McpToolError(`Player id ${text} is not a GameFeed player id (expected 5 or more digits).`, {
|
|
215
|
+
kind: 'unknown',
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return Number(text.slice(2, -2));
|
|
219
|
+
}
|
|
220
|
+
/** {@link inGameId}, or `undefined` for a player whose id is not a GameFeed id (e.g. an AI slot). */
|
|
221
|
+
export function inGameOf(player) {
|
|
222
|
+
try {
|
|
223
|
+
return inGameId(player);
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/** In-game id → GameFeed player, for the players whose ids can be derived. */
|
|
230
|
+
export function playersByInGameId(players) {
|
|
231
|
+
const out = new Map();
|
|
232
|
+
for (const p of players) {
|
|
233
|
+
try {
|
|
234
|
+
out.set(inGameId(p), p);
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
// Not a wrapped id (e.g. an AI slot): it cannot own a standing entry by it.
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
/** The owner as the analysis core sees it, players keyed by GameFeed id. */
|
|
243
|
+
export function ownerOf(t, players) {
|
|
244
|
+
if (t.fogged)
|
|
245
|
+
return { kind: 'unknown' };
|
|
246
|
+
if (t.owner === OWNER_NEUTRAL)
|
|
247
|
+
return { kind: 'neutral' };
|
|
248
|
+
if (t.owner === OWNER_PICKABLE)
|
|
249
|
+
return { kind: 'available' };
|
|
250
|
+
if (t.owner > 0)
|
|
251
|
+
return { kind: 'player', playerId: players.get(t.owner)?.id ?? t.owner };
|
|
252
|
+
return { kind: 'unknown' };
|
|
253
|
+
}
|
|
254
|
+
/** The live standing as a BoardState (verified through the bridge). */
|
|
255
|
+
export function toBoardState(territories, players) {
|
|
256
|
+
const byInGame = playersByInGameId(players);
|
|
257
|
+
return {
|
|
258
|
+
source: 'browser',
|
|
259
|
+
verified: true,
|
|
260
|
+
territories: territories.map((t) => {
|
|
261
|
+
const out = {
|
|
262
|
+
territoryId: t.territoryId,
|
|
263
|
+
owner: ownerOf(t, byInGame),
|
|
264
|
+
// territoryOf already nulls these; kept for a LiveTerritory built elsewhere.
|
|
265
|
+
armies: t.fogLevel === FOG_LEVEL_OWNER_ONLY ? null : t.armies,
|
|
266
|
+
fogLevel: FOG_LEVEL_NAMES[t.fogLevel] ?? String(t.fogLevel),
|
|
267
|
+
};
|
|
268
|
+
if (t.specialUnits?.length) {
|
|
269
|
+
out.specialUnits = t.specialUnits.map((u) => ({
|
|
270
|
+
kind: u.kind,
|
|
271
|
+
id: u.id,
|
|
272
|
+
playerId: byInGame.get(u.owner)?.id ?? u.owner,
|
|
273
|
+
}));
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}),
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The live standing as the analysis `BoardState`, seen by `me`: my territories
|
|
281
|
+
* are keyed by my GameFeed id (the id the planner takes as `me`), other known
|
|
282
|
+
* players by theirs, unknown players by their in-game id. Fogged territories
|
|
283
|
+
* are owner `unknown` with `null` armies.
|
|
284
|
+
*/
|
|
285
|
+
export function boardStateFromLive(territories, me, players = []) {
|
|
286
|
+
return toBoardState(territories, [...players.filter((p) => p.id !== me.id), me]);
|
|
287
|
+
}
|