web/website/node/pol-repeatable-quests.mjs
2026-11-21 20:22:37 +01:00

39 lines
5.1 KiB
JavaScript

// Server-owned episode definitions for the EXISTING POL quest engine. Each
// episode has its own ordinary QST and immutable claim; old history is retained.
export const POL_REPEATABLES=Object.freeze([
{key:'PVP',metric:'pol-repeat-pvp',title:'Defeat a rival',units:1,xp:100,issuer:'gurney_halleck',issuerName:'Gurney Halleck'},
{key:'MOBS',metric:'pol-repeat-mobs',title:'Clear hostile creatures',units:10,xp:50,issuer:'gurney_halleck',issuerName:'Gurney Halleck'},
{key:'DEATH',metric:'pol-repeat-death',title:'Learn from defeat',units:5,xp:25,issuer:'gurney_halleck',issuerName:'Gurney Halleck'},
{key:'PVE',metric:'pol-repeat-pve-death',title:'Survive the next encounter',units:5,xp:25,issuer:'gurney_halleck',issuerName:'Gurney Halleck'},
{key:'SPICE',metric:'pol-repeat-spice',title:'Gather the spice',units:100,xp:100,issuer:'stilgar',issuerName:'Stilgar'},
{key:'CARGO',metric:'pol-repeat-smuggling',title:'Run the hidden cargo',units:100,xp:100,issuer:'esmar_tuek',issuerName:'Esmar Tuek'}
].map(Object.freeze));
export const POL_REPEATABLE_POLICY=Object.freeze({version:1,growth:1.02,maximumMultiplier:5,defaultCurrencyReward:null});
const uuid=v=>typeof v==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(v);
export const repeatablePolicyDefinitions=()=>POL_REPEATABLES.map(r=>({id:'RPT-'+r.key,xp:r.xp}));
export function repeatableEpisode(accountId,key,episode,{season='S01',baseXP}={}){
const type=POL_REPEATABLES.find(r=>r.key===key);
if(!uuid(accountId)||!type||!Number.isSafeInteger(episode)||episode<1||episode>Math.floor(Number.MAX_SAFE_INTEGER/type.units)||!/^S\d{2,4}$/.test(season)||!Number.isSafeInteger(baseXP)||baseXP<0||baseXP>1000000)throw TypeError('Invalid repeatable quest episode');
const id='RPT-'+type.key+'-'+accountId.toLowerCase()+'-'+episode;if(id.length>64)throw TypeError('Repeatable episode reference exceeds its bound');
const xp=Math.ceil(baseXP*Math.min(POL_REPEATABLE_POLICY.maximumMultiplier,Math.pow(POL_REPEATABLE_POLICY.growth,Math.min(episode-1,1000))));
return Object.freeze({id,title:type.title+' · '+episode,objective:'Complete '+type.units+' more verified '+(key==='CARGO'?'smuggled units':key==='SPICE'?'harvested spice':key==='PVP'?'player defeats':key==='MOBS'?'mob defeats':'deaths')+'.',branch:'repeatables',metric:type.metric,target:episode*type.units,requires:[],requiresAny:[],season,xp,revision:1,reward:null,repeatable:true,episode,policyDefinitionId:'RPT-'+type.key,issuer:type.issuer,issuerName:type.issuerName,optional:false,verifiedOnly:true,xpType:'pol',executionRequirements:Object.freeze({website:true,minecraft:true})});
}
export function parseRepeatableEpisode(id,accountId){
if(!uuid(accountId)||typeof id!=='string')throw TypeError('Current repeatable player required');
for(const r of POL_REPEATABLES){const prefix='RPT-'+r.key+'-'+accountId.toLowerCase()+'-';if(!id.startsWith(prefix))continue;const suffix=id.slice(prefix.length);if(!/^[1-9]\d{0,15}$/.test(suffix)||!Number.isSafeInteger(Number(suffix)))throw TypeError('Invalid repeatable episode');return {key:r.key,episode:Number(suffix)};}
return null;
}
export async function repeatableDefinitions(store,actor,{season='S01',requestedDefinitionId=null}={}){
if(!uuid(actor?.id))throw Error('Current repeatable player required');
const configs=await store.query('SELECT definition_id,xp FROM jarvis_pol_quest_config WHERE definition_id IN ('+POL_REPEATABLES.map(()=>'?').join(',')+')',POL_REPEATABLES.map(t=>'RPT-'+t.key));
const state=await store.query('SELECT s.definition_id FROM jarvis_pol_quest_states s LEFT JOIN jarvis_pol_quest_claims c ON c.key_id=s.key_id WHERE s.account_id=? AND s.season_id=? AND c.key_id IS NULL',[actor.id,season]);
const definitions=[],all=new Set(),next=new Map(POL_REPEATABLES.map(t=>[t.key,1]));
for(const row of state){const parsed=parseRepeatableEpisode(row.definition_id,actor.id);if(parsed)all.add(row.definition_id);}
for(const type of POL_REPEATABLES){const rows=await store.query('SELECT MAX(s.progress_value) target FROM jarvis_pol_quest_states s JOIN jarvis_pol_quest_claims c ON c.key_id=s.key_id WHERE s.account_id=? AND s.season_id=? AND s.definition_id LIKE ?',[actor.id,season,'RPT-'+type.key+'-'+actor.id.toLowerCase()+'-%']);const target=Number(rows[0]?.target||0);if(!Number.isSafeInteger(target)||target<0||target%type.units)throw Error('Repeatable claim history requires review');next.set(type.key,target/type.units+1);}
if(requestedDefinitionId&&parseRepeatableEpisode(requestedDefinitionId,actor.id)){const rows=await store.query('SELECT definition_id FROM jarvis_pol_quest_states WHERE account_id=? AND season_id=? AND definition_id=?',[actor.id,season,requestedDefinitionId]);if(rows.length===1)all.add(requestedDefinitionId);}
for(const type of POL_REPEATABLES){const config=configs.find(c=>c.definition_id==='RPT-'+type.key);if(!config||!Number.isSafeInteger(config.xp)||config.xp<0)continue;
const episodes=new Set([next.get(type.key)]);for(const id of all){const p=parseRepeatableEpisode(id,actor.id);if(p.key===type.key)episodes.add(p.episode);}
for(const episode of [...episodes].sort((a,b)=>a-b))definitions.push(repeatableEpisode(actor.id,type.key,episode,{season,baseXP:config.xp}));
}
return definitions;
}