gwent-pro-ladder-lib
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -13,78 +13,79 @@ /** | ||
const pro_ladder_url = "https://masters.playgwent.com/en/rankings/pro-ladder/"; | ||
const factions = ['Nilfgaard','Scoia\'tael','Northern Realms','Skellige','Monsters']; | ||
const pro_ladder_url = 'https://masters.playgwent.com/en/rankings/pro-ladder/'; | ||
const factions = [ | ||
'Nilfgaard', | ||
"Scoia'tael", | ||
'Northern Realms', | ||
'Skellige', | ||
'Monsters' | ||
]; | ||
const page_count = 20; | ||
const players_limit = 2860; | ||
function get_page(text) | ||
{ | ||
return text.split('class="btn">Next</a></div></div></div></div>')[0]; | ||
function get_page(text) { | ||
return text.split('class="btn">Next</a></div></div></div></div>')[0]; | ||
} | ||
function get_players(text) | ||
{ | ||
return text.split('<div class="c-ranking-mobile-table__nick">').slice(1); | ||
function get_players(text) { | ||
return text.split('<div class="td-nick">').slice(1); | ||
} | ||
function clean_faction_html(text) | ||
{ | ||
let result = text.replace(/ matches|Current fMMR:|\/100/g, '') | ||
.replace(/<[^>]*>/g, '!') | ||
.replace(/[!]+/g, '!') | ||
.replace('/','') | ||
.replace(/,/g,'') | ||
.split(/[!]+/g); | ||
return result.slice(1,-2); | ||
function clean_faction_html(text) { | ||
let result = text | ||
.replace(/ matches|Current fMMR:|\/100/g, '') | ||
.replace(/<[^>]*>/g, '!') | ||
.replace(/[!]+/g, '!') | ||
.replace('/', '') | ||
.replace(/,/g, '') | ||
.split(/[!]+/g); | ||
return result.slice(1, -2); | ||
} | ||
function clean_player_html(text) | ||
{ | ||
function clean_player_html(text) { | ||
text = text.split(/flag-icon-|">/g)[1] + text; | ||
let result = text.replace(/ matches/g, '') | ||
.replace(/<[^>]*>/g, '!') | ||
.replace(/[!]+/g, '!') | ||
.replace(/,/g,'') | ||
.split(/[!]+/g); | ||
return result.slice(0,-2); | ||
let result = text | ||
.replace(/ matches/g, '') | ||
.replace(/<[^>]*>/g, '!') | ||
.replace(/[!]+/g, '!') | ||
.replace(/,/g, '') | ||
.split(/[!]+/g); | ||
return result.slice(0, -2); | ||
} | ||
function parse_faction(text,index) | ||
{ | ||
function parse_faction(text, index) { | ||
const faction = factions[index]; | ||
const faction_stats = clean_faction_html(text).map((str)=>Number(str)); | ||
const peak_mmr = faction_stats[0]; | ||
const faction_stats = clean_faction_html(text).map(str => Number(str)); | ||
const peak_mmr = faction_stats[0]; | ||
const current_mmr = faction_stats[1]; | ||
var real_mmr; | ||
var matches; | ||
if(faction_stats.length>3) | ||
{ | ||
if (faction_stats.length > 3) { | ||
real_mmr = faction_stats[2]; | ||
matches = faction_stats[3]; | ||
}else | ||
{ | ||
} else { | ||
real_mmr = current_mmr; | ||
matches = faction_stats[2]; | ||
} | ||
return {faction,peak_mmr,current_mmr,real_mmr,matches}; | ||
return { faction, peak_mmr, current_mmr, real_mmr, matches }; | ||
} | ||
function parse_player(text) | ||
{ | ||
let [player_html,...factions] = text.split('<div class="c-ranking-details__content">'); | ||
function parse_player(text) { | ||
let [player_html, ...factions] = text.split( | ||
'<div class="c-ranking-details__content">' | ||
); | ||
let player_stats = clean_player_html(player_html); | ||
factions = factions.map(parse_faction); | ||
const iso = player_stats[0]; | ||
const name = countries.getName(iso.toUpperCase(),'en'); | ||
const country = {iso,name}; | ||
const name = countries.getName(iso.toUpperCase(), 'en'); | ||
const country = { iso, name }; | ||
const player = player_stats[1]; | ||
const mmr = Number(player_stats[2]); | ||
const total_matches = Number(player_stats[3]); | ||
return {player,country,mmr,total_matches,factions}; | ||
return { player, country, mmr, total_matches, factions }; | ||
} | ||
async function get_ladder_players(count){ | ||
async function get_ladder_players(count) { | ||
let promises = []; | ||
count = Math.min(count,players_limit); | ||
for(let page=1;((page-1)*20)<count;page++) | ||
{ | ||
count = Math.min(count, players_limit); | ||
for (let page = 1; (page - 1) * 20 < count; page++) { | ||
promises.push(axios.get(`${pro_ladder_url}${page}`)); | ||
@@ -94,14 +95,12 @@ } | ||
let players = []; | ||
for(let response of responses) | ||
{ | ||
for (let response of responses) { | ||
let result = get_players(get_page(response.data)).map(parse_player); | ||
players.push(...result); | ||
} | ||
return players.slice(0,count); | ||
return players.slice(0, count); | ||
} | ||
module.exports = | ||
{ | ||
module.exports = { | ||
players_limit, | ||
get_ladder_players | ||
}; |
{ | ||
"name": "gwent-pro-ladder-lib", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Small library to request gwent pro ladder stats as json", | ||
@@ -16,3 +16,9 @@ "main": "gwent-pro-ladder-lib.js", | ||
"i18n-iso-countries": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^4.0.1" | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
9564
6
108
1