From d5bbe2a16972734654b71e2f772427fe78812f27 Mon Sep 17 00:00:00 2001 From: maxswa Date: Sun, 5 Jan 2020 20:43:15 -0500 Subject: [PATCH] Update getActivityPage to accept bosses. --- README.md | 8 +++++++- src/hiscores.ts | 10 ++++++---- src/types.ts | 1 + src/utils/constants.ts | 26 ++++++++++++++------------ src/utils/helpers.ts | 4 ++-- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 6b8c278..ebbb5bb 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ const topPage = await getSkillPage('overall'); | Tournament | `tournament` | | Leagues | `seasonal` | -`getSkillPage` and `getActivityPage` require a skill/activity and optionally a gamemode and page: +`getSkillPage` and `getActivityPage` require a skill / activity and optionally a gamemode and page: ```javascript hiscores @@ -109,6 +109,12 @@ Activities consist of all levels of clue scrolls as well as minigames and bosses | Bounty Hunter (Hunter) | `hunterBH` | | Last Man Standing | `lastManStanding` | +### Leagues + +| Activity | Param | +| ------------- | :------------: | +| League Points | `leaguePoints` | + ### Bosses | Boss Name | Param | diff --git a/src/hiscores.ts b/src/hiscores.ts index 45ca8d7..cd7c5d5 100644 --- a/src/hiscores.ts +++ b/src/hiscores.ts @@ -151,14 +151,15 @@ export async function getSkillPage( const players: PlayerSkillRow[] = playersHTML.map(row => { const cells = row.children.filter(el => el.name === 'td'); const [rankEl, nameCell, levelEl, xpEl] = cells; - const [nameEl] = nameCell.children.filter(el => el.name === 'a'); + const nameEl = nameCell.children.find(el => el.name === 'a'); + const isDead = !!nameCell.children.find(el => el.name === 'img'); return { name: rsnFromElement(nameEl), rank: numberFromElement(rankEl), level: numberFromElement(levelEl), xp: numberFromElement(xpEl), - dead: nameCell.children.length === 4, + dead: isDead, }; }); @@ -186,13 +187,14 @@ export async function getActivityPage( const players: PlayerActivityRow[] = playersHTML.map(row => { const cells = row.children.filter(el => el.name === 'td'); const [rankEl, nameCell, scoreEl] = cells; - const [nameEl] = nameCell.children.filter(el => el.name === 'a'); + const nameEl = nameCell.children.find(el => el.name === 'a'); + const isDead = !!nameCell.children.find(el => el.name === 'img'); return { name: rsnFromElement(nameEl), rank: numberFromElement(rankEl), score: numberFromElement(scoreEl), - dead: nameCell.children.length === 4, + dead: isDead, }; }); diff --git a/src/types.ts b/src/types.ts index d42941c..4c06052 100644 --- a/src/types.ts +++ b/src/types.ts @@ -109,6 +109,7 @@ export type Boss = export type Bosses = { [Type in Boss]: Activity }; export type ActivityName = + | 'leaguePoints' | 'hunterBH' | 'rogueBH' | 'lastManStanding' diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 8a6914b..221c8bb 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -60,18 +60,6 @@ export const CLUES: ClueType[] = [ 'master', ]; export const BH_MODES: BHType[] = ['rogue', 'hunter']; -export const ACTIVITIES: ActivityName[] = [ - 'hunterBH', - 'rogueBH', - 'lastManStanding', - 'allClues', - 'beginnerClues', - 'easyClues', - 'mediumClues', - 'hardClues', - 'eliteClues', - 'masterClues', -]; export const GAMEMODES: Gamemode[] = [ 'main', 'ironman', @@ -126,6 +114,20 @@ export const BOSSES: Boss[] = [ 'zalcano', 'zulrah', ]; +export const ACTIVITIES: ActivityName[] = [ + 'leaguePoints', + 'hunterBH', + 'rogueBH', + 'allClues', + 'beginnerClues', + 'easyClues', + 'mediumClues', + 'hardClues', + 'eliteClues', + 'masterClues', + 'lastManStanding', + ...BOSSES, +]; export type FormattedBossNames = { [key in Boss]: string; diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 52dc209..c971a40 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -41,7 +41,7 @@ export const numberFromElement = (el: CheerioElement) => { return parseInt(number, 10); }; -export const rsnFromElement = (el: CheerioElement) => { - const innerText = el.firstChild.data; +export const rsnFromElement = (el: CheerioElement | undefined) => { + const innerText = el?.firstChild.data; return innerText ? innerText.replace(/\uFFFD/g, ' ') : ''; };