mirror of
https://github.com/maxswa/osrs-json-hiscores.git
synced 2025-10-15 10:19:04 +00:00
added error handling for invalid data
This commit is contained in:
6
.idea/jsLibraryMappings.xml
generated
Normal file
6
.idea/jsLibraryMappings.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptLibraryMappings">
|
||||
<includedPredefinedLibrary name="Node.js Core" />
|
||||
</component>
|
||||
</project>
|
72
.idea/markdown-navigator.xml
generated
Normal file
72
.idea/markdown-navigator.xml
generated
Normal file
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="MarkdownProjectSettings">
|
||||
<PreviewSettings splitEditorLayout="SPLIT" splitEditorPreview="PREVIEW" useGrayscaleRendering="false" zoomFactor="1.0" maxImageWidth="0" showGitHubPageIfSynced="false" allowBrowsingInPreview="false" synchronizePreviewPosition="true" highlightPreviewType="NONE" highlightFadeOut="5" highlightOnTyping="true" synchronizeSourcePosition="true" verticallyAlignSourceAndPreviewSyncPosition="true" showSearchHighlightsInPreview="false" showSelectionInPreview="true">
|
||||
<PanelProvider>
|
||||
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.panel" providerName="Default - Swing" />
|
||||
</PanelProvider>
|
||||
</PreviewSettings>
|
||||
<ParserSettings gitHubSyntaxChange="false">
|
||||
<PegdownExtensions>
|
||||
<option name="ABBREVIATIONS" value="false" />
|
||||
<option name="ANCHORLINKS" value="true" />
|
||||
<option name="ASIDE" value="false" />
|
||||
<option name="ATXHEADERSPACE" value="true" />
|
||||
<option name="AUTOLINKS" value="true" />
|
||||
<option name="DEFINITIONS" value="false" />
|
||||
<option name="DEFINITION_BREAK_DOUBLE_BLANK_LINE" value="false" />
|
||||
<option name="FENCED_CODE_BLOCKS" value="true" />
|
||||
<option name="FOOTNOTES" value="false" />
|
||||
<option name="HARDWRAPS" value="false" />
|
||||
<option name="HTML_DEEP_PARSER" value="false" />
|
||||
<option name="INSERTED" value="false" />
|
||||
<option name="QUOTES" value="false" />
|
||||
<option name="RELAXEDHRULES" value="true" />
|
||||
<option name="SMARTS" value="false" />
|
||||
<option name="STRIKETHROUGH" value="true" />
|
||||
<option name="SUBSCRIPT" value="false" />
|
||||
<option name="SUPERSCRIPT" value="false" />
|
||||
<option name="SUPPRESS_HTML_BLOCKS" value="false" />
|
||||
<option name="SUPPRESS_INLINE_HTML" value="false" />
|
||||
<option name="TABLES" value="true" />
|
||||
<option name="TASKLISTITEMS" value="true" />
|
||||
<option name="TOC" value="false" />
|
||||
<option name="WIKILINKS" value="true" />
|
||||
</PegdownExtensions>
|
||||
<ParserOptions>
|
||||
<option name="COMMONMARK_LISTS" value="true" />
|
||||
<option name="DUMMY" value="false" />
|
||||
<option name="EMOJI_SHORTCUTS" value="true" />
|
||||
<option name="FLEXMARK_FRONT_MATTER" value="false" />
|
||||
<option name="GFM_LOOSE_BLANK_LINE_AFTER_ITEM_PARA" value="false" />
|
||||
<option name="GFM_TABLE_RENDERING" value="true" />
|
||||
<option name="GITBOOK_URL_ENCODING" value="false" />
|
||||
<option name="GITHUB_EMOJI_URL" value="false" />
|
||||
<option name="GITHUB_LISTS" value="false" />
|
||||
<option name="GITHUB_WIKI_LINKS" value="true" />
|
||||
<option name="JEKYLL_FRONT_MATTER" value="false" />
|
||||
<option name="SIM_TOC_BLANK_LINE_SPACER" value="true" />
|
||||
</ParserOptions>
|
||||
</ParserSettings>
|
||||
<HtmlSettings headerTopEnabled="false" headerBottomEnabled="false" bodyTopEnabled="false" bodyBottomEnabled="false" embedUrlContent="false" addPageHeader="true">
|
||||
<GeneratorProvider>
|
||||
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.generator" providerName="Default Swing HTML Generator" />
|
||||
</GeneratorProvider>
|
||||
<headerTop />
|
||||
<headerBottom />
|
||||
<bodyTop />
|
||||
<bodyBottom />
|
||||
</HtmlSettings>
|
||||
<CssSettings previewScheme="UI_SCHEME" cssUri="" isCssUriEnabled="false" isCssTextEnabled="false" isDynamicPageWidth="true">
|
||||
<StylesheetProvider>
|
||||
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.css" providerName="Default Swing Stylesheet" />
|
||||
</StylesheetProvider>
|
||||
<ScriptProviders />
|
||||
<cssText />
|
||||
</CssSettings>
|
||||
<HtmlExportSettings updateOnSave="false" parentDir="$ProjectFileDir$" targetDir="$ProjectFileDir$" cssDir="" scriptDir="" plainHtml="false" imageDir="" copyLinkedImages="false" imageUniquifyType="0" targetExt="" useTargetExt="false" noCssNoScripts="false" linkToExportedHtml="true" exportOnSettingsChange="true" regenerateOnProjectOpen="false" />
|
||||
<LinkMapSettings>
|
||||
<textMaps />
|
||||
</LinkMapSettings>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
300
hiscores.js
Normal file
300
hiscores.js
Normal file
@@ -0,0 +1,300 @@
|
||||
const statsURL = {
|
||||
main: 'http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=',
|
||||
iron: 'http://services.runescape.com/m=hiscore_oldschool_ironman/index_lite.ws?player=',
|
||||
ult: 'http://services.runescape.com/m=hiscore_oldschool_ultimate/index_lite.ws?player=',
|
||||
hc: 'http://services.runescape.com/m=hiscore_oldschool_hardcore_ironman/index_lite.ws?player='
|
||||
},
|
||||
scoresURL = {
|
||||
main: 'http://services.runescape.com/m=hiscore_oldschool/overall.ws?',
|
||||
iron: 'http://services.runescape.com/m=hiscore_oldschool_ironman/overall.ws?',
|
||||
ult: 'http://services.runescape.com/m=hiscore_oldschool_ultimate/overall.ws?',
|
||||
hc: 'http://services.runescape.com/m=hiscore_oldschool_hardcore_ironman/overall.ws?'
|
||||
},
|
||||
hiscores = {
|
||||
skills: [
|
||||
'overall',
|
||||
'attack',
|
||||
'defence',
|
||||
'strength',
|
||||
'hitpoints',
|
||||
'ranged',
|
||||
'prayer',
|
||||
'magic',
|
||||
'cooking',
|
||||
'woodcutting',
|
||||
'fletching',
|
||||
'fishing',
|
||||
'firemaking',
|
||||
'crafting',
|
||||
'smithing',
|
||||
'mining',
|
||||
'herblore',
|
||||
'agility',
|
||||
'thieving',
|
||||
'slayer',
|
||||
'farming',
|
||||
'runecraft',
|
||||
'hunter',
|
||||
'construction'],
|
||||
other: [
|
||||
'easyclues',
|
||||
'mediumclues',
|
||||
'allclues',
|
||||
'roguebh',
|
||||
'hunterbh',
|
||||
'hardclues',
|
||||
'lms',
|
||||
'eliteclues',
|
||||
'masterclues'
|
||||
]
|
||||
},
|
||||
validModes = ['full', 'main', 'iron', 'hc', 'ult']
|
||||
|
||||
async function getStats (rsn, mode = 'full') {
|
||||
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(!validModes.includes(mode.toLowerCase())) {
|
||||
throw Error('Invalid game mode')
|
||||
}
|
||||
else {
|
||||
return await getPlayerStats(rsn, mode.toLowerCase())
|
||||
}
|
||||
}
|
||||
|
||||
async function getPlayerStats (rsn, mode) {
|
||||
let player = {
|
||||
rsn: rsn,
|
||||
mode: mode,
|
||||
dead: false,
|
||||
deironed: false,
|
||||
main: {},
|
||||
iron: {},
|
||||
hc: {},
|
||||
ult: {}
|
||||
}
|
||||
if(mode === 'full') {
|
||||
const responses = []
|
||||
let csv
|
||||
|
||||
responses[0] = await fetch(statsURL.main + encodeURIComponent(rsn))
|
||||
if (responses[0].ok) {
|
||||
responses[1] = await fetch(statsURL.iron + encodeURIComponent(rsn))
|
||||
if (responses[1].ok) {
|
||||
responses[2] = await fetch(statsURL.hc + encodeURIComponent(rsn))
|
||||
if (responses[2].ok) {
|
||||
player.mode = 'hc'
|
||||
}
|
||||
else {
|
||||
responses[3] = await fetch(statsURL.ult + encodeURIComponent(rsn))
|
||||
if (responses[3].ok) {
|
||||
player.mode = 'ult'
|
||||
}
|
||||
else {
|
||||
player.mode = 'iron'
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
player.mode = 'main'
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw Error('Player not found')
|
||||
}
|
||||
|
||||
//TODO make a function to handle the csv
|
||||
switch (player.mode) {
|
||||
case 'main':
|
||||
csv = await responses[0].text()
|
||||
player.main = parseStats(csv)
|
||||
break
|
||||
case 'iron':
|
||||
csv = await responses[0].text()
|
||||
player.main = parseStats(csv)
|
||||
csv = await responses[1].text()
|
||||
player.iron = parseStats(csv)
|
||||
if(player.main.stats.overall.xp !== player.iron.stats.overall.xp) {
|
||||
player.deironed = true
|
||||
player.mode = 'main'
|
||||
}
|
||||
break
|
||||
case 'hc':
|
||||
csv = await responses[0].text()
|
||||
player.main = parseStats(csv)
|
||||
csv = await responses[1].text()
|
||||
player.iron = parseStats(csv)
|
||||
csv = await responses[2].text()
|
||||
player.hc = parseStats(csv)
|
||||
if(player.iron.stats.overall.xp !== player.hc.stats.overall.xp) {
|
||||
player.dead = true
|
||||
player.mode = 'iron'
|
||||
}
|
||||
if(player.main.stats.overall.xp !== player.iron.stats.overall.xp) {
|
||||
player.deironed = true
|
||||
player.mode = 'main'
|
||||
}
|
||||
break
|
||||
case 'ult':
|
||||
csv = await responses[0].text()
|
||||
player.main = parseStats(csv)
|
||||
csv = await responses[1].text()
|
||||
player.iron = parseStats(csv)
|
||||
csv = await responses[3].text()
|
||||
player.ult = parseStats(csv)
|
||||
if(player.main.stats.overall.xp !== player.iron.stats.overall.xp) {
|
||||
player.deironed = true
|
||||
player.mode = 'main'
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return player
|
||||
}
|
||||
else {
|
||||
const response = await fetch(statsURL[mode] + encodeURIComponent(rsn))
|
||||
if(!response.ok) {
|
||||
throw Error('Player not found')
|
||||
}
|
||||
const csv = await response.text()
|
||||
player[mode] = parseStats(csv)
|
||||
return player
|
||||
}
|
||||
}
|
||||
|
||||
async function getHiscores (mode, category, page) {
|
||||
let url = scoresURL[mode.toLowerCase()]
|
||||
|
||||
if(hiscores.skills.includes(category.toLowerCase())) {
|
||||
url += 'table=' + hiscores.skills.indexOf(category.toLowerCase()) + '&page=' + page
|
||||
const response = await fetch(url)
|
||||
let playersHTML, players = [], element = document.createElement('html')
|
||||
element.innerHTML = await response.text()
|
||||
playersHTML = element.querySelectorAll('.personal-hiscores__row')
|
||||
|
||||
for(let player of playersHTML) {
|
||||
let attributes = player.querySelectorAll('td')
|
||||
players.push({
|
||||
category: category,
|
||||
rank: attributes[0].innerHTML.slice(1, -1),
|
||||
rsn: attributes[1].childNodes[1].innerHTML.replace(/ /g, ' '),
|
||||
level: attributes[2].innerHTML.slice(1, -1),
|
||||
xp: attributes[3].innerHTML.slice(1, -1),
|
||||
mode: mode
|
||||
})
|
||||
}
|
||||
|
||||
return players
|
||||
}
|
||||
else if(hiscores.other.includes(category.toLowerCase())) {
|
||||
url += 'category_type=1' + '&table=' + hiscores.other.indexOf(category.toLowerCase()) + '&page=' + page
|
||||
const response = await fetch(url)
|
||||
let playersHTML, players = [], element = document.createElement('html')
|
||||
element.innerHTML = await response.text()
|
||||
playersHTML = element.querySelectorAll('.personal-hiscores__row')
|
||||
|
||||
for(let player of playersHTML) {
|
||||
let attributes = player.querySelectorAll('td')
|
||||
players.push({
|
||||
category: category,
|
||||
rank: attributes[0].innerHTML.slice(1, -1),
|
||||
rsn: attributes[1].childNodes[1].innerHTML.replace(/ /g, ' '),
|
||||
score: attributes[2].innerHTML.slice(1, -1),
|
||||
mode: mode
|
||||
})
|
||||
}
|
||||
|
||||
return players
|
||||
}
|
||||
}
|
||||
|
||||
let parseStats = (csv) => {
|
||||
let stats = {
|
||||
stats:{
|
||||
overall:{rank:0,level:0,xp:0},
|
||||
attack:{rank:0,level:0,xp:0},
|
||||
defence:{rank:0,level:0,xp:0},
|
||||
strength:{rank:0,level:0,xp:0},
|
||||
hitpoints:{rank:0,level:0,xp:0},
|
||||
ranged:{rank:0,level:0,xp:0},
|
||||
prayer:{rank:0,level:0,xp:0},
|
||||
magic:{rank:0,level:0,xp:0},
|
||||
cooking:{rank:0,level:0,xp:0},
|
||||
woodcutting:{rank:0,level:0,xp:0},
|
||||
fletching:{rank:0,level:0,xp:0},
|
||||
fishing:{rank:0,level:0,xp:0},
|
||||
firemaking:{rank:0,level:0,xp:0},
|
||||
crafting:{rank:0,level:0,xp:0},
|
||||
smithing:{rank:0,level:0,xp:0},
|
||||
mining:{rank:0,level:0,xp:0},
|
||||
herblore:{rank:0,level:0,xp:0},
|
||||
agility:{rank:0,level:0,xp:0},
|
||||
thieving:{rank:0,level:0,xp:0},
|
||||
slayer:{rank:0,level:0,xp:0},
|
||||
farming:{rank:0,level:0,xp:0},
|
||||
runecraft:{rank:0,level:0,xp:0},
|
||||
hunter:{rank:0,level:0,xp:0},
|
||||
construction:{rank:0,level:0,xp:0}
|
||||
},
|
||||
clues:{
|
||||
all:{rank:0,score:0},
|
||||
easy:{rank:0,score:0},
|
||||
medium:{rank:0,score:0},
|
||||
hard:{rank:0,score:0},
|
||||
elite:{rank:0,score:0},
|
||||
master:{rank:0,score:0}
|
||||
},
|
||||
bh:{
|
||||
rogue:{rank:0,score:0},
|
||||
hunter:{rank:0,score:0}
|
||||
},
|
||||
lms:{rank:0,score:0}
|
||||
}, splitCSV = csv.split('\n'), i = 0, skillInfo
|
||||
for (let skill of hiscores.skills) {
|
||||
skillInfo = splitCSV[i].split(',')
|
||||
stats.stats[skill].rank = skillInfo[0]
|
||||
stats.stats[skill].level = skillInfo[1]
|
||||
stats.stats[skill].xp = skillInfo[2]
|
||||
i++
|
||||
}
|
||||
|
||||
skillInfo = splitCSV[26].split(',')
|
||||
stats.clues.all.rank = skillInfo[0]
|
||||
stats.clues.all.score = skillInfo[1]
|
||||
skillInfo = splitCSV[24].split(',')
|
||||
stats.clues.easy.rank = skillInfo[0]
|
||||
stats.clues.easy.score = skillInfo[1]
|
||||
skillInfo = splitCSV[25].split(',')
|
||||
stats.clues.medium.rank = skillInfo[0]
|
||||
stats.clues.medium.score = skillInfo[1]
|
||||
skillInfo = splitCSV[29].split(',')
|
||||
stats.clues.hard.rank = skillInfo[0]
|
||||
stats.clues.hard.score = skillInfo[1]
|
||||
skillInfo = splitCSV[31].split(',')
|
||||
stats.clues.elite.rank = skillInfo[0]
|
||||
stats.clues.elite.score = skillInfo[1]
|
||||
skillInfo = splitCSV[32].split(',')
|
||||
stats.clues.master.rank = skillInfo[0]
|
||||
stats.clues.master.score = skillInfo[1]
|
||||
|
||||
skillInfo = splitCSV[27].split(',')
|
||||
stats.bh.rogue.rank = skillInfo[0]
|
||||
stats.bh.rogue.score = skillInfo[1]
|
||||
skillInfo = splitCSV[28].split(',')
|
||||
stats.bh.hunter.rank = skillInfo[0]
|
||||
stats.bh.hunter.score = skillInfo[1]
|
||||
|
||||
skillInfo = splitCSV[30].split(',')
|
||||
stats.lms.rank = skillInfo[0]
|
||||
stats.lms.score = skillInfo[1]
|
||||
|
||||
return stats
|
||||
}
|
||||
|
||||
module.exports = {getStats, getHiscores}
|
5
package-lock.json
generated
Normal file
5
package-lock.json
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "osrs-json-hiscores",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1
|
||||
}
|
21
package.json
Normal file
21
package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "osrs-json-hiscores",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "hiscores.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node hiscores.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/maxswa/osrs-json-hiscores.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/maxswa/osrs-json-hiscores/issues"
|
||||
},
|
||||
"homepage": "https://github.com/maxswa/osrs-json-hiscores#readme"
|
||||
}
|
Reference in New Issue
Block a user