osrs-json-hiscores 2.14.1 → 2.15.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/lib/hiscores.js CHANGED
@@ -1,401 +1,401 @@
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;
24
- return g = { next: verb(0), "throw": verb(1), "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 (_) 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
- Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.getActivityPage = exports.getSkillPage = exports.getStatsByGamemode = exports.getStats = exports.parseStats = exports.getRSNFormat = void 0;
51
- var jsdom_1 = require("jsdom");
52
- var utils_1 = require("./utils");
53
- /**
54
- * Screen scrapes the hiscores to get the formatted rsn of a player.
55
- *
56
- * @param rsn Username of the player.
57
- * @param config Optional axios request config object.
58
- * @returns Formatted version of the rsn.
59
- */
60
- function getRSNFormat(rsn, config) {
61
- return __awaiter(this, void 0, void 0, function () {
62
- var url, response, dom, anchor, _a;
63
- return __generator(this, function (_b) {
64
- switch (_b.label) {
65
- case 0:
66
- utils_1.validateRSN(rsn);
67
- url = utils_1.getPlayerTableURL('main', rsn);
68
- _b.label = 1;
69
- case 1:
70
- _b.trys.push([1, 3, , 4]);
71
- return [4 /*yield*/, utils_1.httpGet(url, config)];
72
- case 2:
73
- response = _b.sent();
74
- dom = new jsdom_1.JSDOM(response.data);
75
- anchor = dom.window.document.querySelector('.personal-hiscores__row.personal-hiscores__row--type-highlight a');
76
- if (anchor) {
77
- return [2 /*return*/, utils_1.rsnFromElement(anchor)];
78
- }
79
- throw Error('Player not found');
80
- case 3:
81
- _a = _b.sent();
82
- throw Error('Player not found');
83
- case 4: return [2 /*return*/];
84
- }
85
- });
86
- });
87
- }
88
- exports.getRSNFormat = getRSNFormat;
89
- /**
90
- * Parses CSV string of raw stats and returns a stats object.
91
- *
92
- * @param csv Raw CSV from the official OSRS API.
93
- * @returns Parsed stats object.
94
- */
95
- function parseStats(csv) {
96
- var splitCSV = csv
97
- .split('\n')
98
- .filter(function (entry) { return !!entry; })
99
- .map(function (stat) { return stat.split(','); });
100
- if (splitCSV.length !==
101
- utils_1.SKILLS.length + utils_1.BH_MODES.length + utils_1.CLUES.length + utils_1.BOSSES.length + 5) {
102
- throw Error(utils_1.INVALID_FORMAT_ERROR);
103
- }
104
- var skillObjects = splitCSV
105
- .filter(function (stat) { return stat.length === 3; })
106
- .map(function (stat) {
107
- var rank = stat[0], level = stat[1], xp = stat[2];
108
- var skill = {
109
- rank: parseInt(rank, 10),
110
- level: parseInt(level, 10),
111
- xp: parseInt(xp, 10)
112
- };
113
- return skill;
114
- });
115
- var activityObjects = splitCSV
116
- .filter(function (stat) { return stat.length === 2; })
117
- .map(function (stat) {
118
- var rank = stat[0], score = stat[1];
119
- var activity = {
120
- rank: parseInt(rank, 10),
121
- score: parseInt(score, 10)
122
- };
123
- return activity;
124
- });
125
- var leaguePoints = activityObjects.splice(0, 1)[0];
126
- var bhObjects = activityObjects.splice(0, utils_1.BH_MODES.length);
127
- var clueObjects = activityObjects.splice(0, utils_1.CLUES.length);
128
- var _a = activityObjects.splice(0, 4), lastManStanding = _a[0], pvpArena = _a[1], soulWarsZeal = _a[2], riftsClosed = _a[3];
129
- var bossObjects = activityObjects.splice(0, utils_1.BOSSES.length);
130
- var skills = skillObjects.reduce(function (prev, curr, index) {
131
- var newSkills = __assign({}, prev);
132
- newSkills[utils_1.SKILLS[index]] = curr;
133
- return newSkills;
134
- }, {});
135
- var bountyHunter = bhObjects.reduce(function (prev, curr, index) {
136
- var newBH = __assign({}, prev);
137
- newBH[utils_1.BH_MODES[index]] = curr;
138
- return newBH;
139
- }, {});
140
- var clues = clueObjects.reduce(function (prev, curr, index) {
141
- var newClues = __assign({}, prev);
142
- newClues[utils_1.CLUES[index]] = curr;
143
- return newClues;
144
- }, {});
145
- var bosses = bossObjects.reduce(function (prev, curr, index) {
146
- var newBosses = __assign({}, prev);
147
- newBosses[utils_1.BOSSES[index]] = curr;
148
- return newBosses;
149
- }, {});
150
- var stats = {
151
- skills: skills,
152
- leaguePoints: leaguePoints,
153
- bountyHunter: bountyHunter,
154
- lastManStanding: lastManStanding,
155
- pvpArena: pvpArena,
156
- soulWarsZeal: soulWarsZeal,
157
- riftsClosed: riftsClosed,
158
- clues: clues,
159
- bosses: bosses
160
- };
161
- return stats;
162
- }
163
- exports.parseStats = parseStats;
164
- /**
165
- * Fetches stats from the OSRS API and consolidates the info into a player object.
166
- *
167
- * **Note:** This function will make up to 5 separate network requests.
168
- * As such, it is highly subject to the performance of the official OSRS API.
169
- *
170
- * @param rsn Username of the player.
171
- * @returns Player object.
172
- */
173
- function getStats(rsn, options) {
174
- var _a, _b, _c, _d;
175
- return __awaiter(this, void 0, void 0, function () {
176
- var otherGamemodes, shouldGetFormattedRsn, mainRes, emptyResponse_1, getModeStats, formattedName, _e, player, ironRes, hcRes, ultRes;
177
- var _this = this;
178
- return __generator(this, function (_f) {
179
- switch (_f.label) {
180
- case 0:
181
- utils_1.validateRSN(rsn);
182
- otherGamemodes = (_a = options === null || options === void 0 ? void 0 : options.otherGamemodes) !== null && _a !== void 0 ? _a : [
183
- 'ironman',
184
- 'hardcore',
185
- 'ultimate'
186
- ];
187
- shouldGetFormattedRsn = (_b = options === null || options === void 0 ? void 0 : options.shouldGetFormattedRsn) !== null && _b !== void 0 ? _b : true;
188
- return [4 /*yield*/, utils_1.httpGet(utils_1.getStatsURL('main', rsn), (_c = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _c === void 0 ? void 0 : _c.main)];
189
- case 1:
190
- mainRes = _f.sent();
191
- if (!(mainRes.status === 200)) return [3 /*break*/, 9];
192
- emptyResponse_1 = {
193
- status: 404,
194
- data: '',
195
- statusText: '',
196
- headers: {},
197
- config: {}
198
- };
199
- getModeStats = function (mode) { return __awaiter(_this, void 0, void 0, function () {
200
- var _a;
201
- return __generator(this, function (_b) {
202
- return [2 /*return*/, otherGamemodes.includes(mode)
203
- ? utils_1.httpGet(utils_1.getStatsURL(mode, rsn), (_a = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _a === void 0 ? void 0 : _a[mode]).catch(function (err) { return err; })
204
- : emptyResponse_1];
205
- });
206
- }); };
207
- if (!shouldGetFormattedRsn) return [3 /*break*/, 3];
208
- return [4 /*yield*/, getRSNFormat(rsn, (_d = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _d === void 0 ? void 0 : _d.rsn).catch(function () { return undefined; })];
209
- case 2:
210
- _e = _f.sent();
211
- return [3 /*break*/, 4];
212
- case 3:
213
- _e = undefined;
214
- _f.label = 4;
215
- case 4:
216
- formattedName = _e;
217
- player = {
218
- name: formattedName !== null && formattedName !== void 0 ? formattedName : rsn,
219
- mode: 'main',
220
- dead: false,
221
- deulted: false,
222
- deironed: false
223
- };
224
- player.main = parseStats(mainRes.data);
225
- return [4 /*yield*/, getModeStats('ironman')];
226
- case 5:
227
- ironRes = _f.sent();
228
- if (!(ironRes.status === 200)) return [3 /*break*/, 8];
229
- player.ironman = parseStats(ironRes.data);
230
- return [4 /*yield*/, getModeStats('hardcore')];
231
- case 6:
232
- hcRes = _f.sent();
233
- return [4 /*yield*/, getModeStats('ultimate')];
234
- case 7:
235
- ultRes = _f.sent();
236
- if (hcRes.status === 200) {
237
- player.mode = 'hardcore';
238
- player.hardcore = parseStats(hcRes.data);
239
- if (player.ironman.skills.overall.xp !== player.hardcore.skills.overall.xp) {
240
- player.dead = true;
241
- player.mode = 'ironman';
242
- }
243
- if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
244
- player.deironed = true;
245
- player.mode = 'main';
246
- }
247
- }
248
- else if (ultRes.status === 200) {
249
- player.mode = 'ultimate';
250
- player.ultimate = parseStats(ultRes.data);
251
- if (player.ironman.skills.overall.xp !== player.ultimate.skills.overall.xp) {
252
- player.deulted = true;
253
- player.mode = 'ironman';
254
- }
255
- if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
256
- player.deironed = true;
257
- player.mode = 'main';
258
- }
259
- }
260
- else {
261
- player.mode = 'ironman';
262
- if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
263
- player.deironed = true;
264
- player.mode = 'main';
265
- }
266
- }
267
- _f.label = 8;
268
- case 8: return [2 /*return*/, player];
269
- case 9: throw Error('Player not found');
270
- }
271
- });
272
- });
273
- }
274
- exports.getStats = getStats;
275
- /**
276
- * Fetches stats from the OSRS API and returns them as an object.
277
- *
278
- * @param rsn Username of the player.
279
- * @param mode Gamemode to fetch ranks for.
280
- * @param config Optional axios request config object.
281
- * @returns Stats object.
282
- */
283
- function getStatsByGamemode(rsn, mode, config) {
284
- if (mode === void 0) { mode = 'main'; }
285
- return __awaiter(this, void 0, void 0, function () {
286
- var response, stats;
287
- return __generator(this, function (_a) {
288
- switch (_a.label) {
289
- case 0:
290
- utils_1.validateRSN(rsn);
291
- if (!utils_1.GAMEMODES.includes(mode)) {
292
- throw Error('Invalid game mode');
293
- }
294
- return [4 /*yield*/, utils_1.httpGet(utils_1.getStatsURL(mode, rsn), config)];
295
- case 1:
296
- response = _a.sent();
297
- if (response.status !== 200) {
298
- throw Error('Player not found');
299
- }
300
- stats = parseStats(response.data);
301
- return [2 /*return*/, stats];
302
- }
303
- });
304
- });
305
- }
306
- exports.getStatsByGamemode = getStatsByGamemode;
307
- function getSkillPage(skill, mode, page, config) {
308
- if (mode === void 0) { mode = 'main'; }
309
- if (page === void 0) { page = 1; }
310
- return __awaiter(this, void 0, void 0, function () {
311
- var url, response, dom, playersHTML, players;
312
- return __generator(this, function (_a) {
313
- switch (_a.label) {
314
- case 0:
315
- if (!utils_1.GAMEMODES.includes(mode)) {
316
- throw Error('Invalid game mode');
317
- }
318
- else if (!Number.isInteger(page) || page < 1) {
319
- throw Error('Page must be an integer greater than 0');
320
- }
321
- else if (!utils_1.SKILLS.includes(skill)) {
322
- throw Error('Invalid skill');
323
- }
324
- url = utils_1.getSkillPageURL(mode, skill, page);
325
- return [4 /*yield*/, utils_1.httpGet(url, config)];
326
- case 1:
327
- response = _a.sent();
328
- dom = new jsdom_1.JSDOM(response.data);
329
- playersHTML = dom.window.document.querySelectorAll('.personal-hiscores__row');
330
- players = [];
331
- playersHTML.forEach(function (row) {
332
- var rankEl = row.querySelector('td');
333
- var nameEl = row.querySelector('td a');
334
- var levelEl = row.querySelector('td.left + td');
335
- var xpEl = row.querySelector('td.left + td + td');
336
- var isDead = !!row.querySelector('td img');
337
- players.push({
338
- name: utils_1.rsnFromElement(nameEl),
339
- rank: utils_1.numberFromElement(rankEl),
340
- level: utils_1.numberFromElement(levelEl),
341
- xp: utils_1.numberFromElement(xpEl),
342
- dead: isDead
343
- });
344
- });
345
- return [2 /*return*/, players];
346
- }
347
- });
348
- });
349
- }
350
- exports.getSkillPage = getSkillPage;
351
- /**
352
- * Screen scrapes a hiscores page of an activity or boss and returns an array of up to 25 players.
353
- *
354
- * @param activity Name of the activity or boss to fetch hiscores for.
355
- * @param mode Gamemode to fetch ranks for.
356
- * @param page Page number.
357
- * @param config Optional axios request config object.
358
- * @returns Array of `PlayerActivityRow` objects.
359
- */
360
- function getActivityPage(activity, mode, page, config) {
361
- if (mode === void 0) { mode = 'main'; }
362
- if (page === void 0) { page = 1; }
363
- return __awaiter(this, void 0, void 0, function () {
364
- var url, response, dom, playersHTML, players;
365
- return __generator(this, function (_a) {
366
- switch (_a.label) {
367
- case 0:
368
- if (!utils_1.GAMEMODES.includes(mode)) {
369
- throw Error('Invalid game mode');
370
- }
371
- else if (!Number.isInteger(page) || page < 1) {
372
- throw Error('Page must be an integer greater than 0');
373
- }
374
- else if (!utils_1.ACTIVITIES.includes(activity)) {
375
- throw Error('Invalid activity');
376
- }
377
- url = utils_1.getActivityPageURL(mode, activity, page);
378
- return [4 /*yield*/, utils_1.httpGet(url, config)];
379
- case 1:
380
- response = _a.sent();
381
- dom = new jsdom_1.JSDOM(response.data);
382
- playersHTML = dom.window.document.querySelectorAll('.personal-hiscores__row');
383
- players = [];
384
- playersHTML.forEach(function (row) {
385
- var rankEl = row.querySelector('td');
386
- var nameEl = row.querySelector('td a');
387
- var scoreEl = row.querySelector('td.left + td');
388
- var isDead = !!row.querySelector('td img');
389
- players.push({
390
- name: utils_1.rsnFromElement(nameEl),
391
- rank: utils_1.numberFromElement(rankEl),
392
- score: utils_1.numberFromElement(scoreEl),
393
- dead: isDead
394
- });
395
- });
396
- return [2 /*return*/, players];
397
- }
398
- });
399
- });
400
- }
401
- exports.getActivityPage = getActivityPage;
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;
24
+ return g = { next: verb(0), "throw": verb(1), "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
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.getActivityPage = exports.getSkillPage = exports.getStatsByGamemode = exports.getStats = exports.parseStats = exports.getRSNFormat = void 0;
51
+ var jsdom_1 = require("jsdom");
52
+ var utils_1 = require("./utils");
53
+ /**
54
+ * Screen scrapes the hiscores to get the formatted rsn of a player.
55
+ *
56
+ * @param rsn Username of the player.
57
+ * @param config Optional axios request config object.
58
+ * @returns Formatted version of the rsn.
59
+ */
60
+ function getRSNFormat(rsn, config) {
61
+ return __awaiter(this, void 0, void 0, function () {
62
+ var url, response, dom, anchor, _a;
63
+ return __generator(this, function (_b) {
64
+ switch (_b.label) {
65
+ case 0:
66
+ (0, utils_1.validateRSN)(rsn);
67
+ url = (0, utils_1.getPlayerTableURL)('main', rsn);
68
+ _b.label = 1;
69
+ case 1:
70
+ _b.trys.push([1, 3, , 4]);
71
+ return [4 /*yield*/, (0, utils_1.httpGet)(url, config)];
72
+ case 2:
73
+ response = _b.sent();
74
+ dom = new jsdom_1.JSDOM(response.data);
75
+ anchor = dom.window.document.querySelector('.personal-hiscores__row.personal-hiscores__row--type-highlight a');
76
+ if (anchor) {
77
+ return [2 /*return*/, (0, utils_1.rsnFromElement)(anchor)];
78
+ }
79
+ throw Error('Player not found');
80
+ case 3:
81
+ _a = _b.sent();
82
+ throw Error('Player not found');
83
+ case 4: return [2 /*return*/];
84
+ }
85
+ });
86
+ });
87
+ }
88
+ exports.getRSNFormat = getRSNFormat;
89
+ /**
90
+ * Parses CSV string of raw stats and returns a stats object.
91
+ *
92
+ * @param csv Raw CSV from the official OSRS API.
93
+ * @returns Parsed stats object.
94
+ */
95
+ function parseStats(csv) {
96
+ var splitCSV = csv
97
+ .split('\n')
98
+ .filter(function (entry) { return !!entry; })
99
+ .map(function (stat) { return stat.split(','); });
100
+ if (splitCSV.length !==
101
+ utils_1.SKILLS.length + utils_1.BH_MODES.length + utils_1.CLUES.length + utils_1.BOSSES.length + 5) {
102
+ throw Error(utils_1.INVALID_FORMAT_ERROR);
103
+ }
104
+ var skillObjects = splitCSV
105
+ .filter(function (stat) { return stat.length === 3; })
106
+ .map(function (stat) {
107
+ var rank = stat[0], level = stat[1], xp = stat[2];
108
+ var skill = {
109
+ rank: parseInt(rank, 10),
110
+ level: parseInt(level, 10),
111
+ xp: parseInt(xp, 10)
112
+ };
113
+ return skill;
114
+ });
115
+ var activityObjects = splitCSV
116
+ .filter(function (stat) { return stat.length === 2; })
117
+ .map(function (stat) {
118
+ var rank = stat[0], score = stat[1];
119
+ var activity = {
120
+ rank: parseInt(rank, 10),
121
+ score: parseInt(score, 10)
122
+ };
123
+ return activity;
124
+ });
125
+ var leaguePoints = activityObjects.splice(0, 1)[0];
126
+ var bhObjects = activityObjects.splice(0, utils_1.BH_MODES.length);
127
+ var clueObjects = activityObjects.splice(0, utils_1.CLUES.length);
128
+ var _a = activityObjects.splice(0, 4), lastManStanding = _a[0], pvpArena = _a[1], soulWarsZeal = _a[2], riftsClosed = _a[3];
129
+ var bossObjects = activityObjects.splice(0, utils_1.BOSSES.length);
130
+ var skills = skillObjects.reduce(function (prev, curr, index) {
131
+ var newSkills = __assign({}, prev);
132
+ newSkills[utils_1.SKILLS[index]] = curr;
133
+ return newSkills;
134
+ }, {});
135
+ var bountyHunter = bhObjects.reduce(function (prev, curr, index) {
136
+ var newBH = __assign({}, prev);
137
+ newBH[utils_1.BH_MODES[index]] = curr;
138
+ return newBH;
139
+ }, {});
140
+ var clues = clueObjects.reduce(function (prev, curr, index) {
141
+ var newClues = __assign({}, prev);
142
+ newClues[utils_1.CLUES[index]] = curr;
143
+ return newClues;
144
+ }, {});
145
+ var bosses = bossObjects.reduce(function (prev, curr, index) {
146
+ var newBosses = __assign({}, prev);
147
+ newBosses[utils_1.BOSSES[index]] = curr;
148
+ return newBosses;
149
+ }, {});
150
+ var stats = {
151
+ skills: skills,
152
+ leaguePoints: leaguePoints,
153
+ bountyHunter: bountyHunter,
154
+ lastManStanding: lastManStanding,
155
+ pvpArena: pvpArena,
156
+ soulWarsZeal: soulWarsZeal,
157
+ riftsClosed: riftsClosed,
158
+ clues: clues,
159
+ bosses: bosses
160
+ };
161
+ return stats;
162
+ }
163
+ exports.parseStats = parseStats;
164
+ /**
165
+ * Fetches stats from the OSRS API and consolidates the info into a player object.
166
+ *
167
+ * **Note:** This function will make up to 5 separate network requests.
168
+ * As such, it is highly subject to the performance of the official OSRS API.
169
+ *
170
+ * @param rsn Username of the player.
171
+ * @returns Player object.
172
+ */
173
+ function getStats(rsn, options) {
174
+ var _a, _b, _c, _d;
175
+ return __awaiter(this, void 0, void 0, function () {
176
+ var otherGamemodes, shouldGetFormattedRsn, mainRes, emptyResponse_1, getModeStats, formattedName, _e, player, ironRes, hcRes, ultRes;
177
+ var _this = this;
178
+ return __generator(this, function (_f) {
179
+ switch (_f.label) {
180
+ case 0:
181
+ (0, utils_1.validateRSN)(rsn);
182
+ otherGamemodes = (_a = options === null || options === void 0 ? void 0 : options.otherGamemodes) !== null && _a !== void 0 ? _a : [
183
+ 'ironman',
184
+ 'hardcore',
185
+ 'ultimate'
186
+ ];
187
+ shouldGetFormattedRsn = (_b = options === null || options === void 0 ? void 0 : options.shouldGetFormattedRsn) !== null && _b !== void 0 ? _b : true;
188
+ return [4 /*yield*/, (0, utils_1.httpGet)((0, utils_1.getStatsURL)('main', rsn), (_c = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _c === void 0 ? void 0 : _c.main)];
189
+ case 1:
190
+ mainRes = _f.sent();
191
+ if (!(mainRes.status === 200)) return [3 /*break*/, 9];
192
+ emptyResponse_1 = {
193
+ status: 404,
194
+ data: '',
195
+ statusText: '',
196
+ headers: {},
197
+ config: {}
198
+ };
199
+ getModeStats = function (mode) { return __awaiter(_this, void 0, void 0, function () {
200
+ var _a;
201
+ return __generator(this, function (_b) {
202
+ return [2 /*return*/, otherGamemodes.includes(mode)
203
+ ? (0, utils_1.httpGet)((0, utils_1.getStatsURL)(mode, rsn), (_a = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _a === void 0 ? void 0 : _a[mode]).catch(function (err) { return err; })
204
+ : emptyResponse_1];
205
+ });
206
+ }); };
207
+ if (!shouldGetFormattedRsn) return [3 /*break*/, 3];
208
+ return [4 /*yield*/, getRSNFormat(rsn, (_d = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _d === void 0 ? void 0 : _d.rsn).catch(function () { return undefined; })];
209
+ case 2:
210
+ _e = _f.sent();
211
+ return [3 /*break*/, 4];
212
+ case 3:
213
+ _e = undefined;
214
+ _f.label = 4;
215
+ case 4:
216
+ formattedName = _e;
217
+ player = {
218
+ name: formattedName !== null && formattedName !== void 0 ? formattedName : rsn,
219
+ mode: 'main',
220
+ dead: false,
221
+ deulted: false,
222
+ deironed: false
223
+ };
224
+ player.main = parseStats(mainRes.data);
225
+ return [4 /*yield*/, getModeStats('ironman')];
226
+ case 5:
227
+ ironRes = _f.sent();
228
+ if (!(ironRes.status === 200)) return [3 /*break*/, 8];
229
+ player.ironman = parseStats(ironRes.data);
230
+ return [4 /*yield*/, getModeStats('hardcore')];
231
+ case 6:
232
+ hcRes = _f.sent();
233
+ return [4 /*yield*/, getModeStats('ultimate')];
234
+ case 7:
235
+ ultRes = _f.sent();
236
+ if (hcRes.status === 200) {
237
+ player.mode = 'hardcore';
238
+ player.hardcore = parseStats(hcRes.data);
239
+ if (player.ironman.skills.overall.xp !== player.hardcore.skills.overall.xp) {
240
+ player.dead = true;
241
+ player.mode = 'ironman';
242
+ }
243
+ if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
244
+ player.deironed = true;
245
+ player.mode = 'main';
246
+ }
247
+ }
248
+ else if (ultRes.status === 200) {
249
+ player.mode = 'ultimate';
250
+ player.ultimate = parseStats(ultRes.data);
251
+ if (player.ironman.skills.overall.xp !== player.ultimate.skills.overall.xp) {
252
+ player.deulted = true;
253
+ player.mode = 'ironman';
254
+ }
255
+ if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
256
+ player.deironed = true;
257
+ player.mode = 'main';
258
+ }
259
+ }
260
+ else {
261
+ player.mode = 'ironman';
262
+ if (player.main.skills.overall.xp !== player.ironman.skills.overall.xp) {
263
+ player.deironed = true;
264
+ player.mode = 'main';
265
+ }
266
+ }
267
+ _f.label = 8;
268
+ case 8: return [2 /*return*/, player];
269
+ case 9: throw Error('Player not found');
270
+ }
271
+ });
272
+ });
273
+ }
274
+ exports.getStats = getStats;
275
+ /**
276
+ * Fetches stats from the OSRS API and returns them as an object.
277
+ *
278
+ * @param rsn Username of the player.
279
+ * @param mode Gamemode to fetch ranks for.
280
+ * @param config Optional axios request config object.
281
+ * @returns Stats object.
282
+ */
283
+ function getStatsByGamemode(rsn, mode, config) {
284
+ if (mode === void 0) { mode = 'main'; }
285
+ return __awaiter(this, void 0, void 0, function () {
286
+ var response, stats;
287
+ return __generator(this, function (_a) {
288
+ switch (_a.label) {
289
+ case 0:
290
+ (0, utils_1.validateRSN)(rsn);
291
+ if (!utils_1.GAMEMODES.includes(mode)) {
292
+ throw Error('Invalid game mode');
293
+ }
294
+ return [4 /*yield*/, (0, utils_1.httpGet)((0, utils_1.getStatsURL)(mode, rsn), config)];
295
+ case 1:
296
+ response = _a.sent();
297
+ if (response.status !== 200) {
298
+ throw Error('Player not found');
299
+ }
300
+ stats = parseStats(response.data);
301
+ return [2 /*return*/, stats];
302
+ }
303
+ });
304
+ });
305
+ }
306
+ exports.getStatsByGamemode = getStatsByGamemode;
307
+ function getSkillPage(skill, mode, page, config) {
308
+ if (mode === void 0) { mode = 'main'; }
309
+ if (page === void 0) { page = 1; }
310
+ return __awaiter(this, void 0, void 0, function () {
311
+ var url, response, dom, playersHTML, players;
312
+ return __generator(this, function (_a) {
313
+ switch (_a.label) {
314
+ case 0:
315
+ if (!utils_1.GAMEMODES.includes(mode)) {
316
+ throw Error('Invalid game mode');
317
+ }
318
+ else if (!Number.isInteger(page) || page < 1) {
319
+ throw Error('Page must be an integer greater than 0');
320
+ }
321
+ else if (!utils_1.SKILLS.includes(skill)) {
322
+ throw Error('Invalid skill');
323
+ }
324
+ url = (0, utils_1.getSkillPageURL)(mode, skill, page);
325
+ return [4 /*yield*/, (0, utils_1.httpGet)(url, config)];
326
+ case 1:
327
+ response = _a.sent();
328
+ dom = new jsdom_1.JSDOM(response.data);
329
+ playersHTML = dom.window.document.querySelectorAll('.personal-hiscores__row');
330
+ players = [];
331
+ playersHTML.forEach(function (row) {
332
+ var rankEl = row.querySelector('td');
333
+ var nameEl = row.querySelector('td a');
334
+ var levelEl = row.querySelector('td.left + td');
335
+ var xpEl = row.querySelector('td.left + td + td');
336
+ var isDead = !!row.querySelector('td img');
337
+ players.push({
338
+ name: (0, utils_1.rsnFromElement)(nameEl),
339
+ rank: (0, utils_1.numberFromElement)(rankEl),
340
+ level: (0, utils_1.numberFromElement)(levelEl),
341
+ xp: (0, utils_1.numberFromElement)(xpEl),
342
+ dead: isDead
343
+ });
344
+ });
345
+ return [2 /*return*/, players];
346
+ }
347
+ });
348
+ });
349
+ }
350
+ exports.getSkillPage = getSkillPage;
351
+ /**
352
+ * Screen scrapes a hiscores page of an activity or boss and returns an array of up to 25 players.
353
+ *
354
+ * @param activity Name of the activity or boss to fetch hiscores for.
355
+ * @param mode Gamemode to fetch ranks for.
356
+ * @param page Page number.
357
+ * @param config Optional axios request config object.
358
+ * @returns Array of `PlayerActivityRow` objects.
359
+ */
360
+ function getActivityPage(activity, mode, page, config) {
361
+ if (mode === void 0) { mode = 'main'; }
362
+ if (page === void 0) { page = 1; }
363
+ return __awaiter(this, void 0, void 0, function () {
364
+ var url, response, dom, playersHTML, players;
365
+ return __generator(this, function (_a) {
366
+ switch (_a.label) {
367
+ case 0:
368
+ if (!utils_1.GAMEMODES.includes(mode)) {
369
+ throw Error('Invalid game mode');
370
+ }
371
+ else if (!Number.isInteger(page) || page < 1) {
372
+ throw Error('Page must be an integer greater than 0');
373
+ }
374
+ else if (!utils_1.ACTIVITIES.includes(activity)) {
375
+ throw Error('Invalid activity');
376
+ }
377
+ url = (0, utils_1.getActivityPageURL)(mode, activity, page);
378
+ return [4 /*yield*/, (0, utils_1.httpGet)(url, config)];
379
+ case 1:
380
+ response = _a.sent();
381
+ dom = new jsdom_1.JSDOM(response.data);
382
+ playersHTML = dom.window.document.querySelectorAll('.personal-hiscores__row');
383
+ players = [];
384
+ playersHTML.forEach(function (row) {
385
+ var rankEl = row.querySelector('td');
386
+ var nameEl = row.querySelector('td a');
387
+ var scoreEl = row.querySelector('td.left + td');
388
+ var isDead = !!row.querySelector('td img');
389
+ players.push({
390
+ name: (0, utils_1.rsnFromElement)(nameEl),
391
+ rank: (0, utils_1.numberFromElement)(rankEl),
392
+ score: (0, utils_1.numberFromElement)(scoreEl),
393
+ dead: isDead
394
+ });
395
+ });
396
+ return [2 /*return*/, players];
397
+ }
398
+ });
399
+ });
400
+ }
401
+ exports.getActivityPage = getActivityPage;