26 lines
2.2 KiB
JavaScript
26 lines
2.2 KiB
JavaScript
import {xpReport} from '../control-panel/public/cp-v2/cp-v2-progression-sidebar.mjs';
|
|
import {canViewAccount} from './postbank-authorisations.mjs';
|
|
import {money} from './postbank-catalogue.mjs';
|
|
// Existing quest/key readers and bank authority only. Never collect facts, claim
|
|
// a reward, sync bank identities, create accounts or change balances on a HUD read.
|
|
export function createGameBriefing({personal,pol,bank=null,authority,clock=Date.now}={}){
|
|
const cache=new Map();
|
|
const summary=data=>{const report=xpReport(data);return report?{level:report.level,xp:report.xp,nextXP:report.nextXP,cap:report.cap,quest:report.quest?.title||null}:null;};
|
|
return async actor=>{
|
|
const now=clock(),prior=cache.get(actor.id);if(prior&&prior.until>now)return prior.value;
|
|
const [p,r,a]=await Promise.allSettled([personal?.quests.list(actor),pol?.quests.list(actor),authority?.(actor)]);
|
|
const context=a.status==='fulfilled'?a.value:null;
|
|
let funds=null;
|
|
if(bank&&context){try{
|
|
const entity=context.entities.find(e=>e.kind==='person'&&e.key===actor.id&&e.canView);
|
|
if(entity){const rows=await bank.query("SELECT a.id,a.balance_minor,a.reserved_minor FROM pb_accounts a JOIN pb_clients c ON c.client_number=a.client_number JOIN pb_entities e ON e.id=c.entity_id WHERE e.id=? AND e.kind='person' AND e.external_key=? AND e.active=1 AND a.state='active' AND a.system_key IS NULL AND a.type_code IN ('102','103','104','108')",[entity.id,actor.id]);
|
|
const allowed=rows.filter(row=>canViewAccount(context,entity.id,row.id));
|
|
if(allowed.length)funds={balance:money(allowed.reduce((n,row)=>n+BigInt(row.balance_minor),0n)),available:money(allowed.reduce((n,row)=>n+BigInt(row.balance_minor)-BigInt(row.reserved_minor),0n)),currency:'DM'};
|
|
}
|
|
}catch{/* Missing bank data is unavailable, never an invented zero balance. */}}
|
|
const names=kind=>context?[...new Set(context.entities.filter(e=>e.kind===kind&&e.canView).map(e=>e.name))]:null;
|
|
const value={personal:p.status==='fulfilled'?summary(p.value):null,reputation:r.status==='fulfilled'?summary(r.value):null,bank:funds,clans:names('clan'),companies:names('company'),updatedAt:now};
|
|
for(const [id,row]of cache)if(row.until<=now)cache.delete(id);
|
|
cache.set(actor.id,{until:now+30000,value});return value;
|
|
};
|
|
}
|

Staff