MC/website/node/imperial-raid-reservations.mjs

15 lines
1.1 KiB
JavaScript

import {clanWarBranch} from './war-rosters.mjs';
// Private Home Block/claim mutation guard only. NPC raids are never projected
// into native wars or faction enemy/PvP state.
export async function imperialRaidHomeReservations(store,{lock=false}={}){
const rows=await store.query("SELECT id,accepted_json FROM jarvis_imperial_raid_orders WHERE state IN ('accepted','warning','started') ORDER BY id"+(lock?' FOR UPDATE':''));
return rows.map(row=>{const p=JSON.parse(row.accepted_json);return {id:row.id,kind:'imperial-raid',reserved:true,active:false,attackerId:p.orderingClanId,defenderId:p.targetClanId};});
}
// Called while the original bank calendar lock is held. Paid accept obtains the
// same lock before any debit, so a new booking cannot race an invisible payment.
export async function imperialRaidCalendarAvailable(central,parties,state){
const clans=new Set(parties.filter(p=>p.kind==='clan').flatMap(p=>clanWarBranch(state.clans,p.id).map(c=>c.id)));
if(!clans.size)return true;
return !(await imperialRaidHomeReservations(central)).some(r=>clans.has(r.attackerId)||clans.has(r.defenderId));
}