web/website/node/game-organisation-policy.mjs
2026-11-27 23:43:05 +00:00

23 lines
2.4 KiB
JavaScript

import {nativeServer} from './native-server-scope.mjs';
import {validUuid} from './game-link.mjs';
export const gameOrganisationError=(message,status=409)=>Object.assign(Error(message),{status});
export function boardingOpen(now){return Math.floor(now/1000)%1200<300;}
export function gameHomeLocation(input){
if(!input||Object.keys(input).some(k=>!['server','worldUuid','worldName','position'].includes(k))||!nativeServer(input.server)||nativeServer(input.worldName)!==nativeServer(input.server)||!validUuid(input.worldUuid)||!input.position||Object.keys(input.position).sort().join(',')!=='x,y,z'||!Object.values(input.position).every(Number.isSafeInteger)||Math.abs(input.position.x)>29999980||Math.abs(input.position.z)>29999980||input.position.y< -64||input.position.y>319)throw gameOrganisationError('Stand in a registered planet world to set your Home Block.',400);
return {server:nativeServer(input.server),worldUuid:input.worldUuid.toLowerCase(),worldName:input.worldName,position:{...input.position}};
}
export function transportDestination(state,kind,row,mode,warKeyId,actorUuid=null){
if(mode==='travel-home'){
if(!row.homeBlock||row.homeBlock.source!=='minecraft'||!nativeServer(row.homeBlock.worldName))throw gameOrganisationError('This organisation has no registered game Home Block.');
return {organisationKind:kind,organisationId:row.id,homeBlock:row.homeBlock};
}
if(mode!=='war-transport'||kind!=='clan')throw gameOrganisationError('War deployment requires a current clan membership.',403);
const war=state.wars.find(w=>w.keyId===warKeyId&&w.active===true&&(w.attackerId===row.id||w.defenderId===row.id));
if(!war)throw gameOrganisationError('This clan has no matching active war.',403);
if(war.roster&&!war.roster.includes(actorUuid))throw gameOrganisationError('War deployment requires a place on the agreed roster.',403);
const target=state.clans.find(c=>c.lifecycle==='active'&&c.id===(war.attackerId===row.id?war.defenderId:war.attackerId));
if(!target?.homeBlock||target.homeBlock.source!=='minecraft')throw gameOrganisationError('The war target has no registered game Home Block.');
// Deploy to the edge of the target chunk, never inside the enemy home vault.
const p=target.homeBlock.position;
return {organisationKind:'clan',organisationId:target.id,warKeyId:war.keyId,homeBlock:{...target.homeBlock,position:{x:Math.floor(p.x/16)*16-8,y:p.y,z:Math.floor(p.z/16)*16+8}}};
}