forked from unimatrix/web
59 lines
8.1 KiB
JavaScript
59 lines
8.1 KiB
JavaScript
import {guestMatches} from './war-guest-identity.mjs';
|
|
import {WAR_MODES,warPartyKey} from '../control-panel/public/war-exchange-policy.mjs';
|
|
import {clanWarBranch,buildClanWarRoster} from './war-rosters.mjs';
|
|
import {militaryClanId} from './war-military-projection.mjs';
|
|
const worlds=new Set(['atreides','harkonnen','corrino','ordos','fremen','arrakis']);
|
|
const fail=message=>{throw Object.assign(Error(message),{status:409});};
|
|
export function politicalWarParties(mode,parties){
|
|
if(!['world','universal','religious'].includes(mode)||!Array.isArray(parties)||parties.length!==WAR_MODES[mode].parties||parties.some(p=>p.kind!=='politics')||new Set(parties.map(p=>p.id)).size!==parties.length)fail('Choose distinct political campaign sides.');
|
|
if(mode==='world'&&parties.some(p=>!worlds.has(p.id)&&p.id!=='emperor'))fail('World wars require native world offices or an expressly challenged Imperial office.');
|
|
if(mode==='universal'&&(!parties.some(p=>p.id==='emperor')||parties.some(p=>p.id!=='emperor'&&!worlds.has(p.id))))fail('Universal conflict requires the Empire and two independent world coalitions.');
|
|
if(mode==='religious'&&(!parties.some(p=>p.id==='spacing_guild')||!parties.some(p=>p.id==='muad_dib')||parties.filter(p=>worlds.has(p.id)).length!==1))fail('War of Faith requires Guild, Church and an independent world coalition.');
|
|
const order=mode==='religious'?['spacing_guild','muad_dib']:mode==='universal'?['emperor']:[];
|
|
const rank=p=>order.includes(p.id)?order.indexOf(p.id):order.length;
|
|
return [...parties].sort((a,b)=>rank(a)-rank(b));
|
|
}
|
|
function office(state,key){const o=state.offices.find(o=>o.officeKey===key);if(!o||o.lifecycle!=='active'||o.militaryReview||!o.leader?.uuid||!o.source?.termId||!o.source?.keyId)fail('A campaign office requires its current verified mandate and military roster.');return o;}
|
|
function affiliatedClans(state,key){return state.clans.filter(c=>{if(c.lifecycle!=='active'||c.protected)return false;let current=c;const seen=new Set();for(let depth=0;current&&depth<32;depth++){if(seen.has(current.id))fail('Military clan hierarchy contains a cycle.');seen.add(current.id);if(current.parent?.kind==='politics')return current.parent.id===key;current=current.parent?.kind==='clan'?state.clans.find(x=>x.id===current.parent.id):null;}if(current)fail('Military hierarchy exceeds supported depth.');return false;});}
|
|
export function politicalTeam(state,party,mode,allies=[],selection=null,guests=[],allowEmpty=false){
|
|
const root=office(state,party.id),participants=[party,...allies.filter(a=>a.sideKey===warPartyKey(party)).map(a=>a.ally)];
|
|
if(mode==='religious'&&party.id==='spacing_guild'&&!participants.some(p=>p.kind==='politics'&&p.id==='emperor'))participants.push({kind:'politics',id:'emperor'});
|
|
const offices=[],clans=new Map(),rosterBranches=[];
|
|
for(const participant of participants){
|
|
if(participant.kind==='politics'){const current=office(state,participant.id);if(offices.some(o=>o.officeKey===current.officeKey))continue;offices.push(current);rosterBranches.push({id:militaryClanId(current.officeKey),ownerUuid:current.leader.uuid,members:current.members||[]});for(const clan of affiliatedClans(state,current.officeKey))clans.set(clan.id,clan);}
|
|
else if(participant.kind==='clan'){const branch=clanWarBranch(state.clans,participant.id);if(branch[0].protected)fail('Protected military clans join through their political office.');for(const clan of branch)clans.set(clan.id,clan);}
|
|
else fail('Invalid coalition participant.');
|
|
}
|
|
const players=buildClanWarRoster([...rosterBranches,...clans.values()],WAR_MODES[mode].playersPerSide,selection,guests,allowEmpty);
|
|
return {root,offices,clans:[...clans.values()],team:{id:militaryClanId(party.id),name:root.name,party,homeServer:root.homeServer,officeKeys:offices.map(o=>o.officeKey),mandates:offices.map(o=>({officeKey:o.officeKey,keyId:o.source.keyId,termId:o.source.termId,ownerUuid:o.leader.uuid})),...players,clanIds:[...new Set([...offices.map(o=>militaryClanId(o.officeKey)),...clans.keys()])],clanDeeds:[...clans.values()].map(c=>({id:c.id,ownerUuid:c.ownerUuid}))}};
|
|
}
|
|
export function buildPoliticalCampaignSnapshot(state,proposal,charters){
|
|
if(proposal.intervention){if(proposal.mode!=='world'||proposal.parties.length!==1||proposal.parties[0].kind!=='politics'||!worlds.has(proposal.parties[0].id))fail('Independent native world required.');}else politicalWarParties(proposal.mode,proposal.parties);const teams=[],provinces=[],seen=new Set(),entities=new Set(),homeServers=[];
|
|
for(const party of proposal.parties){
|
|
const current=politicalTeam(state,party,proposal.mode,proposal.allies||[],proposal.rosters?.[warPartyKey(party)]??null,proposal.guestTeams?.[warPartyKey(party)]||[],proposal.forcedEmperor&&party.id==='emperor'),team=current.team;
|
|
if(!team.roster.length&&!(proposal.forcedEmperor&&party.id==='emperor')||current.root.homeBlock?.source!=='minecraft')fail('Each political side needs a verified roster and registered in-game Home Block.');
|
|
homeServers.push(current.root.homeServer);teams.push(team);
|
|
for(const id of team.clanIds){if(entities.has(id))fail('An organisation cannot participate on opposing sides.');entities.add(id);}
|
|
for(const record of [...current.offices,...current.clans]){
|
|
if(proposal.mode==='world'&&record.officeKey===party.id&¤t.offices.some(o=>o.officeKey==='emperor'))continue;
|
|
if(record.homeBlock?.source!=='minecraft')continue;if(seen.has(record.homeBlock.id))fail('Coalition Home Blocks must be distinct.');seen.add(record.homeBlock.id);
|
|
const political=!!record.officeKey,key=political?record.source.keyId:charters.find(k=>k.clan_id===record.id)?.key_id;
|
|
if(!key)fail('A coalition Home Block requires its current charter.');
|
|
provinces.push({id:record.homeBlock.id,kind:political?'protected':'clan',organisationId:record.id,name:record.name,sideId:team.id,deedKeyId:key,deedOwnerUuid:political?record.leader.uuid:record.ownerUuid,mandateTermId:political?record.source.termId:null,officeKey:record.officeKey||null,claimRevision:record.claimRevision,server:record.homeServer,homeBlock:structuredClone(record.homeBlock),imperial:record.officeKey==='emperor'});
|
|
}
|
|
}
|
|
if(proposal.mode==='world'&&homeServers.length>1&&homeServers[0]===homeServers[1])fail('A world campaign requires two different home worlds.');
|
|
if(['universal','religious'].includes(proposal.mode)&&!provinces.some(p=>p.imperial))fail('Register the Imperial final Home Block in game before agreeing this campaign.');
|
|
// Keep the Imperial finale in the last map slot, independent of side ordering.
|
|
provinces.sort((a,b)=>Number(!!a.imperial)-Number(!!b.imperial));
|
|
return {teams,provinces,coalitions:structuredClone(proposal.allies||[]),...(proposal.forcedEmperor?{forcedEmperor:true}:{})};
|
|
}
|
|
export function currentPoliticalCampaign(campaign,state,charters){
|
|
const valid=new Set();for(const team of campaign.teams){
|
|
const current=politicalTeam(state,team.party,campaign.mode,campaign.coalitions||[],null),members=new Set([...current.team.roster,...current.team.reserves].map(p=>p.uuid));
|
|
for(const mandate of team.mandates||[]){const now=current.offices.find(o=>o.officeKey===mandate.officeKey);if(!now||now.source.keyId!==mandate.keyId||now.source.termId!==mandate.termId||now.leader.uuid!==mandate.ownerUuid)fail('A political campaign mandate changed.');}
|
|
for(const deed of team.clanDeeds||[])if(!current.clans.some(c=>c.id===deed.id&&c.ownerUuid===deed.ownerUuid))fail('A coalition clan deed changed.');
|
|
for(const p of team.roster)if(members.has(p.uuid)||guestMatches(p,current.root.homeServer,state))valid.add(p.uuid);
|
|
for(const province of campaign.provinces.filter(p=>p.sideId===team.id)){const record=province.officeKey?current.offices.find(o=>o.officeKey===province.officeKey):current.clans.find(c=>c.id===province.organisationId);const key=province.officeKey?record?.source.keyId:charters.find(k=>k.clan_id===province.organisationId)?.key_id;if(!record||record.id!==province.organisationId||record.homeServer!==province.server||key!==province.deedKeyId||record.claimRevision!==province.claimRevision||JSON.stringify(record.homeBlock)!==JSON.stringify(province.homeBlock)||province.officeKey&&record.source.termId!==province.mandateTermId)fail('A political campaign Home Block or charter changed.');}
|
|
}return valid;
|
|
}
|

Staff