Last active
October 18, 2022 10:16
ZKill advanced search summary table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tableId = `summary-table`; | |
const billion = 1000000000; | |
const million = 1000000; | |
const thousand = 1000; | |
function shortIskToNum(strIsk) { | |
if (strIsk[strIsk.length - 1] === 'k') { | |
return Math.floor(parseFloat(strIsk.slice(0, -1)) * thousand); | |
} else if (strIsk[strIsk.length - 1] === 'm') { | |
return Math.floor(parseFloat(strIsk.slice(0, -1)) * million); | |
} else if (strIsk[strIsk.length - 1] === 'b') { | |
return Math.floor(parseFloat(strIsk.slice(0, -1)) * billion); | |
} else | |
return 1000; | |
// who cares, it's either hundreds or trillions | |
} | |
function numToShortIsk(num) { | |
if (num > billion) { | |
return (num / billion).toFixed(2) + 'b'; | |
} | |
if (num > million) { | |
return (num / million).toFixed(2) + 'm'; | |
} | |
if (num > thousand) { | |
return (num / thousand).toFixed(2) + 'k'; | |
} | |
return num; | |
} | |
function parseTime(str) { | |
const date = new Date(); | |
let parts = str.split(':'); | |
date.setHours(parts[0]); | |
date.setMinutes(parts[1]); | |
return date; | |
} | |
function genTable() { | |
const rows = document.querySelectorAll('.killListRow'); | |
let endTime, startTimeStr; | |
const aggregate = Array.from(rows).reduce((ac,row,i)=>{ | |
const [time,isk] = row.children[0].textContent.split('\n').filter(x=>x); | |
if (i === 0) | |
endTime = parseTime(time); | |
startTimeStr = time; | |
const system = row.children[2].querySelector('a').textContent; | |
if (ac[system]) { | |
ac[system].kills += 1; | |
ac[system].totalIsk += shortIskToNum(isk); | |
} else { | |
ac[system] = { | |
kills: 1, | |
totalIsk: shortIskToNum(isk) | |
}; | |
} | |
return ac; | |
} | |
, {}); | |
const startTime = parseTime(startTimeStr); | |
console.log((endTime - startTime).toString()); | |
const diff = new Date(endTime - startTime) | |
let results = Object.entries(aggregate).map(([system,details])=>({ | |
system, | |
kills: details.kills, | |
totalIsk: details.totalIsk | |
})); | |
results.sort((a,b)=>b.kills - a.kills); | |
results = results.slice(0, 10); | |
let container = document.getElementById(tableId); | |
if (!container) { | |
container = document.createElement('div'); | |
container.id = tableId; | |
document.getElementById('killmails-result').insertAdjacentElement('beforebegin', container); | |
} | |
container.innerHTML = `<h4>Kills during the last ${diff.getUTCHours()} hour and ${diff.getUTCMinutes()} minutes</h4> | |
<table class="table table-condensed table-striped table-hover"> | |
<thead><tr> | |
<th>System</th> | |
<th>Kills</th> | |
<th>Total Isk</th> | |
</tr></thead> | |
<tbody> | |
${results.map(result=>` | |
<tr> | |
<td>${result.system}</td> | |
<td>${result.kills}</td> | |
<td>${numToShortIsk(result.totalIsk)}</td> | |
</tr> | |
`).join('')} | |
</tbody> | |
</table>`; | |
console.log('table generated'); | |
} | |
genTable(); | |
function debounce(cb, timeout) { | |
let timeoutId; | |
return ()=>{ | |
clearInterval(timeoutId); | |
timeoutId = setTimeout(cb, timeout); | |
} | |
; | |
} | |
const debouncedTable = debounce(genTable, 1000); | |
const targetNode = document.getElementById('killmails-result'); | |
const callback = (mutationList,observer)=>{ | |
debouncedTable(); | |
} | |
; | |
const observer = new MutationObserver(callback); | |
observer.observe(targetNode, { | |
attributes: true, | |
childList: true, | |
subtree: true | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment