MC/website/node/pol-repeatable-quests.mjs
2026-11-27 23:43:05 +00:00

45 lines
6.7 KiB
JavaScript

import {REPEATABLE_COOLDOWNS} from '../control-panel/public/action-cooldown-policy.mjs';
import {FREMEN_FAUNA_TRACKS,FREMEN_REPEAT_REWARD,FREMEN_TURNIN_TRACKS,fremenInitialReward} from '../control-panel/public/cp-v2/cp-v2-fremen-husbandry.mjs';
// 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',branch:'spice-harvesting',title:'Gather the spice',units:100,xp:100,issuer:'stilgar',issuerName:'Stilgar'},
{key:'CARGO',metric:'pol-repeat-smuggling',branch:'smugglers',title:'Run the hidden cargo',units:100,xp:100,issuer:'esmar_tuek',issuerName:'Esmar Tuek'},
...FREMEN_FAUNA_TRACKS.map(t=>({key:t.key,metric:t.metric,title:(t.action==='kill'?'Hunt ':'Breed ')+t.species,units:25,initialUnits:30,requires:[t.finalId],faction:'fremen',branch:'fremen-husbandry',objectiveUnit:t.action==='kill'?t.species+' defeats':t.species+' offspring',...FREMEN_REPEAT_REWARD,issuer:t.issuer,issuerName:t.issuerName})),
...FREMEN_TURNIN_TRACKS.map(t=>({...t,faction:'fremen',branch:'fremen-husbandry'}))
].map(row=>Object.freeze({...row,cooldownMs:row.faction?REPEATABLE_COOLDOWNS.MOBS:REPEATABLE_COOLDOWNS[row.key]})));
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=({faction=null}={})=>POL_REPEATABLES.filter(r=>(r.faction||null)===faction).map(r=>({id:'RPT-'+r.key,xp:r.xp,...(r.faction?{faction:r.faction,amountMinor:r.amountMinor}:{})}));
export function repeatableEpisode(accountId,key,episode,{season='S01',baseXP,baseReward=undefined}={}){
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))));
const target=(type.initialUnits||0)+episode*type.units;if(!Number.isSafeInteger(target))throw TypeError('Repeatable target exceeds its bound');
return Object.freeze({id,title:type.title+' · '+episode,objective:'Complete '+type.units+' more verified '+(type.objectiveUnit||(key==='CARGO'?'smuggled units':key==='SPICE'?'harvested spice':key==='PVP'?'player defeats':key==='MOBS'?'mob defeats':'deaths'))+'.',branch:type.branch||'repeatables',metric:type.metric,target,requires:type.requires||[],requiresAny:[],season,xp,revision:1,reward:baseReward===undefined?fremenInitialReward(type):baseReward,repeatable:true,cooldownMs:type.cooldownMs,cooldownGroup:"RPT-"+type.key,episode,policyDefinitionId:'RPT-'+type.key,issuer:type.issuer,issuerName:type.issuerName,optional:false,verifiedOnly:true,xpType:'pol',...(type.faction?{faction:type.faction,displayUnits:type.units,personalXP:type.personalXP,amountMinor:type.amountMinor}:{}),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,faction=null}={}){
if(!uuid(actor?.id))throw Error('Current repeatable player required');
const enabled=POL_REPEATABLES.filter(r=>!r.faction||r.faction===faction);
const configs=await store.query('SELECT definition_id,xp,reward_json FROM jarvis_pol_quest_config WHERE definition_id IN ('+enabled.map(()=>'?').join(',')+')',enabled.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(enabled.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 enabled){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),offset=type.initialUnits||0;const completed=target?target-offset:0;if(!Number.isSafeInteger(completed)||completed<0||(target&&completed===0)||completed%type.units)throw Error('Repeatable claim history requires review');next.set(type.key,completed/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 enabled){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,...(type.faction?{baseReward:config.reward_json?JSON.parse(config.reward_json):null}:{})}));
}
return definitions;
}