36 lines
5.2 KiB
JavaScript
36 lines
5.2 KiB
JavaScript
import {polDossier} from './pol-dossier-policy.mjs';
|
|
import {POL_QUEST_DEFINITIONS} from '../control-panel/public/cp-v2/cp-v2-pol-quest-catalogue.mjs';
|
|
import {createPOLActivityEvents} from './pol-activity-events.mjs';
|
|
import {currentActivityPlayer} from './quest-activity-events.mjs';
|
|
import {starterIdentity} from './starter-kit-native-design.mjs';
|
|
import {validUuid} from './game-link.mjs';
|
|
|
|
export const POL_DOSSIER_SCHEMA=`CREATE TABLE IF NOT EXISTS jarvis_pol_dossier_steps(account_id CHAR(36) NOT NULL,season_id VARCHAR(8) NOT NULL,definition_id VARCHAR(64) NOT NULL,step_no INT NOT NULL,quest_key_id CHAR(36) NOT NULL,npc_role VARCHAR(32) NOT NULL,npc_uuid CHAR(36) NOT NULL,occurred_at BIGINT NOT NULL,PRIMARY KEY(account_id,season_id,definition_id,step_no)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
|
|
const roles=d=>[d.origin,d.witness,d.origin];
|
|
const error=message=>{throw Object.assign(Error(message),{status:409});};
|
|
const first=async(db,sql,args=[]) => (await db.query(sql,args))[0]||null;
|
|
const eventId=(account,definition,quest)=>starterIdentity('pol-dossier:'+account+':'+definition+':'+quest);
|
|
|
|
export function createPOLDossiers(db,keys,{clock=Date.now}={}){
|
|
const producer=Object.freeze({type:'native-mission-dossier'}),events=createPOLActivityEvents(db,keys,{clock,authorizeProducer:async actor=>actor===producer});
|
|
async function rows(store,actor,definition,season='S01'){return store.query('SELECT * FROM jarvis_pol_dossier_steps WHERE account_id=? AND season_id=? AND definition_id=? ORDER BY step_no',[actor.id,season,definition]);}
|
|
async function presentation(actor,quest,npcRole){const d=polDossier(quest.id);if(!d)return null;const saved=await rows(db,actor,d.id),step=saved.length;if(step>=3)return {complete:true,stage:3,role:d.origin,dialogue:'All three mission-bound records have been verified.'};const role=roles(d)[step];return {complete:false,stage:step,role,dialogue:npcRole===role?(step===0?d.intro:step===1?d.clue:d.question):'Visit the next named contact in this mission.',...(step===2&&npcRole===role?{answers:d.answers}:{}),...(saved.length&&step===1?{previous:d.intro}:{})};}
|
|
async function advance(actor,input,quest){
|
|
const d=polDossier(quest.id);
|
|
if(!d||!validUuid(input.npcUuid)||!Number.isInteger(input.stage)||input.stage<0||input.stage>2||roles(d)[input.stage]!==input.npcRole||input.stage===2&&!Number.isInteger(input.answer)||input.stage!==2&&input.answer!==undefined)error('Use the current mission dialogue at its named contact.');
|
|
if(input.stage===2&&input.answer!==d.correct)error('That answer does not match the verified fictional clue. Review the witness record.');
|
|
const now=clock();const proof=await db.transaction(async tx=>{
|
|
if(!await currentActivityPlayer(tx,actor.id,input.uuid.toLowerCase(),{lock:true}))error('Current linked player required.');
|
|
const definition=POL_QUEST_DEFINITIONS.find(q=>q.id===d.id);if(['atreides','harkonnen','corrino','ordos','fremen'].includes(definition.branch)){const current=await first(tx,'SELECT faction FROM site_character_choices WHERE user_id=? AND minecraft_uuid=? FOR UPDATE',[actor.id,input.uuid.toLowerCase()]);if(current?.faction!==definition.branch)error('Current House affiliation required for this mission.');}
|
|
const season=await first(tx,"SELECT season_id FROM jarvis_pol_progression_seasons WHERE state='active' AND starts_at<=? AND ends_at>? FOR UPDATE",[now,now]);if(season?.season_id!=='S01')error('A current POL season is required.');
|
|
const state=await first(tx,"SELECT s.key_id,s.state,k.revoked_at,k.used_at FROM jarvis_pol_quest_states s JOIN organisation_keys k ON k.id=s.key_id AND k.account_id=s.account_id AND k.key_type='quest' WHERE s.account_id=? AND s.season_id=? AND s.definition_id=? FOR UPDATE",[actor.id,season.season_id,d.id]);if(!state||state.revoked_at!==null||state.used_at!==null||!['active','completed'].includes(state.state))error('Accept the selected mission before collecting its clues.');
|
|
const saved=await rows(tx,actor,d.id);if(saved.some((r,i)=>Number(r.step_no)!==i||r.quest_key_id!==state.key_id||r.npc_role!==roles(d)[i]))error('Mission evidence requires review.');
|
|
if(input.stage>saved.length)error('Complete the preceding mission contact first.');
|
|
if(input.stage===saved.length){await tx.query('INSERT INTO jarvis_pol_dossier_steps(account_id,season_id,definition_id,step_no,quest_key_id,npc_role,npc_uuid,occurred_at) VALUES(?,?,?,?,?,?,?,?)',[actor.id,season.season_id,d.id,input.stage,state.key_id,input.npcRole,input.npcUuid.toLowerCase(),now]);saved.push({step_no:input.stage,occurred_at:now});}
|
|
return {complete:saved.length===3,occurredAt:Number(saved.at(-1).occurred_at),questKey:state.key_id};
|
|
});
|
|
if(proof.complete){const definition=POL_QUEST_DEFINITIONS.find(q=>q.id===d.id),id=eventId(actor.id,d.id,proof.questKey);await events.record(producer,{eventId:id,accountId:actor.id,minecraftUuid:input.uuid.toLowerCase(),seasonId:'S01',metric:definition.metric,quantity:1,occurredAt:proof.occurredAt,reference:'dossier:'+id});}
|
|
return {ok:true,complete:proof.complete,dialogue:input.stage===0?d.intro:input.stage===1?d.clue:'That is the verified conclusion. The dossier is complete.',nextRole:proof.complete?null:roles(d)[input.stage+1]};
|
|
}
|
|
return Object.freeze({presentation,advance});
|
|
}
|

Staff