MC/website/web/diplomacy.js

6 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const diplomacyHouses=['Fremen','Harkonnen','Corrino','Atreides','Ordos'];let diplomacyBusy=false;
const dipEl=(tag,text,cls)=>{const e=document.createElement(tag);if(text!==undefined)e.textContent=text;if(cls)e.className=cls;return e};
function officeCard(title,holder,description,icon){const a=dipEl('article',undefined,'office-card');const img=dipEl('img');img.src='assets/'+icon;img.alt='';a.append(img,dipEl('h3',title),dipEl('p',holder,'office-holder'),dipEl('p',description));return a}
function showOffices(data){const live=data.status==='connected';const absent=live?'Not elected':'Server not connected';document.querySelector('#imperial-offices').replaceChildren(officeCard('Emperor',live?(data.emperor||'Not elected'):absent,'Elected player from the current seven-day election bucket.','faction-corrino.svg'),officeCard('Spice Steward',live?(data.spiceSteward||'Not appointed'):absent,'The Emperor’s current appointed faction for spice rights.','item-11.webp'),officeCard('House of Water',live?(data.houseOfWater||'Not appointed'):absent,'The Emperor’s current appointed faction for water rights.','item-17.webp'));document.querySelector('#faction-leaders').replaceChildren(...diplomacyHouses.map(h=>officeCard('House '+h,live?(data.leaders.find(x=>x.faction===h)?.leader||'Not elected'):absent,'Current elected faction leader. Votes are cast in Minecraft.','faction-'+h.toLowerCase()+'.svg')));document.querySelector('#diplomacy-updated').textContent=live?'Last game snapshot: '+new Date(data.updatedAt).toLocaleString()+' · Election bucket '+data.electionWeek:''}
async function refreshDiplomacy(){if(diplomacyBusy)return;diplomacyBusy=true;const b=document.querySelector('#refresh-diplomacy');b.disabled=true;try{const r=await fetch('/api/diplomacy',{cache:'no-store'});if(!r.ok)throw Error();const data=await r.json();showOffices(data);document.querySelector('#diplomacy-status').textContent=data.status==='connected'?'Connected · Current game offices':data.status==='awaiting_snapshot'?'Connected to Discord · awaiting a fresh diplomacy snapshot':data.status==='unavailable'?'Diplomacy feed temporarily unavailable':'Server not connected · No current leaders or appointments can be confirmed'}catch{showOffices({status:'unavailable'});document.querySelector('#diplomacy-status').textContent='Diplomacy feed temporarily unavailable'}finally{diplomacyBusy=false;b.disabled=false}}
showOffices({status:'not_connected'});document.querySelector('#refresh-diplomacy').addEventListener('click',refreshDiplomacy);refreshDiplomacy();setInterval(()=>{if(!document.hidden)refreshDiplomacy()},60000);