Rotate deferred quest successor retries without starving later players

This commit is contained in:
unimatrixgit 2026-11-29 09:06:59 +01:00
commit d142317b18

View file

@ -20,9 +20,13 @@ export function createQuestProgression(db,keys,{definitions=QUEST_DEFINITIONS,au
// This runs after reward commit: issuing another key must never nest a key transaction.
async function processNextQuests({accountId=null,parentId=null,actor=null}={}){
if(accountId!==null&&!uuid(accountId))throw TypeError('Invalid completion account');
const args=[],where=["state='queued'",parentId?'purpose=?':"purpose LIKE 'quest-next:%'"];
const args=[],where=[parentId?'purpose=?':"purpose LIKE 'quest-next:%'"];
if(parentId)args.push(nextPurpose(parentId));if(accountId){where.push('account_id=?');args.push(accountId);}
const jobs=await db.query('SELECT * FROM jarvis_progression_outbox WHERE '+where.join(' AND ')+' ORDER BY created_at,id LIMIT 20',args);
const scope=where.join(' AND '),select="SELECT * FROM jarvis_progression_outbox WHERE state='queued' AND "+scope+' ORDER BY created_at,id LIMIT 20';
let jobs=await db.query(select,args);
// Drain each bounded wave before retrying blocked predecessors, so an old
// batch cannot starve later players. Keep the original creation timestamps.
if(!jobs.length){await db.query("UPDATE jarvis_progression_outbox SET state='queued' WHERE state='deferred' AND "+scope,args);jobs=await db.query(select,args);}
const result={started:0,failed:0,pending:false,nextQuestId:null,choices:[]};
for(const job of jobs)try{
const current=actor?.id===job.account_id?actor:await resolvePlayer(job.account_id);if(!current)throw Error('Player unavailable');
@ -39,8 +43,8 @@ export function createQuestProgression(db,keys,{definitions=QUEST_DEFINITIONS,au
else if(!children.length)finish=true;
else result.pending=true;
}
if(finish)await db.query("UPDATE jarvis_progression_outbox SET state='completed' WHERE id=? AND state='queued'",[job.id]);
}catch{result.failed++;result.pending=true;}
await db.query("UPDATE jarvis_progression_outbox SET state=? WHERE id=? AND state='queued'",[finish?'completed':'deferred',job.id]);
}catch{result.failed++;result.pending=true;await db.query("UPDATE jarvis_progression_outbox SET state='deferred' WHERE id=? AND state='queued'",[job.id]);}
return result;
}
async function player(actor,store=db){if(!uuid(actor?.id)||!await authorizePlayer(actor,store))throw Error('Current player identity required');}