osrs-json-hiscores 2.26.0 → 2.27.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/README.md CHANGED
@@ -150,8 +150,9 @@ Activities consist of all levels of clue scrolls as well as minigames and bosses
150
150
  | Kreearra | `kreeArra` |
151
151
  | K'ril Tsutsaroth | `krilTsutsaroth` |
152
152
  | Lunar Chests | `lunarChests` |
153
- | Mimic | `mimic` |
153
+ | Mad Angel | `madAngel` |
154
154
  | Maggot King | `maggotKing` |
155
+ | Mimic | `mimic` |
155
156
  | Nex | `nex` |
156
157
  | Nightmare | `nightmare` |
157
158
  | Phosani's Nightmare | `phosanisNightmare` |
@@ -0,0 +1,64 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+ import { Player, Stats, PlayerSkillRow, PlayerActivityRow, GetStatsOptions, HiscoresResponse } from './types';
3
+ import { Gamemode, SkillName, ActivityName } from './utils';
4
+ /**
5
+ * Gets a player's stats from the official OSRS JSON endpoint.
6
+ *
7
+ * @param rsn Username of the player.
8
+ * @param mode Gamemode to fetch ranks for.
9
+ * @param config Optional axios request config object.
10
+ * @returns Official JSON stats object.
11
+ */
12
+ export declare function getOfficialStats(rsn: string, mode?: Gamemode, config?: AxiosRequestConfig): Promise<HiscoresResponse>;
13
+ /**
14
+ * Screen scrapes the hiscores to get the formatted rsn of a player.
15
+ *
16
+ * @param rsn Username of the player.
17
+ * @param config Optional axios request config object.
18
+ * @returns Formatted version of the rsn.
19
+ */
20
+ export declare function getRSNFormat(rsn: string, config?: AxiosRequestConfig, mode?: Gamemode): Promise<string>;
21
+ /**
22
+ * Parses official JSON object of raw stats and returns a stats object.
23
+ *
24
+ * @param csv Raw JSON from the official OSRS API.
25
+ * @returns Parsed stats object.
26
+ */
27
+ export declare function parseJsonStats(json: HiscoresResponse): Stats;
28
+ /**
29
+ * Parses CSV string of raw stats and returns a stats object.
30
+ *
31
+ * @param csv Raw CSV from the official OSRS API.
32
+ * @returns Parsed stats object.
33
+ */
34
+ export declare function parseStats(csv: string): Stats;
35
+ /**
36
+ * Fetches stats from the OSRS API and consolidates the info into a player object.
37
+ *
38
+ * **Note:** This function will make up to 5 separate network requests.
39
+ * As such, it is highly subject to the performance of the official OSRS API.
40
+ *
41
+ * @param rsn Username of the player.
42
+ * @returns Player object.
43
+ */
44
+ export declare function getStats(rsn: string, options?: GetStatsOptions): Promise<Player>;
45
+ /**
46
+ * Fetches stats from the OSRS API and returns them as an object.
47
+ *
48
+ * @param rsn Username of the player.
49
+ * @param mode Gamemode to fetch ranks for.
50
+ * @param config Optional axios request config object.
51
+ * @returns Stats object.
52
+ */
53
+ export declare function getStatsByGamemode(rsn: string, mode?: Gamemode, config?: AxiosRequestConfig): Promise<Stats>;
54
+ export declare function getSkillPage(skill: SkillName, mode?: Gamemode, page?: number, config?: AxiosRequestConfig): Promise<PlayerSkillRow[]>;
55
+ /**
56
+ * Screen scrapes a hiscores page of an activity or boss and returns an array of up to 25 players.
57
+ *
58
+ * @param activity Name of the activity or boss to fetch hiscores for.
59
+ * @param mode Gamemode to fetch ranks for.
60
+ * @param page Page number.
61
+ * @param config Optional axios request config object.
62
+ * @returns Array of `PlayerActivityRow` objects.
63
+ */
64
+ export declare function getActivityPage(activity: ActivityName, mode?: Gamemode, page?: number, config?: AxiosRequestConfig): Promise<PlayerActivityRow[]>;
@@ -0,0 +1,502 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.getOfficialStats = getOfficialStats;
54
+ exports.getRSNFormat = getRSNFormat;
55
+ exports.parseJsonStats = parseJsonStats;
56
+ exports.parseStats = parseStats;
57
+ exports.getStats = getStats;
58
+ exports.getStatsByGamemode = getStatsByGamemode;
59
+ exports.getSkillPage = getSkillPage;
60
+ exports.getActivityPage = getActivityPage;
61
+ var axios_1 = __importDefault(require("axios"));
62
+ var jsdom_1 = require("jsdom");
63
+ var utils_1 = require("./utils");
64
+ /**
65
+ * Gets a player's stats from the official OSRS JSON endpoint.
66
+ *
67
+ * @param rsn Username of the player.
68
+ * @param mode Gamemode to fetch ranks for.
69
+ * @param config Optional axios request config object.
70
+ * @returns Official JSON stats object.
71
+ */
72
+ function getOfficialStats(rsn_1) {
73
+ return __awaiter(this, arguments, void 0, function (rsn, mode, config) {
74
+ var url, response, err_1;
75
+ var _a;
76
+ if (mode === void 0) { mode = 'main'; }
77
+ return __generator(this, function (_b) {
78
+ switch (_b.label) {
79
+ case 0:
80
+ (0, utils_1.validateRSN)(rsn);
81
+ url = (0, utils_1.getStatsURL)(mode, rsn, true);
82
+ _b.label = 1;
83
+ case 1:
84
+ _b.trys.push([1, 3, , 4]);
85
+ return [4 /*yield*/, (0, utils_1.httpGet)(url, config)];
86
+ case 2:
87
+ response = _b.sent();
88
+ return [2 /*return*/, response.data];
89
+ case 3:
90
+ err_1 = _b.sent();
91
+ if (!axios_1.default.isAxiosError(err_1))
92
+ throw err_1;
93
+ if (((_a = err_1.response) === null || _a === void 0 ? void 0 : _a.status) === 404)
94
+ throw new utils_1.PlayerNotFoundError();
95
+ throw new utils_1.HiScoresError();
96
+ case 4: return [2 /*return*/];
97
+ }
98
+ });
99
+ });
100
+ }
101
+ /**
102
+ * Screen scrapes the hiscores to get the formatted rsn of a player.
103
+ *
104
+ * @param rsn Username of the player.
105
+ * @param config Optional axios request config object.
106
+ * @returns Formatted version of the rsn.
107
+ */
108
+ function getRSNFormat(rsn_1, config_1) {
109
+ return __awaiter(this, arguments, void 0, function (rsn, config, mode) {
110
+ var url, response, dom, row, innerHTML, _a;
111
+ var _b, _c;
112
+ if (mode === void 0) { mode = 'main'; }
113
+ return __generator(this, function (_d) {
114
+ switch (_d.label) {
115
+ case 0:
116
+ (0, utils_1.validateRSN)(rsn);
117
+ url = (0, utils_1.getPlayerTableURL)(mode, rsn);
118
+ _d.label = 1;
119
+ case 1:
120
+ _d.trys.push([1, 3, , 4]);
121
+ return [4 /*yield*/, (0, utils_1.httpGet)(url, config)];
122
+ case 2:
123
+ response = _d.sent();
124
+ dom = new jsdom_1.JSDOM(response.data);
125
+ row = dom.window.document.querySelector('tr.personal-hiscores__row.personal-hiscores__row--type-highlight');
126
+ if (row) {
127
+ innerHTML = (row !== null && row !== void 0 ? row : {}).innerHTML;
128
+ return [2 /*return*/, ((_c = (_b = innerHTML === null || innerHTML === void 0 ? void 0 : innerHTML.match(new RegExp(rsn.replace(utils_1.WHITESPACE_REGEX, utils_1.WHITESPACE_REGEX_STRING), 'gi'))) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : rsn)];
129
+ }
130
+ return [3 /*break*/, 4];
131
+ case 3:
132
+ _a = _d.sent();
133
+ throw new utils_1.HiScoresError();
134
+ case 4: throw new utils_1.PlayerNotFoundError();
135
+ }
136
+ });
137
+ });
138
+ }
139
+ /**
140
+ * Parses official JSON object of raw stats and returns a stats object.
141
+ *
142
+ * @param csv Raw JSON from the official OSRS API.
143
+ * @returns Parsed stats object.
144
+ */
145
+ function parseJsonStats(json) {
146
+ var getActivity = function (formattedName) {
147
+ var _a, _b;
148
+ var hiscoresActivity = json.activities.find(
149
+ // We must match on name here since id is not guaranteed to be the same between updates
150
+ function (_a) {
151
+ var name = _a.name;
152
+ return name.toLowerCase() === formattedName.toLowerCase();
153
+ });
154
+ return {
155
+ rank: (_a = hiscoresActivity === null || hiscoresActivity === void 0 ? void 0 : hiscoresActivity.rank) !== null && _a !== void 0 ? _a : -1,
156
+ score: (_b = hiscoresActivity === null || hiscoresActivity === void 0 ? void 0 : hiscoresActivity.score) !== null && _b !== void 0 ? _b : -1
157
+ };
158
+ };
159
+ var reduceActivity = function (keys, formattedNames) {
160
+ return keys.reduce(function (reducer, key) {
161
+ var _a;
162
+ return (__assign(__assign({}, reducer), (_a = {}, _a[key] = getActivity(formattedNames[key]), _a)));
163
+ }, {});
164
+ };
165
+ var skills = utils_1.SKILLS.reduce(function (skillsObject, skillName) {
166
+ var _a;
167
+ var _b, _c, _d;
168
+ var hiscoresSkill = json.skills.find(
169
+ // We must match on name here since id is not guaranteed to be the same between updates
170
+ function (_a) {
171
+ var name = _a.name;
172
+ return name.toLowerCase() === utils_1.FORMATTED_SKILL_NAMES[skillName].toLowerCase();
173
+ });
174
+ return __assign(__assign({}, skillsObject), (_a = {}, _a[skillName] = {
175
+ rank: (_b = hiscoresSkill === null || hiscoresSkill === void 0 ? void 0 : hiscoresSkill.rank) !== null && _b !== void 0 ? _b : -1,
176
+ level: (_c = hiscoresSkill === null || hiscoresSkill === void 0 ? void 0 : hiscoresSkill.level) !== null && _c !== void 0 ? _c : -1,
177
+ xp: (_d = hiscoresSkill === null || hiscoresSkill === void 0 ? void 0 : hiscoresSkill.xp) !== null && _d !== void 0 ? _d : -1
178
+ }, _a));
179
+ }, {});
180
+ var bountyHunter = reduceActivity(utils_1.BH_MODES, utils_1.FORMATTED_BH_NAMES);
181
+ var clues = reduceActivity(utils_1.CLUES, utils_1.FORMATTED_CLUE_NAMES);
182
+ var bosses = reduceActivity(utils_1.BOSSES, utils_1.FORMATTED_BOSS_NAMES);
183
+ var leaguePoints = getActivity(utils_1.FORMATTED_LEAGUE_POINTS);
184
+ var deadmanPoints = getActivity(utils_1.FORMATTED_DEADMAN_POINTS);
185
+ var lastManStanding = getActivity(utils_1.FORMATTED_LMS);
186
+ var pvpArena = getActivity(utils_1.FORMATTED_PVP_ARENA);
187
+ var soulWarsZeal = getActivity(utils_1.FORMATTED_SOUL_WARS);
188
+ var riftsClosed = getActivity(utils_1.FORMATTED_RIFTS_CLOSED);
189
+ var colosseumGlory = getActivity(utils_1.FORMATTED_COLOSSEUM_GLORY);
190
+ var collectionsLogged = getActivity(utils_1.FORMATTED_COLLECTIONS_LOGGED);
191
+ var stats = {
192
+ skills: skills,
193
+ leaguePoints: leaguePoints,
194
+ deadmanPoints: deadmanPoints,
195
+ bountyHunter: bountyHunter,
196
+ lastManStanding: lastManStanding,
197
+ pvpArena: pvpArena,
198
+ soulWarsZeal: soulWarsZeal,
199
+ riftsClosed: riftsClosed,
200
+ colosseumGlory: colosseumGlory,
201
+ collectionsLogged: collectionsLogged,
202
+ clues: clues,
203
+ bosses: bosses
204
+ };
205
+ return stats;
206
+ }
207
+ /**
208
+ * Parses CSV string of raw stats and returns a stats object.
209
+ *
210
+ * @param csv Raw CSV from the official OSRS API.
211
+ * @returns Parsed stats object.
212
+ */
213
+ function parseStats(csv) {
214
+ var splitCSV = csv
215
+ .split('\n')
216
+ .filter(function (entry) { return !!entry; })
217
+ .map(function (stat) { return stat.split(','); });
218
+ if (splitCSV.length !== utils_1.SKILLS.length + utils_1.ACTIVITIES.length) {
219
+ throw new utils_1.InvalidFormatError();
220
+ }
221
+ var skillObjects = splitCSV
222
+ .filter(function (stat) { return stat.length === 3; })
223
+ .map(function (stat) {
224
+ var rank = stat[0], level = stat[1], xp = stat[2];
225
+ var skill = {
226
+ rank: parseInt(rank, 10),
227
+ level: parseInt(level, 10),
228
+ xp: parseInt(xp, 10)
229
+ };
230
+ return skill;
231
+ });
232
+ var activityObjects = splitCSV
233
+ .filter(function (stat) { return stat.length === 2; })
234
+ .map(function (stat) {
235
+ var rank = stat[0], score = stat[1];
236
+ var activity = {
237
+ rank: parseInt(rank, 10),
238
+ score: parseInt(score, 10)
239
+ };
240
+ return activity;
241
+ });
242
+ var _a = activityObjects.splice(0, 2), leaguePoints = _a[0], deadmanPoints = _a[1];
243
+ var bhObjects = activityObjects.splice(0, utils_1.BH_MODES.length);
244
+ var clueObjects = activityObjects.splice(0, utils_1.CLUES.length);
245
+ var _b = activityObjects.splice(0, 6), lastManStanding = _b[0], pvpArena = _b[1], soulWarsZeal = _b[2], riftsClosed = _b[3], colosseumGlory = _b[4], collectionsLogged = _b[5];
246
+ var bossObjects = activityObjects.splice(0, utils_1.BOSSES.length);
247
+ var skills = skillObjects.reduce(function (prev, curr, index) {
248
+ var newSkills = __assign({}, prev);
249
+ newSkills[utils_1.SKILLS[index]] = curr;
250
+ return newSkills;
251
+ }, {});
252
+ var bountyHunter = bhObjects.reduce(function (prev, curr, index) {
253
+ var newBH = __assign({}, prev);
254
+ newBH[utils_1.BH_MODES[index]] = curr;
255
+ return newBH;
256
+ }, {});
257
+ var clues = clueObjects.reduce(function (prev, curr, index) {
258
+ var newClues = __assign({}, prev);
259
+ newClues[utils_1.CLUES[index]] = curr;
260
+ return newClues;
261
+ }, {});
262
+ var bosses = bossObjects.reduce(function (prev, curr, index) {
263
+ var newBosses = __assign({}, prev);
264
+ newBosses[utils_1.BOSSES[index]] = curr;
265
+ return newBosses;
266
+ }, {});
267
+ var stats = {
268
+ skills: skills,
269
+ leaguePoints: leaguePoints,
270
+ deadmanPoints: deadmanPoints,
271
+ bountyHunter: bountyHunter,
272
+ lastManStanding: lastManStanding,
273
+ pvpArena: pvpArena,
274
+ soulWarsZeal: soulWarsZeal,
275
+ riftsClosed: riftsClosed,
276
+ colosseumGlory: colosseumGlory,
277
+ collectionsLogged: collectionsLogged,
278
+ clues: clues,
279
+ bosses: bosses
280
+ };
281
+ return stats;
282
+ }
283
+ /**
284
+ * Fetches stats from the OSRS API and consolidates the info into a player object.
285
+ *
286
+ * **Note:** This function will make up to 5 separate network requests.
287
+ * As such, it is highly subject to the performance of the official OSRS API.
288
+ *
289
+ * @param rsn Username of the player.
290
+ * @returns Player object.
291
+ */
292
+ function getStats(rsn, options) {
293
+ return __awaiter(this, void 0, void 0, function () {
294
+ var otherGamemodes, shouldGetFormattedRsn, main, getModeStats, formattedName, _a, player, iron, hc, ult;
295
+ var _this = this;
296
+ var _b, _c, _d, _e;
297
+ return __generator(this, function (_f) {
298
+ switch (_f.label) {
299
+ case 0:
300
+ (0, utils_1.validateRSN)(rsn);
301
+ otherGamemodes = (_b = options === null || options === void 0 ? void 0 : options.otherGamemodes) !== null && _b !== void 0 ? _b : [
302
+ 'ironman',
303
+ 'hardcore',
304
+ 'ultimate'
305
+ ];
306
+ shouldGetFormattedRsn = (_c = options === null || options === void 0 ? void 0 : options.shouldGetFormattedRsn) !== null && _c !== void 0 ? _c : true;
307
+ return [4 /*yield*/, getOfficialStats(rsn, 'main', (_d = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _d === void 0 ? void 0 : _d.main)];
308
+ case 1:
309
+ main = _f.sent();
310
+ getModeStats = function (mode) { return __awaiter(_this, void 0, void 0, function () {
311
+ var _a;
312
+ return __generator(this, function (_b) {
313
+ return [2 /*return*/, otherGamemodes.includes(mode)
314
+ ? getOfficialStats(rsn, mode, (_a = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _a === void 0 ? void 0 : _a[mode]).catch(function () { return undefined; })
315
+ : undefined];
316
+ });
317
+ }); };
318
+ if (!shouldGetFormattedRsn) return [3 /*break*/, 3];
319
+ return [4 /*yield*/, getRSNFormat(rsn, (_e = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _e === void 0 ? void 0 : _e.rsn).catch(function () { return undefined; })];
320
+ case 2:
321
+ _a = _f.sent();
322
+ return [3 /*break*/, 4];
323
+ case 3:
324
+ _a = undefined;
325
+ _f.label = 4;
326
+ case 4:
327
+ formattedName = _a;
328
+ player = {
329
+ name: formattedName !== null && formattedName !== void 0 ? formattedName : rsn,
330
+ mode: 'main',
331
+ dead: false,
332
+ deulted: false,
333
+ deironed: false
334
+ };
335
+ player.main = parseJsonStats(main);
336
+ return [4 /*yield*/, getModeStats('ironman')];
337
+ case 5:
338
+ iron = _f.sent();
339
+ if (!iron) return [3 /*break*/, 8];
340
+ player.ironman = parseJsonStats(iron);
341
+ return [4 /*yield*/, getModeStats('hardcore')];
342
+ case 6:
343
+ hc = _f.sent();
344
+ return [4 /*yield*/, getModeStats('ultimate')];
345
+ case 7:
346
+ ult = _f.sent();
347
+ if (hc) {
348
+ player.mode = 'hardcore';
349
+ player.hardcore = parseJsonStats(hc);
350
+ if (player.ironman.skills.overall.xp !== player.hardcore.skills.overall.xp) {
351
+ player.dead = true;
352
+ player.mode = 'ironman';
353
+ }
354
+ if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
355
+ player.deironed = true;
356
+ player.mode = 'main';
357
+ }
358
+ }
359
+ else if (ult) {
360
+ player.mode = 'ultimate';
361
+ player.ultimate = parseJsonStats(ult);
362
+ if (player.ironman.skills.overall.xp !== player.ultimate.skills.overall.xp) {
363
+ player.deulted = true;
364
+ player.mode = 'ironman';
365
+ }
366
+ if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
367
+ player.deironed = true;
368
+ player.mode = 'main';
369
+ }
370
+ }
371
+ else {
372
+ player.mode = 'ironman';
373
+ if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
374
+ player.deironed = true;
375
+ player.mode = 'main';
376
+ }
377
+ }
378
+ _f.label = 8;
379
+ case 8: return [2 /*return*/, player];
380
+ }
381
+ });
382
+ });
383
+ }
384
+ /**
385
+ * Fetches stats from the OSRS API and returns them as an object.
386
+ *
387
+ * @param rsn Username of the player.
388
+ * @param mode Gamemode to fetch ranks for.
389
+ * @param config Optional axios request config object.
390
+ * @returns Stats object.
391
+ */
392
+ function getStatsByGamemode(rsn_1) {
393
+ return __awaiter(this, arguments, void 0, function (rsn, mode, config) {
394
+ var response, stats;
395
+ if (mode === void 0) { mode = 'main'; }
396
+ return __generator(this, function (_a) {
397
+ switch (_a.label) {
398
+ case 0:
399
+ (0, utils_1.validateRSN)(rsn);
400
+ if (!utils_1.GAMEMODES.includes(mode)) {
401
+ throw Error('Invalid game mode');
402
+ }
403
+ return [4 /*yield*/, getOfficialStats(rsn, mode, config)];
404
+ case 1:
405
+ response = _a.sent();
406
+ stats = parseJsonStats(response);
407
+ return [2 /*return*/, stats];
408
+ }
409
+ });
410
+ });
411
+ }
412
+ function getSkillPage(skill_1) {
413
+ return __awaiter(this, arguments, void 0, function (skill, mode, page, config) {
414
+ var url, response, dom, playersHTML, players;
415
+ if (mode === void 0) { mode = 'main'; }
416
+ if (page === void 0) { page = 1; }
417
+ return __generator(this, function (_a) {
418
+ switch (_a.label) {
419
+ case 0:
420
+ if (!utils_1.GAMEMODES.includes(mode)) {
421
+ throw Error('Invalid game mode');
422
+ }
423
+ else if (!Number.isInteger(page) || page < 1) {
424
+ throw Error('Page must be an integer greater than 0');
425
+ }
426
+ else if (!utils_1.SKILLS.includes(skill)) {
427
+ throw Error('Invalid skill');
428
+ }
429
+ url = (0, utils_1.getSkillPageURL)(mode, skill, page);
430
+ return [4 /*yield*/, (0, utils_1.httpGet)(url, config)];
431
+ case 1:
432
+ response = _a.sent();
433
+ dom = new jsdom_1.JSDOM(response.data);
434
+ playersHTML = dom.window.document.querySelectorAll('tr.personal-hiscores__row');
435
+ players = [];
436
+ playersHTML.forEach(function (row) {
437
+ // Omit first cell (pre-sailing link)
438
+ var _a = Array.from(row.cells), rankCell = _a[1], nameCell = _a[2], levelCell = _a[3], xpCell = _a[4];
439
+ var isDead = !!nameCell.querySelector('img');
440
+ var nameElement = nameCell.querySelector('a');
441
+ players.push({
442
+ name: (0, utils_1.rsnFromElement)(nameElement),
443
+ rank: (0, utils_1.numberFromElement)(rankCell),
444
+ level: (0, utils_1.numberFromElement)(levelCell),
445
+ xp: (0, utils_1.numberFromElement)(xpCell),
446
+ dead: isDead
447
+ });
448
+ });
449
+ return [2 /*return*/, players];
450
+ }
451
+ });
452
+ });
453
+ }
454
+ /**
455
+ * Screen scrapes a hiscores page of an activity or boss and returns an array of up to 25 players.
456
+ *
457
+ * @param activity Name of the activity or boss to fetch hiscores for.
458
+ * @param mode Gamemode to fetch ranks for.
459
+ * @param page Page number.
460
+ * @param config Optional axios request config object.
461
+ * @returns Array of `PlayerActivityRow` objects.
462
+ */
463
+ function getActivityPage(activity_1) {
464
+ return __awaiter(this, arguments, void 0, function (activity, mode, page, config) {
465
+ var url, response, dom, playersHTML, players;
466
+ if (mode === void 0) { mode = 'main'; }
467
+ if (page === void 0) { page = 1; }
468
+ return __generator(this, function (_a) {
469
+ switch (_a.label) {
470
+ case 0:
471
+ if (!utils_1.GAMEMODES.includes(mode)) {
472
+ throw Error('Invalid game mode');
473
+ }
474
+ else if (!Number.isInteger(page) || page < 1) {
475
+ throw Error('Page must be an integer greater than 0');
476
+ }
477
+ else if (!utils_1.ACTIVITIES.includes(activity)) {
478
+ throw Error('Invalid activity');
479
+ }
480
+ url = (0, utils_1.getActivityPageURL)(mode, activity, page);
481
+ return [4 /*yield*/, (0, utils_1.httpGet)(url, config)];
482
+ case 1:
483
+ response = _a.sent();
484
+ dom = new jsdom_1.JSDOM(response.data);
485
+ playersHTML = dom.window.document.querySelectorAll('tr.personal-hiscores__row');
486
+ players = [];
487
+ playersHTML.forEach(function (row) {
488
+ var _a = Array.from(row.cells), rankCell = _a[0], nameCell = _a[1], scoreCell = _a[2];
489
+ var isDead = !!nameCell.querySelector('img');
490
+ var nameElement = nameCell.querySelector('a');
491
+ players.push({
492
+ name: (0, utils_1.rsnFromElement)(nameElement),
493
+ rank: (0, utils_1.numberFromElement)(rankCell),
494
+ score: (0, utils_1.numberFromElement)(scoreCell),
495
+ dead: isDead
496
+ });
497
+ });
498
+ return [2 /*return*/, players];
499
+ }
500
+ });
501
+ });
502
+ }
@@ -0,0 +1,5 @@
1
+ import * as hiscores from './hiscores';
2
+ export * from './hiscores';
3
+ export * from './types';
4
+ export * from './utils';
5
+ export default hiscores;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var hiscores = __importStar(require("./hiscores"));
40
+ __exportStar(require("./hiscores"), exports);
41
+ __exportStar(require("./types"), exports);
42
+ __exportStar(require("./utils"), exports);
43
+ exports.default = hiscores;