Add getActivityPage

This commit is contained in:
Max
2019-06-14 18:26:26 -04:00
parent b0874bb7e0
commit 08d6d22fb3
3 changed files with 64 additions and 19 deletions

View File

@@ -10,22 +10,24 @@ import {
BH as BHStats, BH as BHStats,
Clues, Clues,
Gamemode, Gamemode,
Category,
SkillName, SkillName,
PlayerSkillRow, PlayerSkillRow,
ActivityName,
PlayerActivityRow,
} from './types'; } from './types';
import { import {
getStatsURL, getStatsURL,
SKILLS, SKILLS,
BH, BH_MODES,
CLUES, CLUES,
MODES, MODES,
getPlayerTableURL, getPlayerTableURL,
getSkillPageURL, getSkillPageURL,
GAMEMODES, GAMEMODES,
OTHER, ACTIVITIES,
numberFromElement, numberFromElement,
rsnFromElement, rsnFromElement,
getActivityPageURL,
} from './utils'; } from './utils';
export async function getStats( export async function getStats(
@@ -115,11 +117,11 @@ export async function getStats(
} }
} }
export const getSkillPage = async ( export async function getSkillPage(
mode: Gamemode,
skill: SkillName, skill: SkillName,
page: number mode: Gamemode = 'main',
): Promise<PlayerSkillRow[]> => { page: number = 1
): Promise<PlayerSkillRow[]> {
if (!GAMEMODES.includes(mode)) { if (!GAMEMODES.includes(mode)) {
throw Error('Invalid game mode'); throw Error('Invalid game mode');
} else if (!Number.isInteger(page) || page < 1) { } else if (!Number.isInteger(page) || page < 1) {
@@ -148,11 +150,44 @@ export const getSkillPage = async (
}); });
return players; return players;
}; }
export const getRSNFormat = async (rsn: string): Promise<string> => { export async function getActivityPage(
activity: ActivityName,
mode: Gamemode = 'main',
page: number = 1
): Promise<PlayerActivityRow[]> {
if (!GAMEMODES.includes(mode)) {
throw Error('Invalid game mode');
} else if (!Number.isInteger(page) || page < 1) {
throw Error('Page must be an integer greater than 0');
} else if (!ACTIVITIES.includes(activity)) {
throw Error('Invalid activity');
}
const url = getActivityPageURL(mode, activity, page);
const response = await axios(url);
const $ = cheerio.load(response.data);
const playersHTML = $('.personal-hiscores__row').toArray();
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');
return {
rsn: rsnFromElement(nameEl),
rank: numberFromElement(rankEl),
score: numberFromElement(scoreEl),
dead: nameCell.children.length === 4,
};
});
return players;
}
export async function getRSNFormat(rsn: string): Promise<string> {
const url = getPlayerTableURL('main', rsn); const url = getPlayerTableURL('main', rsn);
try { try {
const response = await axios(url); const response = await axios(url);
const $ = cheerio.load(response.data); const $ = cheerio.load(response.data);
@@ -164,9 +199,9 @@ export const getRSNFormat = async (rsn: string): Promise<string> => {
} catch { } catch {
throw Error('Player not found'); throw Error('Player not found');
} }
}; }
export const parseStats = (csv: string): Stats => { export function parseStats(csv: string): Stats {
const splitCSV = csv const splitCSV = csv
.split('\n') .split('\n')
.filter(entry => !!entry) .filter(entry => !!entry)
@@ -193,7 +228,7 @@ export const parseStats = (csv: string): Stats => {
return activity; return activity;
}); });
const bhObjects = activityObjects.splice(0, BH.length); const bhObjects = activityObjects.splice(0, BH_MODES.length);
const [lms] = activityObjects.splice(0, 1); const [lms] = activityObjects.splice(0, 1);
const clueObjects = activityObjects.splice(0, CLUES.length); const clueObjects = activityObjects.splice(0, CLUES.length);
@@ -209,7 +244,7 @@ export const parseStats = (csv: string): Stats => {
const bh: BHStats = bhObjects.reduce<BHStats>( const bh: BHStats = bhObjects.reduce<BHStats>(
(prev, curr, index) => { (prev, curr, index) => {
const newBH = { ...prev }; const newBH = { ...prev };
newBH[BH[index]] = curr; newBH[BH_MODES[index]] = curr;
return newBH; return newBH;
}, },
{} as BHStats {} as BHStats
@@ -232,4 +267,4 @@ export const parseStats = (csv: string): Stats => {
}; };
return stats; return stats;
}; }

View File

@@ -56,8 +56,7 @@ export type BHType = 'rogue' | 'hunter';
export type BH = { [Type in BHType]: Activity }; export type BH = { [Type in BHType]: Activity };
export type Category = export type ActivityName =
| SkillName
| 'hunterbh' | 'hunterbh'
| 'roguebh' | 'roguebh'
| 'lms' | 'lms'

View File

@@ -1,11 +1,11 @@
import { Gamemode, Category, SkillName } from '../types'; import { Gamemode, SkillName, ActivityName } from '../types';
import { import {
BASE_URL, BASE_URL,
GAMEMODE_URL, GAMEMODE_URL,
STATS_URL, STATS_URL,
SCORES_URL, SCORES_URL,
SKILLS, SKILLS,
OTHER, ACTIVITIES,
} from './constants'; } from './constants';
export const getStatsURL = (gamemode: Gamemode, rsn: string) => export const getStatsURL = (gamemode: Gamemode, rsn: string) =>
@@ -25,6 +25,17 @@ export const getSkillPageURL = (
skill skill
)}&page=${page}`; )}&page=${page}`;
export const getActivityPageURL = (
gamemode: Gamemode,
activity: ActivityName,
page: number
) =>
`${BASE_URL}${
GAMEMODE_URL[gamemode]
}${SCORES_URL}category_type=1&table=${ACTIVITIES.indexOf(
activity
)}&page=${page}`;
export const numberFromElement = (el: CheerioElement) => { export const numberFromElement = (el: CheerioElement) => {
const innerText = el.firstChild.data; const innerText = el.firstChild.data;
const number = innerText ? innerText.replace(/[\n|,]/g, '') : '-1'; const number = innerText ? innerText.replace(/[\n|,]/g, '') : '-1';