@chrischall/warapp-mcp 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/.claude-plugin/marketplace.json +3 -3
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +28 -17
  4. package/dist/analysis/accept-surrender.js +56 -0
  5. package/dist/analysis/calibrate.js +239 -0
  6. package/dist/analysis/finished-game.js +136 -0
  7. package/dist/analysis/game-lessons.js +217 -0
  8. package/dist/analysis/local-deploy.js +37 -17
  9. package/dist/analysis/opponent-profile.js +6 -5
  10. package/dist/analysis/opponents.js +10 -2
  11. package/dist/analysis/outlook.js +63 -1
  12. package/dist/analysis/plan/shared.js +194 -7
  13. package/dist/analysis/plan/threats.js +103 -29
  14. package/dist/analysis/planner.js +10 -5
  15. package/dist/analysis/stress.js +14 -7
  16. package/dist/analysis/whereabouts.js +3 -0
  17. package/dist/bundle.js +12392 -9245
  18. package/dist/calibration-source.js +75 -0
  19. package/dist/chat.js +73 -0
  20. package/dist/client.js +85 -7
  21. package/dist/finished-store.js +357 -0
  22. package/dist/index.js +1 -1
  23. package/dist/lessons-store.js +356 -0
  24. package/dist/live.js +9 -0
  25. package/dist/map-details.js +127 -0
  26. package/dist/map-stats.js +14 -6
  27. package/dist/map-store.js +44 -6
  28. package/dist/my-games.js +44 -6
  29. package/dist/normalize.js +34 -2
  30. package/dist/notifications.js +143 -0
  31. package/dist/outlook-log.js +28 -1
  32. package/dist/player-record-store.js +2 -0
  33. package/dist/profile-store.js +61 -11
  34. package/dist/protocol/methods.js +86 -2
  35. package/dist/replay.js +16 -4
  36. package/dist/stats.js +26 -4
  37. package/dist/strategy/notes.js +64 -9
  38. package/dist/surrender-pending.js +126 -0
  39. package/dist/tools/accept-surrender.js +311 -0
  40. package/dist/tools/chat-inbox.js +17 -5
  41. package/dist/tools/chat-read-mark.js +179 -0
  42. package/dist/tools/chat.js +50 -8
  43. package/dist/tools/check-plan.js +1 -0
  44. package/dist/tools/commander-check.js +1 -0
  45. package/dist/tools/end-of-game.js +384 -0
  46. package/dist/tools/game.js +31 -6
  47. package/dist/tools/index.js +15 -5
  48. package/dist/tools/live-context.js +1 -1
  49. package/dist/tools/map-details.js +88 -0
  50. package/dist/tools/map-stats.js +15 -2
  51. package/dist/tools/map.js +10 -2
  52. package/dist/tools/my-games.js +77 -7
  53. package/dist/tools/notifications.js +484 -0
  54. package/dist/tools/opponents.js +14 -2
  55. package/dist/tools/past-games.js +94 -0
  56. package/dist/tools/picks.js +15 -2
  57. package/dist/tools/profile-opponents.js +1 -1
  58. package/dist/tools/profile-reads.js +3 -3
  59. package/dist/tools/push.js +72 -12
  60. package/dist/tools/strategy.js +65 -8
  61. package/dist/tools/stress-opponents.js +1 -1
  62. package/dist/tools/surrender-pending.js +74 -0
  63. package/dist/tools/surrender.js +10 -3
  64. package/dist/tools/team.js +1 -5
  65. package/dist/tools/turn.js +79 -15
  66. package/dist/tools/where.js +42 -17
  67. package/dist/tools/write-kit.js +6 -0
  68. package/dist/trend.js +145 -20
  69. package/dist/version.js +1 -1
  70. package/manifest.json +22 -2
  71. package/mint.yaml +14 -9
  72. package/package.json +1 -1
  73. package/server.json +2 -2
  74. package/skills/warapp-mcp/SKILL.md +141 -14
package/dist/replay.js CHANGED
@@ -121,16 +121,28 @@ async function pruneGames(dir, keep) {
121
121
  for (const f of files.slice(0, excess))
122
122
  await unlink(join(folder, f.name)).catch(() => { });
123
123
  }
