Align public government register with agreed Realms Court and Landsraad model

This commit is contained in:
unimatrixgit 2026-11-15 11:24:36 +01:00
commit 0987b8c25a
5 changed files with 11 additions and 7 deletions

View file

@ -1,11 +1,13 @@
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:'house',faction,office:faction==='veiled_concord'?'reverent_mother':'house_leader',name:faction==='veiled_concord'?'Office of the Reverent Mother':scope+' Leadership',scope,art,description:faction==='veiled_concord'?'The elected leadership of the Veiled Concord.':'The elected leadership and public representation of '+scope+'.'}))
...[['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 active=terms.filter(t=>t.kind===office.kind&&t.faction===office.faction&&t.office===office.office&&t.outcome==='winner'&&Number(t.starts_at)<=now&&Number(t.expires_at)>now);
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};
@ -22,6 +24,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(),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({offices:projectGovernment(rows.results||[],now),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'],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'}});}
};}

View file

@ -1,7 +1,7 @@
import test from 'node:test';import assert from 'node:assert/strict';
import {projectGovernment,createPublicGovernment,publicElectionCalendar} from '../node/public-government.mjs';
const now=Date.parse('2026-11-15T10:00:00Z');
test('every office remains present before the first election',()=>{const offices=projectGovernment([],now);assert.equal(offices.length,7);assert.ok(offices.every(o=>o.state==='vacant'&&o.holder===null));assert.equal(offices.filter(o=>o.kind==='imperial').length,1);});
test('every office remains present before the first election',()=>{const offices=projectGovernment([],now);assert.equal(offices.length,8);assert.ok(offices.every(o=>o.state==='vacant'&&o.holder===null));assert.equal(offices.filter(o=>o.kind==='imperial').length,1);});
test('only a single active elected term can supply an officeholder',()=>{const term={kind:'imperial',faction:'imperium',office:'emperor',outcome:'winner',starts_at:now-1,expires_at:now+1,username:'citizen_one',display_name:'Citizen One',player_uuid:'private'};const office=projectGovernment([term],now)[0];assert.equal(office.state,'occupied');assert.equal(office.holder.profile,'/people/citizen_one');assert.ok(!JSON.stringify(office).includes('private'));for(const row of [{...term,expires_at:now},{...term,starts_at:now+1},{...term,outcome:'no_votes'}])assert.equal(projectGovernment([row],now)[0].state,'vacant');assert.equal(projectGovernment([term,term],now)[0].state,'verification_pending');});
test('public calendar includes the currently open voting window',()=>{const calendar=publicElectionCalendar(now);assert.equal(calendar[0].phase,'voting');assert.equal(calendar[0].termWeeks,2);assert.equal(calendar[1].phase,'applications');});
test('public office read rejects overrides and never invents vacancies on a database outage',async()=>{const api=createPublicGovernment({prepare(){throw Error('DB unavailable');}},{clock:()=>now});assert.equal((await api(new Request('https://www.dunerealms.com/api/public/government?user=other'))).status,400);assert.equal((await api(new Request('https://www.dunerealms.com/api/public/government',{method:'POST'}))).status,405);const response=await api(new Request('https://www.dunerealms.com/api/public/government'));assert.equal(response.status,503);assert.equal((await response.json()).offices,undefined);});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,5 @@
const el=(tag,text,cls)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(cls)n.className=cls;return n;};
const definitions=[['emperor','Office of the Emperor','The Imperium','emperor-v1.png','imperial'],['atreides','House Aerdrecht Leadership','House Aerdrecht','house-aerdrecht-v1.png'],['harkonnen','House Harkveen Leadership','House Harkveen','house-harkveen-v1.png'],['corrino','House Korenholt Leadership','House Korenholt','house-korenholt-v1.png'],['ordos','House Oordwijk Leadership','House Oordwijk','house-oordwijk-v1.png'],['fremen','Vrijduin Leadership','Vrijduin','house-vrijduin-v1.png'],['veiled_concord','Office of the Reverent Mother','The Veiled Concord','veiled-concord-v1.png']];
const definitions=[['emperor','Office of the Emperor','The Imperium','emperor-v1.png','imperial'],['atreides','House Aerdrecht Leadership','House Aerdrecht','house-aerdrecht-v1.png'],['harkonnen','House Harkveen Leadership','House Harkveen','house-harkveen-v1.png'],['corrino','House Korenholt Leadership','House Korenholt','house-korenholt-v1.png'],['ordos','House Oordwijk Leadership','House Oordwijk','house-oordwijk-v1.png'],['fremen','Office of the Naib','Vrijduin','house-vrijduin-v1.png'],['veiled_concord','Office of the Reverent Mother','The Veiled Concord','veiled-concord-v1.png','court'],['spacing_guild','Vaerkring Leadership','Vaerkring · Spacing Guild','office-landsraad.png','court']];
const date=n=>new Date(n).toLocaleString('en-GB',{timeZone:'Europe/Brussels',dateStyle:'medium',timeStyle:'short'})+' · Brussels';
function card(office,loading=false){
const c=el('article',undefined,'staff-person'),art=el('img');art.src='/assets/seals/'+office.art;art.alt=office.scope+' insignia';art.width=art.height=100;art.className='government-office-art';
@ -8,7 +8,7 @@ function card(office,loading=false){
else{const placeholder=el('div',undefined,'government-vacant');placeholder.append(el('strong',loading?'AWAITING CONFIRMATION':office.state==='verification_pending'?'AWAITING VERIFICATION':'VACANT'),el('span',loading?'Checking the elected office register…':office.state==='verification_pending'?'The elected identity is being verified.':'Awaiting election'));c.append(placeholder);}
const a=el('a','View elections →','staff-profile-link');a.href='https://cp.dunerealms.com/#politics/'+(office.kind==='imperial'?'imperial-elections':'house-elections');c.append(a);return c;
}
function render(offices,loading=false){document.querySelector('#imperial-offices').replaceChildren(...offices.filter(o=>o.kind==='imperial').map(o=>card(o,loading)));document.querySelector('#house-offices').replaceChildren(...offices.filter(o=>o.kind!=='imperial').map(o=>card(o,loading)));}
function render(offices,loading=false){document.querySelector('#court-offices').replaceChildren(...offices.filter(o=>o.kind==='court').map(o=>card(o,loading)));document.querySelector('#imperial-offices').replaceChildren(...offices.filter(o=>o.kind==='imperial').map(o=>card(o,loading)));document.querySelector('#house-offices').replaceChildren(...offices.filter(o=>o.kind==='house').map(o=>card(o,loading)));}
render(definitions.map(([key,name,scope,art,kind='house'])=>({key,name,scope,art,kind})),true);
try{
const response=await fetch('/api/public/government',{cache:'no-store'});if(!response.ok)throw Error();const data=await response.json();if(!Array.isArray(data.offices)||data.offices.length!==definitions.length)throw Error();