MC/website/web/organisation-research-policy.mjs

22 lines
2.1 KiB
JavaScript

import {refitReputationLevel} from './organisation-refit-policy.mjs';
// Specialist research is personal to one organisation. The server verifies
// membership, reputation and actual surrendered samples before recording it.
export const RESEARCH_POLICY='organisation-research-v1';
export const RESEARCH_COOLDOWN_MS=60*60*1000;
export const RESEARCH_QUALITIES=Object.freeze(['common','uncommon','rare','very_rare','legendary']);
const samples=['oak_log','cobblestone','coal','raw_copper','raw_iron','raw_gold','copper_ingot','iron_ingot','gold_ingot','lapis_lazuli','redstone','diamond','emerald','netherite_scrap','netherite_ingot'];
export function researchNode(kind,tier){
if(!['mark','quality'].includes(kind)||!Number.isInteger(tier)||tier<0||tier>(kind==='mark'?14:4))throw TypeError('Choose a registered research stage.');
const mark=kind==='mark'?tier:[0,3,6,9,12][tier],step=mark+1;
return {kind,tier,mark,quality:kind==='quality'?RESEARCH_QUALITIES[tier]:null,label:kind==='mark'?'MK '+tier:RESEARCH_QUALITIES[tier].replace('_',' '),reputationLevel:refitReputationLevel(Math.max(1,mark)),samples:[{material:samples[mark],quantity:Math.min(64,8+mark*4)}],xp:25*step,personalXP:5*step,amountMinor:1000*step,cooldownMs:RESEARCH_COOLDOWN_MS};
}
export function researchId(owner,node){
if(!['clan','company','politics','office'].includes(owner?.kind)||typeof owner.id!=='string'||!/^[a-z0-9_-]{1,64}$/i.test(owner.id))throw TypeError('Registered research owner required.');
// Office keys are short; UUIDs retain their full identity without separators.
const id='RD-'+({clan:'C',company:'B',politics:'P',office:'O'})[owner.kind]+'-'+owner.id.replaceAll('-','')+'-'+(node.kind==='mark'?'M':'Q')+node.tier;
if(id.length>64)throw TypeError('Research identity exceeds its bound.');return id;
}
export function researchUnlocked(design,claimed,owner){
if(!RESEARCH_QUALITIES.includes(design.quality)||!Number.isInteger(design.equipmentMark)||design.equipmentMark<0||design.equipmentMark>14)return false;
return claimed.has(researchId(owner,researchNode('mark',design.equipmentMark)))&&claimed.has(researchId(owner,researchNode('quality',RESEARCH_QUALITIES.indexOf(design.quality))));
}