124
+ /**
125
+ * `game`'s raw settings without free text or names (`sanitizeSettings`, against its players' and its own
126
+ * names), the overridden bonuses re-written in their verified shape; `undefined` without settings. What the
127
+ * replay record and the end-of-game record (src/finished-store.ts) keep as the settings shape.
128
+ */
129
+ export function nameFreeSettings(game) {
130
+ const s = game.settings;
131
+ if (!s)
132
+ return undefined;
133
+ const names = [...game.players.map((p) => p.name), ...(game.name !== undefined ? [game.name] : [])];
134
+ const settings = sanitizeSettings(s.raw, names);
135
+ if (s.overriddenBonuses)
136
+ settings.OverriddenBonuses = [...s.overriddenBonuses].map(([bonusID, value]) => ({ bonusID, value }));
137
+ return settings;
138
+ }
124
139
  /** Keep the settings needed to replay `game` (name-free). `false` when it has none or the write failed; never throws. */
125
140
  export async function writeGameRecord(dir, game) {
126
141
  const s = game.settings;
127
142
  if (!s)
128
143
  return false;
129
144
  try {
130
- const names = [...game.players.map((p) => p.name), ...(game.name !== undefined ? [game.name] : [])];
131
- const settings = sanitizeSettings(s.raw, names);
132
- if (s.overriddenBonuses)
133
- settings.OverriddenBonuses = [...s.overriddenBonuses].map(([bonusID, value]) => ({ bonusID, value }));
145
+ const settings = nameFreeSettings(game);
134
146
  const derived = {
135
147
  ...(s.mods !== undefined ? { mods: s.mods } : {}),
136
148
  ...(s.commerce !== undefined ? { commerce: s.commerce } : {}),
package/dist/stats.js CHANGED
@@ -22,7 +22,8 @@ import { mkdir, readdir, readFile, rename, unlink, writeFile } from 'node:fs/pro
22
22
  import { join } from 'node:path';
23
23
  import { readEnvVar } from '@chrischall/mcp-utils';
24
24
  import { isHosted } from './hosted.js';
25
- import { readableRecord, recordFromGame } from './map-stats.js';
25
+ import { RETIRED_LAYOUTS, readableRecord, recordFromGame } from './map-stats.js';
26
+ import { historyReport, safeKeys } from './trend.js';
26
27
  // The hosted check is shared (src/hosted.ts); re-exported for existing imports.
27
28
  export { HOSTED_MARKERS, isHosted } from './hosted.js';
28
29
  /** The opt-in switch. */
@@ -31,6 +32,8 @@ export const DEFAULT_NEW_GAMES = 10;
31
32
  export const MAX_NEW_GAMES = 50;
32
33
  /** The least time between two requests the collector makes. */
33
34
  export const MIN_GAP_MS = 1000;
35
+ /** At most this many unreadable games are listed with their reason (#302). */
36
+ export const MAX_UNREADABLE_LISTED = 10;
34
37
  /** How long a game seen unfinished is skipped before it is fetched again. */
35
38
  export const UNFINISHED_RETRY_MS = 60 * 60 * 1000;
36
39
  const truthy = (v) => v !== undefined && /^(1|true|yes|on)$/i.test(v.trim());
@@ -49,6 +52,7 @@ const isRecord = (v) => {
49
52
  return (typeof r.gameId === 'number' &&
50
53
  typeof r.mapId === 'number' &&
51
54
  typeof r.turnsSeen === 'number' &&
55
+ (r.layout === undefined || typeof r.layout === 'string') &&
52
56
  Array.isArray(r.players) &&
53
57
  r.players.every((p) => !!p &&
54
58
  typeof p === 'object' &&
@@ -151,8 +155,8 @@ export class StatsCollector {
151
155
  #unfinished = new Map();
152
156
  /** Games that failed this process: tried again only after every untried game. */
153
157
  #failed = new Set();
154
- /** Finished games whose history did not parse this process: not fetched again. */
155
- #unreadable = new Set();
158
+ /** Finished games whose history did not parse this process, with why (#302): not fetched again. */
159
+ #unreadable = new Map();
156
160
  #last;
157
161
  /** The request queue: each throttle waits for the one before it, so overlapping calls never share a slot. */
158
162
  #queue = Promise.resolve();
@@ -199,6 +203,14 @@ export class StatsCollector {
199
203
  };
200
204
  // Newest first, but a game that already failed this process goes last, so a
201
205
  // permanently broken game at the head of the feed cannot eat the budget every call.
206
+ const listUnreadable = (gameId) => {
207
+ const why = this.#unreadable.get(gameId);
208
+ if (!why)
209
+ return;
210
+ const list = (res.unreadableGames ??= []);
211
+ if (list.length < MAX_UNREADABLE_LISTED)
212
+ list.push({ gameId, historyUnreadable: why });
213
+ };
202
214
  const ordered = [...ids.filter((id) => !this.#failed.has(id)), ...ids.filter((id) => this.#failed.has(id))];
203
215
  for (const id of ordered) {
204
216
  if (index.has(id)) {
@@ -207,6 +219,7 @@ export class StatsCollector {
207
219
  }
208
220
  if (this.#unreadable.has(id)) {
209
221
  res.unreadable++;
222
+ listUnreadable(id);
210
223
  continue;
211
224
  }
212
225
  const seen = this.#unfinished.get(id);
@@ -239,7 +252,16 @@ export class StatsCollector {
239
252
  throw new Error('no map');
240
253
  const record = recordFromGame(game, map);
241
254
  if (!readableRecord(record)) {
242
- this.#unreadable.add(id);
255
+ const report = historyReport(game, { needPlayerLand: true });
256
+ const keys = safeKeys(game.feedKeys ?? []);
257
+ this.#unreadable.set(id, 'unreadable' in report
258
+ ? report.unreadable
259
+ : record.layout !== undefined && RETIRED_LAYOUTS.has(record.layout)
260
+ ? // Read under a layout the live check found misnumbered (#302).
261
+ { reason: 'retired-layout', keys, turns: report.read.turns, layout: record.layout }
262
+ : // A history with player land that still yields no starts (the first board with land shows none of the game's players).
263
+ { reason: 'no-starts', keys, turns: report.read.turns });
264
+ listUnreadable(id);
243
265
  this.#failed.delete(id);
244
266
  res.unreadable++;
245
267
  continue;
@@ -259,6 +259,16 @@ export const STRATEGY_NOTES = [
259
259
  source: S.oneVsOne,
260
260
  basis: 'source',
261
261
  },
262
+ {
263
+ id: '1v1-gate-budget',
264
+ topic: 'format',
265
+ when: on({ format: ['1v1'], phase: ['opening', 'expansion', 'contact'] }),
266
+ text: 'Before you open a contact, count the single-entry gates it makes. Each must hold more than (the stack ' +
267
+ "beside it + the opponent's whole deploy) × the game's offensive kill rate, and each new gate costs that again. " +
268
+ 'Take the cheap bonuses nobody contests first, and open a gate only when your income pays for all of them.',
269
+ source: S.oneVsOne,
270
+ basis: 'derived',
271
+ },
262
272
  {
263
273
  id: 'ffa-strike-last',
264
274
  topic: 'format',
@@ -449,6 +459,17 @@ export const STRATEGY_NOTES = [
449
459
  basis: 'source',
450
460
  encoded: 'deploys are limited to each pool (src/analysis/local-deploy.ts, warapp_plan_turn)',
451
461
  },
462
+ {
463
+ id: 'settings-local-deploy-front',
464
+ topic: 'settings',
465
+ when: on({ localDeployments: true, phase: ['contact', 'late'] }),
466
+ text: "Under Local Deployment a front can receive only the opponent's free income, the pool of a bonus it holds " +
467
+ 'that the front stands in, and next turn the armies that walk up from a pooled bonus behind it. Size each ' +
468
+ 'border on that, not on its total income, which is spread over pools it cannot move.',
469
+ source: S.localDeployments,
470
+ basis: 'derived',
471
+ encoded: '#293: each border is sized on the free income plus the pools it could hold there, the stacks one step behind for next turn (warapp_plan_turn, warapp_check_plan)',
472
+ },
452
473
  {
453
474
  id: 'settings-no-split',
454
475
  topic: 'settings',
@@ -1139,6 +1160,17 @@ export const STRATEGY_NOTES = [
1139
1160
  source: S.moveOrder,
1140
1161
  basis: 'source',
1141
1162
  },
1163
+ {
1164
+ id: 'timing-read-first-mover-strike',
1165
+ topic: 'timing',
1166
+ when: on({ format: ['1v1'], moveOrder: ['Cycle', 'NoLuckCycle'], phase: ['contact', 'late'] }),
1167
+ text: 'Who moved first? At 0% luck, a first order whose target held more than any deploy the opponent could ' +
1168
+ 'have had, or whose stack had left, means the opponent ran first: an inference; test with warapp_check_plan. ' +
1169
+ 'Turns alternate: the opponent first on N, so on N+1 pass first_mover: me, only when sure. ' +
1170
+ 'Strike big on turns you move first.',
1171
+ source: S.moveOrder,
1172
+ basis: 'derived',
1173
+ },
1142
1174
  {
1143
1175
  id: 'timing-delay-pairs',
1144
1176
  topic: 'timing',
@@ -1268,6 +1300,16 @@ export const STRATEGY_NOTES = [
1268
1300
  source: S.smallEarthGuide,
1269
1301
  basis: 'source',
1270
1302
  },
1303
+ {
1304
+ id: 'fighting-contested-bonus-mass',
1305
+ topic: 'fighting',
1306
+ when: on({ phase: ['contact', 'late'] }),
1307
+ text: 'A bonus a member or two short that an enemy stack keeps retaking will not fall to a few armies a turn. ' +
1308
+ "Mass for one turn (its stack plus its owner's deploy on every missing member) or leave it. While that " +
1309
+ 'stack roams inside the bonus, garrison the members it can reach before retaking the one it left.',
1310
+ source: S.beginnerGuide,
1311
+ basis: 'derived',
1312
+ },
1271
1313
  {
1272
1314
  id: 'fighting-hidden-stack',
1273
1315
  topic: 'fighting',
@@ -1322,6 +1364,16 @@ export const STRATEGY_NOTES = [
1322
1364
  source: S.fog,
1323
1365
  basis: 'source',
1324
1366
  },
1367
+ {
1368
+ id: 'reading-split-growth',
1369
+ topic: 'reading',
1370
+ when: on({ fog: ['LightFog', 'Foggy', 'ModerateFog', 'VeryFoggy', 'ExtremeFog'], phase: ['contact', 'late'] }),
1371
+ text: "Before calling a game even, split each enemy stack's growth three ways: armies a neighbour in view could " +
1372
+ 'have moved in, armies that came out of the fog, and what can only be deploy. Only the last is income; ' +
1373
+ 'while most of its frontier is hidden, read a low estimate as a floor.',
1374
+ source: S.oneVsOne,
1375
+ basis: 'derived',
1376
+ },
1325
1377
  {
1326
1378
  id: 'reading-stack-arrival',
1327
1379
  topic: 'reading',
@@ -1869,10 +1921,10 @@ export const STRATEGY_NOTES = [
1869
1921
  id: 'ffa-surrender-veto',
1870
1922
  topic: 'format',
1871
1923
  when: on({ format: ['ffa'] }),
1872
- text: 'Where surrenders must be accepted, every other player must accept before a loser leaves; its land then ' +
1924
+ text: 'Where surrenders must be accepted, all others must accept before a loser leaves; its land then ' +
1873
1925
  'turns neutral or goes to an AI, as the game is set. Accept when that helps you more than your rivals; ' +
1874
- 'if it would mostly feed a rival, refusing keeps the player in (it must move or be booted). Accept in ' +
1875
- 'the game client.',
1926
+ 'if it would mostly feed a rival, refusing keeps the player in (it must move or be booted). ' +
1927
+ 'warapp_accept_surrender accepts (unverified live).',
1876
1928
  source: S.surrender,
1877
1929
  basis: 'derived',
1878
1930
  },
@@ -2119,15 +2171,18 @@ export function notesFor(profile, topic, context = {}) {
2119
2171
  return generalNotes(topic).filter((n) => n.when(profile, context));
2120
2172
  }
2121
2173
  /**
2122
- * A game's profile from GameFeed alone: format, settings and phase. It is seen from the
2123
- * first seated player — the format does not depend on the seat — and the phase reads
2124
- * GameFeed's standing when there is one (without it no contact or territory-share late
2125
- * game can be seen). The standing is `unknown`: it needs income estimates, which
2126
- * warapp_plan_turn makes.
2174
+ * A game's profile from GameFeed's players and settings: format, settings and phase. It is seen
2175
+ * from the signed-in account's seat when `me` names a seated player, else from the first seated
2176
+ * player (#309: in a free-for-all contact is judged from a seat). The phase reads the board when
2177
+ * there is one (without it no contact or territory-share late game can be seen). The standing is
2178
+ * `unknown`: it needs income estimates, which warapp_plan_turn makes.
2127
2179
  */
2128
2180
  export function feedProfile(input) {
2129
2181
  const playing = input.players.filter((p) => (p.state ?? 'Playing') === 'Playing');
2130
- const me = (playing[0] ?? input.players[0])?.id ?? 0;
2182
+ // Only a seat still playing: an account that is out has no opponents, and judging for it would count
2183
+ // every player left as one (two left would read as a free-for-all).
2184
+ const seated = input.me !== undefined && playing.some((p) => p.id === input.me) ? input.me : undefined;
2185
+ const me = seated ?? (playing[0] ?? input.players[0])?.id ?? 0;
2131
2186
  return gameProfile({
2132
2187
  graph: input.graph,
2133
2188
  board: input.board ?? { source: 'gamefeed', territories: [], verified: false },
@@ -0,0 +1,126 @@
1
+ // A pending surrender, read from the game details (#317). Read from the client bundle locally,
2
+ // UNVERIFIED live (docs/WEB-PROTOCOL.md, "Accepting a surrender").
3
+ //
4
+ // GameFeed keeps a player who asked to surrender `Playing`, and neither GetMyGames nor
5
+ // LatestGameInfo says anything about it: only the game details do. GameRefresh (39) asked with
6
+ // `nonChat` answers `warlight.shared.GameDetailsAndChat`, whose second field is the game
7
+ // (`warlight.shared.GameWL`); the game's eighth field is its players, a dict of in-game id →
8
+ // `warlight.shared.GamePlayer`. Of a player's 36 fields this reads the AI id, "human turned into
9
+ // an AI", the in-game id, who accepted its surrender, its state and the `Surrendered` flag.
10
+ //
11
+ // The client's player reader first sets a link back to the game being read (only when it is read
12
+ // inside one), so a decoded player can carry the game as an extra leading field: such a value is
13
+ // left out before the fields are counted. Records are found by registered type name and fields by
14
+ // read order, with exact counts, so a field added or moved reads as unknown rather than wrong.
15
+ //
16
+ // Readers here never throw: a shape this server does not understand is `undefined` (unknown).
17
+ import { fields, isObj } from './protocol/decoded.js';
18
+ import { GAME_DETAILS_AND_CHAT } from './chat.js';
19
+ import { playerStateName } from './player-states.js';
20
+ export const GAME_WL = 'warlight.shared.GameWL';
21
+ export const GAME_PLAYER = 'warlight.shared.GamePlayer';
22
+ /** GameWL: its field count and where the players are, in read order. */
23
+ export const GAME_WL_FIELDS = 14;
24
+ const GW_PLAYERS = 7;
25
+ /** GamePlayer: its field count and the positions read, in read order. */
26
+ export const GAME_PLAYER_FIELDS = 36;
27
+ const GPL = { aiId: 0, turnedAI: 14, id: 20, accepted: 21, state: 28, surrendered: 29 };
28
+ /** Whether a details seat is still playing (GamePlayerState `Playing`, src/player-states.ts). */
29
+ export const seatPlaying = (s) => playerStateName(s.state) === 'Playing';
30
+ const isInt = (v) => typeof v === 'number' && Number.isInteger(v);
31
+ const optInt = (v) => v === null || isInt(v);
32
+ /** The accepted-by dict (pairs of in-game id and time) as ids; `undefined` when it is not one. */
33
+ function acceptedIds(v) {
34
+ if (v === null)
35
+ return [];
36
+ if (!Array.isArray(v))
37
+ return undefined;
38
+ const out = [];
39
+ for (const pair of v) {
40
+ if (!Array.isArray(pair) || pair.length !== 2 || !isInt(pair[0]))
41
+ return undefined;
42
+ out.push(pair[0]);
43
+ }
44
+ return out;
45
+ }
46
+ /** One decoded GamePlayer as a seat; `undefined` when a field is not what it should be. */
47
+ function seatOf(p) {
48
+ if (!isObj(p) || p.$type !== GAME_PLAYER)
49
+ return undefined;
50
+ // The link back to the game the reader may set first is not a read field.
51
+ const f = fields(p).filter((v) => !(isObj(v) && v.$type === GAME_WL));
52
+ if (f.length !== GAME_PLAYER_FIELDS)
53
+ return undefined;
54
+ const aiId = f[GPL.aiId];
55
+ const turnedAI = f[GPL.turnedAI];
56
+ const id = f[GPL.id];
57
+ const state = f[GPL.state];
58
+ const surrendered = f[GPL.surrendered];
59
+ const acceptedBy = acceptedIds(f[GPL.accepted]);
60
+ if (!optInt(aiId) || !optInt(id) || typeof turnedAI !== 'boolean' || !isInt(state) || typeof surrendered !== 'boolean')
61
+ return undefined;
62
+ if (acceptedBy === undefined)
63
+ return undefined;
64
+ const seatId = id ?? aiId;
65
+ if (seatId === null)
66
+ return undefined;
67
+ return { id: seatId, ai: aiId !== null, turnedAI, state, surrendered, acceptedBy };
68
+ }
69
+ /**
70
+ * The seats of a GameRefresh reply asked with `nonChat` (the body positioned at the result-present
71
+ * flag); `undefined` when there is no result, no game, no players, or a shape this server does not
72
+ * understand. Never throws.
73
+ */
74
+ export function readGameDetailsSeats(bundle, body) {
75
+ try {
76
+ if (!body.bool())
77
+ return undefined;
78
+ const ret = bundle.readClass(GAME_DETAILS_AND_CHAT, body);
79
+ if (!isObj(ret) || ret.$type !== GAME_DETAILS_AND_CHAT)
80
+ return undefined;
81
+ const game = fields(ret)[1];
82
+ if (!isObj(game) || game.$type !== GAME_WL)
83
+ return undefined;
84
+ const gf = fields(game);
85
+ if (gf.length !== GAME_WL_FIELDS)
86
+ return undefined;
87
+ const players = gf[GW_PLAYERS];
88
+ if (!Array.isArray(players))
89
+ return undefined;
90
+ const seats = [];
91
+ for (const pair of players) {
92
+ if (!Array.isArray(pair) || pair.length !== 2)
93
+ return undefined;
94
+ if (pair[1] === null)
95
+ continue;
96
+ const seat = seatOf(pair[1]);
97
+ if (!seat)
98
+ return undefined;
99
+ seats.push(seat);
100
+ }
101
+ return seats.length ? seats : undefined;
102
+ }
103
+ catch {
104
+ return undefined;
105
+ }
106
+ }
107
+ /**
108
+ * The surrenders pending in a game, other than my own (`me`: my in-game id, `undefined` when unknown),
109
+ * each with whether my acceptance is owed — the client's own test: the other seat `Playing` with
110
+ * `Surrendered` set, my seat `Playing` and neither an AI nor turned into one, and my id not among
111
+ * those that accepted. `feedIdOf` maps an in-game id to its GameFeed id where the caller knows it.
112
+ * In seat order (war.app's).
113
+ */
114
+ export function pendingSurrenders(seats, me, feedIdOf = () => null) {
115
+ const mine = me === undefined ? undefined : seats.find((s) => s.id === me);
116
+ const canAccept = mine !== undefined && seatPlaying(mine) && !mine.ai && !mine.turnedAI;
117
+ return seats
118
+ .filter((s) => s.surrendered && seatPlaying(s) && s.id !== me)
119
+ .map((s) => ({
120
+ inGameId: s.id,
121
+ player: feedIdOf(s.id),
122
+ owedByMe: mine === undefined ? null : canAccept && !s.acceptedBy.includes(me),
123
+ }));
124
+ }
125
+ /** Whether any of `pending` waits on my acceptance. */
126
+ export const surrenderOwed = (pending) => (pending ?? []).some((p) => p.owedByMe === true);