MC/website/node/public-government.mjs

29 lines
3.7 KiB
JavaScript

import {electionDates,electionWindow} from '../control-panel/public/election-rules.mjs';
export const PUBLIC_OFFICES=Object.freeze([
{key:'emperor',kind:'imperial',faction:'imperium',office:'emperor',name:'Office of the Emperor',scope:'The Imperium',art:'emperor-v1.png',description:'The elected imperial office, representing the realms across the Imperium.'},
...[['atreides','House Aerdrecht','house-aerdrecht-v1.png'],['harkonnen','House Harkveen','house-harkveen-v1.png'],['corrino','House Korenholt','house-korenholt-v1.png'],['ordos','House Oordwijk','house-oordwijk-v1.png'],['fremen','Vrijduin','house-vrijduin-v1.png'],['veiled_concord','The Veiled Concord','veiled-concord-v1.png']].map(([faction,scope,art])=>({key:faction,kind:faction==='veiled_concord'?'court':'house',faction,office:faction==='veiled_concord'?'reverent_mother':'house_leader',name:faction==='veiled_concord'?'Office of the Reverent Mother':faction==='fremen'?'Office of the Naib':scope+' Leadership',scope,art,description:faction==='veiled_concord'?'The elected leadership of the Veiled Concord.':'The elected leadership and public representation of '+scope+'.'})),
{key:'spacing_guild',kind:'court',faction:'spacing_guild',office:'guild_leader',name:'Vaerkring Leadership',scope:'Vaerkring · Spacing Guild',art:'office-landsraad.png',description:'The economic branch of the Imperial Court, connecting companies and their trade interests.'}
]);
export function projectGovernment(terms,now=Date.now()){
return PUBLIC_OFFICES.map(office=>{
const ledgerKind=office.faction==='veiled_concord'?'house':office.kind;
const active=terms.filter(t=>t.kind===ledgerKind&&t.faction===office.faction&&t.office===office.office&&t.outcome==='winner'&&Number(t.starts_at)<=now&&Number(t.expires_at)>now);
const term=active.length===1?active[0]:null;
const holder=term&&/^[a-z0-9_]{3,32}$/i.test(term.username||'')?{username:term.username,name:term.display_name||term.username,profile:'/people/'+encodeURIComponent(term.username),avatar:'https://cp.dunerealms.com/api/avatars/face/roster/'+encodeURIComponent(term.username)}:null;
return {...office,state:holder?'occupied':active.length?'verification_pending':'vacant',holder,term:holder?{startsAt:Number(term.starts_at),expiresAt:Number(term.expires_at)}:null};
});
}
export function publicElectionCalendar(now=Date.now()){
return ['house','imperial'].map(kind=>{
const dates=electionDates(kind,now-3*3600000,1),window=electionWindow(kind,dates[0],now);
return {kind,...window,termWeeks:kind==='house'?2:4};
});
}
export function createPublicGovernment(db,{clock=Date.now}={}){return async request=>{
if(request.method!=='GET')return Response.json({message:'Read-only public office register.'},{status:405});
if(new URL(request.url).search)return Response.json({message:'No identity overrides are accepted.'},{status:400});
try{
const now=clock(),rows=await db.prepare("SELECT t.kind,t.faction,t.office,t.starts_at,t.expires_at,t.outcome,a.username,a.display_name FROM political_office_terms t LEFT JOIN site_accounts a ON a.id=t.user_id AND a.password_hash NOT LIKE '!%' WHERE t.outcome='winner' AND t.starts_at<=? AND t.expires_at>? ORDER BY t.starts_at DESC LIMIT 100").bind(now,now).all();
return Response.json({structure:{landsraad:['emperor','atreides','harkonnen','corrino','ordos'],realms:['atreides','harkonnen','corrino','ordos','fremen'],court:['veiled_concord','spacing_guild'],clanAffiliation:'home_world',companiesBranch:'spacing_guild'},offices:projectGovernment(rows.results||[],now),elections:publicElectionCalendar(now),observedAt:now},{headers:{'Cache-Control':'public,max-age=30'}});
}catch{return Response.json({message:'The public office register is temporarily unavailable.'},{status:503,headers:{'Cache-Control':'no-store'}});}
};}