web/website/node/game-briefing.mjs

31 lines
3 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 async function readGameMemberships(db,actor){
const clans=await db.query('SELECT DISTINCT c.name FROM site_clans c JOIN site_clan_members m ON m.clan_id=c.id WHERE m.user_id=? AND c.active=1 ORDER BY c.name LIMIT 20',[actor.id]);
const companies=await db.query("SELECT DISTINCT c.name FROM corrino_companies c LEFT JOIN site_company_members m ON m.company_id=c.id WHERE c.status='active' AND (m.user_id=? OR c.ceo_account_id=?) ORDER BY c.name LIMIT 20",[actor.id,actor.id]);
return {clans:clans.map(row=>row.name),companies:companies.map(row=>row.name)};
}
export function createGameBriefing({personal,pol,bank=null,authority,organisations=null,identity=null,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,o,i]=await Promise.allSettled([personal?.quests.list(actor),pol?.quests.list(actor),authority?.(actor),organisations?.(actor),identity?.(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=>organisations?(o.status==='fulfilled'?o.value?.[kind==='clan'?'clans':'companies']||null:null):context?[...new Set(context.entities.filter(e=>e.kind===kind&&e.canView).map(e=>e.name))]:null;
const value={identity:i.status==='fulfilled'?i.value||null:null,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;
};
}