63 lines
4.2 KiB
JavaScript
63 lines
4.2 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){
|
|
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});}
|
|
}
|
|
}
|
|
if(selection!==null){
|
|
if(!Array.isArray(selection)||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 buildClanCampaignSnapshot(state,parties,charters,selections={}){
|
|
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 players=buildClanWarRoster(branch,WAR_MODES.clan.playersPerSide,selections['clan:'+party.id]??null);
|
|
if(!players.roster.length)reject('Each side needs a verified player roster.');
|
|
teams.push({id:root.id,name:root.name,party,...players});
|
|
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};
|
|
}
|

Staff