Skip to content

Instantly share code, notes, and snippets.

@malte-laukoetter
Last active January 25, 2018 14:35
Show Gist options
  • Save malte-laukoetter/1ce0f134d76b712c789ed1d72c76386d to your computer and use it in GitHub Desktop.
Save malte-laukoetter/1ce0f134d76b712c789ed1d72c76386d to your computer and use it in GitHub Desktop.
calculates the 20 players that have gained the most points on the currently viewed leaderboard on https://hive.lergin.de/leaderboards when executed in the devtools console
var lead = document.querySelector("my-app").shadowRoot.querySelector("hive-gamemode-leaderboards");
var prop = gameModeConfigs[lead.gamemode].leaderboard[0].key;
var collection = firebase.default.firestore().collection("gameLeaderboards").doc(lead.gamemode).collection("data");
var changeDate = new Date(lead.date);
var changeAmount = lead.timeForChanges.split('_')[1]
switch (lead.timeForChanges.split('_')[0]) {
case "day":
changeDate.setDate(changeDate.getDate() - changeAmount);
break;
case "month":
changeDate.setMonth(changeDate.getMonth() - changeAmount);
break;
case "year":
changeDate.setFullYear(changeDate.getFullYear() - changeAmount);
break;
}
Promise.all([0,1,2,3,4,5,6,7,8,9].map(async a =>
[
await collection.doc(`${lead.date}-${a}`).get().then(res => JSON.parse(LZString.decompressFromBase64(res.data().a.toBase64()))),
await collection.doc(`${changeDate.toISOString().substring(0, 10)}-${a}`).get().then(res => JSON.parse(LZString.decompressFromBase64(res.data().a.toBase64()))),
]
))
.then( res => {
let [currArr, prevArr] = res.reduce(([currArr, prevArr], [curr, prev]) => [currArr.concat(curr), prevArr.concat(prev)], [[],[]])
currArr.map(place => {
let placePrevData = prevArr.filter(a => a.uuid === place.uuid);
if (placePrevData[0]) {
place.prev = Object.keys(placePrevData[0])
.map(key => [key, place[key] - placePrevData[0][key] || NaN])
.reduce((obj, [key, val]) => { obj[key] = val; return obj }, {})
}
return place;
})
.filter(a => a.prev && a.prev[prop])
.sort((a,b) => b.prev[prop] - a.prev[prop] || 0)
.filter((a, i) => i < 20)
.map(a => `${a.name} : ${a.prev[prop]}`)
.forEach(a => console.log(a))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment