Add two-chamber election catalog and searchable candidate register

This commit is contained in:
unimatrixgit 2026-11-15 21:41:11 +01:00
commit e7c8951511

View file

@ -1,22 +1,19 @@
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.'}
]);
import {ELECTION_RULES,electionDates,electionWindow} from '../control-panel/public/election-rules.mjs';
import {ELECTIONS} from '../control-panel/public/political-catalog.mjs';
export const PUBLIC_OFFICES=Object.freeze(ELECTIONS.map(e=>({key:({bene:'veiled_concord',guild:'spacing_guild','clan-delegate':'clan_delegate'})[e.id]||e.id,kind:e.kind,faction:e.faction,office:({imperial:'emperor',house:'house_leader',bene:'reverent_mother',guild:'guild_leader',clan_delegate:'clan_delegate'})[e.kind],name:e.label,scope:e.kind==='house'?e.label.replace(' Leader',''):e.label,chamber:e.chamber,art:e.seal,description:e.chamber+' · Elected for four weeks.'})));
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 ledgerKinds=office.kind==='bene'?['bene','house']:office.kind==='guild'?['guild','court']:[office.kind];
const active=terms.filter(t=>ledgerKinds.includes(t.kind)&&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=>{
return Object.keys(ELECTION_RULES).map(kind=>{
const dates=electionDates(kind,now-3*3600000,1),window=electionWindow(kind,dates[0],now);
return {kind,...window,termWeeks:kind==='house'?2:4};
return {kind,...window,termWeeks:4};
});
}
export async function readGovernmentOffices(db,now=Date.now()){
@ -28,6 +25,6 @@ export function createPublicGovernment(db,{clock=Date.now}={}){return async requ
if(new URL(request.url).search)return Response.json({message:'No identity overrides are accepted.'},{status:400});
try{
const now=clock(),offices=await readGovernmentOffices(db,now);
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,elections:publicElectionCalendar(now),observedAt:now},{headers:{'Cache-Control':'public,max-age=30'}});
return Response.json({structure:{landsraad:['emperor','atreides','harkonnen','corrino','ordos'],realms:['atreides','harkonnen','corrino','ordos','fremen'],court:['veiled_concord','spacing_guild','clan_delegate'],chambers:[{name:'Imperial Senate',offices:['emperor','atreides','harkonnen','corrino','ordos']},{name:'Small Council',offices:['veiled_concord','spacing_guild','clan_delegate']}],clanAffiliation:'home_world',companiesBranch:'spacing_guild'},offices,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'}});}
};}