MC/website/node/house-prologue.mjs
2026-11-27 23:43:05 +00:00

24 lines
3.9 KiB
JavaScript

import {firstLoginState} from './first-login.mjs';
import {verifiedPersonalActivities} from './personal-activity-events.mjs';
import {verifiedStarterKitEvidence} from './starter-kit-events.mjs';
export const HOUSE_PROLOGUE_SCHEMA="CREATE TABLE IF NOT EXISTS jarvis_house_prologue_policy(id INT PRIMARY KEY,activated_at BIGINT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
export const HOUSE_MENTOR_CONTACTS=Object.freeze({atreides:'thufir_hawat',harkonnen:'piter_de_vries',corrino:'hasimir_fenring',ordos:'roma_atani',fremen:'stilgar'});
const HOUSE_INTRODUCTIONS=Object.freeze({atreides:'A House is more than a banner. Let your loyalty be earned through your work on Caladan.',harkonnen:'Industry rewards precision. Learn the tools of Giedi Prime before you seek greater influence.',corrino:'A citizen of Kaitain enters the Imperial register with duty and a name of their own.',ordos:'Every enterprise begins with a clear account. Prepare your equipment and learn the laws of Sigmadraken IV.',fremen:'The desert remembers every promise. Prepare your water and equipment before the journey to Arrakis.'});
export const HOUSE_PROLOGUE_IDS=Object.freeze(['S1-A-PROLOGUE-MENTAT','S1-A-PROLOGUE-READY']);
export async function initializeHousePrologue(db,{clock=Date.now}={}){await db.query(HOUSE_PROLOGUE_SCHEMA);await db.query('INSERT INTO jarvis_house_prologue_policy(id,activated_at) VALUES(1,?) ON DUPLICATE KEY UPDATE id=VALUES(id)',[clock()]);db.housePrologueEnabled=true;}
export async function housePrologueState(db,keys,actor,uuid,{clock=Date.now}={}){
if(!db.housePrologueEnabled)return{required:false,ready:true,legacy:true};
const policy=(await db.query('SELECT activated_at FROM jarvis_house_prologue_policy WHERE id=1'))[0];if(!policy)throw Error('House Prologue policy unavailable');
const rows=await db.query('SELECT l.minecraft_uuid,l.linked_at,i.first_linked_at,a.display_name,p.bio,c.faction FROM site_game_links l JOIN site_accounts a ON a.id=l.user_id LEFT JOIN site_game_identities i ON i.minecraft_uuid=l.minecraft_uuid LEFT JOIN site_profiles p ON p.user_id=a.id LEFT JOIN site_character_choices c ON c.user_id=a.id AND c.minecraft_uuid=l.minecraft_uuid WHERE l.user_id=? AND l.minecraft_uuid=?',[actor.id,uuid.toLowerCase()]);
if(rows.length!==1)return{required:true,ready:false,linked:false};const r=rows[0],mentor=HOUSE_MENTOR_CONTACTS[r.faction];
const oldLink=[r.linked_at,r.first_linked_at].some(v=>v!=null&&Number(v)<Number(policy.activated_at));
const oldTravel=(await db.query("SELECT c.key_id FROM jarvis_quest_claims c JOIN jarvis_quest_states s ON s.key_id=c.key_id WHERE c.account_id=? AND s.definition_id='S1-A-04' AND c.claimed_at<? LIMIT 1",[actor.id,Number(policy.activated_at)])).length===1;
if(oldLink||oldTravel)return{required:false,ready:true,legacy:true,linked:true,mentor};
const now=clock(),season=(await db.query("SELECT season_id,starts_at,ends_at FROM jarvis_progression_seasons WHERE state='active' AND starts_at<=? AND ends_at>?",[now,now]))[0];
if(!season)return{required:true,ready:false,linked:true,mentor};
const firstLoginStore=typeof db.prepare==="function"?db:{prepare:sql=>({bind:(...args)=>({first:async()=>(await db.query(sql,args))[0]||null})})};const onboarding=!(await firstLoginState(firstLoginStore,actor)),profile=!!r.display_name?.trim()&&String(r.bio||'').trim().length>=10;
const proof=mentor&&await verifiedPersonalActivities(db,keys,{accountId:actor.id,minecraftUuid:uuid.toLowerCase(),seasonId:season.season_id,startsAt:Number(season.starts_at),endsAt:Number(season.ends_at),now,metric:'house-prologue-mentat'});
const introduced=!!mentor&&!!proof?.houseMentorIntroductions?.some(p=>p.role===mentor);
const kit=!!await verifiedStarterKitEvidence(db,actor.id,season.season_id,{minecraftUuid:uuid});
return{required:true,linked:true,mentor:mentor||null,house:r.faction||null,dialogue:HOUSE_INTRODUCTIONS[r.faction]||null,onboarding,profile,introduced,kit,ready:!!mentor&&onboarding&&profile&&introduced&&kit};
}