import {createProgressionMilestoneNews} from './progression-milestone-news.mjs'; import {createQuestProgression} from './progression-quests.mjs'; import {createXPInstruments} from './progression-xp-instruments.mjs'; import {firstLoginState} from './first-login.mjs'; import {can} from './permissions.mjs'; const first=async(db,sql,args=[])=>{const rows=await db.query(sql,args);return rows[0]||null;}; export function createProgressionRuntime(central,bank,keys,{clock=Date.now,resolveCase=async()=>null,verifyAcquisition=async()=>false}={}){ const observer=Object.freeze({internal:'progression-source-reader'}); const resolvePlayer=id=>first(central,"SELECT id,username,display_name,role FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%'",[id]); async function authorizePlayer(actor,store){ if(typeof actor.revalidate==='function'&&(await actor.revalidate())?.id!==actor.id)return false; return !!await first(store,"SELECT id FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%'",[actor.id]); } const quests=createQuestProgression(central,keys,{clock,authorizePlayer,resolvePlayer, authorizeObservation:async actor=>actor===observer, // A relation label alone cannot prove a settled transfer or its beneficiary. // The existing deed settlement flow must supply its verified adapter. verifyAcquisition:async(actor,input,tx)=>actor===observer&&await verifyAcquisition(input,tx) }); const xp=createXPInstruments(central,keys,{clock,resolveCase,authorizeIssue:async(actor,type)=>{ const fresh=await resolvePlayer(actor.id);if(!fresh)return false; return can(central,fresh,type==='xp-correction'?'web.progression.xp.correct':'web.progression.seasons.manage'); }}); async function collect(actor){ if(!await authorizePlayer(actor,central))throw Error('Current player identity required'); const now=clock(),seasons=await central.query("SELECT season_id,starts_at FROM jarvis_progression_seasons WHERE state='active' AND starts_at<=? AND ends_at>?",[now,now]); if(seasons.length!==1)return {state:'season-not-configured',sources:[]};const season=seasons[0],sources=[]; const save=async(metric,value)=>{ const quantity=Number(value);if(!Number.isSafeInteger(quantity)||quantity<0)throw Error('Source counter is not safe'); await quests.observe(observer,{accountId:actor.id,seasonId:season.season_id,metric,value:quantity,eventId:'web:'+actor.id+':'+season.season_id+':'+metric+':'+quantity,source:'verified-website'}); }; const source=async(name,metrics,work)=>{try{await work();sources.push({name,metrics,state:'available'});}catch{sources.push({name,metrics,state:'unavailable'});}}; await source('profile',['onboarding','profile','faction'],async()=>{ const profile=await first(central,'SELECT a.display_name,p.bio,l.minecraft_uuid FROM site_accounts a LEFT JOIN site_profiles p ON p.user_id=a.id LEFT JOIN site_game_links l ON l.user_id=a.id WHERE a.id=?',[actor.id]); if(profile?.minecraft_uuid&&!await firstLoginState(central,actor))await save('onboarding',1); if(profile?.display_name?.trim()&&String(profile.bio||'').trim().length>=10)await save('profile',1); const choice=await first(central,'SELECT c.faction FROM site_character_choices c JOIN site_game_links l ON l.user_id=c.user_id AND l.minecraft_uuid=c.minecraft_uuid WHERE c.user_id=?',[actor.id]);if(choice?.faction)await save('faction',1); }); await source('forum',['forum-registration','forum-contributions'],async()=>{ const account=await first(central,'SELECT uid FROM drforum_dune_accounts WHERE website_id=?',[actor.id]);if(!account)return;await save('forum-registration',1); // Count unique meaningful, moderated-visible messages, with at most the first // eligible contribution per thread/day; copied text never adds extra progress. const count=await first(central,"SELECT COUNT(DISTINCT SHA2(LOWER(TRIM(p.message)),256)) total FROM drforum_posts p JOIN drforum_threads t ON t.tid=p.tid WHERE p.uid=? AND p.visible=1 AND t.visible=1 AND p.dateline>=? AND LENGTH(TRIM(p.message))>=40 AND NOT EXISTS(SELECT 1 FROM drforum_posts earlier WHERE earlier.uid=p.uid AND earlier.tid=p.tid AND earlier.visible=1 AND LENGTH(TRIM(earlier.message))>=40 AND FLOOR(earlier.dateline/86400)=FLOOR(p.dateline/86400) AND earlier.pid{ const transfers=await first(bank,"SELECT COUNT(*) total FROM pb_orders o JOIN pb_accounts a ON a.id=o.source_id JOIN pb_clients c ON c.client_number=a.client_number JOIN pb_entities e ON e.id=c.entity_id JOIN pb_accounts target ON target.id=o.target_id WHERE e.kind='person' AND e.external_key=? AND o.state='posted' AND o.mode IN ('standard','instant') AND o.posted_at>=? AND o.source_id<>o.target_id AND (target.client_number IS NULL OR target.client_number<>a.client_number)",[actor.id,Number(season.starts_at)]);await save('bank-transfer',transfers.total); const wages=await first(bank,"SELECT COUNT(*) total,COALESCE(SUM(o.amount_minor),0) earned FROM pb_orders o JOIN pb_accounts a ON a.id=o.target_id JOIN pb_clients c ON c.client_number=a.client_number JOIN pb_entities e ON e.id=c.entity_id WHERE e.kind='person' AND e.external_key=? AND o.state='posted' AND o.mode='payroll' AND o.posted_at>=?",[actor.id,Number(season.starts_at)]);await save('wage-received',wages.total);await save('earned-minor',wages.earned); }); return {state:'verified',sources}; } return Object.freeze({quests,xp,collect,announcements:createProgressionMilestoneNews(central,keys,{clock})}); }