forked from unimatrix/web
38 lines
3.6 KiB
JavaScript
38 lines
3.6 KiB
JavaScript
import {createHash} from 'node:crypto';
|
|
import {setEntityParent} from './entity-relations.mjs';
|
|
import {transferOfficeDeed} from './office-deed-transfer.mjs';
|
|
const fail=message=>{throw Object.assign(Error(message),{status:409});};
|
|
const id=(campaign,label)=>{const h=createHash('sha256').update(campaign+':'+label).digest('hex');return h.slice(0,8)+'-'+h.slice(8,12)+'-4'+h.slice(13,16)+'-a'+h.slice(17,20)+'-'+h.slice(20,32);};
|
|
|
|
// Called only by the verified victory consumer, inside consumption of its
|
|
// existing staff-parent-link instrument. No request may supply these deeds.
|
|
export async function politicalConquestAuthority(tx,campaign,state){
|
|
const winner=campaign.teams.find(t=>t.id===campaign.winnerSideId),mandate=winner?.mandates.find(m=>m.officeKey===winner.party.id);
|
|
if(!mandate)fail('The winning political mandate is unavailable.');
|
|
const person=await tx.first("SELECT a.id FROM site_accounts a JOIN site_game_links g ON g.user_id=a.id WHERE LOWER(g.minecraft_uuid)=? AND a.password_hash NOT LIKE '!%' AND a.role NOT IN ('official_account','system_account','memorial') FOR UPDATE",[mandate.ownerUuid.toLowerCase()]);
|
|
if(!person)fail('The winning office requires its current personal controller.');
|
|
const losing=campaign.teams.filter(t=>t.id!==winner.id),losingClans=new Set(losing.flatMap(t=>(t.clanDeeds||[]).map(c=>c.id)));
|
|
const roots=state.clans.filter(c=>losingClans.has(c.id)&&!c.protected&&!(c.parent?.kind==='clan'&&losingClans.has(c.parent.id))).map(c=>c.id).sort();
|
|
const imperial=campaign.provinces.find(p=>p.imperial&&p.sideId!==winner.id);
|
|
const estates=imperial?[...state.clans.map(c=>({...c,kind:'clan'})),...(state.companies||[]).map(c=>({...c,kind:'company'}))].filter(c=>c.lifecycle==='active'&&!c.protected&&!c.hidden&&c.ownerUuid===imperial.deedOwnerUuid&&(!c.parent||c.parent.kind==='politics'&&c.parent.id==='emperor')).map(c=>({kind:c.kind,id:c.id})).sort((a,b)=>(a.kind+a.id).localeCompare(b.kind+b.id)):[];
|
|
return {winnerId:winner.id,winnerOwnerId:person.id,winnerOffice:winner.party.id,winnerUuid:mandate.ownerUuid,loserIds:losing.map(t=>t.id),clanRoots:roots,imperial:imperial?{sourceTermId:imperial.mandateTermId,previousUuid:imperial.deedOwnerUuid,estates}:null};
|
|
}
|
|
export async function settlePoliticalConquest(tx,{campaign,authority,keyId,now}){
|
|
const changes=[];
|
|
for(const clan of authority.clanRoots){
|
|
// Imperial personal estates stay under the Imperial institution after its
|
|
// succession; unrelated clients retain their complete original subtrees.
|
|
const imperialEstate=authority.imperial?.estates.some(e=>e.kind==='clan'&&e.id===clan);
|
|
const parent=imperialEstate?'emperor':authority.winnerOffice;
|
|
await setEntityParent(tx,{childKind:'clan',childId:clan,parentKind:'politics',parentId:parent,origin:'conquest',keyId,requestId:id(campaign.id,'clan:'+clan),actorId:authority.winnerOwnerId,now});
|
|
changes.push({kind:'clan',id:clan,parent});
|
|
}
|
|
let succession=null;
|
|
if(authority.imperial){
|
|
succession=await transferOfficeDeed(tx,{termId:id(campaign.id,'imperial-term'),sourceTermId:authority.imperial.sourceTermId,officeKey:'emperor',winnerId:authority.winnerOwnerId,playerUuid:authority.winnerUuid,sourceId:campaign.id,method:'war',now});
|
|
for(const estate of authority.imperial.estates){if(changes.some(c=>c.kind===estate.kind&&c.id===estate.id))continue;
|
|
await setEntityParent(tx,{childKind:estate.kind,childId:estate.id,parentKind:'politics',parentId:'emperor',origin:'conquest',keyId,requestId:id(campaign.id,estate.kind+':'+estate.id),actorId:authority.winnerOwnerId,now});changes.push({...estate,parent:'emperor'});
|
|
}
|
|
}
|
|
return {parentChanges:changes,imperialSuccession:succession};
|
|
}
|

Staff