MC/website/node/travel-holdings.mjs

22 lines
1.4 KiB
JavaScript

// Does a traveller have a clan or company registered (Home Block) on a world? Reads the same central
// native state JARVIS projects, so the answer matches what players see in game.
const norm=value=>typeof value==='string'?value.toLowerCase().replaceAll('-','_'):'';
/** Pure matcher over a native-state snapshot. Hidden, retired and office records never count. */
export function holdsOnWorld(state,uuid,world){
const target=norm(world),who=String(uuid||'').toLowerCase();if(!target||!who)return false;
for(const group of [state?.clans,state?.companies])for(const record of group||[]){
const home=record.homeBlock;
if(record.lifecycle!=='active'||record.hidden||norm(record.homeServer)!==target||home?.source!=='minecraft'||!Number.isSafeInteger(home.revision)||home.revision<1||norm(home.worldName)!==target)continue;
if((record.members||[]).some(m=>String(m.uuid||'').toLowerCase()===who))return true;
}
return false;
}
export function createNativeHoldingCheck(db,{ttlMs=60000,clock=Date.now,read=null}={}){
let cache=null;
async function load(fresh){
const now=clock();if(!fresh&&cache&&now-cache.at<ttlMs)return cache.state;
const state=read?await read():await(await import('./game-native-state.mjs')).readGameNativeState(db);
cache={at:now,state};return state;
}
return async(_user,uuid,world,{fresh=false}={})=>holdsOnWorld(await load(fresh),uuid,world);
}