@chrischall/warapp-mcp 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. package/.claude-plugin/marketplace.json +4 -4
  2. package/.claude-plugin/plugin.json +2 -2
  3. package/README.md +211 -9
  4. package/dist/analysis/accept-surrender.js +56 -0
  5. package/dist/analysis/award.js +159 -0
  6. package/dist/analysis/bonuses.js +98 -0
  7. package/dist/analysis/breach.js +62 -0
  8. package/dist/analysis/calibrate.js +239 -0
  9. package/dist/analysis/card-support.js +329 -0
  10. package/dist/analysis/card-threats.js +234 -0
  11. package/dist/analysis/cards-in-effect.js +130 -0
  12. package/dist/analysis/chains.js +59 -0
  13. package/dist/analysis/chokepoints.js +140 -0
  14. package/dist/analysis/combat.js +231 -0
  15. package/dist/analysis/commander-safety.js +149 -0
  16. package/dist/analysis/commitments.js +338 -0
  17. package/dist/analysis/exchange.js +155 -0
  18. package/dist/analysis/finished-game.js +136 -0
  19. package/dist/analysis/game-lessons.js +217 -0
  20. package/dist/analysis/graph.js +82 -0
  21. package/dist/analysis/guard-off.js +54 -0
  22. package/dist/analysis/guess.js +351 -0
  23. package/dist/analysis/headroom.js +241 -0
  24. package/dist/analysis/hold-diff.js +64 -0
  25. package/dist/analysis/how-lost.js +130 -0
  26. package/dist/analysis/index.js +14 -0
  27. package/dist/analysis/local-deploy.js +179 -0
  28. package/dist/analysis/lookahead.js +108 -0
  29. package/dist/analysis/opponent-profile.js +552 -0
  30. package/dist/analysis/opponents.js +1253 -0
  31. package/dist/analysis/outlook.js +695 -0
  32. package/dist/analysis/overrides.js +60 -0
  33. package/dist/analysis/picks.js +1879 -0
  34. package/dist/analysis/plan/after-leftover.js +140 -0
  35. package/dist/analysis/plan/cap-spend.js +242 -0
  36. package/dist/analysis/plan/captures.js +1195 -0
  37. package/dist/analysis/plan/commander.js +236 -0
  38. package/dist/analysis/plan/completed.js +29 -0
  39. package/dist/analysis/plan/conceded-out.js +131 -0
  40. package/dist/analysis/plan/estimates.js +715 -0
  41. package/dist/analysis/plan/guard-off.js +232 -0
  42. package/dist/analysis/plan/holds.js +1631 -0
  43. package/dist/analysis/plan/leftover.js +760 -0
  44. package/dist/analysis/plan/move-order.js +319 -0
  45. package/dist/analysis/plan/orders.js +317 -0
  46. package/dist/analysis/plan/relations.js +702 -0
  47. package/dist/analysis/plan/report.js +672 -0
  48. package/dist/analysis/plan/shared.js +1405 -0
  49. package/dist/analysis/plan/stand-still.js +823 -0
  50. package/dist/analysis/plan/state.js +44 -0
  51. package/dist/analysis/plan/strike.js +1416 -0
  52. package/dist/analysis/plan/targets.js +651 -0
  53. package/dist/analysis/plan/team.js +299 -0
  54. package/dist/analysis/plan/threats.js +1001 -0
  55. package/dist/analysis/plan/transfers.js +328 -0
  56. package/dist/analysis/plan/util.js +5 -0
  57. package/dist/analysis/plan/vacated.js +285 -0
  58. package/dist/analysis/planner.js +2927 -0
  59. package/dist/analysis/player-record.js +144 -0
  60. package/dist/analysis/player.js +268 -0
  61. package/dist/analysis/profile.js +347 -0
  62. package/dist/analysis/relations.js +1030 -0
  63. package/dist/analysis/schedule.js +88 -0
  64. package/dist/analysis/since-last.js +50 -0
  65. package/dist/analysis/static-seats.js +71 -0
  66. package/dist/analysis/stress.js +584 -0
  67. package/dist/analysis/team-brief.js +151 -0
  68. package/dist/analysis/tripwire.js +644 -0
  69. package/dist/analysis/turn-sim.js +259 -0
  70. package/dist/analysis/whereabouts.js +1112 -0
  71. package/dist/board.js +105 -0
  72. package/dist/bundle.js +121082 -36148
  73. package/dist/calibration-source.js +75 -0
  74. package/dist/cards.js +435 -0
  75. package/dist/chat-cursor.js +135 -0
  76. package/dist/chat.js +265 -0
  77. package/dist/client.js +439 -0
  78. package/dist/config.js +62 -0
  79. package/dist/direct/link.js +102 -0
  80. package/dist/direct/session.js +267 -0
  81. package/dist/direct/transport.js +424 -0
  82. package/dist/finished-store.js +357 -0
  83. package/dist/guess-salt.js +65 -0
  84. package/dist/hosted.js +24 -0
  85. package/dist/index.js +33 -0
  86. package/dist/join-color.js +140 -0
  87. package/dist/lessons-store.js +356 -0
  88. package/dist/live.js +287 -0
  89. package/dist/map-details.js +127 -0
  90. package/dist/map-stats.js +160 -0
  91. package/dist/map-store.js +242 -0
  92. package/dist/move-order.js +141 -0
  93. package/dist/my-games.js +218 -0
  94. package/dist/normalize.js +443 -0
  95. package/dist/notifications.js +143 -0
  96. package/dist/open-games.js +537 -0
  97. package/dist/outlook-log.js +165 -0
  98. package/dist/path-lock.js +22 -0
  99. package/dist/pick-list.js +193 -0
  100. package/dist/player-record-store.js +231 -0
  101. package/dist/player-states.js +31 -0
  102. package/dist/profile-store.js +727 -0
  103. package/dist/protocol/bundle.js +375 -0
  104. package/dist/protocol/codec.js +556 -0
  105. package/dist/protocol/decoded.js +12 -0
  106. package/dist/protocol/interp.js +1904 -0
  107. package/dist/protocol/methods.js +615 -0
  108. package/dist/protocol/rpc.js +202 -0
  109. package/dist/push/listener.js +393 -0
  110. package/dist/push/packets.js +66 -0
  111. package/dist/push/socketio.js +333 -0
  112. package/dist/push/websocket.js +179 -0
  113. package/dist/redact.js +62 -0
  114. package/dist/relations-store.js +242 -0
  115. package/dist/replay.js +227 -0
  116. package/dist/scalars.js +28 -0
  117. package/dist/scout.js +346 -0
  118. package/dist/stats.js +291 -0
  119. package/dist/strategy/card-clock.js +185 -0
  120. package/dist/strategy/map-notes.js +128 -0
  121. package/dist/strategy/notes.js +2223 -0
  122. package/dist/surrender-pending.js +126 -0
  123. package/dist/team-cards.js +84 -0
  124. package/dist/team-orders.js +150 -0
  125. package/dist/teams.js +19 -0
  126. package/dist/tools/accept-surrender.js +311 -0
  127. package/dist/tools/award.js +206 -0
  128. package/dist/tools/begin-turn.js +162 -0
  129. package/dist/tools/board.js +77 -0
  130. package/dist/tools/card-advice.js +256 -0
  131. package/dist/tools/card-effects.js +49 -0
  132. package/dist/tools/card-plays.js +434 -0
  133. package/dist/tools/card-support-advice.js +140 -0
  134. package/dist/tools/chat-inbox.js +158 -0
  135. package/dist/tools/chat-read-mark.js +179 -0
  136. package/dist/tools/chat.js +525 -0
  137. package/dist/tools/check-plan.js +689 -0
  138. package/dist/tools/commander-check.js +103 -0
  139. package/dist/tools/commitments.js +216 -0
  140. package/dist/tools/common.js +143 -0
  141. package/dist/tools/diplomacy.js +342 -0
  142. package/dist/tools/end-of-game.js +384 -0
  143. package/dist/tools/game.js +151 -0
  144. package/dist/tools/guess-view.js +56 -0
  145. package/dist/tools/healthcheck.js +90 -0
  146. package/dist/tools/hints.js +34 -0
  147. package/dist/tools/how-lost.js +154 -0
  148. package/dist/tools/index.js +152 -0
  149. package/dist/tools/join-game.js +327 -0
  150. package/dist/tools/leave-game.js +181 -0
  151. package/dist/tools/live-context.js +339 -0
  152. package/dist/tools/live.js +404 -0
  153. package/dist/tools/map-details.js +88 -0
  154. package/dist/tools/map-stats.js +130 -0
  155. package/dist/tools/map.js +141 -0
  156. package/dist/tools/my-games.js +193 -0
  157. package/dist/tools/notifications.js +484 -0
  158. package/dist/tools/open-games.js +104 -0
  159. package/dist/tools/opponents.js +163 -0
  160. package/dist/tools/order-options.js +135 -0
  161. package/dist/tools/past-games.js +94 -0
  162. package/dist/tools/picks.js +305 -0
  163. package/dist/tools/profile-hook.js +156 -0
  164. package/dist/tools/profile-opponents.js +121 -0
  165. package/dist/tools/profile-reads.js +99 -0
  166. package/dist/tools/push.js +445 -0
  167. package/dist/tools/relations.js +611 -0
  168. package/dist/tools/resolve.js +45 -0
  169. package/dist/tools/route.js +36 -0
  170. package/dist/tools/scout.js +87 -0
  171. package/dist/tools/session.js +163 -0
  172. package/dist/tools/strategy.js +257 -0
  173. package/dist/tools/stress-opponents.js +83 -0
  174. package/dist/tools/surrender-pending.js +74 -0
  175. package/dist/tools/surrender.js +247 -0
  176. package/dist/tools/survival.js +189 -0
  177. package/dist/tools/team-brief.js +266 -0
  178. package/dist/tools/team-source.js +222 -0
  179. package/dist/tools/team.js +178 -0
  180. package/dist/tools/turn.js +2825 -0
  181. package/dist/tools/uncommit.js +160 -0
  182. package/dist/tools/where.js +300 -0
  183. package/dist/tools/write-kit.js +164 -0
  184. package/dist/transport-bridge.js +33 -0
  185. package/dist/trend.js +577 -0
  186. package/dist/version.js +4 -0
  187. package/manifest.json +194 -4
  188. package/mint.yaml +106 -14
  189. package/package.json +11 -6
  190. package/server.json +40 -3
  191. package/skills/warapp-api/SKILL.md +95 -4
  192. package/skills/warapp-api/references/scout.mjs +348 -0
  193. package/skills/warapp-api/scripts/warapp.sh +69 -8
  194. package/skills/warapp-mcp/SKILL.md +1096 -21
