@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.
Files changed (177) hide show
  1. package/.claude-plugin/marketplace.json +4 -4
  2. package/.claude-plugin/plugin.json +2 -2
  3. package/README.md +199 -8
  4. package/dist/analysis/award.js +159 -0
  5. package/dist/analysis/bonuses.js +98 -0
  6. package/dist/analysis/breach.js +62 -0
  7. package/dist/analysis/card-support.js +329 -0
  8. package/dist/analysis/card-threats.js +234 -0
  9. package/dist/analysis/cards-in-effect.js +130 -0
  10. package/dist/analysis/chains.js +59 -0
  11. package/dist/analysis/chokepoints.js +140 -0
  12. package/dist/analysis/combat.js +231 -0
  13. package/dist/analysis/commander-safety.js +149 -0
  14. package/dist/analysis/commitments.js +338 -0
  15. package/dist/analysis/exchange.js +155 -0
  16. package/dist/analysis/graph.js +82 -0
  17. package/dist/analysis/guard-off.js +54 -0
  18. package/dist/analysis/guess.js +351 -0
  19. package/dist/analysis/headroom.js +241 -0
  20. package/dist/analysis/hold-diff.js +64 -0
  21. package/dist/analysis/how-lost.js +130 -0
  22. package/dist/analysis/index.js +14 -0
  23. package/dist/analysis/local-deploy.js +159 -0
  24. package/dist/analysis/lookahead.js +108 -0
  25. package/dist/analysis/opponent-profile.js +551 -0
  26. package/dist/analysis/opponents.js +1245 -0
  27. package/dist/analysis/outlook.js +633 -0
  28. package/dist/analysis/overrides.js +60 -0
  29. package/dist/analysis/picks.js +1879 -0
  30. package/dist/analysis/plan/after-leftover.js +140 -0
  31. package/dist/analysis/plan/cap-spend.js +242 -0
  32. package/dist/analysis/plan/captures.js +1195 -0
  33. package/dist/analysis/plan/commander.js +236 -0
  34. package/dist/analysis/plan/completed.js +29 -0
  35. package/dist/analysis/plan/conceded-out.js +131 -0
  36. package/dist/analysis/plan/estimates.js +715 -0
  37. package/dist/analysis/plan/guard-off.js +232 -0
  38. package/dist/analysis/plan/holds.js +1631 -0
  39. package/dist/analysis/plan/leftover.js +760 -0
  40. package/dist/analysis/plan/move-order.js +319 -0
  41. package/dist/analysis/plan/orders.js +317 -0
  42. package/dist/analysis/plan/relations.js +702 -0
  43. package/dist/analysis/plan/report.js +672 -0
  44. package/dist/analysis/plan/shared.js +1218 -0
  45. package/dist/analysis/plan/stand-still.js +823 -0
  46. package/dist/analysis/plan/state.js +44 -0
  47. package/dist/analysis/plan/strike.js +1416 -0
  48. package/dist/analysis/plan/targets.js +651 -0
  49. package/dist/analysis/plan/team.js +299 -0
  50. package/dist/analysis/plan/threats.js +927 -0
  51. package/dist/analysis/plan/transfers.js +328 -0
  52. package/dist/analysis/plan/util.js +5 -0
  53. package/dist/analysis/plan/vacated.js +285 -0
  54. package/dist/analysis/planner.js +2922 -0
  55. package/dist/analysis/player-record.js +144 -0
  56. package/dist/analysis/player.js +268 -0
  57. package/dist/analysis/profile.js +347 -0
  58. package/dist/analysis/relations.js +1030 -0
  59. package/dist/analysis/schedule.js +88 -0
  60. package/dist/analysis/since-last.js +50 -0
  61. package/dist/analysis/static-seats.js +71 -0
  62. package/dist/analysis/stress.js +577 -0
  63. package/dist/analysis/team-brief.js +151 -0
  64. package/dist/analysis/tripwire.js +644 -0
  65. package/dist/analysis/turn-sim.js +259 -0
  66. package/dist/analysis/whereabouts.js +1109 -0
  67. package/dist/board.js +105 -0
  68. package/dist/bundle.js +117959 -36172
  69. package/dist/cards.js +435 -0
  70. package/dist/chat-cursor.js +135 -0
  71. package/dist/chat.js +192 -0
  72. package/dist/client.js +361 -0
  73. package/dist/config.js +62 -0
  74. package/dist/direct/link.js +102 -0
  75. package/dist/direct/session.js +267 -0
  76. package/dist/direct/transport.js +424 -0
  77. package/dist/guess-salt.js +65 -0
  78. package/dist/hosted.js +24 -0
  79. package/dist/index.js +33 -0
  80. package/dist/join-color.js +140 -0
  81. package/dist/live.js +278 -0
  82. package/dist/map-stats.js +152 -0
  83. package/dist/map-store.js +204 -0
  84. package/dist/move-order.js +141 -0
  85. package/dist/my-games.js +180 -0
  86. package/dist/normalize.js +411 -0
  87. package/dist/open-games.js +537 -0
  88. package/dist/outlook-log.js +138 -0
  89. package/dist/path-lock.js +22 -0
  90. package/dist/pick-list.js +193 -0
  91. package/dist/player-record-store.js +229 -0
  92. package/dist/player-states.js +31 -0
  93. package/dist/profile-store.js +677 -0
  94. package/dist/protocol/bundle.js +375 -0
  95. package/dist/protocol/codec.js +556 -0
  96. package/dist/protocol/decoded.js +12 -0
  97. package/dist/protocol/interp.js +1904 -0
  98. package/dist/protocol/methods.js +531 -0
  99. package/dist/protocol/rpc.js +202 -0
  100. package/dist/push/listener.js +393 -0
  101. package/dist/push/packets.js +66 -0
  102. package/dist/push/socketio.js +333 -0
  103. package/dist/push/websocket.js +179 -0
  104. package/dist/redact.js +62 -0
  105. package/dist/relations-store.js +242 -0
  106. package/dist/replay.js +215 -0
  107. package/dist/scalars.js +28 -0
  108. package/dist/scout.js +346 -0
  109. package/dist/stats.js +269 -0
  110. package/dist/strategy/card-clock.js +185 -0
  111. package/dist/strategy/map-notes.js +128 -0
  112. package/dist/strategy/notes.js +2168 -0
  113. package/dist/team-cards.js +84 -0
  114. package/dist/team-orders.js +150 -0
  115. package/dist/teams.js +19 -0
  116. package/dist/tools/award.js +206 -0
  117. package/dist/tools/begin-turn.js +162 -0
  118. package/dist/tools/board.js +77 -0
  119. package/dist/tools/card-advice.js +256 -0
  120. package/dist/tools/card-effects.js +49 -0
  121. package/dist/tools/card-plays.js +434 -0
  122. package/dist/tools/card-support-advice.js +140 -0
  123. package/dist/tools/chat-inbox.js +146 -0
  124. package/dist/tools/chat.js +483 -0
  125. package/dist/tools/check-plan.js +688 -0
  126. package/dist/tools/commander-check.js +102 -0
  127. package/dist/tools/commitments.js +216 -0
  128. package/dist/tools/common.js +143 -0
  129. package/dist/tools/diplomacy.js +342 -0
  130. package/dist/tools/game.js +126 -0
  131. package/dist/tools/guess-view.js +56 -0
  132. package/dist/tools/healthcheck.js +90 -0
  133. package/dist/tools/hints.js +34 -0
  134. package/dist/tools/how-lost.js +154 -0
  135. package/dist/tools/index.js +142 -0
  136. package/dist/tools/join-game.js +327 -0
  137. package/dist/tools/leave-game.js +181 -0
  138. package/dist/tools/live-context.js +339 -0
  139. package/dist/tools/live.js +404 -0
  140. package/dist/tools/map-stats.js +117 -0
  141. package/dist/tools/map.js +133 -0
  142. package/dist/tools/my-games.js +123 -0
  143. package/dist/tools/open-games.js +104 -0
  144. package/dist/tools/opponents.js +151 -0
  145. package/dist/tools/order-options.js +135 -0
  146. package/dist/tools/picks.js +292 -0
  147. package/dist/tools/profile-hook.js +156 -0
  148. package/dist/tools/profile-opponents.js +121 -0
  149. package/dist/tools/profile-reads.js +99 -0
  150. package/dist/tools/push.js +385 -0
  151. package/dist/tools/relations.js +611 -0
  152. package/dist/tools/resolve.js +45 -0
  153. package/dist/tools/route.js +36 -0
  154. package/dist/tools/scout.js +87 -0
  155. package/dist/tools/session.js +163 -0
  156. package/dist/tools/strategy.js +200 -0
  157. package/dist/tools/stress-opponents.js +83 -0
  158. package/dist/tools/surrender.js +240 -0
  159. package/dist/tools/survival.js +189 -0
  160. package/dist/tools/team-brief.js +266 -0
  161. package/dist/tools/team-source.js +222 -0
  162. package/dist/tools/team.js +182 -0
  163. package/dist/tools/turn.js +2761 -0
  164. package/dist/tools/uncommit.js +160 -0
  165. package/dist/tools/where.js +275 -0
  166. package/dist/tools/write-kit.js +158 -0
  167. package/dist/transport-bridge.js +33 -0
  168. package/dist/trend.js +452 -0
  169. package/dist/version.js +4 -0
  170. package/manifest.json +174 -4
  171. package/mint.yaml +101 -14
  172. package/package.json +11 -6
  173. package/server.json +40 -3
  174. package/skills/warapp-api/SKILL.md +95 -4
  175. package/skills/warapp-api/references/scout.mjs +348 -0
  176. package/skills/warapp-api/scripts/warapp.sh +69 -8
  177. package/skills/warapp-mcp/SKILL.md +969 -21
