105 lines
3.7 KiB
JavaScript
105 lines
3.7 KiB
JavaScript
function sameArrays(array1, array2) {
|
|
if(array1.length != array2.length) return false;
|
|
for(let e of array1) {
|
|
if(!array2.includes(e)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
async function championInfos(client, patch, championId) {
|
|
const database = client.db("matches");
|
|
const matches = database.collection(patch)
|
|
const allMatches = matches.find()
|
|
|
|
let winningMatches = 0;
|
|
let losingMatches = 0;
|
|
let totalMatches = 0;
|
|
const runes = [];
|
|
for await (let match of allMatches) {
|
|
totalMatches += 1;
|
|
for(let participant of match.info.participants) {
|
|
if(participant.championId != championId) continue;
|
|
|
|
if(participant.win)
|
|
winningMatches += 1;
|
|
else
|
|
losingMatches += 1;
|
|
|
|
const primaryStyle = participant.perks.styles[0].style
|
|
const secondaryStyle = participant.perks.styles[1].style
|
|
const selections = []
|
|
for(let style of participant.perks.styles) {
|
|
for(let perk of style.selections) {
|
|
selections.push(perk.perk)
|
|
}
|
|
}
|
|
const gameRunes = {count:1, primaryStyle: primaryStyle, secondaryStyle: secondaryStyle, selections: selections};
|
|
let addRunes = true;
|
|
for(let rune of runes) {
|
|
if(rune.primaryStyle == gameRunes.primaryStyle
|
|
&& rune.secondaryStyle == gameRunes.secondaryStyle
|
|
&& sameArrays(rune.selections, gameRunes.selections)) {
|
|
rune.count++; addRunes = false; break;
|
|
}
|
|
}
|
|
if(addRunes) runes.push(gameRunes)
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Filter runes to keep 3 most played
|
|
const maxes = [0, 0, 0] // 24, 2, 1 -> 18 > 1 -> 24, 2, 2 -> 18 > 2 -> 24, 24, 2
|
|
const maxRunes = [null, null, null]
|
|
for(let rune of runes) {
|
|
let maxcount = 2;
|
|
if(rune.count <= maxes[maxcount]) continue;
|
|
|
|
while(maxcount > 0 && rune.count > maxes[maxcount]) {
|
|
maxes[maxcount] = maxes[maxcount - 1];
|
|
maxRunes[maxcount] = maxRunes[maxcount - 1];
|
|
maxcount--;
|
|
}
|
|
|
|
rune.pickrate = rune.count / (winningMatches + losingMatches)
|
|
if(rune.count <= maxes[maxcount]) maxcount++;
|
|
maxes[maxcount] = rune.count
|
|
maxRunes[maxcount] = rune
|
|
}
|
|
|
|
return {id: championId,
|
|
winrate:winningMatches / (winningMatches + losingMatches),
|
|
gameCount:(winningMatches + losingMatches),
|
|
pickrate:(winningMatches + losingMatches)/totalMatches,
|
|
runes: maxRunes.filter((x) => x != null)
|
|
};
|
|
}
|
|
|
|
async function makeChampionStat(client, patch, championId) {
|
|
const championInfo = await championInfos(client, patch, championId)
|
|
const database = client.db("champions")
|
|
const collection = database.collection(patch)
|
|
await collection.updateOne({id: championInfo.id}, {$set: championInfo}, { upsert: true })
|
|
}
|
|
|
|
async function championList() {
|
|
const response = await fetch("https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champion-summary.json");
|
|
const list = await response.json()
|
|
return list.slice(1)
|
|
}
|
|
|
|
async function makeChampionsStats(client, patch) {
|
|
const list = await championList()
|
|
console.log("Generating stats for " + list.length + " champions")
|
|
let i = 0;
|
|
for(let champion of list) {
|
|
console.log("Entry " + i + "/" + list.length + " ...")
|
|
await makeChampionStat(client, patch, champion.id)
|
|
i += 1
|
|
}
|
|
|
|
const database = client.db("champions")
|
|
const collection = database.collection(patch)
|
|
await collection.createIndex({id:1})
|
|
}
|
|
|
|
module.exports = {makeChampionsStats} |