web/website/node/war-rosters.mjs
2026-11-27 23:43:05 +00:00

65 lines
5.4 KiB
JavaScript

import {WAR_MODES} from '../control-panel/public/war-exchange-policy.mjs';
const uuid=value=>typeof value==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(value);
const reject=message=>{throw Object.assign(Error(message),{status:409});};
// Read-only projection of the original hierarchy. A temporary supermother must
// never flatten these records or replace their owners and primary memberships.
export function clanWarBranch(clans,rootId){
if(!Array.isArray(clans)||clans.length>500)reject('Clan hierarchy requires review.');
const active=new Map(clans.filter(c=>c.lifecycle==='active').map(c=>[c.id,c]));
if(!active.has(rootId))reject('The war clan is no longer active.');
const branch=[];
for(const clan of active.values()){
let current=clan;const seen=new Set();
for(let depth=0;current&&depth<32;depth++){
if(seen.has(current.id))reject('Clan hierarchy contains a cycle.');seen.add(current.id);
if(current.id===rootId)branch.push({clan,depth});
current=current.parent?.kind==='clan'?active.get(current.parent.id):null;
if(current&&depth===31)reject('Clan hierarchy exceeds supported depth.');
}
}
return branch.sort((a,b)=>a.depth-b.depth||a.clan.id.localeCompare(b.clan.id)).map(r=>r.clan);
}
export function buildClanWarRoster(branch,capacity,selection=null,guests=[],allowEmpty=false){
if(!Number.isSafeInteger(capacity)||capacity<1||capacity>86)throw TypeError('Invalid war roster capacity.');
const seen=new Set(),members=[];
for(const clan of branch){
const priority=m=>m.uuid?.toLowerCase()===clan.ownerUuid?.toLowerCase()?-1:({owner:0,heir:1}[m.role]??2);
const ordered=[...(clan.members||[])].sort((a,b)=>priority(a)-priority(b)||String(a.uuid).localeCompare(String(b.uuid)));
for(const member of ordered){
const id=member.uuid?.toLowerCase();if(!uuid(id))reject('A roster member needs a verified game identity.');
if(!seen.has(id)){seen.add(id);members.push({uuid:id,name:member.name,clanId:clan.id});}
}
}
for(const guest of guests){const id=guest.uuid?.toLowerCase();if(!uuid(id)||!uuid(guest.accountId)||!guest.homeServer)reject('A guest needs its verified roster acceptance.');if(!seen.has(id)){seen.add(id);members.push({...guest,uuid:id,guest:true,clanId:null});}}
if(selection!==null){
if(!Array.isArray(selection)||(!allowEmpty&&selection.length<1)||selection.length>capacity||selection.some(id=>!uuid(id))||new Set(selection).size!==selection.length)reject('Choose a unique roster within the side capacity.');
const available=new Map(members.map(m=>[m.uuid,m]));
if(selection.some(id=>!available.has(id)))reject('A selected player no longer belongs to this clan branch.');
const chosen=new Set(selection);return {roster:selection.map(id=>available.get(id)),reserves:members.filter(m=>!chosen.has(m.uuid)),clanIds:branch.map(c=>c.id)};
}
return {roster:members.slice(0,capacity),reserves:members.slice(capacity),clanIds:branch.map(c=>c.id)};
}
export function imperialClanSupport(state,root,allies=[]){const pledged=allies.some(a=>a.sideKey==='clan:'+root.id&&a.ally?.kind==='politics'&&a.ally.id==='emperor');if(!pledged)return null;if(root.parent?.kind!=='politics'||root.parent.id!=='emperor'||root.parent.origin!=='conquest')reject('Imperial client deed changed.');const office=state.offices?.find(o=>o.officeKey==='emperor');if(!office||office.militaryReview||office.lifecycle!=='active'||!office.leader?.uuid||!office.source?.termId||!office.source?.keyId)reject('Imperial protection requires the current military mandate.');return {id:office.id,ownerUuid:office.leader.uuid,members:office.members||[],mandate:{officeKey:'emperor',keyId:office.source.keyId,termId:office.source.termId,ownerUuid:office.leader.uuid}};}
export function buildClanCampaignSnapshot(state,parties,charters,selections={},guestTeams={},allies=[]){
if(parties.length!==2||parties.some(p=>p.kind!=='clan'))reject('Choose the two agreed clan sides.');
const teams=[],provinces=[];
for(const party of parties){
const branch=clanWarBranch(state.clans,party.id),root=branch[0];
const branchIds=new Set(branch.map(c=>c.id));if((state.wars||[]).some(w=>w.active&&(branchIds.has(w.attackerId)||branchIds.has(w.defenderId))))reject('Finish the current rebellion before booking another campaign.');
if(root.protected)reject('Protected official clans cannot be challenged in a clan war.');
if(!root.homeBlock||root.homeBlock.source!=='minecraft')reject('Each side must register its Home Block in game first.');
const support=imperialClanSupport(state,root,allies),players=buildClanWarRoster(support?[...branch,support]:branch,WAR_MODES.clan.playersPerSide,selections['clan:'+party.id]??null,guestTeams['clan:'+party.id]||[]);
if(!players.roster.length)reject('Each side needs a verified player roster.');
teams.push({id:root.id,name:root.name,party,homeServer:root.homeServer,...players,...(support?{mandates:[support.mandate]}:{})});
for(const clan of branch){
if(clan.protected||!clan.homeBlock||clan.homeBlock.source!=='minecraft')continue;
const charter=charters.find(c=>c.clan_id===clan.id);
if(!charter||!uuid(charter.key_id)||!uuid(clan.ownerUuid))reject('A participating Home Block needs its current clan charter and verified owner.');
provinces.push({id:clan.homeBlock.id,kind:'clan',organisationId:clan.id,name:clan.name,sideId:root.id,deedKeyId:charter.key_id,deedVersion:clan.version,deedOwnerUuid:clan.ownerUuid,claimRevision:clan.claimRevision,server:clan.homeServer,homeBlock:structuredClone(clan.homeBlock)});
}
}
return {teams,provinces};
}