Separate getStats into getStatsByGamemode

This commit is contained in:
maxswa
2019-06-17 16:32:51 -04:00
parent 93d6961a4c
commit 68eb807657

View File

@@ -2,7 +2,6 @@ import axios from 'axios';
import * as cheerio from 'cheerio'; import * as cheerio from 'cheerio';
import { import {
Player, Player,
Mode,
Activity, Activity,
Skill, Skill,
Stats, Stats,
@@ -20,7 +19,6 @@ import {
SKILLS, SKILLS,
BH_MODES, BH_MODES,
CLUES, CLUES,
MODES,
getPlayerTableURL, getPlayerTableURL,
getSkillPageURL, getSkillPageURL,
GAMEMODES, GAMEMODES,
@@ -30,21 +28,15 @@ import {
getActivityPageURL, getActivityPageURL,
} from './utils'; } from './utils';
export async function getStats( export async function getStats(rsn: string): Promise<Player> {
rsn: string,
mode: Mode = 'full'
): Promise<Player> {
if (typeof rsn !== 'string') { if (typeof rsn !== 'string') {
throw Error('RSN must be a string'); throw Error('RSN must be a string');
} else if (!/^[a-zA-Z0-9 _]+$/.test(rsn)) { } else if (!/^[a-zA-Z0-9 _]+$/.test(rsn)) {
throw Error('RSN contains invalid character'); throw Error('RSN contains invalid character');
} else if (rsn.length > 12 || rsn.length < 1) { } else if (rsn.length > 12 || rsn.length < 1) {
throw Error('RSN must be between 1 and 12 characters'); throw Error('RSN must be between 1 and 12 characters');
} else if (!MODES.includes(mode)) {
throw Error('Invalid game mode');
} }
if (mode === 'full') {
const mainRes = await axios(getStatsURL('main', rsn)); const mainRes = await axios(getStatsURL('main', rsn));
if (mainRes.status === 200) { if (mainRes.status === 200) {
const otherResponses = await Promise.all([ const otherResponses = await Promise.all([
@@ -101,21 +93,28 @@ export async function getStats(
return player; return player;
} }
throw Error('Player not found'); throw Error('Player not found');
} else { }
export async function getStatsByGamemode(
rsn: string,
mode: Gamemode = 'main'
): Promise<Stats> {
if (typeof rsn !== 'string') {
throw Error('RSN must be a string');
} else if (!/^[a-zA-Z0-9 _]+$/.test(rsn)) {
throw Error('RSN contains invalid character');
} else if (rsn.length > 12 || rsn.length < 1) {
throw Error('RSN must be between 1 and 12 characters');
} else if (!GAMEMODES.includes(mode)) {
throw Error('Invalid game mode');
}
const response = await axios(getStatsURL(mode, rsn)); const response = await axios(getStatsURL(mode, rsn));
if (response.status !== 200) { if (response.status !== 200) {
throw Error('Player not found'); throw Error('Player not found');
} }
const player: Player = { const stats: Stats = parseStats(response.data);
rsn,
mode, return stats;
dead: false,
deulted: false,
deironed: false,
[mode]: parseStats(response.data),
};
return player;
}
} }
export async function getSkillPage( export async function getSkillPage(
@@ -219,10 +218,11 @@ export function parseStats(csv: string): Stats {
const skillObjects: Skill[] = splitCSV const skillObjects: Skill[] = splitCSV
.filter(stat => stat.length === 3) .filter(stat => stat.length === 3)
.map(stat => { .map(stat => {
const [rank, level, xp] = stat;
const skill: Skill = { const skill: Skill = {
rank: parseInt(stat[0], 10), rank: parseInt(rank, 10),
level: parseInt(stat[1], 10), level: parseInt(level, 10),
xp: parseInt(stat[2], 10), xp: parseInt(xp, 10),
}; };
return skill; return skill;
}); });
@@ -230,9 +230,10 @@ export function parseStats(csv: string): Stats {
const activityObjects: Activity[] = splitCSV const activityObjects: Activity[] = splitCSV
.filter(stat => stat.length === 2) .filter(stat => stat.length === 2)
.map(stat => { .map(stat => {
const [rank, score] = stat;
const activity: Activity = { const activity: Activity = {
rank: parseInt(stat[0], 10), rank: parseInt(rank, 10),
score: parseInt(stat[1], 10), score: parseInt(score, 10),
}; };
return activity; return activity;
}); });