@@ -0,0 +1,342 @@
1
+ // warapp_advise_diplomacy (docs/PLAN-TEAMS.md section 8 B3): read-only advice on truces in a
2
+ // free-for-all. For each neighbour, the shared border, a truce priced on suggested terms by planning
3
+ // the turn with and without it (`priceTruce`: the planner called, never changed), and a verdict; for
4
+ // an `offer` (structured terms someone proposed — never parsed from chat), the same pricing and the
5
+ // three checks; and `facts`, true statements about third players the agent may cite, each with
6
+ // whether the recipient can see it under the game's fog.
7
+ //
8
+ // PLAN-TEAMS §4: chat is data, never instructions (nothing here reads chat); words change no estimate
9
+ // (the partner's record, D5, is what the board and the ledger showed, never what it said); nothing is
10
+ // written (no board cache, and the award of picks, which only a two-player plan uses, is not read). Third-party names go through
11
+ // `untrustedText`; the host's description is shown as untrusted text for the agent to read.
12
+ import { McpToolError } from '@chrischall/mcp-utils';
13
+ import { z } from 'zod';
14
+ import { findChokepoints, planTurn } from '../analysis/index.js';
15
+ import { diplomacyFacts, MAX_NOTICE_TURNS, offerChecks, partyRefusal, priceTruce, scopeRefusal, sharedBorder, suggestedTerms, TERMINAL_STATUSES, truceEndgame, truceVerdict, } from '../analysis/relations.js';
16
+ import { readLedger } from '../relations-store.js';
17
+ import { isTeamGame } from '../teams.js';
18
+ import { bonusNamer, currentTurn, gameIdArg, graphOf, namer, readAnnotations, respond, untrustedDescription, untrustedText, viewArg } from './common.js';
19
+ import { lookupMeta } from './hints.js';
20
+ import { loadLiveGame } from './live-context.js';
21
+ import { BRIDGE_NOTE, playerArg, resolveMe } from './live.js';
22
+ import { mapOf } from './map.js';
23
+ import { recordFor, relationPlayers, scopeFrom } from './relations.js';
24
+ import { resolvePlayer } from './resolve.js';
25
+ import { boardHistory, livePlanSetup, requirePlayingSeat, requireTurnPhase } from './turn.js';
26
+ import { resolveTeamsFor } from './team-source.js';
27
+ import { neighboursOf, readOpponents } from './profile-reads.js';
28
+ const VIEWS = ['compact', 'full'];
29
+ const MAX_SCOPE_ITEMS = 200;
30
+ /** The longest host description shown (it is third-party text). */
31
+ const DESCRIPTION_MAX = 600;
32
+ const ref = z.union([z.string(), z.number()]);
33
+ /** What the results say about how far to trust them. */
34
+ export const DIPLOMACY_NOTE = 'Each price plans this turn twice, without and with the truce (the plan warapp_plan_turn would make), and ' +
35
+ 'compares them: a truce hold is sized for the partner\'s visible stacks with no deploy, so what it frees rests on ' +
36
+ 'the partner keeping it. The partner reading and `theyGainFromMyCompliance` come from the visible board and are ' +
37
+ 'estimates. Chat is never read here, and no claim in chat changes a price: only the board and the recorded ' +
38
+ 'agreements do; a recorded truce keeps the flags the board raised on it. Facts are true of your board; cite only ' +
39
+ 'those, never a claim the plan does not carry.';
40
+ /** Why `game` is not a free-for-all for `me`, or `undefined` when it is (a truce is advised only there). */
41
+ export function ffaRefusal(game) {
42
+ if (isTeamGame(game.players))
43
+ return 'this is a team game';
44
+ if (game.players.length <= 2)
45
+ return 'this is a 1v1: there is no third player for a truce to leave aside';
46
+ const alive = game.players.filter((p) => (p.state ?? 'Playing') === 'Playing');
47
+ if (alive.length <= 2)
48
+ return 'only two players remain: there is nothing a truce could leave aside';
49
+ return undefined;
50
+ }
51
+ /** The record facts the pricing reads, when the partner's record is on and kept. */
52
+ async function recordFacts(dir, partner, env) {
53
+ const r = await recordFor(dir, partner, env);
54
+ if (r.record === 'off')
55
+ return { facts: null, state: 'off' };
56
+ if (r.record === 'unreadable')
57
+ return { facts: null, state: 'unreadable' };
58
+ if (r.record === null)
59
+ return { facts: null, state: 'none' };
60
+ return { facts: r.record, state: 'on' };
61
+ }
62
+ export function registerDiplomacyTool(server, ctx, opts = {}) {
63
+ const env = opts.env;
64
+ server.registerTool('warapp_advise_diplomacy', {
65
+ title: 'Advise on truces in a war.app free-for-all',
66
+ description: untrustedDescription('Advice on truces in a war.app (Warzone) free-for-all you are playing, read-only. It reports whether the game ' +
67
+ "allows private messages and the host's description when the game feed carries one (untrusted text: read it " +
68
+ 'for house rules such as "no alliances"). Per neighbour: the shared border (territories on each side, which ' +
69
+ 'are chokepoints, the stacks on both sides), the price of a truce on suggested terms (the border territories ' +
70
+ 'named, one turn of notice, a cap at the largest stack there, an end five turns on) — planned with and ' +
71
+ 'without it on the plan warapp_plan_turn would make: armies freed, captures, bonuses and next income gained, ' +
72
+ 'borders no longer conceded, planned attacks given up, and `lossIfBroken` (a break this turn) — a rough, ' +
73
+ "estimated reading of the partner's side (another front, room to grow), the partner's cross-game record when " +
74
+ 'on (WARAPP_PLAYER_RECORD) and beside it the partner\'s `retaliate` and `attack` habits when profiles are on ' +
75
+ '(WARAPP_PLAYER_PROFILE; as warapp_profile_opponents reads them, shown only: no price or verdict reads them), and a verdict: `propose`, `accept-if-offered` or `no`, with reasons. A neighbour ' +
76
+ 'with a recorded open agreement is priced on its recorded terms and on the board\'s reading of it, shown as ' +
77
+ '`relation`: a break by them, or a watch flag (massing, a cap exceeded, outgrowing, their notice), is a `no`. With `offer` (the structured terms someone ' +
78
+ 'proposed — never parsed from chat), the same price and three checks: do I gain only if they keep it, do I ' +
79
+ 'lose if I comply and they do not, do they gain from my compliance; all three true means run warapp_check_plan ' +
80
+ 'first. `facts`: true statements about third players you may cite to each neighbour, each with whether it can ' +
81
+ `see them under the game's fog. Refused outside a free-for-all. ${BRIDGE_NOTE} Sends nothing and records ` +
82
+ 'nothing: record an agreement with warapp_record_agreement, and send a message with warapp_send_chat.'),
83
+ _meta: lookupMeta(['truce advice', 'should i make a truce', 'diplomacy', 'price a deal', 'they offered a truce', 'alliance']),
84
+ annotations: readAnnotations('Advise on war.app truces'),
85
+ inputSchema: z.object({
86
+ game_id: gameIdArg,
87
+ player: playerArg,
88
+ offer: z
89
+ .object({
90
+ with: ref.describe('Who proposed it (name or GameFeed id).'),
91
+ territories: z.array(ref).max(MAX_SCOPE_ITEMS).optional().describe('Only these territories (names or ids) are covered.'),
92
+ bonuses: z.array(ref).max(MAX_SCOPE_ITEMS).optional().describe("Only these bonuses' territories (names or ids) are covered."),
93
+ notice_turns: z.coerce.number().int().min(0).max(MAX_NOTICE_TURNS).optional().describe('Turns of notice before it ends (default 1).'),
94
+ border_cap: z.coerce.number().int().min(0).optional().describe('The most armies either side keeps on a territory of the covered border.'),
95
+ until_turn: z.coerce.number().int().min(1).optional().describe('The last turn it binds.'),
96
+ })
97
+ .optional()
98
+ .describe('Terms someone proposed, as structured terms (omit both territories and bonuses to cover everything).'),
99
+ view: viewArg(VIEWS, 'compact (default) gives each neighbour\'s border, price, verdict and facts; "full" adds the partner reading, the loss if broken in detail, the plan\'s warnings and the record\'s facts.'),
100
+ }),
101
+ }, async ({ game_id, player, offer, view }) => {
102
+ const feed = await ctx.client.getGame(game_id, { settings: true });
103
+ requireTurnPhase(feed);
104
+ const seat = await resolveMe(ctx, feed.players, player);
105
+ requirePlayingSeat(feed, seat);
106
+ // The teams, after the seat refusals: GameFeed's, else the game list's (asked once per game, kept).
107
+ const { game, me, known: teamsKnown } = await resolveTeamsFor(ctx, feed, seat);
108
+ const ffa = ffaRefusal(game);
109
+ if (ffa)
110
+ throw new McpToolError(`No truce advice for game ${game.id}: ${ffa}. Truces are advised only in a free-for-all.`, {
111
+ hint: 'In a 1v1 every player is an opponent; in a team game, coordinate with your teammates instead.',
112
+ });
113
+ const map = mapOf(game);
114
+ const graph = graphOf(map);
115
+ const turn = currentTurn(game);
116
+ const players = relationPlayers(game.players);
117
+ // An offer is checked before any board read: a refused partner or scope reads nothing.
118
+ const offered = offer ? offerTerms(game, map, graph, me, offer) : undefined;
119
+ const [{ territories, info }, history] = await Promise.all([loadLiveGame(ctx, game), boardHistory(ctx, game, me, turn)]);
120
+ // award: false — the award of picks shapes only a two-player plan, and reading it on turn 1 writes it.
121
+ const setup = await livePlanSetup(ctx, { game, map, me, turn, territories, info, history, award: false, teamsKnown });
122
+ const input = setup.input;
123
+ // The no-deal plan, shared by every partner with no recorded relation; for one with a relation
124
+ // the input already holds its truce, so priceTruce plans the no-deal side itself.
125
+ const without = planTurn(input);
126
+ const related = new Set((input.relations ?? []).map((r) => r.player));
127
+ const board = setup.board;
128
+ const chokepoints = new Set(findChokepoints(graph).map((c) => c.territoryId));
129
+ const ledger = ctx.dataDir ? await readLedger(ctx.dataDir, game.id, me.id) : undefined;
130
+ // Part D (docs/PLAN-PROFILES.md section 11): each neighbour's `retaliate` and `attack`, beside the
131
+ // deal record. Shown only: no verdict or price reads them. Read-only (nothing kept here); none with
132
+ // profiling off or while the teams cannot be read (any player may be my teammate).
133
+ const habitReads = teamsKnown && turn !== null
134
+ ? await readOpponents({
135
+ dataDir: ctx.dataDir,
136
+ env: env ?? process.env,
137
+ game,
138
+ me,
139
+ teamsKnown,
140
+ boards: [...history.boards, { turn, board: setup.seen }],
141
+ }).catch(() => undefined)
142
+ : undefined;
143
+ const habitsOf = (partner) => {
144
+ if (!habitReads?.on)
145
+ return undefined;
146
+ const r = habitReads.reads.find((x) => x.id === partner);
147
+ return r ? { retaliate: r.estimates.retaliate, attack: r.estimates.attack } : undefined;
148
+ };
149
+ // The endgame (T3): no other opponent's income still a match for either of us — a truce then relaxes nothing.
150
+ const incomes = new Map(game.players
151
+ .filter((p) => p.id !== me.id && (p.state ?? 'Playing') === 'Playing')
152
+ .map((p) => {
153
+ const t = setup.tracks?.find((x) => x.playerId === p.id);
154
+ return [p.id, t && t.firstSeen !== null ? (t.income ?? t.visibleIncome) : null];
155
+ }));
156
+ const endgameWith = (partner) => {
157
+ const mine = setup.boardIncome;
158
+ const theirs = incomes.get(partner);
159
+ const others = [...incomes].filter(([id]) => id !== partner).map(([, v]) => v);
160
+ if (theirs == null || others.some((v) => v === null))
161
+ return false;
162
+ return truceEndgame({ mine, partner: theirs, others: others });
163
+ };
164
+ const price = async (partner, terms, agreement) => {
165
+ const record = await recordFacts(ctx.dataDir, partner, env);
166
+ const endgame = endgameWith(partner);
167
+ const priced = priceTruce(input, partner, terms, {
168
+ ...(related.has(partner) ? {} : { without }),
169
+ record: record.facts,
170
+ ...(endgame ? { trust: 'watch', watch: ['endgame'] } : {}),
171
+ ...(agreement !== undefined ? { agreement } : {}),
172
+ });
173
+ // A recorded truce keeps the board's reading (priceTruce): a breach or a watch flag is a `no`, with it as the reason.
174
+ const verdict = truceVerdict(priced, setup.income);
175
+ return { priced, verdict, recordState: record.state, related: related.has(partner) };
176
+ };
177
+ const name = namer(map);
178
+ const bonusName = bonusNamer(map);
179
+ const playerRef = (id) => {
180
+ const p = game.players.find((x) => x.id === id);
181
+ return { id, name: p ? untrustedText(p.name) : null };
182
+ };
183
+ const termsView = (t) => ({
184
+ scope: t.scope === 'all'
185
+ ? 'all'
186
+ : 'territories' in t.scope
187
+ ? { territories: t.scope.territories.map(name) }
188
+ : { bonuses: t.scope.bonuses.map(bonusName) },
189
+ noticeTurns: t.noticeTurns,
190
+ ...(t.borderCap !== undefined ? { borderCap: t.borderCap } : {}),
191
+ ...(t.untilTurn !== undefined ? { untilTurn: t.untilTurn } : {}),
192
+ });
193
+ const priceView = (p, rung) => ({
194
+ coveredMine: p.coveredMine.map(name),
195
+ coveredTheirs: p.coveredTheirs.map(name),
196
+ honoured: p.honoured,
197
+ armiesFreed: p.armiesFreed,
198
+ capturesGained: p.capturesGained.map(name),
199
+ ...(p.capturesLost.length ? { capturesLost: p.capturesLost.map(name) } : {}),
200
+ bonusesGained: p.bonusesGained,
201
+ ...(p.bonusesLost.length ? { bonusesLost: p.bonusesLost } : {}),
202
+ incomeGained: p.incomeGained,
203
+ ...(p.concededAvoided.length ? { concededAvoided: p.concededAvoided.map(name) } : {}),
204
+ ...(p.concededAdded.length ? { concededAdded: p.concededAdded.map(name) } : {}),
205
+ ...(p.attacksForgone.length ? { attacksForgone: p.attacksForgone.map(name) } : {}),
206
+ lossIfBroken: p.lossIfBroken === null
207
+ ? null
208
+ : rung === 'full'
209
+ ? { ...p.lossIfBroken, from: name(p.lossIfBroken.from), to: name(p.lossIfBroken.to), territories: p.lossIfBroken.territories.map(name) }
210
+ : {
211
+ from: name(p.lossIfBroken.from),
212
+ to: name(p.lossIfBroken.to),
213
+ territories: p.lossIfBroken.territories.length,
214
+ armies: p.lossIfBroken.armies,
215
+ income: p.lossIfBroken.bonusValue,
216
+ commanderLost: p.lossIfBroken.commanderLost,
217
+ },
218
+ reliability: p.reliability,
219
+ ...(rung === 'full'
220
+ ? {
221
+ relaxed: p.relaxed.map(name),
222
+ fullHold: p.fullHold.map((f) => ({ territory: name(f.territory), why: f.why })),
223
+ partner: { ...p.partner, otherFronts: p.partner.otherFronts.map(playerRef) },
224
+ record: p.record,
225
+ warnings: p.warnings,
226
+ }
227
+ : { partner: { estimate: true, otherFronts: p.partner.otherFronts.length, roomToGrow: p.partner.roomToGrow } }),
228
+ });
229
+ // The board's reading of a recorded truce with this partner (warapp_plan_turn's): state, trust and flags.
230
+ const relationView = (p) => ({
231
+ state: p.state,
232
+ trust: p.trust,
233
+ watch: p.watch,
234
+ ...(p.breach
235
+ ? { breach: { term: p.breach.term, turn: p.breach.turn, evidence: p.breach.evidence, territories: p.breach.territories.map(name) } }
236
+ : {}),
237
+ });
238
+ const factView = (f) => ({
239
+ about: playerRef(f.about),
240
+ text: f.text,
241
+ recipientSees: f.recipientSees,
242
+ ...(f.territory !== undefined ? { territory: name(f.territory) } : {}),
243
+ ...(f.beside !== undefined ? { beside: name(f.beside) } : {}),
244
+ });
245
+ const fog = game.settings?.fog;
246
+ const built = [];
247
+ for (const partner of neighboursOf(graph, board, me.id)) {
248
+ const facts = diplomacyFacts({ graph, board, me: me.id, recipient: partner, ...(fog !== undefined ? { fog } : {}) });
249
+ const refused = partyRefusal(players, me.id, partner);
250
+ if (refused) {
251
+ built.push({ partner, refused, facts });
252
+ continue;
253
+ }
254
+ const border = sharedBorder(graph, board, me.id, partner, chokepoints);
255
+ const open = ledger?.agreements.find((a) => a.with === partner && !TERMINAL_STATUSES.has(a.status));
256
+ const terms = open ? open.terms : suggestedTerms(border, turn);
257
+ const { priced, verdict, recordState, related: rel } = await price(partner, terms, open?.id);
258
+ built.push({ partner, ...(open ? { agreement: open.id } : {}), border, terms, priced, verdict, recordState, related: rel, facts });
259
+ }
260
+ const offerResult = offered
261
+ ? await (async () => {
262
+ const { priced, verdict, recordState, related: rel } = await price(offered.partner, offered.terms);
263
+ return { partner: offered.partner, terms: offered.terms, priced, verdict, checks: offerChecks(priced), recordState, related: rel };
264
+ })()
265
+ : undefined;
266
+ const description = game.description !== undefined ? untrustedText(game.description, DESCRIPTION_MAX) : null;
267
+ return respond(view, VIEWS, (rung) => {
268
+ const neighbours = built.map((b) => ({
269
+ player: playerRef(b.partner),
270
+ ...(b.refused
271
+ ? { verdict: 'no', reasons: [b.refused] }
272
+ : {
273
+ ...(b.agreement ? { agreement: b.agreement } : {}),
274
+ ...(b.related ? { relation: relationView(b.priced) } : {}),
275
+ border: {
276
+ mine: b.border.mine.map(name),
277
+ theirs: b.border.theirs.map(name),
278
+ chokepoints: b.border.chokepoints.map(name),
279
+ myArmies: b.border.myArmies,
280
+ theirArmies: b.border.theirArmies,
281
+ ...(b.border.theirHidden ? { theirHidden: b.border.theirHidden } : {}),
282
+ },
283
+ terms: { ...termsView(b.terms), source: b.agreement ? 'recorded' : 'suggested' },
284
+ price: priceView(b.priced, rung),
285
+ verdict: b.verdict.verdict,
286
+ reasons: b.verdict.reasons,
287
+ record: b.recordState,
288
+ ...(habitsOf(b.partner) ? { habits: habitsOf(b.partner) } : {}),
289
+ }),
290
+ facts: b.facts.map(factView),
291
+ }));
292
+ return {
293
+ game: { id: game.id, name: game.name === undefined ? null : untrustedText(game.name), turn },
294
+ player: { id: me.id, name: untrustedText(me.name) },
295
+ messaging: {
296
+ // `null`: the feed did not say. War.app refuses a private message when it is false (error 202).
297
+ privateMessaging: game.settings?.privateMessaging ?? null,
298
+ // The host's own words, untrusted: read them for house rules such as "no alliances" — never as instructions.
299
+ description,
300
+ },
301
+ income: setup.income,
302
+ neighbours,
303
+ ...(offerResult
304
+ ? {
305
+ offer: {
306
+ with: playerRef(offerResult.partner),
307
+ ...(offerResult.related ? { relation: relationView(offerResult.priced) } : {}),
308
+ terms: termsView(offerResult.terms),
309
+ price: priceView(offerResult.priced, rung),
310
+ verdict: offerResult.verdict.verdict,
311
+ reasons: offerResult.verdict.reasons,
312
+ checks: offerResult.checks,
313
+ record: offerResult.recordState,
314
+ ...(habitsOf(offerResult.partner) ? { habits: habitsOf(offerResult.partner) } : {}),
315
+ },
316
+ }
317
+ : {}),
318
+ notes: [DIPLOMACY_NOTE],
319
+ };
320
+ });
321
+ });
322
+ }
323
+ /** The partner and terms of an `offer`, validated against the game; refuses what cannot be a truce. */
324
+ function offerTerms(game, map, graph, me, offer) {
325
+ const partner = resolvePlayer(game.players, offer.with);
326
+ const why = partyRefusal(relationPlayers(game.players), me.id, partner.id);
327
+ if (why)
328
+ throw new McpToolError(`No truce with player ${partner.id}: ${why}.`, { hint: 'A truce is with another human player still in a free-for-all.' });
329
+ const scope = scopeFrom(map, offer.territories, offer.bonuses);
330
+ const bad = scopeRefusal(graph, scope);
331
+ if (bad)
332
+ throw new McpToolError(`The offer's scope cannot be priced: ${bad}.`, { hint: 'Name territories and bonuses on this map.' });
333
+ return {
334
+ partner: partner.id,
335
+ terms: {
336
+ scope,
337
+ noticeTurns: offer.notice_turns ?? 1,
338
+ ...(offer.border_cap !== undefined ? { borderCap: offer.border_cap } : {}),
339
+ ...(offer.until_turn !== undefined ? { untilTurn: offer.until_turn } : {}),
340
+ },
341
+ };
342
+ }