@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,531 @@
1
+ // war.app web-client method ids (the int32 in every frame header) and the
2
+ // parameter writers this server uses. The name is also the `m=` query value of
3
+ // https://war.app/Services?m=<name>&js=1.
4
+ export const Method = {
5
+ /** Exists in the client's method table, but the web client never calls it (open games come from a page). */
6
+ GetOpenGames: 10,
7
+ GetGameDetails: 38,
8
+ GameRefresh: 39,
9
+ GetLatestGameInfo: 40,
10
+ SendChat: 42,
11
+ GetGameSettings: 48,
12
+ GetMyGames: 50,
13
+ AcceptGame: 60,
14
+ AcceptOpenSeat: 61,
15
+ AcceptOpenAutoGame: 62,
16
+ GetOpenAutoGame: 63,
17
+ DeclineGame: 64,
18
+ Surrender: 67,
19
+ GetMapDetails: 73,
20
+ SendOrders: 74,
21
+ SendInProgressOrders: 75,
22
+ SendTerritoryPicks: 76,
23
+ SendInProgressPicks: 77,
24
+ BeginFirstTurn: 78,
25
+ GetTerritoryPicks: 79,
26
+ UnCommit: 80,
27
+ GetTurns: 81,
28
+ Subscribe: 238,
29
+ GameUpdated: 241,
30
+ };
31
+ const NAMES = new Map(Object.entries(Method).map(([name, id]) => [id, name]));
32
+ /** The method's name for a known id, or undefined. */
33
+ export function methodName(id) {
34
+ return NAMES.get(id);
35
+ }
36
+ /** GetMapDetails(mapId) — takes no auth block. */
37
+ export function writeGetMapDetailsParams(w, mapId) {
38
+ w.int32(mapId);
39
+ }
40
+ /** GetTerritoryPicks(auth, gameId, playerId) — the parameters after the auth block. */
41
+ export function writeGetTerritoryPicksParams(w, gameId, playerId) {
42
+ w.int32(gameId).int32(playerId);
43
+ }
44
+ /**
45
+ * `warlight.shared.MyGamesFilterType`, read from the client bundle (its enum names and the client's
46
+ * own descriptions: "Active games", "All Games", "Finished Games", …; 6 is unused). The filter is
47
+ * sent as one byte. #260: the client's default, `Default` with a count of 10, is the dashboard's
48
+ * short list (live: 10 of an account's 41 games, 7 of them finished).
49
+ */
50
+ export const MyGamesFilterType = {
51
+ ActiveGames: 0,
52
+ ActiveNotEliminated: 1,
53
+ YourTurn: 2,
54
+ All: 3,
55
+ Default: 4,
56
+ YourTurnOrPendingChat: 5,
57
+ ActiveGameOrPendingChat: 7,
58
+ Finished: 8,
59
+ };
60
+ /** The client's sort for its game lists (`MyGamesFilters.get_Default`). */
61
+ export const MY_GAMES_DEFAULT_SORT = 2;
62
+ /** The web client's default filters (`MyGamesFilters.get_Default`), verified through the bridge. */
63
+ export const DEFAULT_MY_GAMES_FILTERS = {
64
+ filterType: MyGamesFilterType.Default,
65
+ count: 10,
66
+ sort: MY_GAMES_DEFAULT_SORT,
67
+ };
68
+ /**
69
+ * GetMyGames(auth, …) — verified through the bridge with the default filters: present +
70
+ * MyGamesFilters { byte filterType, int32 count, byte sort } (required), then bool false,
71
+ * int32 0, bool false, bool false (the client's writer sends those four as constants).
72
+ * Other filter values (#260) follow the same writer and are UNVERIFIED live.
73
+ */
74
+ export function writeGetMyGamesParams(w, filters = DEFAULT_MY_GAMES_FILTERS) {
75
+ w.bool(true).u8(filters.filterType).int32(filters.count).u8(filters.sort);
76
+ w.bool(false).int32(0).bool(false).bool(false);
77
+ }
78
+ /** How many colours war.app's colour list has (`warlight.shared.GameColor` indices 0..435). */
79
+ export const GAME_COLOR_COUNT = 436;
80
+ /**
81
+ * AcceptOpenSeat(auth, …) — read from the client's writer (docs/WEB-PROTOCOL.md,
82
+ * open games): the game id, the colour (a present flag, then the GameColor
83
+ * record — one int32 colour index; war.app refuses an absent colour, verified
84
+ * live as server error 1 "color is null"), the open seat's team, an optional
85
+ * custom-scenario start slot (absent — scenario games are refused), then a bool
86
+ * the client sets when one of the game's mods hooks the join (false: mod games
87
+ * are refused).
88
+ */
89
+ export function writeAcceptOpenSeatParams(w, p) {
90
+ if (!Number.isInteger(p.color) || p.color < 0 || p.color >= GAME_COLOR_COUNT) {
91
+ throw new RangeError(`war.app colour index out of range: ${p.color} (0..${GAME_COLOR_COUNT - 1})`);
92
+ }
93
+ w.int32(p.gameId)
94
+ .bool(true) // colour present
95
+ .int32(p.color) // GameColor: the colour index
96
+ .int32(p.team)
97
+ .bool(false) // start slot
98
+ .bool(false); // a mod hooks this join
99
+ }
100
+ /**
101
+ * An AcceptOpenSeat result: the result-present flag, then a bool and an
102
+ * optional int32. Absent is `undefined` (the client treats it as a plain join).
103
+ */
104
+ export function readAcceptOpenSeatReturn(body) {
105
+ if (!body.bool())
106
+ return undefined;
107
+ const vacationEnded = body.bool();
108
+ return body.bool() ? { vacationEnded, otherGameId: body.int32() } : { vacationEnded };
109
+ }
110
+ /**
111
+ * Surrender(auth, …) — VERIFIED live 2026-11-12 (issue #30): the game id, the
112
+ * surrender flag (false takes back a pending surrender), then the per-method
113
+ * trailing "ends vacation" bool the client sends as false. No result payload.
114
+ */
115
+ export function writeSurrenderParams(w, p) {
116
+ w.int32(p.gameId).bool(p.surrender).bool(false);
117
+ }
118
+ /**
119
+ * BeginFirstTurn(auth, gameId) — the web client's Begin button in a game whose
120
+ * first turn is a speed race (MoveOrder `NoLuckCycle`): war.app withholds the
121
+ * board until it is pressed, and pressing it starts the player's first-turn
122
+ * timer. READ FROM THE CLIENT'S WRITER, NOT VERIFIED LIVE: after the auth block
123
+ * the client writes the game id alone (no trailing bools). The reply is a
124
+ * present flag then the player's GameStanding (this server reads the board back
125
+ * with GetLatestGameInfo instead of decoding it).
126
+ */
127
+ export function writeBeginFirstTurnParams(w, gameId) {
128
+ w.int32(gameId);
129
+ }
130
+ /**
131
+ * UnCommit(auth, gameId) — withdraws the account's committed orders for the
132
+ * current turn ("Modify orders" in the client). VERIFIED FROM THE CLIENT
133
+ * BUNDLE's writer, not yet against a live capture: after the auth block only
134
+ * the int32 game id — no trailing bools — and no result payload.
135
+ */
136
+ export function writeUnCommitParams(w, gameId) {
137
+ w.int32(gameId);
138
+ }
139
+ /**
140
+ * DeclineGame(auth, gameId, modHook) — the lobby's Decline button, which is also how a seated
141
+ * player leaves a game still `WaitingForPlayers` (#217). READ FROM THE CLIENT BUNDLE's writer,
142
+ * NOT VERIFIED LIVE: after the auth block the int32 game id, then one bool the client sets when
143
+ * one of the game's mods hooks the decline (sent false: mod games are refused).
144
+ */
145
+ export function writeDeclineGameParams(w, gameId) {
146
+ w.int32(gameId).bool(false);
147
+ }
148
+ /**
149
+ * A DeclineGame reply: one bool (no present flag), which the client reads as "the chat messages
150
+ * you sent to this game were removed". `undefined` when the reply carries nothing. Never a
151
+ * success signal: the leave is read back from game state.
152
+ */
153
+ export function readDeclineGameReturn(body) {
154
+ return body.remaining > 0 ? body.bool() : undefined;
155
+ }
156
+ /** GetLatestGameInfo(auth, gameId) — the parameters after the auth block. */
157
+ export function writeGetLatestGameInfoParams(w, gameId) {
158
+ w.int32(gameId);
159
+ }
160
+ /**
161
+ * SendTerritoryPicks(auth, gameId, picks) — verified through the bridge: the
162
+ * game id, a present int list of territory ids in priority order, then two
163
+ * bools the client sends as false.
164
+ */
165
+ export function writeSendTerritoryPicksParams(w, gameId, territoryIds) {
166
+ w.int32(gameId)
167
+ .bool(true)
168
+ .list(territoryIds, (ww, id) => ww.int32(id))
169
+ .bool(false)
170
+ .bool(false);
171
+ }
172
+ /**
173
+ * A GetTerritoryPicks result: the result-present flag, then an int32-count list
174
+ * of territory ids in priority order. Absent or null is "no picks".
175
+ */
176
+ export function readTerritoryPicks(body) {
177
+ if (!body.bool())
178
+ return [];
179
+ return body.list((r) => r.int32()) ?? [];
180
+ }
181
+ /**
182
+ * Subscribe(topics) — sent on the push socket, not through `/Services`, and
183
+ * with NO auth block (verified live): a present list of topic strings
184
+ * (`Game.<id>`), then a null string.
185
+ */
186
+ export function writeSubscribeParams(w, topics) {
187
+ w.bool(true)
188
+ .list(topics, (ww, t) => ww.string(t))
189
+ .string(null);
190
+ }
191
+ /** The push topic for a game's updates. */
192
+ export const gameTopic = (gameId) => `Game.${gameId}`;
193
+ /**
194
+ * A Subscribe result: present flag, then a dict topic → error code
195
+ * (EnumSerialization_ErrorTypes, int32). Absent or null means every topic
196
+ * was accepted; an entry is a refused topic.
197
+ */
198
+ export function readSubscribeResult(body) {
199
+ if (!body.bool())
200
+ return new Map();
201
+ const d = body.dict((r) => r.string() ?? '', (r) => r.int32());
202
+ return d ?? new Map();
203
+ }
204
+ /** SendChat channel for the whole game (any other value is a team channel: the sender's team). */
205
+ export const CHAT_CHANNEL_PUBLIC = -1;
206
+ /** The "last seen" chat marker a client that has seen nothing sends (`-1`, as text). */
207
+ export const NO_CHAT_MARKER = '-1';
208
+ /**
209
+ * GameRefresh(auth, …) per docs/WEB-PROTOCOL.md (read from the client's serializer; UNVERIFIED live):
210
+ * game id, the chat and non-chat flags, the public and team "last seen" markers (decimal text; a
211
+ * client that has seen nothing sends `-1`), the lobby flag, then two optional dicts (game-group chat
212
+ * views; absent). Reply: `GameDetailsAndChat`, the chat list first.
213
+ */
214
+ export function writeGameRefreshParams(w, p) {
215
+ w.int32(p.gameId)
216
+ .bool(p.chat)
217
+ .bool(p.nonChat)
218
+ .string(NO_CHAT_MARKER)
219
+ .string(NO_CHAT_MARKER)
220
+ .bool(p.lobby)
221
+ .bool(false)
222
+ .bool(false);
223
+ }
224
+ /**
225
+ * SendChat(auth, …) per docs/WEB-PROTOCOL.md (read from the client's
226
+ * serializer; the owner verifies it live): game id, text, channel, optional
227
+ * private recipient, optional game-group id (absent), two marker strings
228
+ * (absent), two optional "last seen" dicts (absent). An optional int32 is a
229
+ * present flag then the value.
230
+ */
231
+ export function writeSendChatParams(w, p) {
232
+ w.int32(p.gameId).string(p.text).int32(p.channel ?? CHAT_CHANNEL_PUBLIC);
233
+ if (p.recipient === undefined)
234
+ w.bool(false);
235
+ else
236
+ w.bool(true).int32(p.recipient);
237
+ w.bool(false) // game-group id
238
+ .string(null)
239
+ .string(null)
240
+ .bool(false)
241
+ .bool(false);
242
+ }
243
+ // ---------------------------------------------------------------------------
244
+ // Orders (SendOrders, method 74).
245
+ //
246
+ // VERIFIED 2026-11-12: these layouts match a live client turn commit byte for byte
247
+ // (docs/WEB-PROTOCOL.md, Orders; pinned by golden tests in tests/protocol/methods.test.ts).
248
+ // writeSendOrdersParams assembles a whole turn commit from them.
249
+ /** AttackTransfer mode byte. */
250
+ export const AttackTransferMode = { Attack: 1, Transfer: 2, AttackTransfer: 3 };
251
+ const ORDER_TAG_ATTACK_TRANSFER = 1;
252
+ const ORDER_TAG_DEPLOY = 2;
253
+ function writeOrderBase(w, playerId) {
254
+ // playerId, no result, no standing updates
255
+ w.int32(playerId).bool(false).bool(false);
256
+ }
257
+ /** Encode a Deploy order (layout verified against a live turn commit). */
258
+ export function writeDeployOrder(w, o) {
259
+ w.u8(ORDER_TAG_DEPLOY)
260
+ .int32(o.territoryId)
261
+ .bool(o.flag ?? false)
262
+ .int32(o.armies);
263
+ writeOrderBase(w, o.playerId);
264
+ }
265
+ /** The special-unit tag of a Commander (`warlight.shared.Commander`, from the bundle; docs/WEB-PROTOCOL.md). */
266
+ const SPECIAL_UNIT_COMMANDER = 1;
267
+ /**
268
+ * Encode an AttackTransfer order. The layout is verified against a live turn commit (mode 3, no
269
+ * options); the option bytes (mode 1/2, byPercent, attackTeammates, a commander in the Armies)
270
+ * follow the client bundle's writer and are UNVERIFIED live (#165).
271
+ */
272
+ export function writeAttackTransferOrder(w, o) {
273
+ w.u8(ORDER_TAG_ATTACK_TRANSFER)
274
+ .bool(o.attackTeammates ?? false)
275
+ .u8(o.mode ?? AttackTransferMode.AttackTransfer)
276
+ .bool(o.byPercent ?? false)
277
+ .int32(o.from)
278
+ // Armies: not fogged, count, then the special units (count byte; each a tag, a GUID, an owner)
279
+ .bool(false)
280
+ .int32(o.armies);
281
+ if (o.commander)
282
+ w.u8(1).u8(SPECIAL_UNIT_COMMANDER).guid(o.commander.id).int32(o.commander.owner);
283
+ else
284
+ w.u8(0);
285
+ w.int32(o.to);
286
+ writeOrderBase(w, o.playerId);
287
+ }
288
+ /**
289
+ * The card plays this server can encode, by order tag. VERIFIED 2026-11-12
290
+ * (docs/PLAN-CARDS-PUSH.md): Reinforcement (5) live, golden-pinned; Order Priority, Order Delay
291
+ * and Blockade follow the same layout with the fields read from the client. The targeted cards
292
+ * (Spy 6, Airlift 12, Gift 13, Diplomacy 14, Sanctions 15, Reconnaissance 16, Surveillance 17;
293
+ * PLAN-TEAMS part D) are read from the bundle's `GameOrderPlayCard<X>` writers and golden-pinned,
294
+ * but UNVERIFIED live (V9): no commit carrying one has been sent. Bomb (20, `GameOrderPlayCardBomb`)
295
+ * and Emergency Blockade (8, `GameOrderPlayCardAbandon`) likewise: each writes one int32 territory.
296
+ */
297
+ const CardPlayTag = {
298
+ EmergencyBlockade: 8,
299
+ Reinforcement: 5,
300
+ Spy: 6,
301
+ OrderPriority: 10,
302
+ OrderDelay: 11,
303
+ Airlift: 12,
304
+ Gift: 13,
305
+ Diplomacy: 14,
306
+ Sanctions: 15,
307
+ Reconnaissance: 16,
308
+ Surveillance: 17,
309
+ Blockade: 18,
310
+ Bomb: 20,
311
+ };
312
+ /** The card plays whose encoding is read from the bundle but not yet sent live (V9). */
313
+ export const UNVERIFIED_CARD_PLAYS = new Set([
314
+ 'Spy',
315
+ 'Airlift',
316
+ 'Gift',
317
+ 'Diplomacy',
318
+ 'Sanctions',
319
+ 'Reconnaissance',
320
+ 'Surveillance',
321
+ 'Bomb',
322
+ 'EmergencyBlockade',
323
+ ]);
324
+ function need(v, card, what) {
325
+ if (v === undefined)
326
+ throw new Error(`A ${card} card play needs ${what}.`);
327
+ return v;
328
+ }
329
+ /**
330
+ * Encode a play-card order: the tag, the card's own fields in the order the bundle's writer puts
331
+ * them, the card instance GUID in .NET byte order, then the player id and two false bools.
332
+ * Blockade, Emergency Blockade, Bomb, Reconnaissance: the territory. Spy, Sanctions: the player. Surveillance: the bonus.
333
+ * Gift: the recipient, then the territory. Diplomacy: the two players. Airlift: a present
334
+ * `Armies` (not fogged, the count, the special units: none, or my commander — as an AttackTransfer
335
+ * writes it), the from
336
+ * territory, a legacy army count the client leaves 0, the to territory. Throws when a card's
337
+ * fields are missing.
338
+ */
339
+ export function writeCardPlayOrder(w, o) {
340
+ w.u8(CardPlayTag[o.card]);
341
+ switch (o.card) {
342
+ case 'Blockade':
343
+ case 'EmergencyBlockade':
344
+ case 'Bomb':
345
+ case 'Reconnaissance':
346
+ w.int32(need(o.target, o.card, 'a target territory'));
347
+ break;
348
+ case 'Spy':
349
+ case 'Sanctions':
350
+ w.int32(need(o.player, o.card, 'a target player'));
351
+ break;
352
+ case 'Surveillance':
353
+ w.int32(need(o.bonus, o.card, 'a target bonus'));
354
+ break;
355
+ case 'Gift': {
356
+ const player = need(o.player, o.card, 'a recipient player');
357
+ w.int32(player).int32(need(o.target, o.card, 'a territory'));
358
+ break;
359
+ }
360
+ case 'Diplomacy': {
361
+ const [a, b] = need(o.players, o.card, 'two players');
362
+ w.int32(a).int32(b);
363
+ break;
364
+ }
365
+ case 'Airlift':
366
+ if (o.from === undefined || o.to === undefined || o.armies === undefined) {
367
+ throw new Error('An Airlift card play needs from, to and armies.');
368
+ }
369
+ w.bool(true).bool(false).int32(o.armies);
370
+ if (o.commander)
371
+ w.u8(1).u8(SPECIAL_UNIT_COMMANDER).guid(o.commander.id).int32(o.commander.owner);
372
+ else
373
+ w.u8(0);
374
+ w.int32(o.from).int32(0).int32(o.to);
375
+ break;
376
+ default:
377
+ break;
378
+ }
379
+ w.guid(o.cardId);
380
+ writeOrderBase(w, o.playerId);
381
+ }
382
+ const ORDER_TAG_DISCARD = 9;
383
+ /**
384
+ * Encode a discard order (`GameOrderDiscard`): tag 9, the card instance GUID in
385
+ * .NET byte order, then the order base. VERIFIED against the client bundle's
386
+ * writer (the tag, the GUID, the order base), not yet against a live commit.
387
+ */
388
+ export function writeDiscardOrder(w, o) {
389
+ w.u8(ORDER_TAG_DISCARD);
390
+ w.guid(o.cardId);
391
+ writeOrderBase(w, o.playerId);
392
+ }
393
+ /**
394
+ * The `warlight.shared.TurnPhase` each order runs in. war.app refuses a turn
395
+ * whose orders are out of phase order with error 10 `OrdersInIncorrectOrder`
396
+ * (VERIFIED live 2026-11-12: `[OrderPriority, Reinforcement, deploys]` was
397
+ * accepted, `[Reinforcement, OrderPriority, …]` refused). Order Delay runs in
398
+ * the attack/transfer phase (50) — VERIFIED live 2026-11-12: placed with Order
399
+ * Priority (phase 12) it was refused with error 10. Discards run first (phase 10,
400
+ * from the bundle's `GameOrderDiscard.Vd`).
401
+ */
402
+ export const TurnPhase = {
403
+ Discards: 10,
404
+ OrderPriorityCards: 12,
405
+ /** Spy, Reconnaissance and Surveillance (bundle, UNVERIFIED live). */
406
+ SpyingCards: 15,
407
+ ReinforcementCards: 20,
408
+ Deploys: 25,
409
+ /** Bundle (`GameOrderPlayCardBomb`), UNVERIFIED live: after the deploys, before every move. */
410
+ BombCards: 30,
411
+ /** Bundle (`GameOrderPlayCardAbandon`), UNVERIFIED live: the Emergency Blockade, before Airlift and the attacks. */
412
+ EmergencyBlockadeCards: 37,
413
+ /** Bundle, UNVERIFIED live: after the deploys, before the attacks. */
414
+ AirliftCards: 43,
415
+ /** Bundle, UNVERIFIED live. */
416
+ GiftCards: 47,
417
+ Attacks: 50,
418
+ BlockadeCards: 60,
419
+ /** Bundle, UNVERIFIED live. */
420
+ DiplomacyCards: 75,
421
+ /** Bundle, UNVERIFIED live. */
422
+ SanctionsCards: 80,
423
+ };
424
+ const CARD_PHASE = {
425
+ OrderPriority: TurnPhase.OrderPriorityCards,
426
+ OrderDelay: TurnPhase.Attacks,
427
+ Reinforcement: TurnPhase.ReinforcementCards,
428
+ Blockade: TurnPhase.BlockadeCards,
429
+ Bomb: TurnPhase.BombCards,
430
+ EmergencyBlockade: TurnPhase.EmergencyBlockadeCards,
431
+ Spy: TurnPhase.SpyingCards,
432
+ Reconnaissance: TurnPhase.SpyingCards,
433
+ Surveillance: TurnPhase.SpyingCards,
434
+ Airlift: TurnPhase.AirliftCards,
435
+ Gift: TurnPhase.GiftCards,
436
+ Diplomacy: TurnPhase.DiplomacyCards,
437
+ Sanctions: TurnPhase.SanctionsCards,
438
+ };
439
+ /** The turn phase an order runs in. */
440
+ export function turnPhaseOf(o) {
441
+ if (o.kind === 'deploy')
442
+ return TurnPhase.Deploys;
443
+ if (o.kind === 'move')
444
+ return TurnPhase.Attacks;
445
+ if (o.kind === 'discard')
446
+ return TurnPhase.Discards;
447
+ return CARD_PHASE[o.card];
448
+ }
449
+ /**
450
+ * Turn-phase order, and within one phase the card plays after the other orders (so an Order
451
+ * Delay, phase 50, follows every AttackTransfer: the shape of the 2026-11-12 live commit that
452
+ * verified phase 50). The phase is VERIFIED; an Order Delay ahead of a move in phase 50 is
453
+ * UNVERIFIED live (the client bundle lets a player raise it among the moves and its validator
454
+ * refuses only a phase that goes down, but no such commit has been sent), so it is written there
455
+ * only when its order asks (`beforeMove`, #186; docs/WEB-PROTOCOL.md).
456
+ */
457
+ function compareTurnOrders(a, b) {
458
+ return turnPhaseOf(a) - turnPhaseOf(b) || Number(a.kind === 'card') - Number(b.kind === 'card');
459
+ }
460
+ /**
461
+ * The orders in the sequence SendOrders writes them: turn-phase order ({@link compareTurnOrders}),
462
+ * then each Order Delay that names a `beforeMove` short of the move count lifted out and written
463
+ * just above that move (#186 — the bundle's Order Delay order carries no position field, so its
464
+ * place in the list is its position; UNVERIFIED live). Several placed above one move keep their
465
+ * given order. Throws on a `beforeMove` on any other card, or one outside 0..moves.
466
+ */
467
+ function sendOrderSequence(orders) {
468
+ const moveCount = orders.filter((o) => o.kind === 'move').length;
469
+ const placed = new Map();
470
+ for (const o of orders) {
471
+ if (o.kind !== 'card' || o.beforeMove === undefined)
472
+ continue;
473
+ if (o.card !== 'OrderDelay')
474
+ throw new Error(`Only an Order Delay takes a position among the moves, not ${o.card}.`);
475
+ if (!Number.isInteger(o.beforeMove) || o.beforeMove < 0 || o.beforeMove > moveCount) {
476
+ throw new Error(`Order Delay beforeMove ${o.beforeMove} is outside the turn's ${moveCount} moves (0–${moveCount}).`);
477
+ }
478
+ if (o.beforeMove < moveCount)
479
+ placed.set(o, o.beforeMove);
480
+ }
481
+ const sorted = orders.filter((o) => !placed.has(o)).sort(compareTurnOrders);
482
+ if (!placed.size)
483
+ return sorted;
484
+ const out = [];
485
+ let move = 0;
486
+ for (const o of sorted) {
487
+ if (o.kind === 'move') {
488
+ for (const [card, at] of placed)
489
+ if (at === move)
490
+ out.push(card);
491
+ move++;
492
+ }
493
+ out.push(o);
494
+ }
495
+ return out;
496
+ }
497
+ /**
498
+ * SendOrders(auth, …) — verified against a live turn commit: the game id, a
499
+ * present order list (int32 count, then per order a present flag and the
500
+ * order), the turn number (GameFeed `numberOfTurns` + 1), then two bools the
501
+ * client sends as false. Orders are written in turn-phase order (a stable
502
+ * sort, so orders of one phase keep their given order, card plays after
503
+ * the rest): discards, Order Priority and Reinforcement plays, then every deploy, then
504
+ * the moves (AttackTransfers, mode 3 unless the move sets another) followed by any Order Delay
505
+ * plays (or one written above a chosen move, `beforeMove`, UNVERIFIED live: {@link sendOrderSequence}),
506
+ * then any Blockade plays. The targeted cards (UNVERIFIED live, V9) take the bundle's phases: Spy,
507
+ * Reconnaissance and Surveillance with the early cards (15), Bomb (30), Emergency Blockade (37), Airlift
508
+ * (43) and Gift (47) between the deploys and the moves, Diplomacy (75) and Sanctions (80) last.
509
+ */
510
+ export function writeSendOrdersParams(w, gameId, playerId, orders, turnNumber) {
511
+ const sorted = sendOrderSequence(orders);
512
+ w.int32(gameId).bool(true).int32(sorted.length);
513
+ for (const o of sorted) {
514
+ w.bool(true);
515
+ if (o.kind === 'card') {
516
+ const { kind: _kind, beforeMove: _beforeMove, ...play } = o;
517
+ writeCardPlayOrder(w, { playerId, ...play });
518
+ }
519
+ else if (o.kind === 'discard') {
520
+ writeDiscardOrder(w, { playerId, cardId: o.cardId });
521
+ }
522
+ else if (o.kind === 'deploy') {
523
+ writeDeployOrder(w, { playerId, territoryId: o.territoryId, armies: o.armies });
524
+ }
525
+ else {
526
+ const { kind: _kind, ...move } = o;
527
+ writeAttackTransferOrder(w, { playerId, ...move });
528
+ }
529
+ }
530
+ w.int32(turnNumber).bool(false).bool(false);
531
+ }