@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,126 @@
1
+ // A pending surrender, read from the game details (#317). Read from the client bundle locally,
2
+ // UNVERIFIED live (docs/WEB-PROTOCOL.md, "Accepting a surrender").
3
+ //
4
+ // GameFeed keeps a player who asked to surrender `Playing`, and neither GetMyGames nor
5
+ // LatestGameInfo says anything about it: only the game details do. GameRefresh (39) asked with
6
+ // `nonChat` answers `warlight.shared.GameDetailsAndChat`, whose second field is the game
7
+ // (`warlight.shared.GameWL`); the game's eighth field is its players, a dict of in-game id →
8
+ // `warlight.shared.GamePlayer`. Of a player's 36 fields this reads the AI id, "human turned into
9
+ // an AI", the in-game id, who accepted its surrender, its state and the `Surrendered` flag.
10
+ //
11
+ // The client's player reader first sets a link back to the game being read (only when it is read
12
+ // inside one), so a decoded player can carry the game as an extra leading field: such a value is
13
+ // left out before the fields are counted. Records are found by registered type name and fields by
14
+ // read order, with exact counts, so a field added or moved reads as unknown rather than wrong.
15
+ //
16
+ // Readers here never throw: a shape this server does not understand is `undefined` (unknown).
17
+ import { fields, isObj } from './protocol/decoded.js';
18
+ import { GAME_DETAILS_AND_CHAT } from './chat.js';
19
+ import { playerStateName } from './player-states.js';
20
+ export const GAME_WL = 'warlight.shared.GameWL';
21
+ export const GAME_PLAYER = 'warlight.shared.GamePlayer';
22
+ /** GameWL: its field count and where the players are, in read order. */
23
+ export const GAME_WL_FIELDS = 14;
24
+ const GW_PLAYERS = 7;
25
+ /** GamePlayer: its field count and the positions read, in read order. */
26
+ export const GAME_PLAYER_FIELDS = 36;
27
+ const GPL = { aiId: 0, turnedAI: 14, id: 20, accepted: 21, state: 28, surrendered: 29 };
28
+ /** Whether a details seat is still playing (GamePlayerState `Playing`, src/player-states.ts). */
29
+ export const seatPlaying = (s) => playerStateName(s.state) === 'Playing';
30
+ const isInt = (v) => typeof v === 'number' && Number.isInteger(v);
31
+ const optInt = (v) => v === null || isInt(v);
32
+ /** The accepted-by dict (pairs of in-game id and time) as ids; `undefined` when it is not one. */
33
+ function acceptedIds(v) {
34
+ if (v === null)
35
+ return [];
36
+ if (!Array.isArray(v))
37
+ return undefined;
38
+ const out = [];
39
+ for (const pair of v) {
40
+ if (!Array.isArray(pair) || pair.length !== 2 || !isInt(pair[0]))
41
+ return undefined;
42
+ out.push(pair[0]);
43
+ }
44
+ return out;
45
+ }
46
+ /** One decoded GamePlayer as a seat; `undefined` when a field is not what it should be. */
47
+ function seatOf(p) {
48
+ if (!isObj(p) || p.$type !== GAME_PLAYER)
49
+ return undefined;
50
+ // The link back to the game the reader may set first is not a read field.
51
+ const f = fields(p).filter((v) => !(isObj(v) && v.$type === GAME_WL));
52
+ if (f.length !== GAME_PLAYER_FIELDS)
53
+ return undefined;
54
+ const aiId = f[GPL.aiId];
55
+ const turnedAI = f[GPL.turnedAI];
56
+ const id = f[GPL.id];
57
+ const state = f[GPL.state];
58
+ const surrendered = f[GPL.surrendered];
59
+ const acceptedBy = acceptedIds(f[GPL.accepted]);
60
+ if (!optInt(aiId) || !optInt(id) || typeof turnedAI !== 'boolean' || !isInt(state) || typeof surrendered !== 'boolean')
61
+ return undefined;
62
+ if (acceptedBy === undefined)
63
+ return undefined;
64
+ const seatId = id ?? aiId;
65
+ if (seatId === null)
66
+ return undefined;
67
+ return { id: seatId, ai: aiId !== null, turnedAI, state, surrendered, acceptedBy };
68
+ }
69
+ /**
70
+ * The seats of a GameRefresh reply asked with `nonChat` (the body positioned at the result-present
71
+ * flag); `undefined` when there is no result, no game, no players, or a shape this server does not
72
+ * understand. Never throws.
73
+ */
74
+ export function readGameDetailsSeats(bundle, body) {
75
+ try {
76
+ if (!body.bool())
77
+ return undefined;
78
+ const ret = bundle.readClass(GAME_DETAILS_AND_CHAT, body);
79
+ if (!isObj(ret) || ret.$type !== GAME_DETAILS_AND_CHAT)
80
+ return undefined;
81
+ const game = fields(ret)[1];
82
+ if (!isObj(game) || game.$type !== GAME_WL)
83
+ return undefined;
84
+ const gf = fields(game);
85
+ if (gf.length !== GAME_WL_FIELDS)
86
+ return undefined;
87
+ const players = gf[GW_PLAYERS];
88
+ if (!Array.isArray(players))
89
+ return undefined;
90
+ const seats = [];
91
+ for (const pair of players) {
92
+ if (!Array.isArray(pair) || pair.length !== 2)
93
+ return undefined;
94
+ if (pair[1] === null)
95
+ continue;
96
+ const seat = seatOf(pair[1]);
97
+ if (!seat)
98
+ return undefined;
99
+ seats.push(seat);
100
+ }
101
+ return seats.length ? seats : undefined;
102
+ }
103
+ catch {
104
+ return undefined;
105
+ }
106
+ }
107
+ /**
108
+ * The surrenders pending in a game, other than my own (`me`: my in-game id, `undefined` when unknown),
109
+ * each with whether my acceptance is owed — the client's own test: the other seat `Playing` with
110
+ * `Surrendered` set, my seat `Playing` and neither an AI nor turned into one, and my id not among
111
+ * those that accepted. `feedIdOf` maps an in-game id to its GameFeed id where the caller knows it.
112
+ * In seat order (war.app's).
113
+ */
114
+ export function pendingSurrenders(seats, me, feedIdOf = () => null) {
115
+ const mine = me === undefined ? undefined : seats.find((s) => s.id === me);
116
+ const canAccept = mine !== undefined && seatPlaying(mine) && !mine.ai && !mine.turnedAI;
117
+ return seats
118
+ .filter((s) => s.surrendered && seatPlaying(s) && s.id !== me)
119
+ .map((s) => ({
120
+ inGameId: s.id,
121
+ player: feedIdOf(s.id),
122
+ owedByMe: mine === undefined ? null : canAccept && !s.acceptedBy.includes(me),
123
+ }));
124
+ }
125
+ /** Whether any of `pending` waits on my acceptance. */
126
+ export const surrenderOwed = (pending) => (pending ?? []).some((p) => p.owedByMe === true);
@@ -0,0 +1,84 @@
1
+ /** Every card play and discard in my teammates' entered orders (`teammates`: their player ids), in entry order. */
2
+ export function teamCardClaims(entries, teammates) {
3
+ const out = [];
4
+ for (const e of entries ?? []) {
5
+ if (!teammates.has(e.playerId) || !e.orders)
6
+ continue;
7
+ for (const c of e.orders.cards)
8
+ out.push({ player: e.playerId, kind: 'play', ...(c.cardId !== undefined ? { cardId: c.cardId } : {}), type: c.type, name: c.name });
9
+ for (const d of e.orders.discards)
10
+ out.push({ player: e.playerId, kind: 'discard', ...(d.cardId !== undefined ? { cardId: d.cardId } : {}), type: null });
11
+ }
12
+ return out;
13
+ }
14
+ /**
15
+ * The hand less what my teammates have claimed: a claim naming a card instance takes that card; a play
16
+ * naming only its type takes the first unclaimed card of that type (in hand order). Claims matching no card
17
+ * of the hand change nothing.
18
+ */
19
+ export function teamHandLeft(hand, claims) {
20
+ const taken = new Map();
21
+ for (const c of claims.filter((x) => x.cardId !== undefined)) {
22
+ const card = hand.find((h) => h.id === c.cardId);
23
+ if (card && !taken.has(card.id))
24
+ taken.set(card.id, { card, player: c.player, kind: c.kind });
25
+ }
26
+ for (const c of claims.filter((x) => x.cardId === undefined && x.type !== null)) {
27
+ const card = hand.find((h) => h.type === c.type && !taken.has(h.id));
28
+ if (card)
29
+ taken.set(card.id, { card, player: c.player, kind: c.kind });
30
+ }
31
+ return { left: hand.filter((h) => !taken.has(h.id)), claimed: hand.flatMap((h) => (taken.has(h.id) ? [taken.get(h.id)] : [])) };
32
+ }
33
+ /**
34
+ * Who plays each card of the team hand: a teammate for a card they entered, me for a card my orders play
35
+ * or discard, nobody for a card held (a Reinforcement held for the team says so).
36
+ */
37
+ export function teamCardRoles(hand, claimed, mine) {
38
+ const theirs = new Map(claimed.map((c) => [c.card.id, c]));
39
+ const plays = new Set(mine.plays.map((c) => c.id));
40
+ const discards = new Set(mine.discards.map((c) => c.id));
41
+ return hand.map((c) => {
42
+ const t = theirs.get(c.id);
43
+ if (t)
44
+ return {
45
+ card: c.id,
46
+ name: c.name,
47
+ by: t.player,
48
+ action: t.kind,
49
+ why: `player ${t.player} has already entered a ${t.kind} of it: leave it to them, so the team does not ${t.kind} one card twice`,
50
+ };
51
+ if (plays.has(c.id))
52
+ return { card: c.id, name: c.name, by: 'me', action: 'play', why: 'my plan plays it (it is in my orders)' };
53
+ if (discards.has(c.id))
54
+ return { card: c.id, name: c.name, by: 'me', action: 'discard', why: 'my plan discards it: the team hand is over the limit' };
55
+ return {
56
+ card: c.id,
57
+ name: c.name,
58
+ by: null,
59
+ action: 'hold',
60
+ why: 'nobody plays it this turn: whoever needs it plays it, said in team chat first',
61
+ };
62
+ });
63
+ }
64
+ /** Each card of `uses` (my orders' plays and discards) a teammate has claimed, in the order given. */
65
+ export function claimedCardUses(uses, claimed) {
66
+ const by = new Map(claimed.map((c) => [c.card.id, c]));
67
+ return uses.flatMap((u) => {
68
+ const c = by.get(u.card.id);
69
+ return c ? [{ card: u.card, mine: u.kind, player: c.player, theirs: c.kind }] : [];
70
+ });
71
+ }
72
+ /** The warning for {@link claimedCardUses} (card ids and player ids only), or undefined when there is none. */
73
+ export function claimedCardWarning(uses) {
74
+ if (!uses.length)
75
+ return undefined;
76
+ const each = uses.map((u) => `${u.mine === 'play' ? 'plays' : 'discards'} the ${u.card.name} card ${u.card.id}, which player ${u.player} has already entered a ${u.theirs} of`);
77
+ return (`These orders ${each.join('; ')}. A team shares one hand and must not use one card twice: leave it to them, or agree in ` +
78
+ 'team chat who uses it before committing. Their entered orders are read from the live board (UNVERIFIED live, V5).');
79
+ }
80
+ /** The hand with the cards my teammates have claimed moved last (order otherwise kept): a card named by type is then an unclaimed one when there is one. */
81
+ export function unclaimedFirst(hand, claimed) {
82
+ const ids = new Set(claimed.map((c) => c.card.id));
83
+ return [...hand.filter((c) => !ids.has(c.id)), ...hand.filter((c) => ids.has(c.id))];
84
+ }
@@ -0,0 +1,150 @@
1
+ // Teammates' entered orders and picks (C1, docs/PLAN-TEAMS.md), projected from a decoded LatestGameInfo.
2
+ //
3
+ // From the client bundle (read 2026-11-14, not live): LatestGameInfo carries, after the standings, a dict
4
+ // in-game player id → `warlight.shared.TeammateOrders` { a stamp (int), the orders entered (a list of
5
+ // GameOrder, during a turn), the picks entered (a list of territory ids, while distributing) }. The client
6
+ // shows a teammate's orders and picks from it, and counts the card plays and discards in it against the
7
+ // team's hand. UNVERIFIED live (V5): whether war.app fills it before the teammate commits, and what it holds
8
+ // once they have.
9
+ //
10
+ // Field names are minified, so the dict is found by the registered type name of its values, and an order's
11
+ // kind by its class name. Within an order, fields are read by kind and read order, as the bundle's readers
12
+ // fill them: Deploy (territory, free flag, armies, then the base: player …); AttackTransfer (attack-teammates
13
+ // flag, mode byte, by-percent flag, from, Armies, to, then the base); a card play (its own fields, the card
14
+ // GUID, the base); Discard (the GUID, the base). Tolerant throughout: absent means unknown, an entry it
15
+ // cannot read is listed as unread, nothing throws.
16
+ import { cardTypeByName, cardTypeName, normalizeGuid } from './cards.js';
17
+ import { playersByInGameId } from './live.js';
18
+ import { fields, isObj } from './protocol/decoded.js';
19
+ const TEAMMATE_ORDERS = 'warlight.shared.TeammateOrders';
20
+ const ORDER_PREFIX = 'warlight.shared.GameOrder';
21
+ const DEPLOY = 'warlight.shared.GameOrderDeploy';
22
+ const ATTACK_TRANSFER = 'warlight.shared.GameOrderAttackTransfer';
23
+ const DISCARD = 'warlight.shared.GameOrderDiscard';
24
+ const PLAY_CARD = 'warlight.shared.GameOrderPlayCard';
25
+ const ARMIES = 'warlight.shared.Armies';
26
+ const COMMANDER = 'warlight.shared.Commander';
27
+ /** AttackTransfer's mode byte (docs/WEB-PROTOCOL.md "Orders"). */
28
+ const MODES = { 1: 'attack', 2: 'transfer', 3: 'attack-transfer' };
29
+ /** Card-play classes whose name is not the card's (the Emergency Blockade card plays as "Abandon"). */
30
+ const CARD_CLASS_TYPES = { Abandon: 3 };
31
+ const isInt = (v) => typeof v === 'number' && Number.isInteger(v);
32
+ const shortName = (type) => (typeof type === 'string' ? type.replace(/^warlight\.shared\./, '') : 'unknown');
33
+ /** The teammate-orders dict of a decoded LatestGameInfo: the field whose values are TeammateOrders. */
34
+ function teammateDict(info) {
35
+ if (!isObj(info))
36
+ return undefined;
37
+ for (const v of fields(info)) {
38
+ if (!Array.isArray(v))
39
+ continue;
40
+ const pairs = v.filter((e) => Array.isArray(e) && e.length === 2);
41
+ if (pairs.some(([, x]) => isObj(x) && x.$type === TEAMMATE_ORDERS)) {
42
+ return pairs.filter((p) => isObj(p[1]) && p[1].$type === TEAMMATE_ORDERS);
43
+ }
44
+ }
45
+ return undefined;
46
+ }
47
+ function armiesOf(a) {
48
+ if (!isObj(a) || a.$type !== ARMIES)
49
+ return undefined;
50
+ const f = fields(a);
51
+ const numbers = f.filter(isInt);
52
+ if (numbers.length !== 1)
53
+ return undefined;
54
+ const units = f.find((x) => Array.isArray(x)) ?? [];
55
+ return { count: numbers[0], commander: units.some((u) => isObj(u) && u.$type === COMMANDER) };
56
+ }
57
+ /** Reads one order into `out`; false when its shape is not understood. */
58
+ function readOrder(o, out) {
59
+ const type = String(o.$type);
60
+ const f = fields(o);
61
+ const ints = f.filter(isInt);
62
+ if (type === DEPLOY) {
63
+ if (ints.length < 2)
64
+ return false;
65
+ out.deploys.push({ territoryId: ints[0], armies: ints[1] });
66
+ return true;
67
+ }
68
+ if (type === ATTACK_TRANSFER) {
69
+ // attackTeammates, mode, byPercent, from, Armies, to, then the base (player …).
70
+ const [attackTeammates, mode, percent, from, armies, to] = f;
71
+ const army = armiesOf(armies);
72
+ if (typeof attackTeammates !== 'boolean' || typeof percent !== 'boolean' || !isInt(mode) || !isInt(from) || !isInt(to) || !army)
73
+ return false;
74
+ const m = MODES[mode];
75
+ if (!m)
76
+ return false;
77
+ out.moves.push({
78
+ from,
79
+ to,
80
+ armies: army.count,
81
+ mode: m,
82
+ ...(percent ? { percent: true } : {}),
83
+ ...(attackTeammates ? { attackTeammates: true } : {}),
84
+ ...(army.commander ? { commander: true } : {}),
85
+ });
86
+ return true;
87
+ }
88
+ const cardId = f.map(normalizeGuid).find((g) => g !== undefined);
89
+ if (type === DISCARD) {
90
+ out.discards.push(cardId !== undefined ? { cardId } : {});
91
+ return true;
92
+ }
93
+ if (type.startsWith(PLAY_CARD)) {
94
+ const cls = type.slice(PLAY_CARD.length);
95
+ const t = CARD_CLASS_TYPES[cls] ?? cardTypeByName(cls) ?? null;
96
+ out.cards.push({ ...(cardId !== undefined ? { cardId } : {}), type: t, name: t !== null ? cardTypeName(t) : cls || 'card' });
97
+ return true;
98
+ }
99
+ return false;
100
+ }
101
+ function ordersOf(list) {
102
+ if (!Array.isArray(list))
103
+ return null;
104
+ const out = { deploys: [], moves: [], cards: [], discards: [], unread: [] };
105
+ for (const o of list) {
106
+ if (o === null)
107
+ continue;
108
+ if (!isObj(o) || typeof o.$type !== 'string' || !o.$type.startsWith(ORDER_PREFIX) || !readOrder(o, out)) {
109
+ out.unread.push(isObj(o) ? shortName(o.$type) : 'unknown');
110
+ }
111
+ }
112
+ return out;
113
+ }
114
+ /**
115
+ * Every teammate entry of a decoded LatestGameInfo, by in-game id (ascending). `undefined` when the info
116
+ * carries no teammate orders at all (not a team game, or war.app sent none: unknown). Never throws.
117
+ */
118
+ export function readTeammateOrders(info) {
119
+ const dict = teammateDict(info);
120
+ if (!dict)
121
+ return undefined;
122
+ const out = new Map();
123
+ for (const [key, entry] of dict.filter(([k]) => isInt(k)).sort((a, b) => a[0] - b[0])) {
124
+ // The stamp, the orders, the picks, in read order.
125
+ const [, orders, picks] = fields(entry);
126
+ out.set(key, {
127
+ inGameId: key,
128
+ orders: ordersOf(orders),
129
+ picks: Array.isArray(picks) ? picks.filter(isInt) : null,
130
+ });
131
+ }
132
+ return out;
133
+ }
134
+ /** {@link readTeammateOrders} keyed by GameFeed id (the in-game id when the player is unknown). */
135
+ export function teammateOrdersOf(info, players) {
136
+ const raw = readTeammateOrders(info);
137
+ if (!raw)
138
+ return undefined;
139
+ const byInGame = playersByInGameId(players);
140
+ return [...raw.values()].map((r) => ({ playerId: byInGame.get(r.inGameId)?.id ?? r.inGameId, orders: r.orders, picks: r.picks }));
141
+ }
142
+ /**
143
+ * `board` with my teammates' entered orders (`BoardState.teammateOrders`, by GameFeed id, my own entry left
144
+ * out) when the decoded LatestGameInfo carries a teammate-orders dict; the board unchanged otherwise (a 1v1 or
145
+ * FFA board, or war.app sent none: unknown), so no key is added. Never throws.
146
+ */
147
+ export function withTeammateOrders(board, info, me, players) {
148
+ const entries = teammateOrdersOf(info, players)?.filter((e) => e.playerId !== me.id);
149
+ return entries ? { ...board, teammateOrders: entries } : board;
150
+ }
package/dist/teams.js ADDED
@@ -0,0 +1,19 @@
1
+ // Teams: the one test of "is this a team?" shared by the open-games list, joining and the
2
+ // game profile. war.app marks "no team" with -1 (verified from the client bundle, see
3
+ // docs/WEB-PROTOCOL.md "Open games"): any other team value makes it a team game.
4
+ /** The team value of a player or seat in a no-team game. */
5
+ export const NO_TEAM = -1;
6
+ /** Whether a player or seat carries a team (absent and {@link NO_TEAM} do not). */
7
+ export const carriesTeam = (team) => team !== undefined && team !== null && team !== NO_TEAM;
8
+ /** Whether any player of the game carries a team: the game is a team game. */
9
+ export const isTeamGame = (players) => players.some((p) => carriesTeam(p.team));
10
+ /**
11
+ * The players on `me`'s team, never `me` itself (matched by id), in the list's order; none when `me` carries
12
+ * no team. Players are whatever the caller holds (GameFeed players at the tool boundary, keyed by GameFeed id).
13
+ */
14
+ export function teammatesOf(players, me) {
15
+ const team = me.team;
16
+ if (!carriesTeam(team))
17
+ return [];
18
+ return players.filter((p) => p.id !== me.id && p.team === team);
19
+ }