@chrischall/warapp-mcp 0.5.0 → 0.7.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 +133 -8
- package/dist/analysis/accept-surrender.js +20 -2
- package/dist/analysis/calibrate.js +1 -0
- package/dist/analysis/card-support.js +177 -17
- package/dist/analysis/card-threats.js +126 -14
- package/dist/analysis/chat-advice.js +182 -0
- package/dist/analysis/chat-blocks.js +106 -0
- package/dist/analysis/chat-intent.js +559 -0
- package/dist/analysis/chat-read.js +159 -0
- package/dist/analysis/decision-replay.js +279 -0
- package/dist/analysis/decision-truth.js +146 -0
- package/dist/analysis/finished-game.js +62 -3
- package/dist/analysis/game-lessons.js +15 -10
- package/dist/analysis/hold-diff.js +3 -1
- package/dist/analysis/income-trend.js +38 -0
- package/dist/analysis/intel.js +75 -0
- package/dist/analysis/opens.js +64 -0
- package/dist/analysis/opponent-profile.js +121 -10
- package/dist/analysis/opponents.js +246 -20
- package/dist/analysis/outlook.js +360 -15
- package/dist/analysis/picks.js +91 -12
- package/dist/analysis/plan/after-leftover.js +4 -1
- package/dist/analysis/plan/bonus-attempts.js +105 -0
- package/dist/analysis/plan/capture-instead.js +116 -0
- package/dist/analysis/plan/captures.js +49 -7
- package/dist/analysis/plan/conceded-out.js +13 -2
- package/dist/analysis/plan/estimates.js +203 -30
- package/dist/analysis/plan/guard-off.js +2 -1
- package/dist/analysis/plan/holds.js +110 -9
- package/dist/analysis/plan/jabs.js +113 -0
- package/dist/analysis/plan/leftover.js +393 -8
- package/dist/analysis/plan/orders.js +76 -4
- package/dist/analysis/plan/relations.js +135 -2
- package/dist/analysis/plan/report.js +69 -3
- package/dist/analysis/plan/second-sizing.js +133 -0
- package/dist/analysis/plan/shared.js +279 -27
- package/dist/analysis/plan/siege.js +303 -0
- package/dist/analysis/plan/stand-still.js +9 -5
- package/dist/analysis/plan/state.js +8 -0
- package/dist/analysis/plan/stockpile.js +104 -0
- package/dist/analysis/plan/strike.js +39 -7
- package/dist/analysis/plan/targets.js +256 -110
- package/dist/analysis/plan/threats.js +148 -10
- package/dist/analysis/plan/transfers.js +57 -6
- package/dist/analysis/plan/two-move.js +70 -0
- package/dist/analysis/plan/vacated.js +124 -2
- package/dist/analysis/planner.js +527 -49
- package/dist/analysis/profile.js +13 -2
- package/dist/analysis/since-last.js +18 -5
- package/dist/analysis/stress.js +8 -2
- package/dist/board.js +36 -2
- package/dist/bonus-attempt-store.js +55 -0
- package/dist/build-id.js +106 -0
- package/dist/bundle.js +54138 -41059
- package/dist/cards.js +7 -5
- package/dist/chat-automation.js +22 -0
- package/dist/chat.js +35 -1
- package/dist/client.js +14 -0
- package/dist/commerce.js +111 -0
- package/dist/decision-log.js +1093 -0
- package/dist/finished-store.js +5 -1
- package/dist/index.js +9 -1
- package/dist/lessons-store.js +322 -21
- package/dist/live.js +43 -0
- package/dist/move-order.js +64 -3
- package/dist/normalize.js +17 -0
- package/dist/open-games.js +36 -16
- package/dist/outlook-log.js +42 -1
- package/dist/profile-store.js +12 -2
- package/dist/protocol/methods.js +62 -5
- package/dist/protocol/rpc.js +8 -0
- package/dist/relations-store.js +38 -4
- package/dist/review-export.js +198 -0
- package/dist/siege-store.js +49 -0
- package/dist/state-bundle.js +584 -0
- package/dist/state-cli.js +71 -0
- package/dist/strategy/notes.js +56 -0
- package/dist/team-cards.js +25 -0
- package/dist/tools/accept-surrender.js +23 -9
- package/dist/tools/award.js +20 -2
- package/dist/tools/card-advice.js +93 -28
- package/dist/tools/card-plays.js +2 -2
- package/dist/tools/card-support-advice.js +37 -7
- package/dist/tools/chat-advice.js +330 -0
- package/dist/tools/chat-inbox.js +19 -4
- package/dist/tools/chat.js +106 -17
- package/dist/tools/check-plan.js +68 -12
- package/dist/tools/commerce-deploy.js +33 -0
- package/dist/tools/common.js +31 -0
- package/dist/tools/decision-capture.js +459 -0
- package/dist/tools/end-of-game.js +151 -21
- package/dist/tools/game-kind.js +92 -0
- package/dist/tools/how-lost.js +7 -2
- package/dist/tools/index.js +10 -0
- package/dist/tools/join-game.js +10 -4
- package/dist/tools/live-context.js +6 -0
- package/dist/tools/live.js +10 -1
- package/dist/tools/map-curves.js +73 -0
- package/dist/tools/notifications.js +4 -3
- package/dist/tools/opponents.js +2 -0
- package/dist/tools/past-games.js +74 -8
- package/dist/tools/picks.js +61 -10
- package/dist/tools/profile-hook.js +62 -3
- package/dist/tools/profile-opponents.js +4 -0
- package/dist/tools/push.js +2 -2
- package/dist/tools/relations.js +139 -0
- package/dist/tools/scout.js +16 -2
- package/dist/tools/state.js +232 -0
- package/dist/tools/strategy.js +3 -3
- package/dist/tools/stress-opponents.js +7 -2
- package/dist/tools/surrender-pending.js +10 -1
- package/dist/tools/surrender.js +18 -3
- package/dist/tools/survival.js +11 -3
- package/dist/tools/team-brief.js +2 -2
- package/dist/tools/team.js +12 -3
- package/dist/tools/turn.js +643 -76
- package/dist/tools/where.js +3 -1
- package/dist/version.js +1 -1
- package/manifest.json +32 -3
- package/mint.yaml +37 -4
- package/package.json +4 -4
- package/server.json +20 -2
- package/skills/warapp-mcp/SKILL.md +73 -17
package/dist/finished-store.js
CHANGED
|
@@ -36,6 +36,7 @@ import { profileFormatOf } from './analysis/opponent-profile.js';
|
|
|
36
36
|
import { writeJsonAtomic } from './map-store.js';
|
|
37
37
|
import { finishedGameKey, playerKey } from './player-record-store.js';
|
|
38
38
|
import { holdsSeat } from './player-states.js';
|
|
39
|
+
import { contractFinalOutlook } from './outlook-log.js';
|
|
39
40
|
import { withPathLock } from './path-lock.js';
|
|
40
41
|
export const MAX_FINISHED_RECORDS = 500;
|
|
41
42
|
/** The processed marks kept: ten times the record bound. */
|
|
@@ -211,6 +212,7 @@ export function finishedRecordOf(p) {
|
|
|
211
212
|
}
|
|
212
213
|
: {}),
|
|
213
214
|
...(c ? { outcome: { ...c.outcome, winners: c.outcome.winners.map(key) } } : {}),
|
|
215
|
+
...(p.kinds ? { kinds: p.kinds } : {}),
|
|
214
216
|
...(p.outlook ? { outlook: p.outlook } : {}),
|
|
215
217
|
...(p.picks !== undefined ? { picks: p.picks } : {}),
|
|
216
218
|
...(h ? { starts: keyed(h.starts) } : {}),
|
|
@@ -278,7 +280,9 @@ export async function readFinishedRecord(path) {
|
|
|
278
280
|
if (typeof state === 'string')
|
|
279
281
|
return state;
|
|
280
282
|
try {
|
|
281
|
-
|
|
283
|
+
const rec = JSON.parse(await readFile(path, 'utf8'));
|
|
284
|
+
// #454: a record written with the income lean as the outlook's state reads as even with that lean.
|
|
285
|
+
return rec.outlook ? { ...rec, outlook: contractFinalOutlook(rec.outlook) } : rec;
|
|
282
286
|
}
|
|
283
287
|
catch {
|
|
284
288
|
return 'unreadable';
|
package/dist/index.js
CHANGED
|
@@ -13,11 +13,19 @@
|
|
|
13
13
|
// user's signed-in war.app tab by the fetchproxy bridge (src/transport-bridge.ts).
|
|
14
14
|
// One bridge per process, started at boot; start() only loads the identity —
|
|
15
15
|
// the port binds on the first request.
|
|
16
|
+
//
|
|
17
|
+
// `warapp-mcp export-state --out <file>` (src/state-cli.ts) writes this install's history for
|
|
18
|
+
// warapp_import_state instead of serving; it is dispatched before the env file is read, so it never
|
|
19
|
+
// loads a credential.
|
|
16
20
|
import { loadDotenvSafely, runMcp } from '@chrischall/mcp-utils';
|
|
17
21
|
import { envFilePath } from './config.js';
|
|
22
|
+
import { runExportCli } from './state-cli.js';
|
|
18
23
|
import { registerAllTools } from './tools/index.js';
|
|
19
24
|
import { createWarappBridge } from './transport-bridge.js';
|
|
20
25
|
import { VERSION } from './version.js';
|
|
26
|
+
if (process.argv[2] === 'export-state') {
|
|
27
|
+
process.exit(await runExportCli(process.argv.slice(3)));
|
|
28
|
+
}
|
|
21
29
|
await loadDotenvSafely({ path: envFilePath() });
|
|
22
30
|
const bridge = createWarappBridge();
|
|
23
31
|
// A failed start (e.g. an unwritable identity dir) is not a boot failure: the
|
|
@@ -28,6 +36,6 @@ await runMcp({
|
|
|
28
36
|
version: VERSION,
|
|
29
37
|
tools: [(server) => registerAllTools(server, { bridge })],
|
|
30
38
|
shutdown: { onSignal: () => bridge.transport().close() },
|
|
31
|
-
banner: `[warapp-mcp] v${VERSION} — war.app (Warzone) advisor; joining, leaving or surrendering a game, accepting a surrender, Begin, picks, turn orders, withdrawing them, chat, marking notifications and chat read, deleting notifications and linking a session are confirm-gated. Set WARAPP_EMAIL and WARAPP_API_TOKEN. ` +
|
|
39
|
+
banner: `[warapp-mcp] v${VERSION} — war.app (Warzone) advisor; joining, leaving or surrendering a game, accepting a surrender, Begin, picks, turn orders, withdrawing them, chat, marking notifications and chat read, deleting notifications, importing local history and linking a session are confirm-gated. Set WARAPP_EMAIL and WARAPP_API_TOKEN. ` +
|
|
32
40
|
'This project was developed and is maintained by AI (Claude). Use at your own discretion.',
|
|
33
41
|
});
|
package/dist/lessons-store.js
CHANGED
|
@@ -3,22 +3,29 @@
|
|
|
3
3
|
// - `<dataDir>/maps-record/<mapId>.json` (version 1), one per map I finished a game on: the games
|
|
4
4
|
// kept and their outcomes, and per game (at most `MAX_MAP_ENTRIES`, the oldest dropped first, its
|
|
5
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
|
|
7
|
-
// to beat). At most `MAX_MAP_RECORDS` maps, the least recently written dropped first.
|
|
6
|
+
// touch) and income curve, and each winner's start region, income curve (#419) and income after turns 4
|
|
7
|
+
// and 8 (the pace to beat). At most `MAX_MAP_RECORDS` maps, the least recently written dropped first.
|
|
8
8
|
// - `<dataDir>/lessons.json` (version 1): per format the games, outcomes, how each lost game was lost
|
|
9
9
|
// (a cause, never who), the turn my income first went flat, and the tracker's estimates scored
|
|
10
10
|
// against the true incomes by frontier coverage (#294): aggregates only — a game's entry (at most
|
|
11
11
|
// `MAX_LESSON_ENTRIES`) holds the same counts for that game alone, so processing it again replaces
|
|
12
12
|
// its share, never adds it twice; a dropped entry's counts stay in the totals.
|
|
13
13
|
//
|
|
14
|
+
// - #437: a lottery or no-show game (`excludedKind`) is in neither store's figures: each keeps
|
|
15
|
+
// `excluded: { lottery, noShow, keys }` instead (a game counted apart once; one counted before is taken
|
|
16
|
+
// back out). Map entries keep the game's kinds and settings key and flags, and the views read only the
|
|
17
|
+
// games matching the current game's format and settings (`MapMatch`).
|
|
18
|
+
//
|
|
14
19
|
// Every game is keyed by `finishedGameKey` (an HMAC of my account and the game under the one shared
|
|
15
20
|
// salt): no game or player id, no name, no clock. Territory, bonus and map ids are the map's own,
|
|
16
21
|
// public data. 0600 files in 0700 folders, atomic writes under the in-process path lock; a file that
|
|
17
22
|
// exists but does not parse (not JSON, not an object, another version, a malformed total) is never
|
|
18
23
|
// 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
|
|
24
|
+
// Profiles and lessons INFORM ONLY (P1): no planner decision reads any of this, save the winners' curves
|
|
25
|
+
// (#419, owner decision of 2026-11-20: `mapPaceAt` / `formatPaceAt`, thin evidence's upper bound).
|
|
20
26
|
import { readdir, readFile, stat, unlink } from 'node:fs/promises';
|
|
21
27
|
import { join } from 'node:path';
|
|
28
|
+
import { GAME_KINDS } from './analysis/finished-game.js';
|
|
22
29
|
import { addCalibration, COVERAGE_BUCKETS, } from './analysis/game-lessons.js';
|
|
23
30
|
import { PROFILE_FORMATS } from './analysis/opponent-profile.js';
|
|
24
31
|
import { writeJsonAtomic } from './map-store.js';
|
|
@@ -39,6 +46,7 @@ const isFormat = (v) => PROFILE_FORMATS.includes(v);
|
|
|
39
46
|
const RESULTS = ['won', 'lost', 'surrendered', 'eliminated', 'booted', 'ended-by-vote', 'unknown'];
|
|
40
47
|
const isResult = (v) => RESULTS.includes(v);
|
|
41
48
|
const CAUSES = ['commander', 'last-territories', 'unknown', 'surrendered', 'booted', 'still-holding'];
|
|
49
|
+
const kindsOf = (v) => Array.isArray(v) && v.every((k) => GAME_KINDS.includes(k)) ? v : undefined;
|
|
42
50
|
/** Read a store: `missing`, the parsed object, or `unreadable` (exists, not JSON or not an object). */
|
|
43
51
|
async function readStore(path) {
|
|
44
52
|
let text;
|
|
@@ -64,6 +72,51 @@ const bumpCount = (c, k, by) => {
|
|
|
64
72
|
else
|
|
65
73
|
delete c[k];
|
|
66
74
|
};
|
|
75
|
+
// ---- #437: games counted apart ----
|
|
76
|
+
/** Excluded games' keys kept per store (the newest), so a game is never counted apart twice. */
|
|
77
|
+
export const MAX_EXCLUDED_KEYS = 500;
|
|
78
|
+
const isExcludedKey = (k) => isRec(k) && typeof k.key === 'string' && HASH.test(k.key) && (k.kind === 'lottery' || k.kind === 'no-show');
|
|
79
|
+
/** The excluded block as stored: absent → `null`; malformed → `undefined`. */
|
|
80
|
+
function excludedOf(v) {
|
|
81
|
+
if (v === undefined)
|
|
82
|
+
return null;
|
|
83
|
+
if (!isRec(v) || !isInt(v.lottery) || !isInt(v.noShow) || !Array.isArray(v.keys) || !v.keys.every(isExcludedKey))
|
|
84
|
+
return undefined;
|
|
85
|
+
return { lottery: v.lottery, noShow: v.noShow, keys: v.keys.map((k) => ({ key: k.key, kind: k.kind })) };
|
|
86
|
+
}
|
|
87
|
+
const bump = (e, kind, by) => ({
|
|
88
|
+
lottery: e.lottery + (kind === 'lottery' ? by : 0),
|
|
89
|
+
noShow: e.noShow + (kind === 'no-show' ? by : 0),
|
|
90
|
+
});
|
|
91
|
+
/** `ex` with `key` counted apart as `kind` (a key there under another kind moves; the same: unchanged). */
|
|
92
|
+
function withExcluded(ex, key, kind) {
|
|
93
|
+
const e = withoutExcluded(ex, key, kind) ?? { lottery: 0, noShow: 0, keys: [] };
|
|
94
|
+
if (e.keys.some((k) => k.key === key))
|
|
95
|
+
return e;
|
|
96
|
+
return { ...bump(e, kind, 1), keys: [...e.keys, { key, kind }].slice(-MAX_EXCLUDED_KEYS) };
|
|
97
|
+
}
|
|
98
|
+
/** `ex` without `key` (a game counted apart before, now counted otherwise), unless it is kept as `keep`. */
|
|
99
|
+
function withoutExcluded(ex, key, keep) {
|
|
100
|
+
const old = ex?.keys.find((k) => k.key === key);
|
|
101
|
+
if (!ex || !old || old.kind === keep)
|
|
102
|
+
return ex;
|
|
103
|
+
return { ...bump(ex, old.kind, -1), keys: ex.keys.filter((k) => k.key !== key) };
|
|
104
|
+
}
|
|
105
|
+
/** The counts a summary shows (absent when nothing was counted apart). */
|
|
106
|
+
const excludedCounts = (ex) => (ex && (ex.lottery || ex.noShow) ? { excluded: { lottery: ex.lottery, noShow: ex.noShow } } : {});
|
|
107
|
+
const SETTINGS_KEY = /^sha256:[0-9a-f]{16}$/;
|
|
108
|
+
/** A map entry's settings as stored; `undefined` when malformed. */
|
|
109
|
+
function entrySettingsOf(v) {
|
|
110
|
+
if (!isRec(v) || !(v.key === null || (typeof v.key === 'string' && SETTINGS_KEY.test(v.key))))
|
|
111
|
+
return undefined;
|
|
112
|
+
if ((v.localDeployments !== undefined && typeof v.localDeployments !== 'boolean') || (v.commanders !== undefined && typeof v.commanders !== 'boolean'))
|
|
113
|
+
return undefined;
|
|
114
|
+
return {
|
|
115
|
+
key: v.key,
|
|
116
|
+
...(typeof v.localDeployments === 'boolean' ? { localDeployments: v.localDeployments } : {}),
|
|
117
|
+
...(typeof v.commanders === 'boolean' ? { commanders: v.commanders } : {}),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
67
120
|
const regionOf = (v) => (isRec(v) && ints(v.territories) && ints(v.bonuses) ? { territories: v.territories, bonuses: v.bonuses } : undefined);
|
|
68
121
|
function mapEntryOf(v) {
|
|
69
122
|
if (!isRec(v) || typeof v.key !== 'string' || !HASH.test(v.key) || !isFormat(v.format) || !isInt(v.seats) || !isResult(v.result) || !intOrNull(v.turns))
|
|
@@ -79,8 +132,16 @@ function mapEntryOf(v) {
|
|
|
79
132
|
const r = regionOf(w);
|
|
80
133
|
if (!r || !isRec(w) || !intOrNull(w.income4) || !intOrNull(w.income8))
|
|
81
134
|
return undefined;
|
|
82
|
-
|
|
135
|
+
// #419: the winner's curve, absent on entries kept before it.
|
|
136
|
+
if (w.income !== undefined && !(Array.isArray(w.income) && w.income.every(intOrNull)))
|
|
137
|
+
return undefined;
|
|
138
|
+
winners.push({ ...r, ...(w.income !== undefined ? { income: w.income } : {}), income4: w.income4, income8: w.income8 });
|
|
83
139
|
}
|
|
140
|
+
const kinds = kindsOf(v.kinds);
|
|
141
|
+
const settings = entrySettingsOf(v.settings);
|
|
142
|
+
// An additive field that does not read leaves the entry out, as any malformed field does.
|
|
143
|
+
if ((v.kinds !== undefined && !kinds) || (v.settings !== undefined && !settings))
|
|
144
|
+
return undefined;
|
|
84
145
|
return {
|
|
85
146
|
key: v.key,
|
|
86
147
|
format: v.format,
|
|
@@ -89,6 +150,8 @@ function mapEntryOf(v) {
|
|
|
89
150
|
turns: v.turns,
|
|
90
151
|
me: { ...mine, income: m.income, income4: m.income4, income8: m.income8 },
|
|
91
152
|
winners,
|
|
153
|
+
...(kinds ? { kinds } : {}),
|
|
154
|
+
...(settings ? { settings } : {}),
|
|
92
155
|
};
|
|
93
156
|
}
|
|
94
157
|
/** The record of `mapId`: an empty one when missing, `unreadable` when it cannot be parsed. Never throws. */
|
|
@@ -100,10 +163,11 @@ export async function readMapRecord(dir, mapId) {
|
|
|
100
163
|
if (v === 'missing')
|
|
101
164
|
return { version: 1, mapId, games: 0, outcomes: {}, entries: [] };
|
|
102
165
|
const outcomes = countsOf(v.outcomes);
|
|
103
|
-
|
|
166
|
+
const excluded = excludedOf(v.excluded);
|
|
167
|
+
if (v.version !== 1 || v.mapId !== mapId || !isInt(v.games) || !outcomes || excluded === undefined)
|
|
104
168
|
return 'unreadable';
|
|
105
169
|
const entries = (Array.isArray(v.entries) ? v.entries : []).map(mapEntryOf).filter((e) => e !== undefined);
|
|
106
|
-
return { version: 1, mapId, games: v.games, outcomes, entries };
|
|
170
|
+
return { version: 1, mapId, games: v.games, outcomes, entries, ...(excluded ? { excluded } : {}) };
|
|
107
171
|
}
|
|
108
172
|
catch {
|
|
109
173
|
return 'unreadable';
|
|
@@ -128,7 +192,9 @@ export async function recordMapGame(dir, mapId, entry, keep = MAX_MAP_RECORDS) {
|
|
|
128
192
|
rec.games++;
|
|
129
193
|
bumpCount(rec.outcomes, entry.result, 1);
|
|
130
194
|
rec.entries = [...rec.entries.filter((e) => e.key !== entry.key), entry].slice(-MAX_MAP_ENTRIES);
|
|
131
|
-
|
|
195
|
+
// #437: a game counted apart before is counted as a game now.
|
|
196
|
+
const excluded = withoutExcluded(rec.excluded, entry.key);
|
|
197
|
+
await writeJsonAtomic(path, mapRecordDir(dir), { ...rec, ...(excluded ? { excluded } : {}) });
|
|
132
198
|
return 'kept';
|
|
133
199
|
});
|
|
134
200
|
if (r === 'kept')
|
|
@@ -139,6 +205,73 @@ export async function recordMapGame(dir, mapId, entry, keep = MAX_MAP_RECORDS) {
|
|
|
139
205
|
return 'failed';
|
|
140
206
|
}
|
|
141
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* #419: give a game's winners kept before #419 their income curves, from the history its end-of-game record kept
|
|
210
|
+
* (`curves`: each winner's start territories and curve). A winner is matched by its start territories, or, with
|
|
211
|
+
* one winner and one curve, as that one; a winner with a curve keeps it. `filled` when one was added, `unchanged`
|
|
212
|
+
* when none could be, `missing` without the record or the game, `failed` on an unreadable record (never
|
|
213
|
+
* overwritten) or a failed write. Never throws.
|
|
214
|
+
*/
|
|
215
|
+
export async function fillWinnerCurves(dir, mapId, key, curves) {
|
|
216
|
+
const path = mapRecordPath(dir, mapId);
|
|
217
|
+
const same = (a, b) => a.length === b.length && [...a].sort((x, y) => x - y).every((x, i) => x === [...b].sort((p, q) => p - q)[i]);
|
|
218
|
+
try {
|
|
219
|
+
return await withPathLock(path, async () => {
|
|
220
|
+
const rec = await readMapRecord(dir, mapId);
|
|
221
|
+
if (rec === 'unreadable')
|
|
222
|
+
return 'failed';
|
|
223
|
+
const e = rec.entries.find((x) => x.key === key);
|
|
224
|
+
if (!e)
|
|
225
|
+
return 'missing';
|
|
226
|
+
let filled = false;
|
|
227
|
+
e.winners = e.winners.map((w) => {
|
|
228
|
+
if (w.income)
|
|
229
|
+
return w;
|
|
230
|
+
const c = curves.find((x) => same(x.territories, w.territories)) ?? (e.winners.length === 1 && curves.length === 1 ? curves[0] : undefined);
|
|
231
|
+
if (!c)
|
|
232
|
+
return w;
|
|
233
|
+
filled = true;
|
|
234
|
+
return { territories: w.territories, bonuses: w.bonuses, income: c.income, income4: w.income4, income8: w.income8 };
|
|
235
|
+
});
|
|
236
|
+
if (!filled)
|
|
237
|
+
return 'unchanged';
|
|
238
|
+
await writeJsonAtomic(path, mapRecordDir(dir), rec);
|
|
239
|
+
return 'filled';
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
return 'failed';
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* #437: count a lottery or no-show game apart in its map's record: never in its games, outcomes or entries
|
|
248
|
+
* (a game counted there before is taken back out), once per game. `excluded`, or `failed` (an unreadable
|
|
249
|
+
* record is never overwritten; a failed write). Never throws.
|
|
250
|
+
*/
|
|
251
|
+
export async function recordMapExcluded(dir, mapId, key, kind, keep = MAX_MAP_RECORDS) {
|
|
252
|
+
const path = mapRecordPath(dir, mapId);
|
|
253
|
+
try {
|
|
254
|
+
const r = await withPathLock(path, async () => {
|
|
255
|
+
const rec = await readMapRecord(dir, mapId);
|
|
256
|
+
if (rec === 'unreadable')
|
|
257
|
+
return 'failed';
|
|
258
|
+
const old = rec.entries.find((e) => e.key === key);
|
|
259
|
+
if (old) {
|
|
260
|
+
bumpCount(rec.outcomes, old.result, -1);
|
|
261
|
+
rec.games = Math.max(0, rec.games - 1);
|
|
262
|
+
rec.entries = rec.entries.filter((e) => e.key !== key);
|
|
263
|
+
}
|
|
264
|
+
await writeJsonAtomic(path, mapRecordDir(dir), { ...rec, excluded: withExcluded(rec.excluded, key, kind) });
|
|
265
|
+
return 'excluded';
|
|
266
|
+
});
|
|
267
|
+
if (r === 'excluded')
|
|
268
|
+
await pruneMapRecords(dir, keep, path).catch(() => undefined);
|
|
269
|
+
return r;
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
return 'failed';
|
|
273
|
+
}
|
|
274
|
+
}
|
|
142
275
|
async function pruneMapRecords(dir, keep, protect) {
|
|
143
276
|
const folder = mapRecordDir(dir);
|
|
144
277
|
const files = [];
|
|
@@ -173,24 +306,136 @@ const median = (xs) => {
|
|
|
173
306
|
const m = Math.floor(s.length / 2);
|
|
174
307
|
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
175
308
|
};
|
|
176
|
-
/**
|
|
177
|
-
|
|
309
|
+
/** The match without its unset fields. */
|
|
310
|
+
const matchBy = (m) => ({
|
|
311
|
+
...(m.format !== undefined ? { format: m.format } : {}),
|
|
312
|
+
...(m.localDeployments !== undefined ? { localDeployments: m.localDeployments } : {}),
|
|
313
|
+
...(m.commanders !== undefined ? { commanders: m.commanders } : {}),
|
|
314
|
+
});
|
|
315
|
+
/** The entries `match` reads, and how many matched only because a flag was not kept. Pure. */
|
|
316
|
+
export function matchedEntries(rec, match) {
|
|
317
|
+
if (!match)
|
|
318
|
+
return { entries: rec.entries, settingsUnknown: 0 };
|
|
319
|
+
const m = matchBy(match);
|
|
320
|
+
let settingsUnknown = 0;
|
|
321
|
+
const entries = rec.entries.filter((e) => {
|
|
322
|
+
if (m.format !== undefined && e.format !== m.format)
|
|
323
|
+
return false;
|
|
324
|
+
let unknown = false;
|
|
325
|
+
for (const flag of ['localDeployments', 'commanders']) {
|
|
326
|
+
if (m[flag] === undefined)
|
|
327
|
+
continue;
|
|
328
|
+
const kept = e.settings?.[flag];
|
|
329
|
+
if (kept === undefined)
|
|
330
|
+
unknown = true;
|
|
331
|
+
else if (kept !== m[flag])
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
if (unknown)
|
|
335
|
+
settingsUnknown++;
|
|
336
|
+
return true;
|
|
337
|
+
});
|
|
338
|
+
return { entries, settingsUnknown };
|
|
339
|
+
}
|
|
340
|
+
/** The outcomes of `entries`. */
|
|
341
|
+
function outcomesOf(entries) {
|
|
342
|
+
const out = {};
|
|
343
|
+
for (const e of entries)
|
|
344
|
+
bumpCount(out, e.result, 1);
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* What a map's record says in short: games, my results, and the winners' pace beside mine (medians over the
|
|
349
|
+
* kept games). With `match` (#437), only the games of the current game's format (and settings): their games,
|
|
350
|
+
* results and pace, and `matched` says how many of the kept entries matched. `excluded`: lottery and no-show
|
|
351
|
+
* games counted apart. Pure.
|
|
352
|
+
*/
|
|
353
|
+
export function mapRecordSummary(rec, match) {
|
|
178
354
|
const of = (xs) => median(xs.filter((x) => x !== null));
|
|
179
|
-
const
|
|
355
|
+
const { entries, settingsUnknown } = matchedEntries(rec, match);
|
|
356
|
+
const winners = entries.flatMap((e) => e.winners);
|
|
180
357
|
return {
|
|
181
|
-
games: rec.games,
|
|
182
|
-
outcomes: rec.outcomes,
|
|
358
|
+
games: match ? entries.length : rec.games,
|
|
359
|
+
outcomes: match ? outcomesOf(entries) : rec.outcomes,
|
|
183
360
|
pace: {
|
|
184
361
|
winners: { income4: of(winners.map((w) => w.income4)), income8: of(winners.map((w) => w.income8)) },
|
|
185
|
-
mine: { income4: of(
|
|
362
|
+
mine: { income4: of(entries.map((e) => e.me.income4)), income8: of(entries.map((e) => e.me.income8)) },
|
|
186
363
|
},
|
|
364
|
+
...(match ? { matched: { by: matchBy(match), entries: entries.length, of: rec.entries.length, settingsUnknown } } : {}),
|
|
365
|
+
...excludedCounts(rec.excluded),
|
|
187
366
|
};
|
|
188
367
|
}
|
|
368
|
+
// ---- #419: the fog prior's pace ----
|
|
369
|
+
/**
|
|
370
|
+
* #419 (owner decision of 2026-11-20): the past games a pace needs. With fewer on this map (of this format and
|
|
371
|
+
* settings) the pace is the format's, relative to my income; with fewer there too, none.
|
|
372
|
+
*/
|
|
373
|
+
export const MIN_PACE_GAMES = 3;
|
|
374
|
+
/** A game's winner income after `afterTurn` completed turns: the highest of its winners' (`null` when none was kept). */
|
|
375
|
+
function winnerAt(e, afterTurn) {
|
|
376
|
+
let best = null;
|
|
377
|
+
for (const w of e.winners) {
|
|
378
|
+
const x = w.income?.[afterTurn];
|
|
379
|
+
if (typeof x === 'number' && (best === null || x > best))
|
|
380
|
+
best = x;
|
|
381
|
+
}
|
|
382
|
+
return best;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* #419: the median past winner's income after `afterTurn` completed turns on this map (rounded up), over the
|
|
386
|
+
* games `match` reads (#437), one figure a game (its highest winner); `null` with fewer than
|
|
387
|
+
* {@link MIN_PACE_GAMES} games that kept one. Pure.
|
|
388
|
+
*/
|
|
389
|
+
export function mapPaceAt(rec, afterTurn, match) {
|
|
390
|
+
const xs = matchedEntries(rec, match).entries.flatMap((e) => {
|
|
391
|
+
const w = winnerAt(e, afterTurn);
|
|
392
|
+
return w === null ? [] : [w];
|
|
393
|
+
});
|
|
394
|
+
if (xs.length < MIN_PACE_GAMES)
|
|
395
|
+
return null;
|
|
396
|
+
return { income: Math.ceil(median(xs) - 1e-9), games: xs.length };
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* #419: the relative per-format pace: the median, over every map's games `match` reads, of the winner's income ÷
|
|
400
|
+
* mine after `afterTurn` completed turns (a game where I earned nothing then, or either figure is missing, left
|
|
401
|
+
* out), rounded to 2 places; `null` with fewer than {@link MIN_PACE_GAMES} games. Pure.
|
|
402
|
+
*/
|
|
403
|
+
export function formatPaceAt(recs, afterTurn, match) {
|
|
404
|
+
const xs = [];
|
|
405
|
+
for (const rec of recs)
|
|
406
|
+
for (const e of matchedEntries(rec, match).entries) {
|
|
407
|
+
const w = winnerAt(e, afterTurn);
|
|
408
|
+
const m = e.me.income[afterTurn];
|
|
409
|
+
if (w !== null && typeof m === 'number' && m > 0)
|
|
410
|
+
xs.push(w / m);
|
|
411
|
+
}
|
|
412
|
+
if (xs.length < MIN_PACE_GAMES)
|
|
413
|
+
return null;
|
|
414
|
+
return { ratio: Math.round(median(xs) * 100) / 100, games: xs.length };
|
|
415
|
+
}
|
|
416
|
+
/** Every map record kept (unreadable ones left out), by map id. Never throws. */
|
|
417
|
+
export async function readMapRecords(dir) {
|
|
418
|
+
const out = [];
|
|
419
|
+
let names;
|
|
420
|
+
try {
|
|
421
|
+
names = await readdir(mapRecordDir(dir));
|
|
422
|
+
}
|
|
423
|
+
catch {
|
|
424
|
+
return out;
|
|
425
|
+
}
|
|
426
|
+
const ids = names.filter((n) => /^\d+\.json$/.test(n)).map((n) => Number(n.slice(0, -5))).sort((a, b) => a - b);
|
|
427
|
+
for (const id of ids) {
|
|
428
|
+
const r = await readMapRecord(dir, id);
|
|
429
|
+
if (r !== 'unreadable')
|
|
430
|
+
out.push(r);
|
|
431
|
+
}
|
|
432
|
+
return out;
|
|
433
|
+
}
|
|
189
434
|
/** 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) {
|
|
435
|
+
export function mapRecordStarts(rec, match) {
|
|
191
436
|
const mine = new Map();
|
|
192
437
|
const winners = new Map();
|
|
193
|
-
for (const e of rec.entries) {
|
|
438
|
+
for (const e of matchedEntries(rec, match).entries) {
|
|
194
439
|
for (const b of e.me.bonuses) {
|
|
195
440
|
const x = mine.get(b) ?? { games: 0, outcomes: {} };
|
|
196
441
|
x.games++;
|
|
@@ -238,9 +483,10 @@ function lessonEntryOf(v) {
|
|
|
238
483
|
return undefined;
|
|
239
484
|
const loss = lossOf(v.howLost);
|
|
240
485
|
const calibration = calibrationOf(v.calibration);
|
|
241
|
-
|
|
486
|
+
const kinds = kindsOf(v.kinds);
|
|
487
|
+
if (loss === undefined || !calibration || (v.kinds !== undefined && !kinds))
|
|
242
488
|
return undefined;
|
|
243
|
-
return { key: v.key, format: v.format, seats: v.seats, result: v.result, flatAfterTurn: v.flatAfterTurn, howLost: loss, calibration };
|
|
489
|
+
return { key: v.key, format: v.format, seats: v.seats, result: v.result, flatAfterTurn: v.flatAfterTurn, howLost: loss, calibration, ...(kinds ? { kinds } : {}) };
|
|
244
490
|
}
|
|
245
491
|
function totalsOf(v) {
|
|
246
492
|
if (!isRec(v) || !isInt(v.games))
|
|
@@ -261,7 +507,8 @@ export async function readLessons(dir) {
|
|
|
261
507
|
return v;
|
|
262
508
|
if (v === 'missing')
|
|
263
509
|
return { version: 1, byFormat: {}, entries: [] };
|
|
264
|
-
|
|
510
|
+
const excluded = excludedOf(v.excluded);
|
|
511
|
+
if (v.version !== 1 || !isRec(v.byFormat) || excluded === undefined)
|
|
265
512
|
return 'unreadable';
|
|
266
513
|
const byFormat = {};
|
|
267
514
|
for (const [k, t] of Object.entries(v.byFormat)) {
|
|
@@ -271,7 +518,7 @@ export async function readLessons(dir) {
|
|
|
271
518
|
byFormat[k] = totals;
|
|
272
519
|
}
|
|
273
520
|
const entries = (Array.isArray(v.entries) ? v.entries : []).map(lessonEntryOf).filter((e) => e !== undefined);
|
|
274
|
-
return { version: 1, byFormat, entries };
|
|
521
|
+
return { version: 1, byFormat, entries, ...(excluded ? { excluded } : {}) };
|
|
275
522
|
}
|
|
276
523
|
catch {
|
|
277
524
|
return 'unreadable';
|
|
@@ -313,7 +560,9 @@ export async function recordLessons(dir, entry) {
|
|
|
313
560
|
applyEntry(t, entry, 1);
|
|
314
561
|
file.byFormat[entry.format] = t;
|
|
315
562
|
file.entries = [...file.entries.filter((e) => e.key !== entry.key), entry].slice(-MAX_LESSON_ENTRIES);
|
|
316
|
-
|
|
563
|
+
// #437: a game counted apart before is counted as a game now.
|
|
564
|
+
const excluded = withoutExcluded(file.excluded, entry.key);
|
|
565
|
+
await writeJsonAtomic(path, dir, { ...file, ...(excluded ? { excluded } : {}) });
|
|
317
566
|
return 'kept';
|
|
318
567
|
});
|
|
319
568
|
}
|
|
@@ -321,6 +570,36 @@ export async function recordLessons(dir, entry) {
|
|
|
321
570
|
return 'failed';
|
|
322
571
|
}
|
|
323
572
|
}
|
|
573
|
+
/**
|
|
574
|
+
* #437: count a lottery or no-show game apart: never in a format's totals or the entries (a game counted
|
|
575
|
+
* there before has its share taken back), once per game. `excluded`, or `failed` (an unreadable store is
|
|
576
|
+
* never overwritten; a failed write). Never throws.
|
|
577
|
+
*/
|
|
578
|
+
export async function recordLessonsExcluded(dir, key, kind) {
|
|
579
|
+
const path = lessonsPath(dir);
|
|
580
|
+
try {
|
|
581
|
+
return await withPathLock(path, async () => {
|
|
582
|
+
const file = await readLessons(dir);
|
|
583
|
+
if (file === 'unreadable')
|
|
584
|
+
return 'failed';
|
|
585
|
+
const old = file.entries.find((e) => e.key === key);
|
|
586
|
+
if (old) {
|
|
587
|
+
const t = file.byFormat[old.format];
|
|
588
|
+
if (t) {
|
|
589
|
+
applyEntry(t, old, -1);
|
|
590
|
+
if (t.games <= 0)
|
|
591
|
+
delete file.byFormat[old.format];
|
|
592
|
+
}
|
|
593
|
+
file.entries = file.entries.filter((e) => e.key !== key);
|
|
594
|
+
}
|
|
595
|
+
await writeJsonAtomic(path, dir, { ...file, excluded: withExcluded(file.excluded, key, kind) });
|
|
596
|
+
return 'excluded';
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
catch {
|
|
600
|
+
return 'failed';
|
|
601
|
+
}
|
|
602
|
+
}
|
|
324
603
|
const r2 = (x) => Math.round(x * 100) / 100;
|
|
325
604
|
/** A calibration's bands summarized ({@link RatioSummary}); a band with no sample is left out. Pure. */
|
|
326
605
|
export function calibrationSummary(c) {
|
|
@@ -352,5 +631,27 @@ export function lessonsSummary(file) {
|
|
|
352
631
|
calibration: calibrationSummary(t.calibration),
|
|
353
632
|
};
|
|
354
633
|
}
|
|
355
|
-
return { games, byFormat, calibration: calibrationSummary(all) };
|
|
634
|
+
return { games, byFormat, calibration: calibrationSummary(all), ...excludedCounts(file.excluded) };
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* #436: the outlook's calibration — per coverage band, past games' tracked income ÷ the true income
|
|
638
|
+
* (geometric mean and samples): this format's band when it has `min` samples or more, else every format's
|
|
639
|
+
* band together; a band with fewer than `min` either way is left out. Pure.
|
|
640
|
+
*/
|
|
641
|
+
export function outlookCalibration(file, format, min) {
|
|
642
|
+
let all = {};
|
|
643
|
+
for (const f of PROFILE_FORMATS) {
|
|
644
|
+
const t = file.byFormat[f];
|
|
645
|
+
if (t && t.games > 0)
|
|
646
|
+
all = addCalibration(all, t.calibration);
|
|
647
|
+
}
|
|
648
|
+
const own = file.byFormat[format]?.calibration ?? {};
|
|
649
|
+
const out = {};
|
|
650
|
+
for (const k of COVERAGE_BUCKETS) {
|
|
651
|
+
const x = (own[k]?.n ?? 0) >= min ? own[k] : all[k];
|
|
652
|
+
if (!x || x.n < min)
|
|
653
|
+
continue;
|
|
654
|
+
out[k] = { geoMean: r2(Math.exp(x.logSum / x.n)), n: x.n };
|
|
655
|
+
}
|
|
656
|
+
return out;
|
|
356
657
|
}
|
package/dist/live.js
CHANGED
|
@@ -76,6 +76,19 @@ function specialUnitOf(u) {
|
|
|
76
76
|
const kind = UNIT_KINDS[u.$type] ?? 'unknown';
|
|
77
77
|
return kind === 'unknown' ? { kind, type: u.$type, id, owner } : { kind, id, owner };
|
|
78
78
|
}
|
|
79
|
+
/** The structure type of a City in a territory standing's structures dict (`StructureType2`, client 6.08.0.2). */
|
|
80
|
+
export const CITY_STRUCTURE = '1';
|
|
81
|
+
/** The cities a territory standing's structures dict (its only dict keyed by strings) holds; 0 without one. */
|
|
82
|
+
function citiesOf(values) {
|
|
83
|
+
for (const v of values) {
|
|
84
|
+
const list = entries(v);
|
|
85
|
+
if (!list?.length || !list.every((e) => typeof e.key === 'string' && typeof e.value === 'number'))
|
|
86
|
+
continue;
|
|
87
|
+
const city = list.find((e) => e.key === CITY_STRUCTURE);
|
|
88
|
+
return typeof city?.value === 'number' && city.value > 0 ? city.value : 0;
|
|
89
|
+
}
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
79
92
|
function territoryOf(key, ts) {
|
|
80
93
|
const values = fields(ts);
|
|
81
94
|
const ints = values.filter((v) => typeof v === 'number');
|
|
@@ -120,6 +133,9 @@ function territoryOf(key, ts) {
|
|
|
120
133
|
const units = armyFields.find((v) => Array.isArray(v));
|
|
121
134
|
if (!armiesHidden && units?.length)
|
|
122
135
|
out.specialUnits = units.map(specialUnitOf);
|
|
136
|
+
const cities = citiesOf(values);
|
|
137
|
+
if (cities > 0)
|
|
138
|
+
out.cities = cities;
|
|
123
139
|
return out;
|
|
124
140
|
}
|
|
125
141
|
/**
|
|
@@ -174,6 +190,31 @@ export function currentStanding(info) {
|
|
|
174
190
|
}
|
|
175
191
|
return fields(info).find((v) => isType(v, GAME_STANDING));
|
|
176
192
|
}
|
|
193
|
+
/** The resource type of gold (`ResourceType.Gold`, client 6.08.0.2). */
|
|
194
|
+
export const GOLD_RESOURCE = 1;
|
|
195
|
+
/**
|
|
196
|
+
* #332, commerce games: the gold `inGameId` holds at the start of this turn, from the current standing's Resources —
|
|
197
|
+
* its one field shaped player → (resource type → amount) (GameStanding's 7th field, client 6.08.0.2; VERIFIED live
|
|
198
|
+
* 2026-11-17: 25 and 8 in two games, each the turn's income with nothing carried). Under fog it lists only my own
|
|
199
|
+
* entry, so another player's gold is never read. `undefined` when there is no standing, no such field, two of them
|
|
200
|
+
* (never guessed), or no gold entry for that player.
|
|
201
|
+
*/
|
|
202
|
+
export function standingGold(info, inGameId) {
|
|
203
|
+
const standing = currentStanding(info);
|
|
204
|
+
if (!standing)
|
|
205
|
+
return undefined;
|
|
206
|
+
const isPair = (x) => Array.isArray(x) && x.length === 2 && typeof x[0] === 'number' && typeof x[1] === 'number';
|
|
207
|
+
const resources = fields(standing).filter((v) => {
|
|
208
|
+
const list = entries(v);
|
|
209
|
+
return (!!list?.length &&
|
|
210
|
+
list.every((e) => typeof e.key === 'number' && Array.isArray(e.value) && e.value.every(isPair)));
|
|
211
|
+
});
|
|
212
|
+
if (resources.length !== 1)
|
|
213
|
+
return undefined;
|
|
214
|
+
const mine = entries(resources[0]).find((e) => e.key === inGameId);
|
|
215
|
+
const gold = mine?.value?.find(([type]) => type === GOLD_RESOURCE);
|
|
216
|
+
return gold ? gold[1] : undefined;
|
|
217
|
+
}
|
|
177
218
|
/** The territories of a decoded LatestGameInfo; `null` when it carries no standing. */
|
|
178
219
|
export function standingFromInfo(info) {
|
|
179
220
|
const standing = currentStanding(info);
|
|
@@ -272,6 +313,8 @@ export function toBoardState(territories, players) {
|
|
|
272
313
|
playerId: byInGame.get(u.owner)?.id ?? u.owner,
|
|
273
314
|
}));
|
|
274
315
|
}
|
|
316
|
+
if (t.cities)
|
|
317
|
+
out.cities = t.cities;
|
|
275
318
|
return out;
|
|
276
319
|
}),
|
|
277
320
|
};
|