@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.
- package/.claude-plugin/marketplace.json +4 -4
- package/.claude-plugin/plugin.json +2 -2
- package/README.md +211 -9
- package/dist/analysis/accept-surrender.js +56 -0
- package/dist/analysis/award.js +159 -0
- package/dist/analysis/bonuses.js +98 -0
- package/dist/analysis/breach.js +62 -0
- package/dist/analysis/calibrate.js +239 -0
- package/dist/analysis/card-support.js +329 -0
- package/dist/analysis/card-threats.js +234 -0
- package/dist/analysis/cards-in-effect.js +130 -0
- package/dist/analysis/chains.js +59 -0
- package/dist/analysis/chokepoints.js +140 -0
- package/dist/analysis/combat.js +231 -0
- package/dist/analysis/commander-safety.js +149 -0
- package/dist/analysis/commitments.js +338 -0
- package/dist/analysis/exchange.js +155 -0
- package/dist/analysis/finished-game.js +136 -0
- package/dist/analysis/game-lessons.js +217 -0
- package/dist/analysis/graph.js +82 -0
- package/dist/analysis/guard-off.js +54 -0
- package/dist/analysis/guess.js +351 -0
- package/dist/analysis/headroom.js +241 -0
- package/dist/analysis/hold-diff.js +64 -0
- package/dist/analysis/how-lost.js +130 -0
- package/dist/analysis/index.js +14 -0
- package/dist/analysis/local-deploy.js +179 -0
- package/dist/analysis/lookahead.js +108 -0
- package/dist/analysis/opponent-profile.js +552 -0
- package/dist/analysis/opponents.js +1253 -0
- package/dist/analysis/outlook.js +695 -0
- package/dist/analysis/overrides.js +60 -0
- package/dist/analysis/picks.js +1879 -0
- package/dist/analysis/plan/after-leftover.js +140 -0
- package/dist/analysis/plan/cap-spend.js +242 -0
- package/dist/analysis/plan/captures.js +1195 -0
- package/dist/analysis/plan/commander.js +236 -0
- package/dist/analysis/plan/completed.js +29 -0
- package/dist/analysis/plan/conceded-out.js +131 -0
- package/dist/analysis/plan/estimates.js +715 -0
- package/dist/analysis/plan/guard-off.js +232 -0
- package/dist/analysis/plan/holds.js +1631 -0
- package/dist/analysis/plan/leftover.js +760 -0
- package/dist/analysis/plan/move-order.js +319 -0
- package/dist/analysis/plan/orders.js +317 -0
- package/dist/analysis/plan/relations.js +702 -0
- package/dist/analysis/plan/report.js +672 -0
- package/dist/analysis/plan/shared.js +1405 -0
- package/dist/analysis/plan/stand-still.js +823 -0
- package/dist/analysis/plan/state.js +44 -0
- package/dist/analysis/plan/strike.js +1416 -0
- package/dist/analysis/plan/targets.js +651 -0
- package/dist/analysis/plan/team.js +299 -0
- package/dist/analysis/plan/threats.js +1001 -0
- package/dist/analysis/plan/transfers.js +328 -0
- package/dist/analysis/plan/util.js +5 -0
- package/dist/analysis/plan/vacated.js +285 -0
- package/dist/analysis/planner.js +2927 -0
- package/dist/analysis/player-record.js +144 -0
- package/dist/analysis/player.js +268 -0
- package/dist/analysis/profile.js +347 -0
- package/dist/analysis/relations.js +1030 -0
- package/dist/analysis/schedule.js +88 -0
- package/dist/analysis/since-last.js +50 -0
- package/dist/analysis/static-seats.js +71 -0
- package/dist/analysis/stress.js +584 -0
- package/dist/analysis/team-brief.js +151 -0
- package/dist/analysis/tripwire.js +644 -0
- package/dist/analysis/turn-sim.js +259 -0
- package/dist/analysis/whereabouts.js +1112 -0
- package/dist/board.js +105 -0
- package/dist/bundle.js +121082 -36148
- package/dist/calibration-source.js +75 -0
- package/dist/cards.js +435 -0
- package/dist/chat-cursor.js +135 -0
- package/dist/chat.js +265 -0
- package/dist/client.js +439 -0
- package/dist/config.js +62 -0
- package/dist/direct/link.js +102 -0
- package/dist/direct/session.js +267 -0
- package/dist/direct/transport.js +424 -0
- package/dist/finished-store.js +357 -0
- package/dist/guess-salt.js +65 -0
- package/dist/hosted.js +24 -0
- package/dist/index.js +33 -0
- package/dist/join-color.js +140 -0
- package/dist/lessons-store.js +356 -0
- package/dist/live.js +287 -0
- package/dist/map-details.js +127 -0
- package/dist/map-stats.js +160 -0
- package/dist/map-store.js +242 -0
- package/dist/move-order.js +141 -0
- package/dist/my-games.js +218 -0
- package/dist/normalize.js +443 -0
- package/dist/notifications.js +143 -0
- package/dist/open-games.js +537 -0
- package/dist/outlook-log.js +165 -0
- package/dist/path-lock.js +22 -0
- package/dist/pick-list.js +193 -0
- package/dist/player-record-store.js +231 -0
- package/dist/player-states.js +31 -0
- package/dist/profile-store.js +727 -0
- package/dist/protocol/bundle.js +375 -0
- package/dist/protocol/codec.js +556 -0
- package/dist/protocol/decoded.js +12 -0
- package/dist/protocol/interp.js +1904 -0
- package/dist/protocol/methods.js +615 -0
- package/dist/protocol/rpc.js +202 -0
- package/dist/push/listener.js +393 -0
- package/dist/push/packets.js +66 -0
- package/dist/push/socketio.js +333 -0
- package/dist/push/websocket.js +179 -0
- package/dist/redact.js +62 -0
- package/dist/relations-store.js +242 -0
- package/dist/replay.js +227 -0
- package/dist/scalars.js +28 -0
- package/dist/scout.js +346 -0
- package/dist/stats.js +291 -0
- package/dist/strategy/card-clock.js +185 -0
- package/dist/strategy/map-notes.js +128 -0
- package/dist/strategy/notes.js +2223 -0
- package/dist/surrender-pending.js +126 -0
- package/dist/team-cards.js +84 -0
- package/dist/team-orders.js +150 -0
- package/dist/teams.js +19 -0
- package/dist/tools/accept-surrender.js +311 -0
- package/dist/tools/award.js +206 -0
- package/dist/tools/begin-turn.js +162 -0
- package/dist/tools/board.js +77 -0
- package/dist/tools/card-advice.js +256 -0
- package/dist/tools/card-effects.js +49 -0
- package/dist/tools/card-plays.js +434 -0
- package/dist/tools/card-support-advice.js +140 -0
- package/dist/tools/chat-inbox.js +158 -0
- package/dist/tools/chat-read-mark.js +179 -0
- package/dist/tools/chat.js +525 -0
- package/dist/tools/check-plan.js +689 -0
- package/dist/tools/commander-check.js +103 -0
- package/dist/tools/commitments.js +216 -0
- package/dist/tools/common.js +143 -0
- package/dist/tools/diplomacy.js +342 -0
- package/dist/tools/end-of-game.js +384 -0
- package/dist/tools/game.js +151 -0
- package/dist/tools/guess-view.js +56 -0
- package/dist/tools/healthcheck.js +90 -0
- package/dist/tools/hints.js +34 -0
- package/dist/tools/how-lost.js +154 -0
- package/dist/tools/index.js +152 -0
- package/dist/tools/join-game.js +327 -0
- package/dist/tools/leave-game.js +181 -0
- package/dist/tools/live-context.js +339 -0
- package/dist/tools/live.js +404 -0
- package/dist/tools/map-details.js +88 -0
- package/dist/tools/map-stats.js +130 -0
- package/dist/tools/map.js +141 -0
- package/dist/tools/my-games.js +193 -0
- package/dist/tools/notifications.js +484 -0
- package/dist/tools/open-games.js +104 -0
- package/dist/tools/opponents.js +163 -0
- package/dist/tools/order-options.js +135 -0
- package/dist/tools/past-games.js +94 -0
- package/dist/tools/picks.js +305 -0
- package/dist/tools/profile-hook.js +156 -0
- package/dist/tools/profile-opponents.js +121 -0
- package/dist/tools/profile-reads.js +99 -0
- package/dist/tools/push.js +445 -0
- package/dist/tools/relations.js +611 -0
- package/dist/tools/resolve.js +45 -0
- package/dist/tools/route.js +36 -0
- package/dist/tools/scout.js +87 -0
- package/dist/tools/session.js +163 -0
- package/dist/tools/strategy.js +257 -0
- package/dist/tools/stress-opponents.js +83 -0
- package/dist/tools/surrender-pending.js +74 -0
- package/dist/tools/surrender.js +247 -0
- package/dist/tools/survival.js +189 -0
- package/dist/tools/team-brief.js +266 -0
- package/dist/tools/team-source.js +222 -0
- package/dist/tools/team.js +178 -0
- package/dist/tools/turn.js +2825 -0
- package/dist/tools/uncommit.js +160 -0
- package/dist/tools/where.js +300 -0
- package/dist/tools/write-kit.js +164 -0
- package/dist/transport-bridge.js +33 -0
- package/dist/trend.js +577 -0
- package/dist/version.js +4 -0
- package/manifest.json +194 -4
- package/mint.yaml +106 -14
- package/package.json +11 -6
- package/server.json +40 -3
- package/skills/warapp-api/SKILL.md +95 -4
- package/skills/warapp-api/references/scout.mjs +348 -0
- package/skills/warapp-api/scripts/warapp.sh +69 -8
- package/skills/warapp-mcp/SKILL.md +1096 -21
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// war.app's in-app notifications (#311): a GetInAppNotifications reply (method 317), decoded by the
|
|
2
|
+
// client bundle's own readers by stable type name, like GetMyGames — fields by read order, checked by
|
|
3
|
+
// type, never by their minified names (docs/WEB-PROTOCOL.md, Notifications and read marks).
|
|
4
|
+
// UNVERIFIED live: read from the bundle, nothing here has been checked against a real reply.
|
|
5
|
+
//
|
|
6
|
+
// What a tool reports is name-free beyond ids: a notification's id, time, unread flag, a kind and the
|
|
7
|
+
// game id its link names. Its title and data are text war.app writes (with other players' names in
|
|
8
|
+
// them): they are read only to classify it, never passed on. The record's fourth field, a string the
|
|
9
|
+
// client never reads (perhaps a type name), is unverified free text: it is neither reported nor used.
|
|
10
|
+
import { McpToolError } from '@chrischall/mcp-utils';
|
|
11
|
+
import { fields, isObj } from './protocol/decoded.js';
|
|
12
|
+
import { NOTIFICATION_ID } from './protocol/methods.js';
|
|
13
|
+
export { NOTIFICATIONS_PAGE_SIZE } from './protocol/methods.js';
|
|
14
|
+
export const IN_APP_NOTIFICATIONS_RETURN = 'warlight.shared.GetInAppNotificationsReturn';
|
|
15
|
+
export const IN_APP_NOTIFICATION = 'warlight.shared.InAppNotification';
|
|
16
|
+
/** InAppNotification field positions in read order, and its whole field count. */
|
|
17
|
+
const NF = { sent: 0, data: 1, id: 2, n3: 3, n4: 4, title: 5, unread: 6 };
|
|
18
|
+
export const IN_APP_NOTIFICATION_FIELDS = 7;
|
|
19
|
+
/** The GetInAppNotifications reply arrived in a shape this server does not understand. */
|
|
20
|
+
export class NotificationsFormatError extends McpToolError {
|
|
21
|
+
constructor(detail) {
|
|
22
|
+
super(`war.app's notification list arrived in an unexpected shape (${detail}).`, {
|
|
23
|
+
kind: 'unknown',
|
|
24
|
+
hint: 'The war.app client may have changed; the notifications can still be read in the war.app tab.',
|
|
25
|
+
});
|
|
26
|
+
this.name = 'NotificationsFormatError';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const isInt = (v) => typeof v === 'number' && Number.isInteger(v);
|
|
30
|
+
const isStr = (v) => v === null || typeof v === 'string';
|
|
31
|
+
function notificationOf(n) {
|
|
32
|
+
if (!isObj(n) || n.$type !== IN_APP_NOTIFICATION)
|
|
33
|
+
throw new NotificationsFormatError('a notification is not an InAppNotification');
|
|
34
|
+
const f = fields(n);
|
|
35
|
+
if (f.length !== IN_APP_NOTIFICATION_FIELDS) {
|
|
36
|
+
throw new NotificationsFormatError(`an InAppNotification has ${f.length} fields, not ${IN_APP_NOTIFICATION_FIELDS}`);
|
|
37
|
+
}
|
|
38
|
+
const sent = f[NF.sent];
|
|
39
|
+
const data = f[NF.data];
|
|
40
|
+
const rawId = f[NF.id];
|
|
41
|
+
const n3 = f[NF.n3];
|
|
42
|
+
const n4 = f[NF.n4];
|
|
43
|
+
const title = f[NF.title];
|
|
44
|
+
const unread = f[NF.unread];
|
|
45
|
+
if (!(sent === null || sent instanceof Date) || !isStr(data) || !isInt(n3) || !isStr(n4) || !isStr(title) || typeof unread !== 'boolean') {
|
|
46
|
+
throw new NotificationsFormatError('an InAppNotification lacks its fields');
|
|
47
|
+
}
|
|
48
|
+
// The client parses the id text as a number; the interpreter may hand back either.
|
|
49
|
+
const id = typeof rawId === 'string' ? rawId.trim() : isInt(rawId) && rawId >= 0 ? String(rawId) : undefined;
|
|
50
|
+
if (id === undefined || !NOTIFICATION_ID.test(id))
|
|
51
|
+
throw new NotificationsFormatError('a notification id is not a number');
|
|
52
|
+
return { id, sent: sent !== null && Number.isFinite(sent.getTime()) ? sent : null, data, n3, n4, title, unread };
|
|
53
|
+
}
|
|
54
|
+
/** Read a GetInAppNotifications result (the body positioned at the result-present flag). */
|
|
55
|
+
export function readInAppNotifications(bundle, body) {
|
|
56
|
+
if (!body.bool())
|
|
57
|
+
return { items: [], total: 0 };
|
|
58
|
+
const ret = bundle.readClass(IN_APP_NOTIFICATIONS_RETURN, body);
|
|
59
|
+
if (!isObj(ret) || ret.$type !== IN_APP_NOTIFICATIONS_RETURN)
|
|
60
|
+
throw new NotificationsFormatError('no GetInAppNotificationsReturn');
|
|
61
|
+
const f = fields(ret);
|
|
62
|
+
if (f.length !== 2)
|
|
63
|
+
throw new NotificationsFormatError(`a GetInAppNotificationsReturn has ${f.length} fields, not 2`);
|
|
64
|
+
const [list, total] = f;
|
|
65
|
+
if (!(list === null || Array.isArray(list)) || !isInt(total))
|
|
66
|
+
throw new NotificationsFormatError('no notification list');
|
|
67
|
+
const items = (list ?? []).filter((n) => n !== null).map(notificationOf);
|
|
68
|
+
return { items, total: Math.max(total, items.length) };
|
|
69
|
+
}
|
|
70
|
+
/** The most data text read for a link: anything longer is not looked at. */
|
|
71
|
+
const MAX_DATA = 64 * 1024;
|
|
72
|
+
/** Hosts a game link may name (war.app, and the old Warzone domain the site still answers on). */
|
|
73
|
+
const GAME_HOSTS = new Set(['war.app', 'www.war.app', 'warzone.com', 'www.warzone.com']);
|
|
74
|
+
/** The game id one link names (`GameID=` on a war.app page, or a relative path), else null. */
|
|
75
|
+
function gameIdOfUrl(url) {
|
|
76
|
+
if (typeof url !== 'string' || url.length > 2048)
|
|
77
|
+
return null;
|
|
78
|
+
let parsed;
|
|
79
|
+
try {
|
|
80
|
+
parsed = new URL(url, 'https://war.app/');
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:')
|
|
86
|
+
return null;
|
|
87
|
+
if (!GAME_HOSTS.has(parsed.hostname.toLowerCase()))
|
|
88
|
+
return null;
|
|
89
|
+
for (const [k, v] of parsed.searchParams) {
|
|
90
|
+
if (k.toLowerCase() === 'gameid' && /^\d{1,10}$/.test(v)) {
|
|
91
|
+
const id = Number(v);
|
|
92
|
+
if (id > 0 && id <= 0x7fffffff)
|
|
93
|
+
return id;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
/** The game a notification's data links to: its `Url`, else the first of its `Urls` that names one. */
|
|
99
|
+
export function gameIdFromData(data) {
|
|
100
|
+
if (data === null || data.length > MAX_DATA)
|
|
101
|
+
return null;
|
|
102
|
+
let obj;
|
|
103
|
+
try {
|
|
104
|
+
obj = JSON.parse(data);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
if (!isObj(obj))
|
|
110
|
+
return null;
|
|
111
|
+
const direct = gameIdOfUrl(obj.Url);
|
|
112
|
+
if (direct !== null)
|
|
113
|
+
return direct;
|
|
114
|
+
if (Array.isArray(obj.Urls)) {
|
|
115
|
+
for (const u of obj.Urls.slice(0, 20)) {
|
|
116
|
+
const id = isObj(u) ? gameIdOfUrl(u.Url) : null;
|
|
117
|
+
if (id !== null)
|
|
118
|
+
return id;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The title of a game-ended notification, in the client's own wording (an owner's screenshot,
|
|
125
|
+
* 2026-11-16): `Game "<name>" has ended. You can now view its history`. Matched on that structure,
|
|
126
|
+
* anchored at both the leading `Game "` and the `" has ended.` after the name, never on a bare
|
|
127
|
+
* "ended" or "finished" anywhere in the text (another notification can quote a player's words).
|
|
128
|
+
* The name may itself hold quotes, so it is matched greedily up to the last `" has ended.`.
|
|
129
|
+
*/
|
|
130
|
+
const ENDED_TITLE = /^Game ".*" has ended\.(\s|$)/s;
|
|
131
|
+
/** The longest title looked at. */
|
|
132
|
+
const MAX_TITLE = 500;
|
|
133
|
+
/** A notification as a tool reports it: ids, time, flags and its kind, never its text. */
|
|
134
|
+
export function classifyNotification(n) {
|
|
135
|
+
const ended = n.title !== null && n.title.length <= MAX_TITLE && ENDED_TITLE.test(n.title);
|
|
136
|
+
return {
|
|
137
|
+
id: n.id,
|
|
138
|
+
kind: ended ? 'game-ended' : 'other',
|
|
139
|
+
gameId: gameIdFromData(n.data),
|
|
140
|
+
time: n.sent ? n.sent.toISOString() : null,
|
|
141
|
+
unread: n.unread,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
// Open games: the list war.app's OpenGames page embeds, decoded by the client
|
|
2
|
+
// bundle's own GameInfo reader and normalized into what a player picks a game by.
|
|
3
|
+
//
|
|
4
|
+
// VERIFIED 2026-11-12 (docs/PLAN-OPEN-GAMES.md): `GET /MultiPlayer/OpenGames`
|
|
5
|
+
// carries `<script type="text/plain" id="AllOpenGamesData">HEX</script>`; the
|
|
6
|
+
// client hex-decodes it and reads an int32 count, then that many
|
|
7
|
+
// `warlight.shared.GameInfo` records (no present flags). Field names are
|
|
8
|
+
// minified, so GameInfo and its GameInfoSettings are read by registered type
|
|
9
|
+
// name and fields by read order (positions below, each checked for its type),
|
|
10
|
+
// the same as `my-games.ts`. Each record must have exactly the client's field
|
|
11
|
+
// count: one with a field inserted or dropped would shift every position, so it
|
|
12
|
+
// throws OpenGamesFormatError (#133). Otherwise the reader stays loose: a record
|
|
13
|
+
// whose positions do not hold their types is skipped, a payload that stops
|
|
14
|
+
// decoding keeps what was read, and a missing or malformed payload is an empty
|
|
15
|
+
// list with a note.
|
|
16
|
+
// Whatever the record does not carry reliably is omitted, never guessed.
|
|
17
|
+
import { McpToolError } from '@chrischall/mcp-utils';
|
|
18
|
+
import { armiesToTake } from './analysis/combat.js';
|
|
19
|
+
import { DEFAULT_BASE_INCOME } from './analysis/player.js';
|
|
20
|
+
import { cardTypeName } from './cards.js';
|
|
21
|
+
import { colorIndexOf } from './join-color.js';
|
|
22
|
+
import { GAME_INFO, GAME_INFO_FIELDS, GAME_INFO_PLAYER, GAME_INFO_PLAYER_FIELDS } from './my-games.js';
|
|
23
|
+
import { Reader } from './protocol/codec.js';
|
|
24
|
+
import { fields, isObj } from './protocol/decoded.js';
|
|
25
|
+
import { holdsSeat, playerStateName } from './player-states.js';
|
|
26
|
+
import { carriesTeam } from './teams.js';
|
|
27
|
+
export const OPEN_GAMES_URL = 'https://war.app/MultiPlayer/OpenGames';
|
|
28
|
+
export { GAME_INFO };
|
|
29
|
+
const GAME_INFO_SETTINGS = 'warlight.shared.GameInfoSettings';
|
|
30
|
+
/**
|
|
31
|
+
* The most records an OpenGames payload may claim. The count is read from the payload, and each
|
|
32
|
+
* record is a run of the downloaded bundle's reader, so it is bounded: the page lists a few hundred
|
|
33
|
+
* open games at most, and this is far above that.
|
|
34
|
+
*/
|
|
35
|
+
export const MAX_OPEN_GAMES = 5_000;
|
|
36
|
+
/** GameInfo field positions in read order. */
|
|
37
|
+
const GI = {
|
|
38
|
+
randomTeamsOf: 0,
|
|
39
|
+
entryFee: 3,
|
|
40
|
+
prize: 4,
|
|
41
|
+
bootTime: 5,
|
|
42
|
+
gameId: 7,
|
|
43
|
+
mapId: 10,
|
|
44
|
+
maxPlayers: 12,
|
|
45
|
+
name: 13,
|
|
46
|
+
openSeats: 15,
|
|
47
|
+
players: 16,
|
|
48
|
+
realTime: 19,
|
|
49
|
+
settings: 21,
|
|
50
|
+
};
|
|
51
|
+
/** GameInfoPlayer: the colour (an optional GameColor), the in-game id, the state (GamePlayerState) and the team. */
|
|
52
|
+
// `ai` is the client's IsAI (GameInfoPlayer is built from GamePlayer's get_IsAI in that place; read from the bundle).
|
|
53
|
+
const GP = { color: 0, ai: 4, id: 6, state: 7, team: 8 };
|
|
54
|
+
/** GameInfoSettings field positions in read order. */
|
|
55
|
+
const GS = {
|
|
56
|
+
armyCap: 0,
|
|
57
|
+
autoDist: 1,
|
|
58
|
+
bonusArmiesPer: 2,
|
|
59
|
+
settingsString: 3,
|
|
60
|
+
attackOnly: 4,
|
|
61
|
+
transferOnly: 5,
|
|
62
|
+
commerce: 6,
|
|
63
|
+
defensiveKillRate: 7,
|
|
64
|
+
distribution: 9,
|
|
65
|
+
commanders: 10,
|
|
66
|
+
overriddenBonuses: 11,
|
|
67
|
+
bankingBoot: 12,
|
|
68
|
+
localDeployments: 13,
|
|
69
|
+
luck: 14,
|
|
70
|
+
minArmies: 15,
|
|
71
|
+
/** MoveOrderEnum (read from the bundle, #132): 0 Random, 1 Cycle, 2 NoLuckCycle. */
|
|
72
|
+
moveOrder: 16,
|
|
73
|
+
multiAttack: 17,
|
|
74
|
+
noSplit: 18,
|
|
75
|
+
wastelands: 19,
|
|
76
|
+
offensiveKillRate: 20,
|
|
77
|
+
oneArmyStandsGuard: 21,
|
|
78
|
+
ranked: 22,
|
|
79
|
+
roundingMode: 23,
|
|
80
|
+
};
|
|
81
|
+
/** GameInfoSettings' whole field count (as the client's reader has it, 2026-11-14). */
|
|
82
|
+
const GAME_INFO_SETTINGS_FIELDS = 24;
|
|
83
|
+
/** The distribution mode of a custom scenario. */
|
|
84
|
+
const DIST_CUSTOM_SCENARIO = -3;
|
|
85
|
+
const DEFAULT_MIN_ARMIES = 5;
|
|
86
|
+
const DEFAULT_OFFENSIVE = 0.6;
|
|
87
|
+
const DEFAULT_DEFENSIVE = 0.7;
|
|
88
|
+
const DEFAULT_ROUNDING = 1;
|
|
89
|
+
/** MoveOrderEnum's NoLuckCycle: the first turn is a speed race (docs/WEB-PROTOCOL.md "Begin first turn"). */
|
|
90
|
+
const MOVE_ORDER_NO_LUCK_CYCLE = 2;
|
|
91
|
+
/** The rule naming a non-default base income (`Minimum armies per turn is <n>`). */
|
|
92
|
+
const RULE_MIN_ARMIES = 'Minimum armies per turn is';
|
|
93
|
+
/** #276: the rule naming a game without One Army Must Stand Guard (the guard-on rule is `One army must stand guard`). */
|
|
94
|
+
export const GUARD_OFF_RULE = 'One army must stand guard is off';
|
|
95
|
+
/** The rule naming overridden bonus values. */
|
|
96
|
+
const RULE_OVERRIDDEN_BONUSES = 'Overridden bonuses';
|
|
97
|
+
/** TimeSpans at or beyond this many ms are war.app's "never". */
|
|
98
|
+
const NEVER_MS = 922_336_666_814_565;
|
|
99
|
+
/**
|
|
100
|
+
* A GameInfo (or its player or settings record) without exactly the client's field count: the
|
|
101
|
+
* client added, dropped or moved a field, so every position read would be another field's (#133).
|
|
102
|
+
*/
|
|
103
|
+
export class OpenGamesFormatError extends McpToolError {
|
|
104
|
+
constructor(detail) {
|
|
105
|
+
super(`war.app's open-games list arrived in an unexpected shape (${detail}).`, {
|
|
106
|
+
kind: 'unknown',
|
|
107
|
+
hint: 'The war.app client may have changed. warapp_my_games and warapp_get_game still work.',
|
|
108
|
+
});
|
|
109
|
+
this.name = 'OpenGamesFormatError';
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** `o`'s fields, refused unless there are exactly `count` of them. */
|
|
113
|
+
function fieldsOf(o, type, count) {
|
|
114
|
+
const f = fields(o);
|
|
115
|
+
if (f.length !== count)
|
|
116
|
+
throw new OpenGamesFormatError(`a ${type.replace('warlight.shared.', '')} has ${f.length} fields, not ${count}`);
|
|
117
|
+
return f;
|
|
118
|
+
}
|
|
119
|
+
const isInt = (v) => typeof v === 'number' && Number.isInteger(v);
|
|
120
|
+
const isNum = (v) => typeof v === 'number' && Number.isFinite(v);
|
|
121
|
+
const isBool = (v) => typeof v === 'boolean';
|
|
122
|
+
const isIntList = (v) => Array.isArray(v) && v.every(isInt);
|
|
123
|
+
/** A TimeSpan in ms as war.app shows it: `3d`, `1d12h`, `6h`, `2h30m`, `5m`, `45s`. */
|
|
124
|
+
function formatDuration(ms) {
|
|
125
|
+
const days = Math.floor(ms / 86_400_000);
|
|
126
|
+
const hours = Math.floor(ms / 3_600_000) % 24;
|
|
127
|
+
const mins = Math.floor(ms / 60_000) % 60;
|
|
128
|
+
if (days > 0)
|
|
129
|
+
return hours > 0 ? `${days}d${hours}h` : `${days}d`;
|
|
130
|
+
if (hours > 5)
|
|
131
|
+
return `${hours}h`;
|
|
132
|
+
if (hours > 0)
|
|
133
|
+
return mins > 0 ? `${hours}h${mins}m` : `${hours}h`;
|
|
134
|
+
if (mins > 0)
|
|
135
|
+
return `${mins}m`;
|
|
136
|
+
return `${Math.floor(ms / 1000) % 60}s`;
|
|
137
|
+
}
|
|
138
|
+
const pct = (rate) => Math.round(rate * 100);
|
|
139
|
+
/** The settings string `<ver>;<f|r>;<card ids>;<p|b>;<o|p>;<m|n>`: cards and mods. */
|
|
140
|
+
function settingsString(s) {
|
|
141
|
+
const parts = s.split(';');
|
|
142
|
+
if (!['1', '2', '3'].includes(parts[0] ?? ''))
|
|
143
|
+
return undefined;
|
|
144
|
+
const cards = (parts[2] ?? '')
|
|
145
|
+
.split(',')
|
|
146
|
+
.filter((c) => c.trim() !== '')
|
|
147
|
+
.map(Number)
|
|
148
|
+
.filter((n) => Number.isInteger(n));
|
|
149
|
+
return { cards, mods: parts[0] === '3' && parts[5] === 'm' };
|
|
150
|
+
}
|
|
151
|
+
/** The flags a GameInfoSettings record carries; `undefined` when it is not one. */
|
|
152
|
+
function readSettings(v) {
|
|
153
|
+
if (!isObj(v) || v.$type !== GAME_INFO_SETTINGS)
|
|
154
|
+
return undefined;
|
|
155
|
+
const f = fieldsOf(v, GAME_INFO_SETTINGS, GAME_INFO_SETTINGS_FIELDS);
|
|
156
|
+
const rules = [];
|
|
157
|
+
const gaps = [];
|
|
158
|
+
let cards;
|
|
159
|
+
const bool = (k) => f[GS[k]] === true;
|
|
160
|
+
const dist = f[GS.distribution];
|
|
161
|
+
const scenario = isInt(dist) && dist === DIST_CUSTOM_SCENARIO;
|
|
162
|
+
if (scenario) {
|
|
163
|
+
rules.push('Custom scenario');
|
|
164
|
+
gaps.push('Custom scenario');
|
|
165
|
+
}
|
|
166
|
+
else if (isBool(f[GS.autoDist])) {
|
|
167
|
+
rules.push(f[GS.autoDist] ? 'Automatic distribution' : 'Manual distribution');
|
|
168
|
+
}
|
|
169
|
+
if (bool('commanders')) {
|
|
170
|
+
rules.push('Commanders');
|
|
171
|
+
gaps.push('Commanders');
|
|
172
|
+
}
|
|
173
|
+
const ss = typeof f[GS.settingsString] === 'string' ? settingsString(f[GS.settingsString]) : undefined;
|
|
174
|
+
if (ss?.cards.length) {
|
|
175
|
+
cards = ss.cards.map(cardTypeName);
|
|
176
|
+
rules.push(`Cards: ${cards.join(', ')}`);
|
|
177
|
+
}
|
|
178
|
+
const commerce = f[GS.commerce];
|
|
179
|
+
if (isInt(commerce)) {
|
|
180
|
+
rules.push(`Commerce (${commerce === 0 ? 'no army multiplier' : `army multiplier of ${commerce}`})`);
|
|
181
|
+
gaps.push('Commerce');
|
|
182
|
+
}
|
|
183
|
+
const banking = f[GS.bankingBoot];
|
|
184
|
+
if (isNum(banking) && banking > 0 && banking < NEVER_MS)
|
|
185
|
+
rules.push(`Start with ${formatDuration(banking)} banked`);
|
|
186
|
+
const waste = f[GS.wastelands];
|
|
187
|
+
if (isInt(waste) && waste > 0 && !scenario)
|
|
188
|
+
rules.push('Wastelands');
|
|
189
|
+
if (bool('oneArmyStandsGuard'))
|
|
190
|
+
rules.push('One army must stand guard');
|
|
191
|
+
else if (f[GS.oneArmyStandsGuard] === false) {
|
|
192
|
+
// #276: a rule, no longer a planner gap — the planner keeps a second line inside a held bonus,
|
|
193
|
+
// raids one step deeper and retakes for free with the guard off (plan/guard-off.ts).
|
|
194
|
+
rules.push(GUARD_OFF_RULE);
|
|
195
|
+
}
|
|
196
|
+
const luck = f[GS.luck];
|
|
197
|
+
const lucky = isNum(luck) && luck !== 0;
|
|
198
|
+
if (lucky)
|
|
199
|
+
rules.push(`Luck modifier ${pct(luck)}%`);
|
|
200
|
+
const rounding = f[GS.roundingMode];
|
|
201
|
+
const oddRounding = isInt(rounding) && rounding !== DEFAULT_ROUNDING;
|
|
202
|
+
if (oddRounding)
|
|
203
|
+
rules.push(`Non-default rounding mode (${rounding})`);
|
|
204
|
+
if (lucky || oddRounding) {
|
|
205
|
+
gaps.push('Luck or rounding — attacks are chances: sized for 95% with luck on (certain otherwise), holds for at most a 5% fall');
|
|
206
|
+
}
|
|
207
|
+
if (f[GS.ranked] === false)
|
|
208
|
+
rules.push('Practice game');
|
|
209
|
+
const minArmies = f[GS.minArmies];
|
|
210
|
+
if (isInt(minArmies) && minArmies !== DEFAULT_MIN_ARMIES)
|
|
211
|
+
rules.push(`${RULE_MIN_ARMIES} ${minArmies}`);
|
|
212
|
+
const per = f[GS.bonusArmiesPer];
|
|
213
|
+
if (isInt(per) && per > 0) {
|
|
214
|
+
// Modelled since #38 (docs/WARAPP-API.md "Income"): a rule, not a gap.
|
|
215
|
+
rules.push(`Extra army for every ${per} ${per === 1 ? 'territory' : 'territories'}`);
|
|
216
|
+
}
|
|
217
|
+
if (bool('overriddenBonuses'))
|
|
218
|
+
rules.push(RULE_OVERRIDDEN_BONUSES);
|
|
219
|
+
if (bool('attackOnly') || bool('transferOnly')) {
|
|
220
|
+
rules.push(bool('attackOnly') && bool('transferOnly')
|
|
221
|
+
? 'Can attack-only and transfer-only'
|
|
222
|
+
: bool('attackOnly')
|
|
223
|
+
? 'Can attack-only'
|
|
224
|
+
: 'Can transfer-only');
|
|
225
|
+
}
|
|
226
|
+
if (ss?.mods) {
|
|
227
|
+
rules.push('Mods');
|
|
228
|
+
gaps.push('Mods');
|
|
229
|
+
}
|
|
230
|
+
// Modelled since #29 (the planner's Local Deployment pools): a rule, not a gap.
|
|
231
|
+
if (bool('localDeployments'))
|
|
232
|
+
rules.push('Local deployments');
|
|
233
|
+
if (bool('noSplit'))
|
|
234
|
+
rules.push('No-split mode');
|
|
235
|
+
const cap = f[GS.armyCap];
|
|
236
|
+
if (isNum(cap)) {
|
|
237
|
+
rules.push(`Army cap: ${cap}× income`);
|
|
238
|
+
gaps.push('Army cap');
|
|
239
|
+
}
|
|
240
|
+
const off = f[GS.offensiveKillRate];
|
|
241
|
+
const def = f[GS.defensiveKillRate];
|
|
242
|
+
if (isNum(off) && isNum(def) && (pct(off) !== pct(DEFAULT_OFFENSIVE) || pct(def) !== pct(DEFAULT_DEFENSIVE))) {
|
|
243
|
+
// Modelled since #28 (every capture is sized at the game's rates): a rule, not a gap.
|
|
244
|
+
rules.push(`Kill rates: offensive ${pct(off)}%, defensive ${pct(def)}%`);
|
|
245
|
+
}
|
|
246
|
+
if (bool('multiAttack'))
|
|
247
|
+
rules.push('Multi-attack');
|
|
248
|
+
if (f[GS.moveOrder] === MOVE_ORDER_NO_LUCK_CYCLE) {
|
|
249
|
+
rules.push('Speed first turn (No-luck cycle move order)');
|
|
250
|
+
gaps.push('Speed first turn — the board stays hidden until Begin (warapp_begin_turn), and whoever orders fastest moves first');
|
|
251
|
+
}
|
|
252
|
+
return { rules, gaps, ...(cards ? { cards } : {}) };
|
|
253
|
+
}
|
|
254
|
+
/** One GameInfoPlayer record, or `undefined` when it does not fit. */
|
|
255
|
+
function lobbyPlayer(p) {
|
|
256
|
+
if (!isObj(p) || p.$type !== GAME_INFO_PLAYER)
|
|
257
|
+
return undefined;
|
|
258
|
+
const f = fieldsOf(p, GAME_INFO_PLAYER, GAME_INFO_PLAYER_FIELDS);
|
|
259
|
+
const id = f[GP.id];
|
|
260
|
+
const state = f[GP.state];
|
|
261
|
+
const team = f[GP.team];
|
|
262
|
+
const color = colorIndexOf(f[GP.color]);
|
|
263
|
+
if (!isInt(id) || !isInt(state) || !isInt(team))
|
|
264
|
+
return undefined;
|
|
265
|
+
const name = playerStateName(state);
|
|
266
|
+
return { id, team, seated: holdsSeat(name), invited: name === 'Invited', color: color ?? null, ai: f[GP.ai] === true };
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* One decoded GameInfo as an open game and its lobby; `undefined` when it is not a GameInfo or a
|
|
270
|
+
* position read does not hold its type. A record without the client's field count throws.
|
|
271
|
+
*/
|
|
272
|
+
function readOpenGame(record) {
|
|
273
|
+
if (!isObj(record) || record.$type !== GAME_INFO)
|
|
274
|
+
return undefined;
|
|
275
|
+
const f = fieldsOf(record, GAME_INFO, GAME_INFO_FIELDS);
|
|
276
|
+
const id = f[GI.gameId];
|
|
277
|
+
const name = f[GI.name];
|
|
278
|
+
const mapId = f[GI.mapId];
|
|
279
|
+
const realTime = f[GI.realTime];
|
|
280
|
+
const seats = f[GI.openSeats];
|
|
281
|
+
const players = f[GI.players];
|
|
282
|
+
if (!isInt(id) || !(name === null || typeof name === 'string') || !isInt(mapId) || !isBool(realTime))
|
|
283
|
+
return undefined;
|
|
284
|
+
if (!(seats === null || isIntList(seats)) || !(players === null || Array.isArray(players)))
|
|
285
|
+
return undefined;
|
|
286
|
+
const openSeatTeams = seats ?? [];
|
|
287
|
+
const all = (players ?? []).map(lobbyPlayer).filter((p) => p !== undefined);
|
|
288
|
+
const seated = all.filter((p) => p.seated);
|
|
289
|
+
const invited = all.filter((p) => p.invited).length;
|
|
290
|
+
const randomTeamsOf = f[GI.randomTeamsOf];
|
|
291
|
+
const random = isInt(randomTeamsOf) && randomTeamsOf > 0 ? randomTeamsOf : undefined;
|
|
292
|
+
// The client's own test (its "Team Game" settings icon): random teams, or any player
|
|
293
|
+
// record — whatever its state — or open seat carrying a team.
|
|
294
|
+
const teamGame = random !== undefined || all.some((p) => carriesTeam(p.team)) || openSeatTeams.some(carriesTeam);
|
|
295
|
+
const settings = readSettings(f[GI.settings]);
|
|
296
|
+
const rules = [...(settings?.rules ?? [])];
|
|
297
|
+
if (teamGame)
|
|
298
|
+
rules.push(random !== undefined ? `Team game (random teams of ${random})` : 'Team game');
|
|
299
|
+
const game = {
|
|
300
|
+
id,
|
|
301
|
+
name: name ?? '',
|
|
302
|
+
url: `https://war.app/MultiPlayer?GameID=${id}`,
|
|
303
|
+
pace: realTime ? 'real-time' : 'multi-day',
|
|
304
|
+
players: seated.length,
|
|
305
|
+
openSeats: openSeatTeams.length,
|
|
306
|
+
seats: seated.length + invited + openSeatTeams.length,
|
|
307
|
+
teamGame,
|
|
308
|
+
mapId,
|
|
309
|
+
entryFeeCoins: isInt(f[GI.entryFee]) ? f[GI.entryFee] : 0,
|
|
310
|
+
prizeCoins: isInt(f[GI.prize]) ? f[GI.prize] : 0,
|
|
311
|
+
rules,
|
|
312
|
+
plannerGaps: settings?.gaps ?? [],
|
|
313
|
+
};
|
|
314
|
+
const boot = f[GI.bootTime];
|
|
315
|
+
if (isNum(boot) && boot > 0 && boot < NEVER_MS) {
|
|
316
|
+
game.bootTime = formatDuration(boot);
|
|
317
|
+
game.bootTimeMinutes = Math.round(boot / 60_000);
|
|
318
|
+
}
|
|
319
|
+
const max = f[GI.maxPlayers];
|
|
320
|
+
if (isInt(max) && max > 0)
|
|
321
|
+
game.maxPlayers = max;
|
|
322
|
+
if (random !== undefined)
|
|
323
|
+
game.randomTeamsOf = random;
|
|
324
|
+
if (teamGame)
|
|
325
|
+
game.openSeatTeams = openSeatTeams;
|
|
326
|
+
if (settings?.cards)
|
|
327
|
+
game.cards = settings.cards;
|
|
328
|
+
const colorsTaken = [...new Set(all.map((p) => p.color).filter((c) => c !== null))];
|
|
329
|
+
return {
|
|
330
|
+
game: orderKeys(game),
|
|
331
|
+
lobby: { seatedPlayerIds: seated.map((p) => p.id), colorsTaken, seated: seated.map((p) => ({ id: p.id, team: p.team, ai: p.ai })) },
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
/** `games` with `joined: true` on each lobby where the in-game id `me` holds a seat. */
|
|
335
|
+
export function markJoined(games, lobbies, me) {
|
|
336
|
+
return games.map((g) => (lobbies.get(g.id)?.seatedPlayerIds.includes(me) ? orderKeys({ ...g, joined: true }) : g));
|
|
337
|
+
}
|
|
338
|
+
/** `game` with a lottery verdict ({@link lotteryOf}) set, in the record's key order. */
|
|
339
|
+
export function withLottery(game, verdict) {
|
|
340
|
+
return orderKeys({ ...game, ...verdict });
|
|
341
|
+
}
|
|
342
|
+
const KEY_ORDER = [
|
|
343
|
+
'id', 'name', 'url', 'pace', 'bootTime', 'bootTimeMinutes', 'players', 'openSeats', 'seats', 'maxPlayers',
|
|
344
|
+
'teamGame', 'joined', 'lottery', 'lotteryBasis', 'randomTeamsOf', 'openSeatTeams', 'mapId', 'entryFeeCoins', 'prizeCoins', 'cards', 'rules', 'plannerGaps',
|
|
345
|
+
];
|
|
346
|
+
function orderKeys(g) {
|
|
347
|
+
const out = {};
|
|
348
|
+
for (const k of KEY_ORDER)
|
|
349
|
+
if (g[k] !== undefined)
|
|
350
|
+
out[k] = g[k];
|
|
351
|
+
return out;
|
|
352
|
+
}
|
|
353
|
+
const PAYLOAD_RE = /<script\b[^>]*\bid=["']AllOpenGamesData["'][^>]*>([\s\S]*?)<\/script>/i;
|
|
354
|
+
const HIDDEN_RE = /(\d+)\s+games?\s+(?:is|are)\s+being\s+hidden\s+due\s+to\s+prerequisites/i;
|
|
355
|
+
function hexBytes(hex) {
|
|
356
|
+
const h = hex.replace(/\s+/g, '');
|
|
357
|
+
if (h.length === 0 || h.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(h))
|
|
358
|
+
return undefined;
|
|
359
|
+
const out = new Uint8Array(h.length / 2);
|
|
360
|
+
for (let i = 0; i < out.length; i++)
|
|
361
|
+
out[i] = parseInt(h.slice(2 * i, 2 * i + 2), 16);
|
|
362
|
+
return out;
|
|
363
|
+
}
|
|
364
|
+
const UNREADABLE = "war.app's open-games data could not be read (the client may have changed).";
|
|
365
|
+
/**
|
|
366
|
+
* The open games a signed-in OpenGames page lists. A missing, malformed or truncated payload is a
|
|
367
|
+
* note; a record without the client's field count throws {@link OpenGamesFormatError}.
|
|
368
|
+
*/
|
|
369
|
+
export function parseOpenGamesPage(html, bundle) {
|
|
370
|
+
const out = { games: [], lobbies: new Map() };
|
|
371
|
+
const hidden = HIDDEN_RE.exec(html);
|
|
372
|
+
if (hidden)
|
|
373
|
+
out.hiddenByPrerequisites = Number(hidden[1]);
|
|
374
|
+
const m = PAYLOAD_RE.exec(html);
|
|
375
|
+
if (!m) {
|
|
376
|
+
out.note = 'The OpenGames page had no open-games data — war.app may have signed this session out.';
|
|
377
|
+
return out;
|
|
378
|
+
}
|
|
379
|
+
const bytes = hexBytes(m[1] ?? '');
|
|
380
|
+
if (!bytes) {
|
|
381
|
+
out.note = UNREADABLE;
|
|
382
|
+
return out;
|
|
383
|
+
}
|
|
384
|
+
const reader = new Reader(bytes);
|
|
385
|
+
let count;
|
|
386
|
+
try {
|
|
387
|
+
count = reader.int32();
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
out.note = UNREADABLE;
|
|
391
|
+
return out;
|
|
392
|
+
}
|
|
393
|
+
if (count < 0 || count > MAX_OPEN_GAMES) {
|
|
394
|
+
out.note = UNREADABLE;
|
|
395
|
+
return out;
|
|
396
|
+
}
|
|
397
|
+
let read = 0;
|
|
398
|
+
// One step budget for the whole payload, so the reader's CPU is bounded per page, not per record.
|
|
399
|
+
const budget = { spent: 0 };
|
|
400
|
+
try {
|
|
401
|
+
for (; read < count; read++) {
|
|
402
|
+
const before = reader.position;
|
|
403
|
+
const record = bundle.readClass(GAME_INFO, reader, budget);
|
|
404
|
+
// A GameInfo always takes bytes; one that takes none means the rest would repeat it endlessly.
|
|
405
|
+
if (reader.position === before)
|
|
406
|
+
throw new Error('a GameInfo record took no bytes');
|
|
407
|
+
const read = readOpenGame(record);
|
|
408
|
+
if (read) {
|
|
409
|
+
out.games.push(read.game);
|
|
410
|
+
out.lobbies.set(read.game.id, read.lobby);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
catch (e) {
|
|
415
|
+
// A shifted layout is the client changing, not a damaged payload: say so loudly.
|
|
416
|
+
if (e instanceof OpenGamesFormatError)
|
|
417
|
+
throw e;
|
|
418
|
+
// Records are back to back with no lengths: after a failed one the rest cannot be found.
|
|
419
|
+
out.note = `${UNREADABLE} Read ${read} of ${count} games before: ${e instanceof Error ? e.message : String(e)}`;
|
|
420
|
+
}
|
|
421
|
+
return out;
|
|
422
|
+
}
|
|
423
|
+
/** The games matching `filters`, in war.app's order. */
|
|
424
|
+
export function filterOpenGames(games, filters) {
|
|
425
|
+
const pace = filters.pace ?? 'multi-day';
|
|
426
|
+
const freeOnly = filters.free_only ?? true;
|
|
427
|
+
const noTeams = filters.exclude_teams ?? true;
|
|
428
|
+
const out = games.filter((g) => (pace === 'both' || g.pace === pace) &&
|
|
429
|
+
(!freeOnly || g.entryFeeCoins === 0) &&
|
|
430
|
+
(!noTeams || !g.teamGame) &&
|
|
431
|
+
(filters.max_players === undefined || (g.maxPlayers ?? g.seats) <= filters.max_players));
|
|
432
|
+
return filters.limit === undefined ? out : out.slice(0, filters.limit);
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* What a lobby's GameFeed settings add to the join preview, which the open-games record does
|
|
436
|
+
* not carry (#132): the neutral-cost screen — a game whose cheapest neutral costs more than a
|
|
437
|
+
* turn of base income expands slowly, and live one cost five turns of income — and Light Fog.
|
|
438
|
+
* Pure; `screen` is absent when the settings name no neutral size.
|
|
439
|
+
*/
|
|
440
|
+
export function feedScreen(s) {
|
|
441
|
+
const gaps = [];
|
|
442
|
+
let screen;
|
|
443
|
+
const sizes = [s.initialNonDistributionArmies, s.initialNeutralsInDistribution].filter((n) => n !== undefined && Number.isFinite(n) && n >= 0);
|
|
444
|
+
if (sizes.length) {
|
|
445
|
+
const cheapest = Math.min(...sizes);
|
|
446
|
+
const base = s.minimumArmyBonus ?? DEFAULT_BASE_INCOME;
|
|
447
|
+
const armies = armiesToTake(cheapest, {
|
|
448
|
+
...(s.offensiveKillRate !== undefined ? { offensiveKillRate: s.offensiveKillRate } : {}),
|
|
449
|
+
...(s.defensiveKillRate !== undefined ? { defensiveKillRate: s.defensiveKillRate } : {}),
|
|
450
|
+
});
|
|
451
|
+
const turns = armies === null || base <= 0 ? null : Math.round((armies / base) * 10) / 10;
|
|
452
|
+
const passes = armies !== null && armies <= base;
|
|
453
|
+
screen = { cheapestNeutral: cheapest, armiesToTake: armies, baseIncome: base, turnsOfBaseIncome: turns, passes };
|
|
454
|
+
if (!passes) {
|
|
455
|
+
gaps.push(armies === null
|
|
456
|
+
? `Costly neutrals — at these kill rates the cheapest (${cheapest}) cannot be taken`
|
|
457
|
+
: `Costly neutrals — the cheapest (${cheapest}) takes ${armies} armies, ${turns ?? '∞'} turns of base income (${base}): expansion is slow`);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (s.fog === 'LightFog') {
|
|
461
|
+
gaps.push('Light Fog — owners show everywhere but armies only near you; the planner counts every unseen stack as unknown');
|
|
462
|
+
}
|
|
463
|
+
return { ...(screen ? { screen } : {}), gaps };
|
|
464
|
+
}
|
|
465
|
+
/** The owner's joining policy (#134): at most this many seats (1v1 preferred). */
|
|
466
|
+
const POLICY_MAX_SEATS = 4;
|
|
467
|
+
/** …and a boot time of at least 12 hours (owner, 2026-11-14: a 12-hour boot is fine; near-real-time means a boot in minutes). */
|
|
468
|
+
const POLICY_MIN_BOOT_MINUTES = 12 * 60;
|
|
469
|
+
/**
|
|
470
|
+
* The owner's joining policy (#134), checked against an open game and, when GameFeed could read
|
|
471
|
+
* the lobby, its neutral-cost screen: no Commerce, mods or custom scenarios; the cheapest neutral
|
|
472
|
+
* costs at most one turn of base income; at most four seats; a boot time of at least 12 hours (no
|
|
473
|
+
* boot time — war.app's "never" — keeps it); no lotteries (#217: `game.lottery`; a likely one whose
|
|
474
|
+
* map size went unread breaks it too). Lobbies are not counted against the game cap. Pure.
|
|
475
|
+
*/
|
|
476
|
+
export function joiningPolicy(game, screen) {
|
|
477
|
+
const breaks = [];
|
|
478
|
+
if (game.rules.some((r) => r.startsWith('Commerce')))
|
|
479
|
+
breaks.push('Commerce — the policy joins no Commerce games');
|
|
480
|
+
if (game.rules.includes('Mods'))
|
|
481
|
+
breaks.push('Mods — the policy joins no mod games');
|
|
482
|
+
if (game.rules.includes('Custom scenario'))
|
|
483
|
+
breaks.push('Custom scenario — the policy joins no custom scenarios');
|
|
484
|
+
if (screen && !screen.passes) {
|
|
485
|
+
breaks.push(screen.armiesToTake === null
|
|
486
|
+
? 'Costly neutrals — the cheapest cannot be taken at these kill rates; the policy is at most one turn of base income'
|
|
487
|
+
: `Costly neutrals — the cheapest takes ${screen.armiesToTake} armies, ${screen.turnsOfBaseIncome ?? '∞'} turns of base income; the policy is at most one turn`);
|
|
488
|
+
}
|
|
489
|
+
const seats = game.maxPlayers ?? game.seats;
|
|
490
|
+
if (seats > POLICY_MAX_SEATS)
|
|
491
|
+
breaks.push(`${seats} seats — the policy is 1v1 or at most four players`);
|
|
492
|
+
if (game.bootTimeMinutes !== undefined && game.bootTimeMinutes < POLICY_MIN_BOOT_MINUTES) {
|
|
493
|
+
breaks.push(`Boot time ${game.bootTime} — the policy wants at least 12 hours`);
|
|
494
|
+
}
|
|
495
|
+
if (game.lottery === true)
|
|
496
|
+
breaks.push(`Lottery (${game.lotteryBasis ?? 'a tiny map'}) — pure luck; the policy joins no lotteries`);
|
|
497
|
+
// A likely lottery (the lottery shape, map size unread) breaks the policy too: a caller filtering on
|
|
498
|
+
// breaks must not let one through (#217 live check).
|
|
499
|
+
if (game.lottery === null)
|
|
500
|
+
breaks.push(`Likely a lottery (${game.lotteryBasis ?? 'map size not read'}) — the policy joins no lotteries`);
|
|
501
|
+
if (breaks.length)
|
|
502
|
+
return { passes: false, breaks };
|
|
503
|
+
const unchecked = [];
|
|
504
|
+
if (!screen)
|
|
505
|
+
unchecked.push("the cheapest neutral's cost");
|
|
506
|
+
return unchecked.length ? { passes: null, breaks, unchecked } : { passes: true, breaks };
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* A map of at most this many territories makes a lottery (#217): every player starts on (or next
|
|
510
|
+
* to) everything there is, so the game is decided by the luck of the distribution and the move
|
|
511
|
+
* order — nothing the planner can learn from. The live case: 2 territories, base income 0, one
|
|
512
|
+
* overridden +1 bonus.
|
|
513
|
+
*/
|
|
514
|
+
export const LOTTERY_MAX_TERRITORIES = 4;
|
|
515
|
+
/** Whether a lobby's settings fit the lottery shape read so far: base income 0 and overridden bonuses. */
|
|
516
|
+
export function lotteryShape(game) {
|
|
517
|
+
return game.rules.includes(`${RULE_MIN_ARMIES} 0`) && game.rules.includes(RULE_OVERRIDDEN_BONUSES);
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Whether an open game is a lottery (#217), from its data, never its name: a map of at most
|
|
521
|
+
* {@link LOTTERY_MAX_TERRITORIES} territories (`territories`, when the map could be read) is one;
|
|
522
|
+
* without the map size, settings with base income 0 and overridden bonuses make it a likely one
|
|
523
|
+
* (`null`). `undefined` when it is not one. Pure.
|
|
524
|
+
*/
|
|
525
|
+
export function lotteryOf(game, territories) {
|
|
526
|
+
const shape = [];
|
|
527
|
+
if (game.rules.includes(`${RULE_MIN_ARMIES} 0`))
|
|
528
|
+
shape.push('base income 0');
|
|
529
|
+
if (game.rules.includes(RULE_OVERRIDDEN_BONUSES))
|
|
530
|
+
shape.push('overridden bonuses');
|
|
531
|
+
if (territories !== undefined) {
|
|
532
|
+
if (territories > LOTTERY_MAX_TERRITORIES)
|
|
533
|
+
return undefined;
|
|
534
|
+
return { lottery: true, lotteryBasis: [`${territories}-territory map`, ...shape].join(', ') };
|
|
535
|
+
}
|
|
536
|
+
return lotteryShape(game) ? { lottery: null, lotteryBasis: `${shape.join(', ')}; map size not read` } : undefined;
|
|
537
|
+
}
|