@@ -0,0 +1,633 @@
1
+ // The outlook (#178): whether a game is being won, lost, is even or is stuck, from my boards over
2
+ // the last turns. Pure and deterministic; no I/O.
3
+ //
4
+ // Strength is armies + STRENGTH_INCOME_TURNS × income (the #167 FFA ranking's measure): what a
5
+ // player can field over the next few turns. Mine is exact. An opponent's is a range under fog:
6
+ // - low: its visible armies + k × the income it visibly earns (both lower bounds);
7
+ // - high: what it could have, at most — every army it can have received and k × the most it
8
+ // could earn now. Received: its start (given, or #205 with the starts unknown under an even
9
+ // split, ⌈territories ÷ players⌉ × the armies each starting territory had) plus, each turn
10
+ // paid, the most it could earn that turn — on a turn I have a board of (#205), the territory
11
+ // income of every territory nobody else visibly holds plus every bonus none of whose
12
+ // territories someone else visibly holds; on any other turn the whole map's territory income
13
+ // plus every bonus, since a bonus held by someone else now may have paid it before — plus,
14
+ // with cards on (#205), the Reinforcement armies the card clock lets it have played by now and
15
+ // over the next k turns (`cardArmies`, capturing every turn).
16
+ // In a team game a teammate can transfer armies to it, so its high is not a ceiling there.
17
+ // Unbounded (`null`) when its start is unknown (a scenario's, or no armies per territory) or
18
+ // cards are on without that bound (a Gift card, a progressive or unknown Reinforcement card).
19
+ // With every territory in view, high = low.
20
+ // The ratio is mine ÷ the strongest opponent's, as a range: `low` against the highest `high`,
21
+ // `high` against the highest `low` (`null`: no upper limit). PLAN-TEAMS C3: in a team game it is by
22
+ // team — mine is my team's (me and each teammate still playing, `minePlayers`), and each enemy team
23
+ // is one side whose low, high and likely high are its players' summed (`strongest.team`, `players`);
24
+ // summed, a teammate's transfers stay inside its team's high. A player with no team is its own side.
25
+ //
26
+ // #208: the high is a bound, and under fog it stays far too loose to call a win until late: the
27
+ // most it could earn each turn is what the whole fog could pay, though most of the fog is
28
+ // neutral. So each opponent also gets a LIKELY high — an estimate, not a bound: the same sum, with
29
+ // every turn's income at most LIKELY_INCOME_MARGIN × the income the tracker reads off my boards
30
+ // (`trackOpponents`: the most it was seen to deploy, its visible income at least), never above
31
+ // that turn's cap, clamped between its low and its high (`source: 'tracked'`). #214: that needs
32
+ // territory in view NOW and an observed income (a turn pair that measured its deploy); one with
33
+ // nothing in view on this board, or never measured, gets a modest labelled prior instead
34
+ // (`source: 'prior'`), PEER-PACED (`peerPace`, owner's decision on #214): each turn's income is
35
+ // LIKELY_INCOME_MARGIN × the higher of my income that turn (on a turn without my board, my latest
36
+ // board before it; before my first board, unknown — the cache keeps ten turns — so a linear ramp
37
+ // from the symmetric opening's income below up to my first board's) and the most any opponent was
38
+ // tracked earning by then
39
+ // (observed deploys up to that turn; on the judged turn its tracked or visible income), the base
40
+ // income at least — at most an even share of the map's income (an even share of the territories at
41
+ // the base income, plus the map's bonuses ÷ the seated players; base income alone when no one is
42
+ // seated) and that turn's cap: a CEILING only, since as a floor it read thousands on a
43
+ // 1,200-territory map whatever anyone earned. It never falls below what the opponent's own evidence
44
+ // says: LIKELY_INCOME_MARGIN × its visible income, and × the income tracked on earlier boards when
45
+ // one was measured before it went out of view; one out of view now also keeps at least the armies
46
+ // and income it held on the latest board it was in view on. So less evidence never reads more
47
+ // hopeful than a measure would — and never its loose high, which used to stand in for it and then
48
+ // outranked every real estimate. Every likely high (tracked or prior) starts symmetric, never on my
49
+ // fortunes since (#214 review: shrinking would read every opponent weaker): the given start; under
50
+ // full distribution the even share, which is then every player's real start; else (TerritoryLimit
51
+ // unknown, so the share is only a bound) my armies on my opening board (turn 1), else war.app's
52
+ // default territory limit (DEFAULT_TERRITORY_LIMIT) at the armies each starting territory had —
53
+ // either at most the bound's start. None is
54
+ // made while any opponent is unbounded. When an estimate exists but does not show a win, the
55
+ // basis names the opponent that blocks it and why.
56
+ // Each turn gets a state: `winning` when even the low ratio is at least OUTLOOK_RATIO (or every
57
+ // opponent is bounded at no strength at all while I have some, or — `estimated` — my strength is
58
+ // at least OUTLOOK_RATIO × the highest likely high), `losing`
59
+ // when even the high ratio is at most 1 / OUTLOOK_RATIO — or (#228, `estimated`) when mine is at most
60
+ // LIKELY_LOSING_RATIO × the highest likely high (1 / OUTLOOK_RATIO once its LIKELY_INCOME_MARGIN is
61
+ // taken back out: it counts every army received and none lost, so alone it read 0.05× in a game I
62
+ // led) AND that is corroborated, by the main front grinding me down or by my income at most
63
+ // BEHIND_RATIO × the most an opponent is tracked earning (the profile's "behind"); such a turn carries
64
+ // `lowLikely`, and so does an uncorroborated one, whose basis says why it is no loss. The front that
65
+ // corroborates it is always forecast on the incomes read off my boards (below), never the plan's
66
+ // (#228 review): warapp_plan_turn plans on an outlook read without the plan's front and then reports
67
+ // one read with it, so a losing state that followed the plan's incomes could differ between the two
68
+ // and the plan would act on a state it never showed — `stalemate` when neither and no territory
69
+ // in view on both boards changed owner since the turn before while I face an enemy and (#201) the
70
+ // main front's multi-turn forecast is a stand-off, else `even`. The main front (#218: `mainFront`,
71
+ // exchange.ts, the planner's definition) is the biggest enemy stack next to me against my biggest
72
+ // stack beside it, both owners deploying their whole income there every turn (`forecastBorder`):
73
+ // mine off my board, theirs the tracked income (its visible income at least) — and on the turn
74
+ // being played, when the plan's `front` is that pair (`front` input), the plan's incomes, so the
75
+ // plan's front outlook and this forecast agree. A front that breaks or grinds me down within the
76
+ // horizon is not stuck, however quiet the last turns were; the basis says what the front assumed.
77
+ // The outlook reports a state only once it has held for OUTLOOK_TURNS consecutive turns; until
78
+ // then `state` is null and the basis says what each turn read. #228: given the plan's profile
79
+ // `standing`, an `even` or `stalemate` latest turn beside `ahead` / `behind` says why the two differ
80
+ // (the standing compares incomes; this outlook needs a decided ratio). The state never reads it.
81
+ //
82
+ // OUTLOOK_RATIO (2.4×) is borrowed from a published bot's "lost" rule and has never been measured
83
+ // for war.app: the outlook is a basis for a decision, not a verdict. The planner acts on it in one
84
+ // way only (#201): once winning or losing, its payback tests count the turns the game has left.
85
+ import { holdsSeat } from '../player-states.js';
86
+ import { carriesTeam, isTeamGame } from '../teams.js';
87
+ import { neighborsOf } from './graph.js';
88
+ import { DEFAULT_TERRITORY_LIMIT, staticSeatsFor, trackOpponents } from './opponents.js';
89
+ import { neutralizeSeats } from './static-seats.js';
90
+ import { ownerShown } from './planner.js';
91
+ import { bonusTerritories } from './bonuses.js';
92
+ import { cardArmiesBy } from '../strategy/card-clock.js';
93
+ import { boardIncome, DEFAULT_BASE_INCOME, territoryIncome } from './player.js';
94
+ import { BEHIND_RATIO, STRENGTH_INCOME_TURNS } from './profile.js';
95
+ import { FORECAST_TURNS, forecastBorder, mainFront } from './exchange.js';
96
+ import { specialUnitStrength } from './combat.js';
97
+ /** Turns a state must hold before it is reported. */
98
+ export const OUTLOOK_TURNS = 3;
99
+ /** Winning at this strength ratio or more, losing at its inverse or less (borrowed, unmeasured). */
100
+ export const OUTLOOK_RATIO = 2.4;
101
+ /** #208: the likely high lets an opponent earn up to this many times its tracked income every turn (unmeasured). */
102
+ export const LIKELY_INCOME_MARGIN = 2;
103
+ /**
104
+ * #228: losing on the estimate needs mine at most this share of the likely high — 1/OUTLOOK_RATIO
105
+ * of it once its LIKELY_INCOME_MARGIN is taken back out, since the likely high is generous to the
106
+ * opponent (it counts every army received, at the margin, and none lost) — and corroboration.
107
+ */
108
+ export const LIKELY_LOSING_RATIO = 1 / (OUTLOOK_RATIO * LIKELY_INCOME_MARGIN);
109
+ /**
110
+ * #214: the peer-paced prior — what an opponent I cannot measure likely earns in a turn:
111
+ * LIKELY_INCOME_MARGIN × the higher of my income that turn and the most any opponent was tracked
112
+ * earning by then, the base income at least; at most `ceiling` (an even share of the map's income:
113
+ * a ceiling only, never a floor); and never below `floor` (what its own evidence says: its visible
114
+ * income, the margin on what it was seen or tracked earning). Pure: the planner reuses it.
115
+ */
116
+ export function peerPace(p) {
117
+ return Math.max(p.floor ?? 0, Math.min(p.ceiling, Math.max(p.base, LIKELY_INCOME_MARGIN * Math.max(p.mine, p.peers))));
118
+ }
119
+ const round2 = (n) => Math.round(n * 100) / 100;
120
+ /** The armies the special units on a territory fight as (commanders), as the planner counts them. */
121
+ const unitsOn = (b, id) => specialUnitStrength(b.territories.find((t) => t.territoryId === id)?.specialUnits);
122
+ const fullyVisible = (g, b) => {
123
+ const seen = new Set();
124
+ for (const t of b.territories)
125
+ if (t.owner.kind !== 'unknown' && t.armies !== null)
126
+ seen.add(t.territoryId);
127
+ return [...g.territoryById.keys()].every((t) => seen.has(t));
128
+ };
129
+ function ownersOf(g, b) {
130
+ const m = new Map();
131
+ for (const t of b.territories) {
132
+ if (!g.territoryById.has(t.territoryId) || t.owner.kind === 'unknown')
133
+ continue;
134
+ m.set(t.territoryId, t.owner.kind === 'player' ? `p${t.owner.playerId}` : 'n');
135
+ }
136
+ return m;
137
+ }
138
+ export function gameOutlook(input) {
139
+ const { graph: g, me } = input;
140
+ const settings = input.settings ?? {};
141
+ const k = STRENGTH_INCOME_TURNS;
142
+ const byTurn = new Map();
143
+ // #270: a static seat (out, no AI on it) is no opponent: its territories read as neutrals.
144
+ const seats = staticSeatsFor(input.game, me);
145
+ for (const b of input.boards)
146
+ byTurn.set(b.turn, seats.size ? { ...b, board: neutralizeSeats(b.board, seats) } : b);
147
+ const all = [...byTurn.keys()].sort((a, b) => a - b);
148
+ // The latest run of consecutive turns, at most OUTLOOK_TURNS long.
149
+ const run = [];
150
+ for (let i = all.length - 1; i >= 0 && run.length < OUTLOOK_TURNS; i--) {
151
+ if (run.length && all[i] !== run[0] - 1)
152
+ break;
153
+ run.unshift(all[i]);
154
+ }
155
+ const players = input.game?.players ?? [];
156
+ const myTeam = players.find((p) => p.id === me)?.team;
157
+ const ally = (id) => id === me || (carriesTeam(myTeam) && players.find((p) => p.id === id)?.team === myTeam);
158
+ const teamGame = isTeamGame(players);
159
+ const stillPlaying = (id) => (players.find((p) => p.id === id)?.state ?? 'Playing') === 'Playing';
160
+ const rules = { base: Math.max(0, Math.floor(settings.baseIncome ?? DEFAULT_BASE_INCOME)), per: settings.bonusArmyPer ?? 0 };
161
+ const allBonuses = [...g.bonusById.values()].reduce((s, b) => s + Math.max(0, b.value), 0);
162
+ // The most any player can earn in a turn: the whole map's territory income (BonusArmyPer) plus
163
+ // every bonus. An observed turn's cap counts only the territories in view, so under BonusArmyPer
164
+ // it is raised by what the rest of the map could add to the base, never past this ceiling.
165
+ const mapTerritories = territoryIncome(g.territoryById.size, rules.base, rules.per);
166
+ const ceiling = mapTerritories + allBonuses;
167
+ // #205: on a turn I have a board of, a player earned at most the territory income of every
168
+ // territory nobody else visibly holds plus every bonus none of whose territories someone else
169
+ // (a player or neutral) visibly holds: income is paid from the board the turn starts on.
170
+ const boardCap = (b, playerId) => {
171
+ const other = new Set();
172
+ for (const t of b.territories) {
173
+ if (!g.territoryById.has(t.territoryId))
174
+ continue;
175
+ const shown = ownerShown(t);
176
+ if (shown && !(shown.owner.kind === 'player' && shown.owner.playerId === playerId))
177
+ other.add(t.territoryId);
178
+ }
179
+ let bonuses = 0;
180
+ for (const bonus of g.bonusById.values()) {
181
+ if (bonus.value <= 0)
182
+ continue;
183
+ const ts = bonusTerritories(g, bonus.id);
184
+ if (ts.length && !ts.some((t) => other.has(t)))
185
+ bonuses += bonus.value;
186
+ }
187
+ return Math.min(ceiling, territoryIncome(g.territoryById.size - other.size, rules.base, rules.per) + bonuses);
188
+ };
189
+ const turnCap = (t, playerId) => {
190
+ const b = byTurn.get(t)?.board;
191
+ return b ? boardCap(b, playerId) : ceiling;
192
+ };
193
+ // Its starting armies: given, or (#205) at most an even share of the map at the armies each
194
+ // starting territory had. Only seated players split the map: GameFeed also lists players who
195
+ // never took a seat (Invited, Declined, RemovedByHost), and counting them would shrink the share.
196
+ const game = input.game;
197
+ const seated = game ? game.players.filter((p) => holdsSeat(p.state)).length : 0;
198
+ const share = seated > 0 ? Math.ceil(g.territoryById.size / seated) : null;
199
+ const start = game?.start
200
+ ? game.start.territories * game.start.armiesPerTerritory
201
+ : game?.startArmiesPerTerritory !== undefined && share !== null
202
+ ? share * game.startArmiesPerTerritory
203
+ : null;
204
+ // #214: an even share of the map's income — the prior's CEILING only (a floor there read
205
+ // thousands on a big map whatever anyone earned).
206
+ const evenShare = seated > 0 ? territoryIncome(share ?? 0, rules.base, rules.per) + Math.floor(allBonuses / seated) : rules.base;
207
+ // #214: the likely high's start — symmetric, never my fortunes since. The given start; under full
208
+ // distribution the even share, which is then every player's real start; else (TerritoryLimit
209
+ // unknown, so the share is only a bound) my opening board (turn 1) when I have it, else war.app's
210
+ // default territory limit at the starting armies — each at most the bound's start. Never the
211
+ // territories I held on a later board: shrinking would read every opponent weaker.
212
+ const mineOn = (t) => byTurn.get(t).board.territories.filter((x) => g.territoryById.has(x.territoryId) && x.owner.kind === 'player' && x.owner.playerId === me);
213
+ const opening = (() => {
214
+ if (start === null)
215
+ return null;
216
+ if (game?.start)
217
+ return { armies: start, territories: game.start.territories };
218
+ if (game?.fullDistribution)
219
+ return { armies: start, territories: share ?? 0 };
220
+ const first = all.find((t) => t <= 1);
221
+ if (first !== undefined) {
222
+ const ts = mineOn(first);
223
+ return { armies: Math.min(start, ts.reduce((s, x) => s + (x.armies ?? 0), 0)), territories: ts.length };
224
+ }
225
+ const n = Math.min(share ?? 0, DEFAULT_TERRITORY_LIMIT);
226
+ return { armies: Math.min(start, n * (game?.startArmiesPerTerritory ?? 0)), territories: n };
227
+ })();
228
+ const likelyStart = opening?.armies ?? null;
229
+ // My income on each of my boards; on a turn between them, the latest board before it. Before my
230
+ // first board it is unknown (the board cache keeps ten turns), so it ramps linearly from that
231
+ // symmetric opening's income (its territories at the base, no bonus) up to my first board's.
232
+ const myIncome = new Map();
233
+ for (const t of all) {
234
+ const ts = new Set();
235
+ for (const x of mineOn(t))
236
+ ts.add(x.territoryId);
237
+ myIncome.set(t, boardIncome(g, ts, rules).total);
238
+ }
239
+ const openingIncome = territoryIncome(opening?.territories ?? 0, rules.base, rules.per);
240
+ const myIncomeAt = (t) => {
241
+ const first = all[0];
242
+ if (first === undefined)
243
+ return rules.base;
244
+ if (t < first) {
245
+ const to = myIncome.get(first);
246
+ return first <= 1 ? to : Math.round(openingIncome + ((to - openingIncome) * Math.max(0, t - 1)) / (first - 1));
247
+ }
248
+ let at = first;
249
+ for (const x of all)
250
+ if (x <= t)
251
+ at = x;
252
+ return myIncome.get(at);
253
+ };
254
+ // Cards: unbounded unless the card clock bounds what they add.
255
+ const cardsBound = !input.cards ? null : (input.cardArmies ?? undefined);
256
+ // The armies and income a player held on the latest of my boards up to `turn` where it had any
257
+ // territory in view, and that board's turn (none when it never had).
258
+ const lastSeen = (playerId, turn) => {
259
+ for (let i = all.length - 1; i >= 0; i--) {
260
+ if (all[i] > turn)
261
+ continue;
262
+ const held = new Set();
263
+ let armies = 0;
264
+ for (const t of byTurn.get(all[i]).board.territories) {
265
+ if (!g.territoryById.has(t.territoryId) || t.owner.kind !== 'player' || t.owner.playerId !== playerId || t.armies === null)
266
+ continue;
267
+ held.add(t.territoryId);
268
+ armies += t.armies;
269
+ }
270
+ if (held.size)
271
+ return { turn: all[i], armies, income: boardIncome(g, held, rules).total };
272
+ }
273
+ return undefined;
274
+ };
275
+ const turns = run.map((turn) => {
276
+ const board = byTurn.get(turn).board;
277
+ // #208: what the tracker reads of each opponent's income off my boards up to this turn.
278
+ let tracked;
279
+ const trackedAll = () => (tracked ??= new Map(trackOpponents(g, all.filter((t) => t <= turn).map((t) => byTurn.get(t)), me, settings, input.game).map((tr) => [
280
+ tr.playerId,
281
+ {
282
+ visible: tr.visible,
283
+ income: tr.income === null ? null : Math.max(tr.visibleIncome, tr.income),
284
+ visibleIncome: tr.visibleIncome,
285
+ observed: tr.turns.map((o) => ({ to: o.to, observed: o.observed })),
286
+ },
287
+ ])));
288
+ const trackedOf = (playerId) => trackedAll().get(playerId);
289
+ // #214: the most any opponent was tracked earning by turn `t`: its observed deploys up to then,
290
+ // and on this turn its tracked (or visible) income.
291
+ const peersAt = (t) => {
292
+ let most = 0;
293
+ for (const [p, tr] of trackedAll()) {
294
+ if (ally(p))
295
+ continue;
296
+ for (const o of tr.observed)
297
+ if (o.to <= t)
298
+ most = Math.max(most, o.observed);
299
+ if (t >= turn)
300
+ most = Math.max(most, tr.visibleIncome, tr.income ?? 0);
301
+ }
302
+ return most;
303
+ };
304
+ const held = new Map();
305
+ const armies = new Map();
306
+ for (const t of board.territories) {
307
+ if (!g.territoryById.has(t.territoryId) || t.owner.kind !== 'player' || t.armies === null)
308
+ continue;
309
+ const p = t.owner.playerId;
310
+ if (!held.has(p))
311
+ held.set(p, new Set());
312
+ held.get(p).add(t.territoryId);
313
+ armies.set(p, (armies.get(p) ?? 0) + t.armies);
314
+ }
315
+ // PLAN-TEAMS C3: in a team game my side is my team: me and each teammate still playing.
316
+ const mates = teamGame
317
+ ? [...new Set([...held.keys(), ...players.map((p) => p.id)])].filter((p) => p !== me && ally(p) && stillPlaying(p)).sort((a, b) => a - b)
318
+ : [];
319
+ const strengthOf = (p) => (armies.get(p) ?? 0) + k * boardIncome(g, held.get(p) ?? new Set(), rules).total;
320
+ const mine = mates.reduce((n, p) => n + strengthOf(p), strengthOf(me));
321
+ // #283: what `mine` is made of.
322
+ const mineFigures = [me, ...mates].reduce((f, p) => ({ armies: f.armies + (armies.get(p) ?? 0), income: f.income + boardIncome(g, held.get(p) ?? new Set(), rules).total }), { armies: 0, income: 0 });
323
+ const open = fullyVisible(g, board);
324
+ const ids = new Set([...held.keys()].filter((p) => !ally(p)));
325
+ for (const p of players)
326
+ if (!ally(p.id) && stillPlaying(p.id))
327
+ ids.add(p.id);
328
+ const rows = [...ids]
329
+ .filter((p) => stillPlaying(p))
330
+ .sort((a, b) => a - b)
331
+ .map((playerId) => {
332
+ const a = armies.get(playerId) ?? 0;
333
+ const income = boardIncome(g, held.get(playerId) ?? new Set(), rules).total;
334
+ const low = a + k * income;
335
+ const seen = { armies: a, income };
336
+ const none = { likely: undefined, source: undefined, why: undefined };
337
+ if (open)
338
+ return { playerId, low, high: low, seen, ...none };
339
+ if (start === null || likelyStart === null || cardsBound === undefined)
340
+ return { playerId, low, high: null, seen, ...none };
341
+ // Each turn's could-earn cap: from that turn's board when I have it, else the ceiling.
342
+ let received = start;
343
+ for (let t = 1; t <= turn; t++)
344
+ received += turnCap(t, playerId);
345
+ const cap = Math.max(income, turnCap(turn, playerId));
346
+ // Cards: what the clock lets it have played by now, and over the next k turns.
347
+ const now = cardsBound ? cardArmiesBy(cardsBound, turn) : 0;
348
+ const ahead = cardsBound ? cardArmiesBy(cardsBound, turn + k) - now : 0;
349
+ const high = Math.max(a, received + now) + k * cap + ahead;
350
+ // #208: the likely high — the same sum at LIKELY_INCOME_MARGIN × its tracked income a turn;
351
+ // #214: only with territory in view now and an observed income, else at the peer-paced
352
+ // prior (`peerPace`). An unmeasured one keeps the margin on its visible income too, and an
353
+ // unseen one measured on earlier boards keeps it on that tracked income: less evidence never
354
+ // reads lower. Both start symmetric (`likelyStart`).
355
+ const tr = trackedOf(playerId);
356
+ const why = !tr || tr.visible === 0 ? 'unseen' : tr.income === null ? 'unmeasured' : undefined;
357
+ const source = why ? 'prior' : 'tracked';
358
+ const own = Math.max(income, LIKELY_INCOME_MARGIN * Math.max(tr?.visibleIncome ?? 0, tr?.income ?? 0));
359
+ const paceAt = (t) => why
360
+ ? peerPace({ base: rules.base, mine: myIncomeAt(t), peers: peersAt(t), ceiling: evenShare, floor: own })
361
+ : LIKELY_INCOME_MARGIN * tr.income;
362
+ const pace = paceAt(turn);
363
+ let likelyReceived = likelyStart;
364
+ for (let t = 1; t <= turn; t++)
365
+ likelyReceived += Math.min(turnCap(t, playerId), paceAt(t));
366
+ // An unseen one keeps at least the armies it was last seen with, plus what the same pace
367
+ // paid it on each turn since, and the income it then held: losing sight of a stack bigger
368
+ // than its pace could have paid never reads more hopeful than seeing it.
369
+ const last = why === 'unseen' ? lastSeen(playerId, turn) : undefined;
370
+ let since = last?.armies ?? 0;
371
+ if (last)
372
+ for (let t = last.turn + 1; t <= turn; t++)
373
+ since += Math.min(turnCap(t, playerId), paceAt(t));
374
+ const likely = Math.max(a, likelyReceived + now, since) + k * Math.max(income, last?.income ?? 0, Math.min(cap, pace)) + ahead;
375
+ return { playerId, low, high, seen, likely: Math.min(high, Math.max(low, likely)), source, why };
376
+ });
377
+ // PLAN-TEAMS C3: an enemy team is one side, its figures summed; elsewhere a side is a player.
378
+ const teamOf = (p) => {
379
+ const t = players.find((x) => x.id === p)?.team;
380
+ return teamGame && carriesTeam(t) ? t : undefined;
381
+ };
382
+ const sides = [];
383
+ for (const r of rows) {
384
+ const t = teamOf(r.playerId);
385
+ const side = t === undefined ? undefined : sides.find((x) => x.team === t);
386
+ if (t === undefined)
387
+ sides.push(r);
388
+ else if (!side)
389
+ sides.push({ ...r, seen: { ...r.seen }, team: t, players: [r.playerId] });
390
+ else {
391
+ side.low += r.low;
392
+ side.seen.armies += r.seen.armies;
393
+ side.seen.income += r.seen.income;
394
+ side.high = side.high === null || r.high === null ? null : side.high + r.high;
395
+ side.likely = side.likely === undefined || r.likely === undefined ? undefined : side.likely + r.likely;
396
+ if (r.source === 'prior' && side.source !== 'prior') {
397
+ side.source = 'prior';
398
+ side.why = r.why;
399
+ }
400
+ side.players.push(r.playerId);
401
+ }
402
+ }
403
+ const sideOf = (x) => (x.team !== undefined ? { team: x.team, players: x.players } : {});
404
+ const maxLow = sides.reduce((m, r) => Math.max(m, r.low), 0);
405
+ const unbounded = sides.some((r) => r.high === null);
406
+ const maxHigh = sides.reduce((m, r) => Math.max(m, r.high ?? 0), 0);
407
+ const top = [...sides].sort((a, b) => (a.high === null ? 0 : 1) - (b.high === null ? 0 : 1) || (b.high ?? 0) - (a.high ?? 0) || b.low - a.low || a.playerId - b.playerId)[0];
408
+ const strongest = top ? { playerId: top.playerId, low: top.low, high: top.high, ...sideOf(top) } : null;
409
+ const ratio = {
410
+ low: !unbounded && maxHigh > 0 ? round2(mine / maxHigh) : 0,
411
+ high: maxLow > 0 ? round2(mine / maxLow) : null,
412
+ };
413
+ // #208: the highest likely high, when every opponent is bounded and one is not in full view.
414
+ let likely;
415
+ if (!unbounded && !open && sides.length) {
416
+ const top = [...sides].sort((a, b) => (b.likely ?? 0) - (a.likely ?? 0) || a.playerId - b.playerId)[0];
417
+ const high = top.likely ?? 0;
418
+ likely = {
419
+ playerId: top.playerId,
420
+ ...sideOf(top),
421
+ high,
422
+ ratio: high > 0 ? round2(mine / high) : 0,
423
+ source: top.source ?? 'tracked',
424
+ ...(top.why ? { why: top.why } : {}),
425
+ };
426
+ }
427
+ // Every opponent bounded at no strength at all while I have some: dominated.
428
+ const dominated = sides.length > 0 && !unbounded && maxHigh === 0 && mine > 0;
429
+ const prev = byTurn.get(turn - 1)?.board;
430
+ let changed = null;
431
+ if (prev) {
432
+ const before = ownersOf(g, prev);
433
+ const after = ownersOf(g, board);
434
+ changed = [...after].filter(([t, o]) => before.has(t) && before.get(t) !== o).length;
435
+ }
436
+ // The first entry per territory, as a linear find would read it.
437
+ const ownerById = new Map();
438
+ for (const x of board.territories)
439
+ if (!ownerById.has(x.territoryId))
440
+ ownerById.set(x.territoryId, x.owner);
441
+ const front = [...(held.get(me) ?? [])].some((t) => neighborsOf(g, t).some((n) => {
442
+ const o = ownerById.get(n);
443
+ return o?.kind === 'player' && !ally(o.playerId);
444
+ }));
445
+ // #201: the main front, forecast forward. #218: the planner's definition (`mainFront`), and on
446
+ // the turn being played the planner's incomes (`input.front`, the plan's `front`), so the plan's
447
+ // front outlook and this forecast are one computation.
448
+ const enemies = [];
449
+ for (const t of board.territories) {
450
+ if (!g.territoryById.has(t.territoryId) || t.armies == null || ownerById.get(t.territoryId) !== t.owner)
451
+ continue;
452
+ if (t.owner.kind !== 'player' || ally(t.owner.playerId))
453
+ continue;
454
+ enemies.push({ id: t.territoryId, armies: t.armies + unitsOn(board, t.territoryId), player: t.owner.playerId });
455
+ }
456
+ const armiesById = new Map();
457
+ for (const t of board.territories)
458
+ if (!armiesById.has(t.territoryId))
459
+ armiesById.set(t.territoryId, t.armies ?? 0);
460
+ const mineNow = held.get(me) ?? new Set();
461
+ const main = mainFront(g, (x) => mineNow.has(x), enemies, (x) => (armiesById.get(x) ?? 0) + unitsOn(board, x));
462
+ let forecast;
463
+ // #228 review: the front on my boards' incomes, for losing on the estimate (equal to `forecast`
464
+ // unless the plan's incomes are given), so the plan's read and the reported one share the state.
465
+ let ownOutcome;
466
+ if (main) {
467
+ const given = input.front && input.front.turn === turn && input.front.from === main.from && input.front.at === main.at ? input.front : undefined;
468
+ // Their income: the plan's estimate on the turn being played; else what the tracker reads
469
+ // off my boards (its tracked income, its visible income at least).
470
+ const tr = given ? undefined : trackedAll().get(main.player);
471
+ const myIncome = given?.myIncome ?? boardIncome(g, mineNow, rules).total;
472
+ const theirIncome = given?.theirIncome ?? Math.max(boardIncome(g, held.get(main.player) ?? new Set(), rules).total, tr?.income ?? 0);
473
+ const forecastAt = (mineIncome, foeIncome) => forecastBorder({
474
+ mine: main.mine,
475
+ theirs: main.theirs,
476
+ myIncome: mineIncome,
477
+ theirIncome: foeIncome,
478
+ outcomeOnly: true,
479
+ settings: {
480
+ ...(settings.offensiveKillRate !== undefined ? { offensiveKillRate: settings.offensiveKillRate } : {}),
481
+ ...(settings.defensiveKillRate !== undefined ? { defensiveKillRate: settings.defensiveKillRate } : {}),
482
+ ...(settings.oneArmyStandsGuard !== undefined ? { oneArmyStandsGuard: settings.oneArmyStandsGuard } : {}),
483
+ },
484
+ });
485
+ const f = forecastAt(myIncome, theirIncome);
486
+ if (!given)
487
+ ownOutcome = f.outcome;
488
+ else {
489
+ const own = trackedAll().get(main.player);
490
+ ownOutcome = forecastAt(boardIncome(g, mineNow, rules).total, Math.max(boardIncome(g, held.get(main.player) ?? new Set(), rules).total, own?.income ?? 0)).outcome;
491
+ }
492
+ forecast = {
493
+ mine: main.mine,
494
+ theirs: main.theirs,
495
+ player: main.player,
496
+ from: main.from,
497
+ at: main.at,
498
+ myIncome,
499
+ theirIncome,
500
+ incomeSource: given ? 'plan' : tr?.income != null && tr.income > boardIncome(g, held.get(main.player) ?? new Set(), rules).total ? 'tracked' : 'visible',
501
+ outcome: f.outcome,
502
+ turn: f.turn,
503
+ };
504
+ }
505
+ const bound = dominated || (sides.length > 0 && ratio.low >= OUTLOOK_RATIO);
506
+ const estimated = !bound && likely !== undefined && likely.high > 0 && likely.ratio >= OUTLOOK_RATIO;
507
+ const lostOnBound = !bound && !estimated && ratio.high !== null && ratio.high <= 1 / OUTLOOK_RATIO;
508
+ // #228: losing on the likely high — far below it (LIKELY_LOSING_RATIO, its margin taken back
509
+ // out), and corroborated: the likely high alone reads every fogged opponent strong (it counts
510
+ // every army received and none lost, so a game I lead can read 0.05×). Corroborated by the
511
+ // main front grinding me down, or by my income at most BEHIND_RATIO × the most an opponent is
512
+ // tracked earning (the profile's "behind").
513
+ // PLAN-TEAMS C3: in a team game my team's income, against the most an enemy team is tracked earning.
514
+ const myNow = mates.reduce((n, p) => n + boardIncome(g, held.get(p) ?? new Set(), rules).total, boardIncome(g, mineNow, rules).total);
515
+ const mostTracked = () => {
516
+ const by = new Map();
517
+ for (const [p, tr] of trackedAll()) {
518
+ if (ally(p) || tr.income === null)
519
+ continue;
520
+ const t = teamOf(p);
521
+ const key = t === undefined ? `p${p}` : `t${t}`;
522
+ by.set(key, (by.get(key) ?? 0) + tr.income);
523
+ }
524
+ let most = 0;
525
+ for (const v of by.values())
526
+ most = Math.max(most, v);
527
+ return most;
528
+ };
529
+ const lowLikely = !bound && !estimated && !lostOnBound && likely !== undefined && likely.high > 0 && likely.ratio <= LIKELY_LOSING_RATIO;
530
+ // #228 review: on my boards' incomes, whatever the plan gave the reported front.
531
+ const groundDown = ownOutcome === 'ground-down';
532
+ const incomeBehind = lowLikely && mostTracked() > 0 && myNow <= BEHIND_RATIO * mostTracked();
533
+ const lostOnEstimate = lowLikely && (groundDown || incomeBehind);
534
+ const state = bound || estimated
535
+ ? 'winning'
536
+ : lostOnBound || lostOnEstimate
537
+ ? 'losing'
538
+ : changed === 0 && front && (!forecast || forecast.outcome === 'stand-off')
539
+ ? 'stalemate'
540
+ : 'even';
541
+ return {
542
+ turn,
543
+ mine,
544
+ ...(teamGame ? { minePlayers: [me, ...mates] } : {}),
545
+ strongest,
546
+ ratio,
547
+ ...(likely ? { likely } : {}),
548
+ figures: { mine: mineFigures, strongest: top ? { ...top.seen } : null },
549
+ changed,
550
+ ...(forecast ? { front: forecast } : {}),
551
+ state,
552
+ ...(estimated || lostOnEstimate ? { estimated: true } : {}),
553
+ ...(lowLikely ? { lowLikely: { myIncome: myNow, mostTracked: mostTracked(), groundDown, losing: lostOnEstimate } } : {}),
554
+ };
555
+ });
556
+ const last = turns.at(-1);
557
+ let heldTurns = 0;
558
+ for (let i = turns.length - 1; i >= 0 && turns[i].state === last?.state; i--)
559
+ heldTurns++;
560
+ const state = last && heldTurns >= OUTLOOK_TURNS ? last.state : null;
561
+ const measure = teamGame
562
+ ? `Strength = armies + ${k} × income; an opponent's is a range under fog (low: what is in view; high: the most it could have received and earn). By team: my team's strength (mine and my teammates' still playing) against the strongest enemy team's, each the sum of its players'; summed, a teammate's transfers stay inside its team's high.`
563
+ : `Strength = armies + ${k} × income; an opponent's is a range under fog (low: what is in view; high: the most it could have received and earn). Per player: my strength alone against the single strongest opponent, teams not summed.`;
564
+ /** PLAN-TEAMS C3: how a side is named — an enemy team with its players, else the player. */
565
+ const sideName = (x) => x.team !== undefined ? `team ${x.team} (players ${x.players.join(', ')})` : `player ${x.playerId}`;
566
+ const cap = (t) => `${t[0].toUpperCase()}${t.slice(1)}`;
567
+ const thresholds = `Winning at ${OUTLOOK_RATIO}× the strongest opponent or more even at its high, losing at 1/${OUTLOOK_RATIO} (${round2(1 / OUTLOOK_RATIO)}×) or less even at its low (or, on an estimate under fog, at ${round2(LIKELY_LOSING_RATIO)}× or less of its likely strength while the main front grinds me down or my income is at most ${BEHIND_RATIO}× the most an opponent is tracked earning), stalemate when nothing in view changed hands for ${OUTLOOK_TURNS} turns along a front whose forecast is a stand-off. The ${OUTLOOK_RATIO}× is borrowed from a bot and was never measured for war.app: a basis for a decision, not a verdict.`;
568
+ // PLAN-TEAMS C3: in a team game the thresholds are against the strongest enemy team.
569
+ const thresholdsText = teamGame
570
+ ? thresholds.replace(/the strongest opponent/g, 'the strongest enemy team').replace('my income is at most', "my team's income is at most").replace('the most an opponent is tracked earning', 'the most an enemy team is tracked earning')
571
+ : thresholds;
572
+ let reading;
573
+ if (!last)
574
+ reading = 'No board to judge.';
575
+ else {
576
+ const s = last.strongest;
577
+ const figures = s
578
+ ? `turn ${last.turn}: ${teamGame ? `my team's ${last.mine}, the strongest enemy ${s.team !== undefined ? `team, ${sideName(s)}` : sideName(s)}` : `mine ${last.mine}, the strongest opponent player ${s.playerId}`} at ${s.high === s.low ? s.low : `${s.low}–${s.high ?? 'unbounded'}`}, ratio ${last.ratio.high === last.ratio.low ? last.ratio.low : `${last.ratio.low}–${last.ratio.high ?? 'unbounded'}`}×`
579
+ : `turn ${last.turn}: mine ${last.mine}, no opponent to compare`;
580
+ // #208: the likely high, said as what it is; #214: a prior says so and why, and an estimate
581
+ // that does not show a win names the opponent that blocks it.
582
+ const l = last.likely;
583
+ const rests = l?.source === 'prior'
584
+ ? `for ${sideName(l)} a prior (${l.why === 'unmeasured' ? 'no observed income: no pair of my boards measured its deploy' : 'no territory in view now'}), paced by its peers: it starts as a symmetric opening, and each turn earns ${LIKELY_INCOME_MARGIN === 2 ? 'twice' : `${LIKELY_INCOME_MARGIN}×`} the higher of my income that turn (ramped up from the opening's before my first board) and the most any opponent was tracked earning by then (the base income at least), at most an even share of the map's income (${evenShare}) and that turn's cap, and at least ${LIKELY_INCOME_MARGIN}× its visible income (and any income tracked earlier)${l.why === 'unmeasured' ? '' : ', and at least the armies and income it held when last in view'}`
585
+ : `each turn at most ${LIKELY_INCOME_MARGIN}× the income the tracker reads off my boards`;
586
+ let est = '';
587
+ if (l && (l.high !== s?.high || l.source === 'prior'))
588
+ est = ` The likely high — an estimate, not a bound: ${rests} — is ${sideName(l)} at ${l.high}, ratio ${l.ratio}×${last.estimated ? `; ${last.state} rests on it` : ''}.`;
589
+ if (l && l.high > 0 && last.state !== 'winning')
590
+ est += ` ${cap(sideName(l))} blocks a win on the estimate: winning needs mine at ${OUTLOOK_RATIO}× its likely high (${round2(OUTLOOK_RATIO * l.high)}), and mine is ${last.mine}.`;
591
+ reading =
592
+ turns.length < OUTLOOK_TURNS
593
+ ? `Only ${turns.length} turn${turns.length === 1 ? '' : 's'} of boards in a row (${OUTLOOK_TURNS} needed), so no state yet; ${figures}.`
594
+ : state
595
+ ? `${state[0].toUpperCase()}${state.slice(1)} for ${OUTLOOK_TURNS} turns running; ${figures}.`
596
+ : `${last.state[0].toUpperCase()}${last.state.slice(1)} for ${heldTurns} of the ${OUTLOOK_TURNS} turns (${turns.map((t) => t.state).join(', ')}), so no state yet; ${figures}.`;
597
+ reading += est;
598
+ // #228: a likely ratio far below parity, said with what it took (or lacked) to read losing.
599
+ const low = last.lowLikely;
600
+ if (low) {
601
+ const share = `mine is at most 1/${round2(1 / LIKELY_LOSING_RATIO)} (${round2(LIKELY_LOSING_RATIO)}×) of the likely high`;
602
+ const incomeText = low.mostTracked > 0
603
+ ? teamGame
604
+ ? `my team's income (${low.myIncome}) at most ${BEHIND_RATIO}× the most an enemy team is tracked earning (${low.mostTracked})`
605
+ : `my income (${low.myIncome}) at most ${BEHIND_RATIO}× the most an opponent is tracked earning (${low.mostTracked})`
606
+ : '';
607
+ const behind = low.mostTracked > 0 && low.myIncome <= BEHIND_RATIO * low.mostTracked;
608
+ reading += low.losing
609
+ ? ` Losing on the estimate: ${share}, corroborated by ${[low.groundDown ? 'the main front grinding me down on the incomes read off my boards' : '', behind ? incomeText : ''].filter(Boolean).join(' and ')}.`
610
+ : ` ${share[0].toUpperCase()}${share.slice(1)}, but that alone is no loss: the likely high counts every army an opponent could have received at ${LIKELY_INCOME_MARGIN}× its pace and none it lost, so it reads every fogged opponent strong. Losing on it needs the main front grinding me down on the incomes read off my boards (not the plan's, so the plan acts on the state shown) or my income at most ${BEHIND_RATIO}× the most an opponent is tracked earning${low.mostTracked > 0 ? ` (mine ${low.myIncome}, the most tracked ${low.mostTracked})` : ' (none is tracked)'}.`;
611
+ }
612
+ // #228: the plan's profile standing is a different measure (incomes, then the armies in view).
613
+ const standing = input.standing;
614
+ if ((last.state === 'even' || last.state === 'stalemate') && (standing === 'ahead' || standing === 'behind'))
615
+ reading += ` The profile's standing reads ${standing} (my income against the strongest opponent's estimate, overruled by the armies in view); this outlook reads ${last.state} because neither the winning nor the losing threshold was met on strength (armies + ${k} × income, and the likely high), a stricter test for a decided game.`;
616
+ // #218: the main front, said with what it assumes, so it reads with the plan's front outlook.
617
+ const fr = last.front;
618
+ if (fr) {
619
+ const theirs = fr.incomeSource === 'plan'
620
+ ? "the plan's estimate"
621
+ : fr.incomeSource === 'tracked'
622
+ ? 'tracked off my boards'
623
+ : 'its visible income, a lower bound under fog';
624
+ const outcome = fr.outcome === 'ground-down'
625
+ ? `I am ground down on turn ${fr.turn}`
626
+ : fr.outcome === 'breaks'
627
+ ? `my stack breaks it on turn ${fr.turn}`
628
+ : `a stand-off over ${FORECAST_TURNS} turns`;
629
+ reading += ` The main front (the biggest enemy stack next to me against my biggest beside it), each side deploying its whole income there every turn (mine ${fr.myIncome}, player ${fr.player}'s ${fr.theirIncome}: ${theirs}): my ${fr.mine} on #${fr.from} against its ${fr.theirs} on #${fr.at}, ${outcome}.`;
630
+ }
631
+ }
632
+ return { state, heldTurns, turns, k, threshold: OUTLOOK_RATIO, basis: `${reading} ${measure} ${thresholdsText}` };
633
+ }