28 lines
2.4 KiB
JavaScript
28 lines
2.4 KiB
JavaScript
import {PROGRESSION_SCHEMA} from './progression-schema.mjs';
|
|
import {createQuestProgression} from './progression-quests.mjs';
|
|
import {POL_QUEST_DEFINITIONS} from '../control-panel/public/cp-v2/cp-v2-pol-quest-catalogue.mjs';
|
|
// Reuse the quest engine with a fixed, server-owned storage namespace. Account
|
|
// identity, authority and organisation keys remain in their original stores.
|
|
const tables=new Map(PROGRESSION_SCHEMA.map(sql=>{const name=sql.match(/CREATE TABLE IF NOT EXISTS ([a-z_]+)/)[1];return[name,name.replace(/^jarvis_/,'jarvis_pol_')];}));
|
|
export const polProgressionSQL=sql=>sql.replace(/\bjarvis_[a-z_]+\b/g,name=>tables.get(name)||name);
|
|
export function polProgressionStore(db){
|
|
const query=(sql,args=[])=>db.query(polProgressionSQL(sql),args);
|
|
const first=async(sql,args=[])=>{const rows=await query(sql,args);return rows[0]||null;};
|
|
return {query,first,transaction:work=>db.transaction(tx=>work(polProgressionStore(tx)))};
|
|
}
|
|
export async function initializePOLProgression(db){for(const sql of PROGRESSION_SCHEMA)await db.query(polProgressionSQL(sql));}
|
|
const scoped=scope=>{if(!/^S\d{2,4}:[A-Za-z0-9_-]+$/.test(scope))throw Error('Invalid POL quest scope');return scope.replace(':',':POL_');};
|
|
const unscoped=scope=>{if(!/^S\d{2,4}:POL_/.test(scope))throw Error('POL quest scope mismatch');return scope.replace(':POL_',':');};
|
|
export function polQuestKeys(keys){return Object.freeze({
|
|
issue:input=>{if(input.keyType!=='quest')throw Error('Only POL quest instruments are supported');return keys.issue({...input,scopeId:scoped(input.scopeId),authorize:(tx,context)=>input.authorize(polProgressionStore(tx),{...context,scopeId:unscoped(context.scopeId)})});},
|
|
list:async id=>(await keys.list(id)).filter(k=>k.keyType==='quest'&&/^S\d{2,4}:POL_/.test(k.scopeId)).map(k=>({...k,scopeId:unscoped(k.scopeId)})),
|
|
consume:async input=>{
|
|
if(input.keyType!=='quest')throw Error('Only POL quest instruments are supported');
|
|
const own=(await keys.list(input.accountId)).find(k=>k.code===input.code&&k.keyType==='quest'&&/^S\d{2,4}:POL_/.test(k.scopeId));
|
|
if(!own)throw Error('POL quest instrument unavailable');
|
|
return keys.consume({...input,apply:(tx,context)=>input.apply(polProgressionStore(tx),{...context,scopeId:unscoped(context.scopeId)})});
|
|
}
|
|
});}
|
|
export function createPOLProgression(db,keys,adapters){
|
|
return createQuestProgression(polProgressionStore(db),polQuestKeys(keys),{...adapters,definitions:POL_QUEST_DEFINITIONS});
|
|
}
|

Staff