forked from unimatrix/web
34 lines
4.3 KiB
JavaScript
34 lines
4.3 KiB
JavaScript
import {creditXP} from './progression-xp-ledger.mjs';
|
|
import {lockXPAccount,readActiveXPSeason,saveXPAccount} from './progression-xp-store.mjs';
|
|
import {xpAtLevel} from './progression-definitions.mjs';
|
|
import {createResearchTreasuryPayments} from './quest-treasury-payments.mjs';
|
|
import {RESEARCH_POLICY} from '../control-panel/public/organisation-research-policy.mjs';
|
|
import {validFaithReward} from './faith-blessings.mjs';
|
|
export async function awardResearch(polStore,actor,d,award,{clock=Date.now,personalMultiplier=1}={}){
|
|
const r=award.reward;
|
|
if(r.schema!=='organisation-research-reward-v1'||r.owner?.kind!==d.owner.kind||r.owner?.id!==d.owner.id||r.personalXP!==d.personalXP||r.amountMinor!==d.amountMinor)throw Error('Bound research reward required.');
|
|
const tx=polStore.sourceStore;if(!tx)throw Error('Atomic research reward store required.');
|
|
const state=await lockXPAccount(tx,actor.id,clock()),season=await readActiveXPSeason(tx,clock(),d.season),capXP=Number(season.level_cap)===0?Number.MAX_SAFE_INTEGER:xpAtLevel(Number(season.level_cap),Number(season.curve_base));
|
|
if(![1,2].includes(personalMultiplier))throw Error('Invalid research blessing multiplier');const personalXP=d.personalXP*personalMultiplier;
|
|
await saveXPAccount(tx,actor.id,creditXP(state,personalXP,{capXP}),season,award.keyId,'research-claimed',{definitionId:d.id,xp:personalXP,organisation:d.owner},clock());
|
|
return {...award,reward:{...r,schema:'organisation-research-funded-v2',policy:RESEARCH_POLICY,accountId:actor.id,definitionId:d.id,seasonId:d.season}};
|
|
}
|
|
export function researchRecipient(owner){
|
|
if(['clan','company'].includes(owner?.kind))return{kind:owner.kind,id:owner.id};
|
|
if(['office','politics'].includes(owner?.kind))return{kind:'office',id:owner.id==='emperor'?'emperor':'leader:'+(({arrakis:'fremen'})[owner.id]||owner.id)};
|
|
throw Error('Registered research organisation required.');
|
|
}
|
|
// Legacy v1 jobs retain their originally promised website-treasury settlement.
|
|
export function createResearchFundedRewards(central,bank,{clock=Date.now}={}){
|
|
async function process(keyId){return central.transaction(async tx=>{
|
|
const job=await tx.first('SELECT * FROM jarvis_pol_quest_reward_jobs WHERE key_id=? FOR UPDATE',[keyId]);if(!job||job.state!=='queued')return'unchanged';
|
|
const row=await tx.first('SELECT s.definition_id,s.account_id,s.season_id,s.state,c.reward_json,c.claimed_at,k.used_at,k.revoked_at,k.key_type,k.scope_id,k.account_id key_account FROM jarvis_pol_quest_states s JOIN jarvis_pol_quest_claims c ON c.key_id=s.key_id JOIN organisation_keys k ON k.id=s.key_id WHERE s.key_id=? FOR UPDATE',[keyId]);
|
|
const r=JSON.parse(job.payload_json);
|
|
if(!row||row.state!=='claimed'||row.used_at==null||row.revoked_at!==null||row.key_type!=='quest'||row.key_account!==job.account_id||row.account_id!==job.account_id||row.scope_id!==job.season_id+':POL_'+row.definition_id||row.reward_json!==job.payload_json||r.schema!=='organisation-research-funded-v1'||r.policy!==RESEARCH_POLICY||r.accountId!==job.account_id||r.seasonId!==job.season_id||r.definitionId!==row.definition_id)throw Error('Consumed research claim required.');
|
|
const blessed=!!r.faith;if(blessed&&(!Number.isSafeInteger(r.faithBaseAmountMinor)||r.faithBaseAmountMinor<1||r.faithBaseAmountMinor>15000||r.amountMinor!==r.faithBaseAmountMinor*2||!await validFaithReward(tx,r.faith,{accountId:row.account_id,claimedAt:Number(row.claimed_at)}))||!blessed&&Object.hasOwn(r,'faithBaseAmountMinor'))throw Error('Verified research blessing required');
|
|
const recipient=researchRecipient(r.owner),payment=createResearchTreasuryPayments(bank,{clock,blessed,kind:recipient.kind,authorize:async()=>true});
|
|
await payment.pay({keyId,recipientId:recipient.id,amountMinor:r.amountMinor,memo:'Research reward '+keyId});
|
|
await tx.query("UPDATE jarvis_pol_quest_reward_jobs SET state='paid' WHERE key_id=?",[keyId]);return'paid';
|
|
});}
|
|
return{async drain(){const rows=await central.query("SELECT key_id FROM jarvis_pol_quest_reward_jobs WHERE state='queued' AND JSON_EXTRACT(payload_json,'$.schema')='organisation-research-funded-v1' ORDER BY created_at,key_id LIMIT 25"),results=[];for(const row of rows)try{results.push({keyId:row.key_id,state:await process(row.key_id)});}catch{results.push({keyId:row.key_id,state:'awaiting-funding-or-review'});}return{attempted:rows.length,results};}};
|
|
}
|

Staff