@chrischall/warapp-mcp 0.22.1 → 0.23.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 +3 -3
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +49 -3
- package/dist/analysis/coalition.js +3 -2
- package/dist/analysis/outlook.js +42 -2
- package/dist/analysis/plan/captures.js +2 -0
- package/dist/analysis/plan/report.js +2 -2
- package/dist/analysis/plan/targets.js +15 -5
- package/dist/analysis/plan/threats.js +55 -2
- package/dist/analysis/planner.js +2 -1
- package/dist/bundle.js +10186 -3727
- package/dist/chat.js +93 -3
- package/dist/clans.js +308 -0
- package/dist/forums.js +70 -0
- package/dist/friends.js +83 -0
- package/dist/index.js +1 -1
- package/dist/invite-code.js +46 -0
- package/dist/join-color.js +23 -13
- package/dist/ladders.js +360 -0
- package/dist/mail.js +295 -0
- package/dist/my-games.js +18 -1
- package/dist/notifications.js +22 -4
- package/dist/open-games.js +29 -2
- package/dist/outlook-log.js +1 -0
- package/dist/protocol/bundle.js +4 -4
- package/dist/protocol/codec.js +1 -1
- package/dist/protocol/methods.js +690 -8
- package/dist/protocol/rpc.js +28 -3
- package/dist/push/socketio.js +2 -2
- package/dist/quickmatch.js +264 -0
- package/dist/surrender-pending.js +62 -2
- package/dist/templates.js +451 -0
- package/dist/tools/boot-player.js +165 -0
- package/dist/tools/chat.js +19 -0
- package/dist/tools/clan-writes.js +208 -0
- package/dist/tools/clans.js +181 -0
- package/dist/tools/end-game-common.js +49 -0
- package/dist/tools/forums.js +535 -0
- package/dist/tools/friends.js +50 -0
- package/dist/tools/game.js +21 -3
- package/dist/tools/index.js +39 -0
- package/dist/tools/invites.js +347 -0
- package/dist/tools/join-game.js +4 -4
- package/dist/tools/ladders.js +418 -0
- package/dist/tools/leave-game.js +85 -27
- package/dist/tools/live-context.js +2 -2
- package/dist/tools/mail.js +600 -0
- package/dist/tools/my-games.js +50 -6
- package/dist/tools/notifications.js +12 -3
- package/dist/tools/nudge.js +178 -0
- package/dist/tools/quickmatch.js +481 -0
- package/dist/tools/react.js +177 -0
- package/dist/tools/session.js +6 -2
- package/dist/tools/team-surrender.js +206 -0
- package/dist/tools/templates.js +342 -0
- package/dist/tools/tournaments.js +495 -0
- package/dist/tools/turn.js +1 -1
- package/dist/tools/vote-to-end.js +142 -0
- package/dist/tools/write-kit.js +22 -1
- package/dist/tournaments.js +321 -0
- package/dist/version.js +1 -1
- package/manifest.json +190 -2
- package/mint.yaml +3 -4
- package/package.json +1 -1
- package/server.json +2 -2
- package/skills/warapp-mcp/SKILL.md +39 -6
package/dist/join-color.js
CHANGED
|
@@ -84,8 +84,8 @@ function colorsOfPlayer(p) {
|
|
|
84
84
|
return undefined;
|
|
85
85
|
return { ...(preferred !== null ? { preferred } : {}), ...(secondary !== null ? { secondary } : {}) };
|
|
86
86
|
}
|
|
87
|
-
/** The
|
|
88
|
-
function
|
|
87
|
+
/** The InitParameters record one candidate startup string decodes to, or `undefined`. */
|
|
88
|
+
function initParametersOf(run, bundle) {
|
|
89
89
|
const bytes = decodeStartupBytes(run);
|
|
90
90
|
if (!bytes)
|
|
91
91
|
return undefined;
|
|
@@ -96,30 +96,40 @@ function colorsIn(run, bundle) {
|
|
|
96
96
|
catch {
|
|
97
97
|
return undefined;
|
|
98
98
|
}
|
|
99
|
-
|
|
100
|
-
return undefined;
|
|
101
|
-
const player = fields(params).find((v) => isObj(v) && v.$type === PLAYER);
|
|
102
|
-
return player ? colorsOfPlayer(player) : undefined;
|
|
99
|
+
return isObj(params) && params.$type === INIT_PARAMETERS ? params : undefined;
|
|
103
100
|
}
|
|
101
|
+
/** The signed-in Player record the startup parameters carry, or `undefined` (signed out). */
|
|
102
|
+
export const signedInPlayerOf = (params) => fields(params).find((v) => isObj(v) && v.$type === PLAYER);
|
|
104
103
|
/**
|
|
105
|
-
* The
|
|
106
|
-
*
|
|
107
|
-
* that read (signed out, or the client changed). Never throws.
|
|
104
|
+
* The first candidate startup string on a war.app page whose InitParameters `pick` accepts (longest
|
|
105
|
+
* candidates first, at most {@link MAX_CANDIDATES}); `undefined` when none does. Never throws.
|
|
108
106
|
*/
|
|
109
|
-
export function
|
|
107
|
+
export function fromStartupParameters(html, bundle, pick) {
|
|
110
108
|
const runs = [...html.matchAll(RUN_RE)].map((m) => m[0]);
|
|
111
109
|
runs.sort((a, b) => b.length - a.length);
|
|
112
110
|
for (const run of runs.slice(0, MAX_CANDIDATES)) {
|
|
113
111
|
// A run can pick up a neighbouring `>` (a tag's end): try it trimmed on either side.
|
|
114
112
|
const tries = run.length % 2 === 0 ? [run] : [run.slice(1), run.slice(0, -1)];
|
|
115
113
|
for (const t of tries) {
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
114
|
+
const params = initParametersOf(t, bundle);
|
|
115
|
+
const got = params ? pick(params) : undefined;
|
|
116
|
+
if (got !== undefined)
|
|
117
|
+
return got;
|
|
119
118
|
}
|
|
120
119
|
}
|
|
121
120
|
return undefined;
|
|
122
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* The signed-in account's preferred and secondary colours, from the client
|
|
124
|
+
* startup parameters a war.app page carries; `undefined` when the page has none
|
|
125
|
+
* that read (signed out, or the client changed). Never throws.
|
|
126
|
+
*/
|
|
127
|
+
export function readAccountColors(html, bundle) {
|
|
128
|
+
return fromStartupParameters(html, bundle, (params) => {
|
|
129
|
+
const player = signedInPlayerOf(params);
|
|
130
|
+
return player ? colorsOfPlayer(player) : undefined;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
123
133
|
/**
|
|
124
134
|
* The colour to join with, as the web client picks it: the preferred colour when
|
|
125
135
|
* no lobby player has it, else the secondary, else the first standard colour
|
package/dist/ladders.js
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
// war.app ladders (#637): the seasons, a season's standings and games, one player's ladder page, the signed-in
|
|
2
|
+
// account's own standings, and the join/leave results. Replies are decoded by the client bundle's own readers by stable
|
|
3
|
+
// type name — `GetLadderSeasonsReturn`, `GetLadderSeasonReturn`, `GetLadderPlayersReturn`, `GetLadderGamesReturn`,
|
|
4
|
+
// `GetLadderPlayerReturn`, `LadderStat`, `LadderSeason`, `LadderPlayer`, `LadderGameInfo` / `LadderGame` /
|
|
5
|
+
// `LadderGamePlayer`, `GetLadderPlayerNotableWin`, `LadderHistoryReturnItem` and `PlayerInfo2` — fields by read order,
|
|
6
|
+
// checked by type, never by their minified names (docs/WEB-PROTOCOL.md, "Ladders"). UNVERIFIED live: read from the 6.09
|
|
7
|
+
// bundle, nothing here has been checked against a real reply.
|
|
8
|
+
//
|
|
9
|
+
// Personal fields never leave this file. Like scouting's profile allowlist (src/scout.ts), a player is projected to an
|
|
10
|
+
// ALLOWLIST: the in-game id, the name, and the ladder's own figures (rank, rating, best rank and rating, league, games
|
|
11
|
+
// at once, when they joined, TrueSkill mean and deviation) — never the clan, colour or anything else a PlayerInfo2
|
|
12
|
+
// carries, and of a player's metadata JSON only the two TrueSkill numbers (any other key is never read out). A game
|
|
13
|
+
// player's metadata is never read.
|
|
14
|
+
import { McpToolError } from '@chrischall/mcp-utils';
|
|
15
|
+
import { fields, isObj } from './protocol/decoded.js';
|
|
16
|
+
const T = 'warlight.shared.';
|
|
17
|
+
export const LADDER_STAT = `${T}LadderStat`;
|
|
18
|
+
export const LADDER_SEASON = `${T}LadderSeason`;
|
|
19
|
+
export const LADDER_PLAYER = `${T}LadderPlayer`;
|
|
20
|
+
export const GET_LADDER_SEASONS_RETURN = `${T}GetLadderSeasonsReturn`;
|
|
21
|
+
export const GET_LADDER_SEASON_RETURN = `${T}GetLadderSeasonReturn`;
|
|
22
|
+
export const GET_LADDER_PLAYERS_RETURN = `${T}GetLadderPlayersReturn`;
|
|
23
|
+
export const GET_LADDER_GAMES_RETURN = `${T}GetLadderGamesReturn`;
|
|
24
|
+
export const GET_LADDER_PLAYER_RETURN = `${T}GetLadderPlayerReturn`;
|
|
25
|
+
const LADDER_GAME_INFO = `${T}LadderGameInfo`;
|
|
26
|
+
const LADDER_GAME = `${T}LadderGame`;
|
|
27
|
+
const LADDER_GAME_PLAYER = `${T}LadderGamePlayer`;
|
|
28
|
+
const NOTABLE_WIN = `${T}GetLadderPlayerNotableWin`;
|
|
29
|
+
const HISTORY_ITEM = `${T}LadderHistoryReturnItem`;
|
|
30
|
+
const PLAYER_INFO = `${T}PlayerInfo2`;
|
|
31
|
+
/** The rank war.app sends for a player who is not ranked. */
|
|
32
|
+
export const NOT_RANKED = 2_000_000_000;
|
|
33
|
+
/** The most list entries read from one reply (a page is 50; a season's history can run long). */
|
|
34
|
+
const MAX_ENTRIES = 2000;
|
|
35
|
+
/** The newest rating-history points a player read returns. */
|
|
36
|
+
export const MAX_HISTORY_POINTS = 30;
|
|
37
|
+
/** The four ladders, as the client describes them (values read from the bundle, worded here). */
|
|
38
|
+
export const LADDERS = {
|
|
39
|
+
'1v1': { key: '1v1', id: 0, name: '1 v 1', seasonId: 0, teamSize: 1, gamesAtOnce: { min: 3, max: 5 }, minLevel: 48 },
|
|
40
|
+
'2v2': { key: '2v2', id: 1, name: '2 v 2', seasonId: 1, teamSize: 2, gamesAtOnce: { min: 2, max: 5 }, minLevel: 51 },
|
|
41
|
+
'3v3': { key: '3v3', id: 4, name: '3 v 3', seasonId: 4, teamSize: 3, gamesAtOnce: { min: 2, max: 5 }, minLevel: 54 },
|
|
42
|
+
seasonal: { key: 'seasonal', id: 2, name: 'Seasonal', seasonId: null, teamSize: 1, gamesAtOnce: null, minLevel: 52 },
|
|
43
|
+
};
|
|
44
|
+
export const LADDER_KEYS = Object.keys(LADDERS);
|
|
45
|
+
/** The ladder with this `LadderEnum` id. */
|
|
46
|
+
export function ladderOf(id) {
|
|
47
|
+
return Object.values(LADDERS).find((l) => l.id === id);
|
|
48
|
+
}
|
|
49
|
+
/** `LadderLeague`, by its byte. */
|
|
50
|
+
const LEAGUES = ['none', 'bronze', 'silver', 'gold', 'sapphire', 'emerald', 'ruby', 'diamond'];
|
|
51
|
+
/** `LadderSeasonState`, by its int. */
|
|
52
|
+
const SEASON_STATES = { 1: 'not-begun', 2: 'in-progress', 3: 'finished', 4: 'finishing' };
|
|
53
|
+
/** A ladder reply arrived in a shape this server does not understand. */
|
|
54
|
+
export class LadderFormatError extends McpToolError {
|
|
55
|
+
constructor(detail) {
|
|
56
|
+
super(`war.app's ladder reply arrived in an unexpected shape (${detail}).`, {
|
|
57
|
+
kind: 'unknown',
|
|
58
|
+
hint: 'The war.app client may have changed; the ladder can still be read in the war.app tab.',
|
|
59
|
+
});
|
|
60
|
+
this.name = 'LadderFormatError';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const isInt = (v) => typeof v === 'number' && Number.isInteger(v);
|
|
64
|
+
const isNum = (v) => typeof v === 'number' && Number.isFinite(v);
|
|
65
|
+
const dateOf = (v) => (v instanceof Date && Number.isFinite(v.getTime()) ? v.toISOString() : null);
|
|
66
|
+
function record(v, type, count) {
|
|
67
|
+
if (!isObj(v) || v.$type !== type)
|
|
68
|
+
throw new LadderFormatError(`expected a ${type.split('.').pop()}`);
|
|
69
|
+
const f = fields(v);
|
|
70
|
+
if (f.length !== count)
|
|
71
|
+
throw new LadderFormatError(`a ${type.split('.').pop()} has ${f.length} fields, not ${count}`);
|
|
72
|
+
return f;
|
|
73
|
+
}
|
|
74
|
+
function list(v, what) {
|
|
75
|
+
if (v === null || v === undefined)
|
|
76
|
+
return [];
|
|
77
|
+
if (!Array.isArray(v))
|
|
78
|
+
throw new LadderFormatError(`no ${what} list`);
|
|
79
|
+
if (v.length > MAX_ENTRIES)
|
|
80
|
+
throw new LadderFormatError(`${what}: ${v.length} entries`);
|
|
81
|
+
return v;
|
|
82
|
+
}
|
|
83
|
+
/** The id → name map of a PlayerInfo2 dict (pairs, as the interpreter gives a dict): of each, the name only. */
|
|
84
|
+
function namesOf(v) {
|
|
85
|
+
const out = new Map();
|
|
86
|
+
for (const e of list(v, 'player')) {
|
|
87
|
+
if (!Array.isArray(e) || e.length !== 2 || !isInt(e[0]))
|
|
88
|
+
continue;
|
|
89
|
+
if (e[1] === null || e[1] === undefined) {
|
|
90
|
+
out.set(e[0], null);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const name = record(e[1], PLAYER_INFO, 8)[4];
|
|
94
|
+
out.set(e[0], typeof name === 'string' ? name : null);
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
const LP = { bestLeague: 0, bestRank: 1, bestRating: 2, previousRank: 3, league: 4, rank: 5, rating: 6, since: 8, gamesAtOnce: 9, metadata: 11, playerId: 12 };
|
|
99
|
+
const LP_FIELDS = 15;
|
|
100
|
+
const rankOf = (v) => (isInt(v) && v > 0 && v !== NOT_RANKED ? v : null);
|
|
101
|
+
const leagueOf = (v) => (isInt(v) && v > 0 && v < LEAGUES.length ? LEAGUES[v] : null);
|
|
102
|
+
function trueSkillOf(metadata) {
|
|
103
|
+
if (typeof metadata !== 'string' || metadata.length > 10_000)
|
|
104
|
+
return null;
|
|
105
|
+
try {
|
|
106
|
+
const o = JSON.parse(metadata);
|
|
107
|
+
const mean = o?.TrueSkillMean;
|
|
108
|
+
const sd = o?.TrueSkillStandardDeviation;
|
|
109
|
+
return isNum(mean) && isNum(sd) ? { mean, sd } : null;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function standingOf(v, names) {
|
|
116
|
+
const f = record(v, LADDER_PLAYER, LP_FIELDS);
|
|
117
|
+
const playerId = f[LP.playerId];
|
|
118
|
+
const gamesAtOnce = f[LP.gamesAtOnce];
|
|
119
|
+
if (!isInt(playerId) || !isInt(gamesAtOnce))
|
|
120
|
+
throw new LadderFormatError('a LadderPlayer lacks its id');
|
|
121
|
+
const bestRating = f[LP.bestRating];
|
|
122
|
+
const rating = f[LP.rating];
|
|
123
|
+
// The allowlist: only these fields ever leave the parser.
|
|
124
|
+
return {
|
|
125
|
+
playerId,
|
|
126
|
+
name: names.get(playerId) ?? null,
|
|
127
|
+
rank: rankOf(f[LP.rank]),
|
|
128
|
+
rating: isNum(rating) ? rating : null,
|
|
129
|
+
bestRank: rankOf(f[LP.bestRank]),
|
|
130
|
+
bestRating: isNum(bestRating) && bestRating > 0 ? bestRating : null,
|
|
131
|
+
previousRank: rankOf(f[LP.previousRank]),
|
|
132
|
+
league: leagueOf(f[LP.league]),
|
|
133
|
+
bestLeague: leagueOf(f[LP.bestLeague]),
|
|
134
|
+
gamesAtOnce,
|
|
135
|
+
participating: gamesAtOnce > 0,
|
|
136
|
+
since: dateOf(f[LP.since]),
|
|
137
|
+
trueSkill: trueSkillOf(f[LP.metadata]),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const standings = (v, names, what) => list(v, what)
|
|
141
|
+
.filter((p) => p !== null && p !== undefined)
|
|
142
|
+
.map((p) => standingOf(p, names));
|
|
143
|
+
const LS = { id: 0, ladder: 1, metadata: 2, number: 3, state: 4, template: 5 };
|
|
144
|
+
function seasonOf(v, templates) {
|
|
145
|
+
const f = record(v, LADDER_SEASON, 6);
|
|
146
|
+
const seasonId = f[LS.id];
|
|
147
|
+
if (!isInt(seasonId))
|
|
148
|
+
throw new LadderFormatError('a LadderSeason lacks its id');
|
|
149
|
+
let meta = {};
|
|
150
|
+
if (typeof f[LS.metadata] === 'string' && f[LS.metadata].length <= 100_000) {
|
|
151
|
+
try {
|
|
152
|
+
const parsed = JSON.parse(f[LS.metadata]);
|
|
153
|
+
if (isObj(parsed))
|
|
154
|
+
meta = parsed;
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// Unreadable metadata: the dates and counts stay null.
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const startMs = typeof meta.StartDate === 'string' ? Date.parse(meta.StartDate) : Number.NaN;
|
|
161
|
+
const lengthDays = isNum(meta.LengthInDays) ? meta.LengthInDays : null;
|
|
162
|
+
const start = Number.isFinite(startMs) ? new Date(startMs) : null;
|
|
163
|
+
const templateId = isInt(f[LS.template]) ? f[LS.template] : null;
|
|
164
|
+
const state = f[LS.state];
|
|
165
|
+
return {
|
|
166
|
+
seasonId,
|
|
167
|
+
ladder: ladderOf(f[LS.ladder])?.key ?? null,
|
|
168
|
+
number: isInt(f[LS.number]) ? f[LS.number] : null,
|
|
169
|
+
state: isInt(state) ? (SEASON_STATES[state] ?? null) : null,
|
|
170
|
+
templateId,
|
|
171
|
+
template: templateId !== null ? (templates?.get(templateId) ?? null) : null,
|
|
172
|
+
start: start ? start.toISOString() : null,
|
|
173
|
+
end: start && lengthDays !== null ? new Date(start.getTime() + lengthDays * 86_400_000).toISOString() : null,
|
|
174
|
+
lengthDays,
|
|
175
|
+
gamesInSeason: isInt(meta.NumberOfGamesInSeason) ? meta.NumberOfGamesInSeason : null,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function gameOf(v, names) {
|
|
179
|
+
if (v === null || v === undefined)
|
|
180
|
+
return null;
|
|
181
|
+
const [game, players] = record(v, LADDER_GAME_INFO, 2);
|
|
182
|
+
if (game === null || game === undefined)
|
|
183
|
+
return null;
|
|
184
|
+
const g = record(game, LADDER_GAME, 3);
|
|
185
|
+
const gameId = g[1];
|
|
186
|
+
if (!isInt(gameId))
|
|
187
|
+
throw new LadderFormatError('a LadderGame lacks its id');
|
|
188
|
+
return {
|
|
189
|
+
gameId,
|
|
190
|
+
seasonId: isInt(g[2]) ? g[2] : null,
|
|
191
|
+
finished: dateOf(g[0]),
|
|
192
|
+
players: list(players, 'game player')
|
|
193
|
+
.filter((p) => p !== null && p !== undefined)
|
|
194
|
+
.map((p) => {
|
|
195
|
+
// A game player's metadata (its field 1) is never read.
|
|
196
|
+
const r = record(p, LADDER_GAME_PLAYER, 5);
|
|
197
|
+
const playerId = r[2];
|
|
198
|
+
if (!isInt(playerId))
|
|
199
|
+
throw new LadderFormatError('a LadderGamePlayer lacks its id');
|
|
200
|
+
return { playerId, name: names.get(playerId) ?? null, team: isInt(r[3]) ? r[3] : null, won: typeof r[4] === 'boolean' ? r[4] : null };
|
|
201
|
+
}),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
const gamesOf = (v, names) => list(v, 'game')
|
|
205
|
+
.map((g) => gameOf(g, names))
|
|
206
|
+
.filter((g) => g !== null);
|
|
207
|
+
/** The class a reply's result holds (after its present flag), or null when absent. */
|
|
208
|
+
function result(bundle, body, type) {
|
|
209
|
+
if (!body.bool())
|
|
210
|
+
return null;
|
|
211
|
+
const ret = bundle.readClass(type, body);
|
|
212
|
+
if (!isObj(ret) || ret.$type !== type)
|
|
213
|
+
throw new LadderFormatError(`no ${type.split('.').pop()}`);
|
|
214
|
+
return fields(ret);
|
|
215
|
+
}
|
|
216
|
+
/** Read a GetLadderStats result: an optional list of optional LadderStat. */
|
|
217
|
+
export function readLadderStats(bundle, body) {
|
|
218
|
+
if (!body.bool())
|
|
219
|
+
return [];
|
|
220
|
+
const n = body.int32();
|
|
221
|
+
if (n === -1)
|
|
222
|
+
return [];
|
|
223
|
+
if (n < 0 || n > 64)
|
|
224
|
+
throw new LadderFormatError(`${n} ladder stats`);
|
|
225
|
+
const out = [];
|
|
226
|
+
for (let i = 0; i < n; i++) {
|
|
227
|
+
if (!body.bool())
|
|
228
|
+
continue;
|
|
229
|
+
const f = record(bundle.readClass(LADDER_STAT, body), LADDER_STAT, 3);
|
|
230
|
+
out.push({
|
|
231
|
+
ladder: ladderOf(f[0])?.key ?? null,
|
|
232
|
+
season: isInt(f[2]) ? f[2] : null,
|
|
233
|
+
standing: f[1] === null || f[1] === undefined ? null : standingOf(f[1], new Map()),
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return out;
|
|
237
|
+
}
|
|
238
|
+
/** Read a GetLadderSeasons result: the seasonal ladder's seasons, newest first as war.app sends them. */
|
|
239
|
+
export function readLadderSeasons(bundle, body) {
|
|
240
|
+
const f = result(bundle, body, GET_LADDER_SEASONS_RETURN);
|
|
241
|
+
if (!f)
|
|
242
|
+
return [];
|
|
243
|
+
if (f.length !== 4)
|
|
244
|
+
throw new LadderFormatError(`a GetLadderSeasonsReturn has ${f.length} fields, not 4`);
|
|
245
|
+
const names = namesOf(f[0]);
|
|
246
|
+
const templates = new Map();
|
|
247
|
+
for (const e of list(f[2], 'template'))
|
|
248
|
+
if (Array.isArray(e) && isInt(e[0]) && typeof e[1] === 'string')
|
|
249
|
+
templates.set(e[0], e[1]);
|
|
250
|
+
const winners = new Map();
|
|
251
|
+
for (const e of list(f[3], 'winner'))
|
|
252
|
+
if (Array.isArray(e) && isInt(e[0]))
|
|
253
|
+
winners.set(e[0], list(e[1], 'winner id').filter(isInt));
|
|
254
|
+
return list(f[1], 'season')
|
|
255
|
+
.filter((s) => s !== null && s !== undefined)
|
|
256
|
+
.map((s) => {
|
|
257
|
+
const season = seasonOf(s, templates);
|
|
258
|
+
return { ...season, winners: (winners.get(season.seasonId) ?? []).map((playerId) => ({ playerId, name: names.get(playerId) ?? null })) };
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
export function readLadderSeason(bundle, body) {
|
|
262
|
+
const f = result(bundle, body, GET_LADDER_SEASON_RETURN);
|
|
263
|
+
if (!f)
|
|
264
|
+
throw new LadderFormatError('no season in the reply');
|
|
265
|
+
if (f.length !== 10)
|
|
266
|
+
throw new LadderFormatError(`a GetLadderSeasonReturn has ${f.length} fields, not 10`);
|
|
267
|
+
const names = namesOf(f[2]);
|
|
268
|
+
if (f[6] === null || f[6] === undefined)
|
|
269
|
+
throw new LadderFormatError('no LadderSeason');
|
|
270
|
+
const template = typeof f[8] === 'string' ? f[8] : null;
|
|
271
|
+
const season = seasonOf(f[6]);
|
|
272
|
+
const top = standings(f[1], names, 'top player');
|
|
273
|
+
const shown = new Set(top.map((p) => p.playerId));
|
|
274
|
+
return {
|
|
275
|
+
season: { ...season, template: season.template ?? template },
|
|
276
|
+
template,
|
|
277
|
+
playerCount: isInt(f[4]) ? f[4] : null,
|
|
278
|
+
top,
|
|
279
|
+
aroundMe: standings(f[0], names, 'player').filter((p) => !shown.has(p.playerId)),
|
|
280
|
+
me: f[9] === null || f[9] === undefined ? null : standingOf(f[9], names),
|
|
281
|
+
games: gamesOf(f[5], names),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/** Read a GetLadderPlayers page. */
|
|
285
|
+
export function readLadderPlayers(bundle, body) {
|
|
286
|
+
const f = result(bundle, body, GET_LADDER_PLAYERS_RETURN);
|
|
287
|
+
if (!f)
|
|
288
|
+
return { players: [], total: null };
|
|
289
|
+
if (f.length !== 3)
|
|
290
|
+
throw new LadderFormatError(`a GetLadderPlayersReturn has ${f.length} fields, not 3`);
|
|
291
|
+
return { players: standings(f[1], namesOf(f[0]), 'player'), total: isInt(f[2]) ? f[2] : null };
|
|
292
|
+
}
|
|
293
|
+
/** Read a GetLadderGames page. */
|
|
294
|
+
export function readLadderGames(bundle, body) {
|
|
295
|
+
const f = result(bundle, body, GET_LADDER_GAMES_RETURN);
|
|
296
|
+
if (!f)
|
|
297
|
+
return { games: [], total: null };
|
|
298
|
+
if (f.length !== 4)
|
|
299
|
+
throw new LadderFormatError(`a GetLadderGamesReturn has ${f.length} fields, not 4`);
|
|
300
|
+
return { games: gamesOf(f[0], namesOf(f[1])), total: isInt(f[3]) ? f[3] : null };
|
|
301
|
+
}
|
|
302
|
+
export function recordOf(games, playerId) {
|
|
303
|
+
const rec = { wins: 0, losses: 0, inProgress: 0, of: 0 };
|
|
304
|
+
for (const g of games) {
|
|
305
|
+
const seat = g.players.find((p) => p.playerId === playerId);
|
|
306
|
+
if (!seat)
|
|
307
|
+
continue;
|
|
308
|
+
rec.of += 1;
|
|
309
|
+
if (g.finished === null)
|
|
310
|
+
rec.inProgress += 1;
|
|
311
|
+
else if (seat.won === true)
|
|
312
|
+
rec.wins += 1;
|
|
313
|
+
else if (seat.won === false || g.players.some((p) => p.won === true))
|
|
314
|
+
rec.losses += 1;
|
|
315
|
+
}
|
|
316
|
+
return rec;
|
|
317
|
+
}
|
|
318
|
+
export function readLadderPlayer(bundle, body, playerId) {
|
|
319
|
+
const f = result(bundle, body, GET_LADDER_PLAYER_RETURN);
|
|
320
|
+
if (!f)
|
|
321
|
+
throw new LadderFormatError('no player in the reply');
|
|
322
|
+
if (f.length !== 9)
|
|
323
|
+
throw new LadderFormatError(`a GetLadderPlayerReturn has ${f.length} fields, not 9`);
|
|
324
|
+
const names = namesOf(f[4]);
|
|
325
|
+
const players = standings(f[2], names, 'player');
|
|
326
|
+
const player = players.find((p) => p.playerId === playerId);
|
|
327
|
+
if (!player)
|
|
328
|
+
throw new LadderFormatError('the player asked is not in the reply');
|
|
329
|
+
const byId = new Map(players.map((p) => [p.playerId, p]));
|
|
330
|
+
if (f[6] === null || f[6] === undefined)
|
|
331
|
+
throw new LadderFormatError('no LadderSeason');
|
|
332
|
+
const games = gamesOf(f[5], names);
|
|
333
|
+
const history = list(f[0], 'history')
|
|
334
|
+
.map((h) => record(h, HISTORY_ITEM, 3))
|
|
335
|
+
.filter((h) => isInt(h[0]))
|
|
336
|
+
.map((h) => ({ point: h[0], rank: rankOf(h[1]), rating: isNum(h[2]) ? h[2] : null }))
|
|
337
|
+
.sort((a, b) => a.point - b.point)
|
|
338
|
+
.slice(-MAX_HISTORY_POINTS);
|
|
339
|
+
return {
|
|
340
|
+
season: seasonOf(f[6]),
|
|
341
|
+
player,
|
|
342
|
+
teammates: list(f[8], 'teammate')
|
|
343
|
+
.filter(isInt)
|
|
344
|
+
.map((id) => byId.get(id))
|
|
345
|
+
.filter((p) => p !== undefined),
|
|
346
|
+
notableWins: list(f[3], 'notable win').map((n) => {
|
|
347
|
+
const r = record(n, NOTABLE_WIN, 3);
|
|
348
|
+
if (!isInt(r[1]))
|
|
349
|
+
throw new LadderFormatError('a notable win lacks its game id');
|
|
350
|
+
return { gameId: r[1], at: dateOf(r[0]), opponent: r[2] === null || r[2] === undefined ? null : standingOf(r[2], names) };
|
|
351
|
+
}),
|
|
352
|
+
ratingHistory: history,
|
|
353
|
+
games,
|
|
354
|
+
record: recordOf(games, playerId),
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
/** A JoinLadder result: whether joining ended the account's vacation; null when the reply carries none. */
|
|
358
|
+
export function readJoinLadderResult(body) {
|
|
359
|
+
return body.remaining > 0 ? body.bool() : null;
|
|
360
|
+
}
|