-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
1 lines (1 loc) · 3.99 KB
/
scripts.js
1
const fileSelector=document.getElementById("file-selector"),trancheSelector=document.getElementById("tranche-selector"),jsonContentDiv=document.getElementById("json-content"),headers=["Tranche","Feca","Osamodas","Enutrof","Sram","Xelor","Ecaflip","Eniripsa","Iop","Cra","Sadida","Sacrieur","Pandawa","Roublard","Zobal","Ouginak","Steamer","Eliotrope","Huppermage"],selectedTranches={};async function fetchData(e){try{const t=await fetch(e);if(!t.ok)throw new Error("Network response was not ok");return await t.json()}catch(e){return{}}}function generateTable(e,t=!1){const a=document.createElement("table"),n=document.createElement("thead"),o=document.createElement("tbody"),l=document.createElement("tr");headers.forEach((e=>{const t=document.createElement("th");t.textContent=e,l.appendChild(t)})),n.appendChild(l);let r=headers.slice(1).reduce(((e,t)=>({...e,[t.toLowerCase()]:0})),{}),c=[];if(Object.keys(e).forEach((t=>{const a=e[t],n=document.createElement("tr"),l=[],s=document.createElement("td");s.textContent=t,n.appendChild(s),headers.slice(1).forEach((e=>{const t=document.createElement("td"),o=a[e.toLowerCase()]||0;t.textContent=o,l.push(o),n.appendChild(t),r[e.toLowerCase()]+=o}));const d=[...l].sort(((e,t)=>t-e));n.childNodes.forEach(((e,t)=>{t>0&&e.classList.add((e=>{const t=d.indexOf(e);return 0===t?"gold":1===t?"silver":2===t?"bronze":t<10?"light-green":t<15?"light-red":t>=15?"heavy-red":""})(l[t-1]))})),o.appendChild(n),c.push(n)})),t){const e=document.createElement("tr"),t=document.createElement("td");t.textContent="Total",e.appendChild(t),headers.slice(1).forEach((t=>{const a=document.createElement("td");a.textContent=r[t.toLowerCase()]||0,e.appendChild(a)}));const a=Object.values(r),n=[...a].sort(((e,t)=>t-e)),l=e=>{const t=n.indexOf(e);return 0===t?"gold":1===t?"silver":2===t?"bronze":t<10?"light-green":t<15?"light-red":t>=15?"heavy-red":""};e.childNodes.forEach(((e,t)=>{t>0&&e.classList.add(l(a[t-1]))})),o.appendChild(e)}return a.appendChild(n),a.appendChild(o),a}function updateFileSelector(e){fileSelector.innerHTML="",e.forEach((e=>{fileSelector.appendChild(new Option(e.display_name,e.path))}))}function updateTrancheSelector(e){trancheSelector.innerHTML="",trancheSelector.appendChild(new Option("Show All","show-all")),e&&(Object.keys(e).forEach((e=>{"Total"!==e&&trancheSelector.appendChild(new Option(e,e))})),trancheSelector.appendChild(new Option("Total","total")))}async function loadFiles(){try{const e=[{path:"./ogrest/08-2024.json",display_name:"08-2024 (Ogrest)"},{path:"./ogrest/09-2024.json",display_name:"09-2024 (Ogrest)"},{path:"./ogrest/10-2024.json",display_name:"10-2024 (Ogrest)"},{path:"./rubilax/08-2024.json",display_name:"08-2024 (Rubilax)"},{path:"./rubilax/09-2024.json",display_name:"09-2024 (Rubilax)"},{path:"./rubilax/10-2024.json",display_name:"10-2024 (Rubilax)"},{path:"./pandora/08-2024.json",display_name:"08-2024 (Pandora)"},{path:"./pandora/09-2024.json",display_name:"09-2024 (Pandora)"},{path:"./pandora/10-2024.json",display_name:"10-2024 (Pandora)"}];updateFileSelector(e),fileSelector.value=e[0].path,await loadFileContent(e[0].path)}catch(e){}}async function loadFileContent(e){trancheSelector.value;const t=await fetchData(e);updateTrancheSelector(t),trancheSelector.value="show-all",displayJson(t,!0),setTimeout((()=>{trancheSelector.value=selectedTranches[e]||"show-all",displayJson(t,"show-all"===trancheSelector.value)}),0)}function displayJson(e,t=!1){jsonContentDiv.innerHTML="";const a=generateTable(e,t);jsonContentDiv.appendChild(a)}fileSelector.addEventListener("change",(function(){loadFileContent(this.value)})),trancheSelector.addEventListener("change",(function(){const e=this.value,t=fileSelector.value;selectedTranches[t]=e,fetchData(t).then((t=>{if("show-all"===e){const e=Object.values(t).reduce(((e,t)=>(Object.keys(t).forEach((a=>{e[a]=(e[a]||0)+t[a]})),e)),{});displayJson({...t,Total:e},!0)}else if("total"===e){displayJson({Total:Object.values(t).reduce(((e,t)=>(Object.keys(t).forEach((a=>{e[a]=(e[a]||0)+t[a]})),e)),{})},!1)}else displayJson({[e]:t[e]})}))})),loadFiles();