MC/website/web/diplomacy.js
2026-11-27 23:43:05 +00:00

6 lines
2.7 KiB
JavaScript
Raw Permalink 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.','appointments/crown.svg'),officeCard('Landsraad · Specerhuis',live?(window.realmNames.text(data.spiceSteward)||'Not appointed'):absent,'The Emperor’s current appointed faction for spice rights.','appointments/spice.svg'),officeCard('Landsraad · Welvaart House',live?(window.realmNames.text(data.houseOfWater)||'Not appointed'):absent,'The Emperor’s current appointed faction for water rights.','appointments/water.svg'));document.querySelector('#faction-leaders').replaceChildren(...diplomacyHouses.map(h=>officeCard('House '+window.realmNames.text(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);