@chrischall/warapp-mcp 0.0.0 → 0.1.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 +199 -8
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -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/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 +159 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +551 -0
- package/dist/analysis/opponents.js +1245 -0
- package/dist/analysis/outlook.js +633 -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 +1218 -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 +927 -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 +2922 -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 +577 -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 +1109 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +117959 -36172
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +192 -0
- package/dist/client.js +361 -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/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/live.js +278 -0
- package/dist/map-stats.js +152 -0
- package/dist/map-store.js +204 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +180 -0
- package/dist/normalize.js +411 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +138 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +229 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +677 -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 +531 -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 +215 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +269 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2168 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -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 +146 -0
- package/dist/tools/chat.js +483 -0
- package/dist/tools/check-plan.js +688 -0
- package/dist/tools/commander-check.js +102 -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/game.js +126 -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 +142 -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-stats.js +117 -0
- package/dist/tools/map.js +133 -0
- package/dist/tools/my-games.js +123 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +151 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/picks.js +292 -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 +385 -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 +200 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender.js +240 -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 +182 -0
- package/dist/tools/turn.js +2761 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +275 -0
- package/dist/tools/write-kit.js +158 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +452 -0
- package/dist/version.js +4 -0
- package/manifest.json +174 -4
- package/mint.yaml +101 -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 +969 -21
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// The colour a join sends. war.app refuses AcceptOpenSeat without one (verified
|
|
2
|
+
// live: server error 1, "color is null"), so warapp_join_game resolves it the way
|
|
3
|
+
// the web client does (verified from the client bundle, docs/WEB-PROTOCOL.md):
|
|
4
|
+
// the account's preferred colour, else its secondary colour — each only when no
|
|
5
|
+
// player in the lobby has it — else a standard colour still free.
|
|
6
|
+
//
|
|
7
|
+
// The account's colours come from the page itself: every war.app page that runs
|
|
8
|
+
// the client starts it with its startup parameters (`warlight.shared.InitParameters`)
|
|
9
|
+
// as one string of two characters per byte, and those carry the signed-in
|
|
10
|
+
// `warlight.shared.Player`. The Player record is read with the bundle's own reader;
|
|
11
|
+
// only its two GameColor fields are kept (the parameters also carry the session's
|
|
12
|
+
// auth string — never read out, never reported).
|
|
13
|
+
import { Reader } from './protocol/codec.js';
|
|
14
|
+
import { fields, isObj } from './protocol/decoded.js';
|
|
15
|
+
import { GAME_COLOR_COUNT } from './protocol/methods.js';
|
|
16
|
+
const GAME_COLOR = 'warlight.shared.GameColor';
|
|
17
|
+
const PLAYER = 'warlight.shared.Player';
|
|
18
|
+
const INIT_PARAMETERS = 'warlight.shared.InitParameters';
|
|
19
|
+
/** Colours 0..23 are the standard ones (no unlock needed); the client offers these first. */
|
|
20
|
+
export const STANDARD_COLORS = 24;
|
|
21
|
+
/** Player field positions in read order (each checked for its type): the two GameColor fields. */
|
|
22
|
+
const PL = { secondary: 18, preferred: 32 };
|
|
23
|
+
const PL_MIN_FIELDS = 33;
|
|
24
|
+
/** The startup string's alphabet: `>` (62) is 0, `M` (77) is 15. */
|
|
25
|
+
const BASE = 62;
|
|
26
|
+
const RUN_RE = /[>-M]{64,}/g;
|
|
27
|
+
/** How many candidate runs a page is searched for (longest first). */
|
|
28
|
+
const MAX_CANDIDATES = 8;
|
|
29
|
+
/**
|
|
30
|
+
* The client's startup-string decoding: two characters per byte, each `code - 62`
|
|
31
|
+
* a nibble; the first carries the byte's even bits (0, 2, 4, 6), the second its odd
|
|
32
|
+
* bits (1, 3, 5, 7). `undefined` for an odd length or a character outside `>`..`M`.
|
|
33
|
+
*/
|
|
34
|
+
export function decodeStartupBytes(s) {
|
|
35
|
+
if (s.length % 2 !== 0)
|
|
36
|
+
return undefined;
|
|
37
|
+
const out = new Uint8Array(s.length / 2);
|
|
38
|
+
for (let i = 0; i < out.length; i++) {
|
|
39
|
+
const g = s.charCodeAt(2 * i) - BASE;
|
|
40
|
+
const f = s.charCodeAt(2 * i + 1) - BASE;
|
|
41
|
+
if (g < 0 || g > 15 || f < 0 || f > 15)
|
|
42
|
+
return undefined;
|
|
43
|
+
let b = 0;
|
|
44
|
+
for (let bit = 0; bit < 4; bit++) {
|
|
45
|
+
b |= ((g >> bit) & 1) << (2 * bit);
|
|
46
|
+
b |= ((f >> bit) & 1) << (2 * bit + 1);
|
|
47
|
+
}
|
|
48
|
+
out[i] = b;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
/** The inverse of {@link decodeStartupBytes} (for synthetic pages in tests). */
|
|
53
|
+
export function encodeStartupBytes(bytes) {
|
|
54
|
+
let s = '';
|
|
55
|
+
for (const b of bytes) {
|
|
56
|
+
let g = 0;
|
|
57
|
+
let f = 0;
|
|
58
|
+
for (let bit = 0; bit < 4; bit++) {
|
|
59
|
+
g |= ((b >> (2 * bit)) & 1) << bit;
|
|
60
|
+
f |= ((b >> (2 * bit + 1)) & 1) << bit;
|
|
61
|
+
}
|
|
62
|
+
s += String.fromCharCode(BASE + g, BASE + f);
|
|
63
|
+
}
|
|
64
|
+
return s;
|
|
65
|
+
}
|
|
66
|
+
/** A GameColor record's index; `null` for an absent colour; `undefined` when it is neither. */
|
|
67
|
+
export function colorIndexOf(v) {
|
|
68
|
+
if (v === null)
|
|
69
|
+
return null;
|
|
70
|
+
if (!isObj(v) || v.$type !== GAME_COLOR)
|
|
71
|
+
return undefined;
|
|
72
|
+
const [index] = fields(v);
|
|
73
|
+
return typeof index === 'number' && Number.isInteger(index) && index >= 0 && index < GAME_COLOR_COUNT
|
|
74
|
+
? index
|
|
75
|
+
: undefined;
|
|
76
|
+
}
|
|
77
|
+
function colorsOfPlayer(p) {
|
|
78
|
+
const f = fields(p);
|
|
79
|
+
if (f.length < PL_MIN_FIELDS)
|
|
80
|
+
return undefined;
|
|
81
|
+
const secondary = colorIndexOf(f[PL.secondary]);
|
|
82
|
+
const preferred = colorIndexOf(f[PL.preferred]);
|
|
83
|
+
if (secondary === undefined || preferred === undefined)
|
|
84
|
+
return undefined;
|
|
85
|
+
return { ...(preferred !== null ? { preferred } : {}), ...(secondary !== null ? { secondary } : {}) };
|
|
86
|
+
}
|
|
87
|
+
/** The account colours in one candidate startup string, or `undefined`. */
|
|
88
|
+
function colorsIn(run, bundle) {
|
|
89
|
+
const bytes = decodeStartupBytes(run);
|
|
90
|
+
if (!bytes)
|
|
91
|
+
return undefined;
|
|
92
|
+
let params;
|
|
93
|
+
try {
|
|
94
|
+
params = bundle.readClass(INIT_PARAMETERS, new Reader(bytes));
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
if (!isObj(params) || params.$type !== INIT_PARAMETERS)
|
|
100
|
+
return undefined;
|
|
101
|
+
const player = fields(params).find((v) => isObj(v) && v.$type === PLAYER);
|
|
102
|
+
return player ? colorsOfPlayer(player) : undefined;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The signed-in account's preferred and secondary colours, from the client
|
|
106
|
+
* startup parameters a war.app page carries; `undefined` when the page has none
|
|
107
|
+
* that read (signed out, or the client changed). Never throws.
|
|
108
|
+
*/
|
|
109
|
+
export function readAccountColors(html, bundle) {
|
|
110
|
+
const runs = [...html.matchAll(RUN_RE)].map((m) => m[0]);
|
|
111
|
+
runs.sort((a, b) => b.length - a.length);
|
|
112
|
+
for (const run of runs.slice(0, MAX_CANDIDATES)) {
|
|
113
|
+
// A run can pick up a neighbouring `>` (a tag's end): try it trimmed on either side.
|
|
114
|
+
const tries = run.length % 2 === 0 ? [run] : [run.slice(1), run.slice(0, -1)];
|
|
115
|
+
for (const t of tries) {
|
|
116
|
+
const colors = colorsIn(t, bundle);
|
|
117
|
+
if (colors)
|
|
118
|
+
return colors;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The colour to join with, as the web client picks it: the preferred colour when
|
|
125
|
+
* no lobby player has it, else the secondary, else the first standard colour
|
|
126
|
+
* (0..23) still free. `undefined` when every standard colour is taken.
|
|
127
|
+
*/
|
|
128
|
+
export function chooseJoinColor(account, taken) {
|
|
129
|
+
const used = new Set(taken);
|
|
130
|
+
if (account?.preferred !== undefined && !used.has(account.preferred)) {
|
|
131
|
+
return { color: account.preferred, source: 'preferred' };
|
|
132
|
+
}
|
|
133
|
+
if (account?.secondary !== undefined && !used.has(account.secondary)) {
|
|
134
|
+
return { color: account.secondary, source: 'secondary' };
|
|
135
|
+
}
|
|
136
|
+
for (let c = 0; c < STANDARD_COLORS; c++)
|
|
137
|
+
if (!used.has(c))
|
|
138
|
+
return { color: c, source: 'free' };
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
package/dist/live.js
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
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
|
+
/** In-game id → GameFeed player, for the players whose ids can be derived. */
|
|
221
|
+
export function playersByInGameId(players) {
|
|
222
|
+
const out = new Map();
|
|
223
|
+
for (const p of players) {
|
|
224
|
+
try {
|
|
225
|
+
out.set(inGameId(p), p);
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
// Not a wrapped id (e.g. an AI slot): it cannot own a standing entry by it.
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return out;
|
|
232
|
+
}
|
|
233
|
+
/** The owner as the analysis core sees it, players keyed by GameFeed id. */
|
|
234
|
+
export function ownerOf(t, players) {
|
|
235
|
+
if (t.fogged)
|
|
236
|
+
return { kind: 'unknown' };
|
|
237
|
+
if (t.owner === OWNER_NEUTRAL)
|
|
238
|
+
return { kind: 'neutral' };
|
|
239
|
+
if (t.owner === OWNER_PICKABLE)
|
|
240
|
+
return { kind: 'available' };
|
|
241
|
+
if (t.owner > 0)
|
|
242
|
+
return { kind: 'player', playerId: players.get(t.owner)?.id ?? t.owner };
|
|
243
|
+
return { kind: 'unknown' };
|
|
244
|
+
}
|
|
245
|
+
/** The live standing as a BoardState (verified through the bridge). */
|
|
246
|
+
export function toBoardState(territories, players) {
|
|
247
|
+
const byInGame = playersByInGameId(players);
|
|
248
|
+
return {
|
|
249
|
+
source: 'browser',
|
|
250
|
+
verified: true,
|
|
251
|
+
territories: territories.map((t) => {
|
|
252
|
+
const out = {
|
|
253
|
+
territoryId: t.territoryId,
|
|
254
|
+
owner: ownerOf(t, byInGame),
|
|
255
|
+
// territoryOf already nulls these; kept for a LiveTerritory built elsewhere.
|
|
256
|
+
armies: t.fogLevel === FOG_LEVEL_OWNER_ONLY ? null : t.armies,
|
|
257
|
+
fogLevel: FOG_LEVEL_NAMES[t.fogLevel] ?? String(t.fogLevel),
|
|
258
|
+
};
|
|
259
|
+
if (t.specialUnits?.length) {
|
|
260
|
+
out.specialUnits = t.specialUnits.map((u) => ({
|
|
261
|
+
kind: u.kind,
|
|
262
|
+
id: u.id,
|
|
263
|
+
playerId: byInGame.get(u.owner)?.id ?? u.owner,
|
|
264
|
+
}));
|
|
265
|
+
}
|
|
266
|
+
return out;
|
|
267
|
+
}),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The live standing as the analysis `BoardState`, seen by `me`: my territories
|
|
272
|
+
* are keyed by my GameFeed id (the id the planner takes as `me`), other known
|
|
273
|
+
* players by theirs, unknown players by their in-game id. Fogged territories
|
|
274
|
+
* are owner `unknown` with `null` armies.
|
|
275
|
+
*/
|
|
276
|
+
export function boardStateFromLive(territories, me, players = []) {
|
|
277
|
+
return toBoardState(territories, [...players.filter((p) => p.id !== me.id), me]);
|
|
278
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { historyBoards } from './trend.js';
|
|
2
|
+
/** Below this many games a rate is reported only with a warning. */
|
|
3
|
+
export const MIN_SAMPLE = 5;
|
|
4
|
+
/**
|
|
5
|
+
* Below this many players a bonus win rate is flagged as a small sample: at 5 players
|
|
6
|
+
* 4 wins (80%) is as consistent with a coin flip as with a strong start (#132).
|
|
7
|
+
*/
|
|
8
|
+
export const MIN_RATE_SAMPLE = 10;
|
|
9
|
+
const byId = (a, b) => a - b;
|
|
10
|
+
const ownedBy = (board, playerId) => new Set(board.territories
|
|
11
|
+
.filter((t) => t.owner.kind === 'player' && t.owner.playerId === playerId)
|
|
12
|
+
.map((t) => t.territoryId));
|
|
13
|
+
/** Positive bonuses with territory members (superbonus members are not counted). */
|
|
14
|
+
const scoringBonuses = (map) => map.bonuses.filter((b) => b.value > 0 && b.territoryIds.length > 0).sort((a, b) => a.id - b.id);
|
|
15
|
+
/** Reduce a finished game (with `GetHistory`) to its stats record. Never throws on a bad history. */
|
|
16
|
+
export function recordFromGame(game, map) {
|
|
17
|
+
const boards = historyBoards(game.history, game.players);
|
|
18
|
+
const start = boards.find(({ board }) => board.territories.some((t) => t.owner.kind === 'player'));
|
|
19
|
+
const later = start ? boards.filter((b) => b.turn >= start.turn) : [];
|
|
20
|
+
const bonuses = scoringBonuses(map);
|
|
21
|
+
const players = [...game.players]
|
|
22
|
+
.sort((a, b) => a.id - b.id)
|
|
23
|
+
.map((p) => {
|
|
24
|
+
const starts = start ? [...ownedBy(start.board, p.id)].sort(byId) : [];
|
|
25
|
+
let anchorBonusId = null;
|
|
26
|
+
let best = 0;
|
|
27
|
+
const startBonusIds = [];
|
|
28
|
+
for (const b of bonuses) {
|
|
29
|
+
const n = b.territoryIds.filter((t) => starts.includes(t)).length;
|
|
30
|
+
if (n > 0)
|
|
31
|
+
startBonusIds.push(b.id);
|
|
32
|
+
if (n > best) {
|
|
33
|
+
best = n;
|
|
34
|
+
anchorBonusId = b.id;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
let firstBonusTurn = null;
|
|
38
|
+
for (const { turn, board } of later) {
|
|
39
|
+
const owned = ownedBy(board, p.id);
|
|
40
|
+
if (bonuses.some((b) => b.territoryIds.every((t) => owned.has(t)))) {
|
|
41
|
+
firstBonusTurn = turn;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { playerId: p.id, won: p.state === 'Won', starts, anchorBonusId, startBonusIds, firstBonusTurn };
|
|
46
|
+
});
|
|
47
|
+
return { gameId: game.id, mapId: map.id, turnsSeen: boards.length, players };
|
|
48
|
+
}
|
|
49
|
+
const round2 = (n) => Math.round(n * 100) / 100;
|
|
50
|
+
function median(xs) {
|
|
51
|
+
if (xs.length === 0)
|
|
52
|
+
return null;
|
|
53
|
+
const s = [...xs].sort(byId);
|
|
54
|
+
const mid = Math.floor(s.length / 2);
|
|
55
|
+
return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
|
|
56
|
+
}
|
|
57
|
+
/** A record the stats can use: its history parsed and some player's starts were read. */
|
|
58
|
+
export const readableRecord = (r) => r.turnsSeen > 0 && r.players.some((p) => p.starts.length > 0);
|
|
59
|
+
function statsFor(mapId, all, top) {
|
|
60
|
+
const games = all.filter(readableRecord);
|
|
61
|
+
const unreadable = all.length - games.length;
|
|
62
|
+
const picks = new Map();
|
|
63
|
+
const anchors = new Map();
|
|
64
|
+
const turns = [];
|
|
65
|
+
const winnerTurns = [];
|
|
66
|
+
let players = 0;
|
|
67
|
+
for (const g of games) {
|
|
68
|
+
for (const p of g.players) {
|
|
69
|
+
players++;
|
|
70
|
+
for (const t of new Set(p.starts)) {
|
|
71
|
+
const s = picks.get(t) ?? { picks: 0, winnerPicks: 0 };
|
|
72
|
+
s.picks++;
|
|
73
|
+
if (p.won)
|
|
74
|
+
s.winnerPicks++;
|
|
75
|
+
picks.set(t, s);
|
|
76
|
+
}
|
|
77
|
+
for (const id of p.startBonusIds ?? (p.anchorBonusId !== null ? [p.anchorBonusId] : [])) {
|
|
78
|
+
const a = anchors.get(id) ?? { players: 0, wins: 0 };
|
|
79
|
+
a.players++;
|
|
80
|
+
if (p.won)
|
|
81
|
+
a.wins++;
|
|
82
|
+
anchors.set(id, a);
|
|
83
|
+
}
|
|
84
|
+
if (p.firstBonusTurn !== null) {
|
|
85
|
+
turns.push(p.firstBonusTurn);
|
|
86
|
+
if (p.won)
|
|
87
|
+
winnerTurns.push(p.firstBonusTurn);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const n = games.length;
|
|
92
|
+
const lowSample = n < MIN_SAMPLE;
|
|
93
|
+
const byTurn = new Map();
|
|
94
|
+
for (const t of turns)
|
|
95
|
+
byTurn.set(t, (byTurn.get(t) ?? 0) + 1);
|
|
96
|
+
const out = {
|
|
97
|
+
mapId,
|
|
98
|
+
sampleSize: n,
|
|
99
|
+
unreadableGames: unreadable,
|
|
100
|
+
lowSample,
|
|
101
|
+
mostPicked: [...picks.entries()]
|
|
102
|
+
.sort(([ta, a], [tb, b]) => b.picks - a.picks || b.winnerPicks - a.winnerPicks || ta - tb)
|
|
103
|
+
.slice(0, top)
|
|
104
|
+
.map(([territoryId, s]) => ({ territoryId, picks: s.picks, pickRate: round2(s.picks / n), winnerPicks: s.winnerPicks })),
|
|
105
|
+
winRateByStartingBonus: [...anchors.entries()]
|
|
106
|
+
.sort(([ba, a], [bb, b]) => b.players - a.players || ba - bb)
|
|
107
|
+
.map(([bonusId, a]) => ({
|
|
108
|
+
bonusId,
|
|
109
|
+
players: a.players,
|
|
110
|
+
wins: a.wins,
|
|
111
|
+
winRate: round2(a.wins / a.players),
|
|
112
|
+
lowSample: a.players < MIN_RATE_SAMPLE,
|
|
113
|
+
})),
|
|
114
|
+
firstBonusTurn: {
|
|
115
|
+
players,
|
|
116
|
+
completed: turns.length,
|
|
117
|
+
neverCompleted: players - turns.length,
|
|
118
|
+
median: median(turns),
|
|
119
|
+
byTurn: [...byTurn.entries()].sort(([a], [b]) => a - b).map(([turn, c]) => ({ turn, players: c })),
|
|
120
|
+
winnersMedian: median(winnerTurns),
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
const left = unreadable > 0
|
|
124
|
+
? ` ${unreadable} game${unreadable === 1 ? '' : 's'} whose history could not be read ${unreadable === 1 ? 'is' : 'are'} left out of every figure.`
|
|
125
|
+
: '';
|
|
126
|
+
if (lowSample) {
|
|
127
|
+
out.note =
|
|
128
|
+
`Only ${n} finished game${n === 1 ? '' : 's'} — fewer than ${MIN_SAMPLE}, so every rate here is ` +
|
|
129
|
+
`anecdotal, not a trend.${left}`;
|
|
130
|
+
}
|
|
131
|
+
else if (left)
|
|
132
|
+
out.note = left.trim();
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Per-map statistics over finished-game records, ascending by map id. Duplicate
|
|
137
|
+
* game ids count once (the first record wins).
|
|
138
|
+
*/
|
|
139
|
+
export function mapStats(games, opts = {}) {
|
|
140
|
+
const top = opts.top ?? 10;
|
|
141
|
+
const seen = new Set();
|
|
142
|
+
const byMap = new Map();
|
|
143
|
+
for (const g of [...games].sort((a, b) => a.gameId - b.gameId)) {
|
|
144
|
+
if (seen.has(g.gameId))
|
|
145
|
+
continue;
|
|
146
|
+
seen.add(g.gameId);
|
|
147
|
+
const list = byMap.get(g.mapId) ?? [];
|
|
148
|
+
list.push(g);
|
|
149
|
+
byMap.set(g.mapId, list);
|
|
150
|
+
}
|
|
151
|
+
return [...byMap.entries()].sort(([a], [b]) => a - b).map(([mapId, list]) => statsFor(mapId, list, top));
|
|
152
|
+
}
|