MC/website/web/equipment-template-policy.mjs

52 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Shared generation limits, not permission to issue equipment or rewrite paid snapshots.
export const EQUIPMENT_POLICY='s1-equipment-ladder-v2';
export const EQUIPMENT_SEASON=Object.freeze({id:'S1',maxMark:14});
export const EQUIPMENT_QUALITIES=Object.freeze(['common','uncommon','rare','very_rare','legendary','mythic']);
export const EQUIPMENT_QUALITY_STYLES=Object.freeze(Object.fromEntries([
['common','Common','&f','#FFFFFF'],['uncommon','Uncommon','&a','#55FF55'],
['rare','Rare','&9','#5555FF'],['very_rare','Very Rare','&5','#AA00AA'],
['legendary','Legendary','&6','#FFAA00'],['mythic','Mythic','&d','#FF55FF']
].map(([id,label,code,hex])=>[id,Object.freeze({label,code,hex})])));
export function qualityForMark(mark){
if(!Number.isInteger(mark)||mark<0||mark>14)throw new TypeError('Choose MK0–MK14.');
return EQUIPMENT_QUALITIES[Math.floor(mark/3)];
}
const slots=['helmet','chestplate','leggings','boots'];
const armour=(protection,unbreaking,extra={})=>Object.fromEntries(slots.map(slot=>[slot,{...(protection?{protection,unbreaking}:{}),...extra}]));
const profiles={
common:armour(0,0),
uncommon:armour(1,1),
rare:armour(2,2),
very_rare:armour(3,3,{mending:1}),
legendary:armour(4,3,{mending:1,thorns:3})
};
Object.assign(profiles.uncommon.boots,{feather_falling:1});
Object.assign(profiles.rare.helmet,{respiration:1,aqua_affinity:1});
Object.assign(profiles.rare.boots,{feather_falling:2,depth_strider:1});
Object.assign(profiles.very_rare.helmet,{respiration:2,aqua_affinity:1});
Object.assign(profiles.very_rare.leggings,{swift_sneak:2});
Object.assign(profiles.very_rare.boots,{feather_falling:3,depth_strider:2});
Object.assign(profiles.legendary.helmet,{respiration:3,aqua_affinity:1});
Object.assign(profiles.legendary.leggings,{swift_sneak:3});
Object.assign(profiles.legendary.boots,{feather_falling:4,depth_strider:3,soul_speed:3});
const fixed=new Set(['mending','aqua_affinity']);
export function equipmentTemplate({quality,mark,season='S1'}={}){
if(season!==EQUIPMENT_SEASON.id||!EQUIPMENT_QUALITIES.includes(quality)||!Number.isInteger(mark)||mark<0||mark>EQUIPMENT_SEASON.maxMark)throw new TypeError('Choose an S1 quality and MK0–MK14.');
if(quality==='mythic'?mark!==14:quality!==qualityForMark(mark))throw new TypeError('Quality must match the MK ladder. Mythic is an MK14 exception.');
const profile=profiles[quality==='mythic'?'legendary':quality];
// Absolute mark progression: changing a later season cap must not change old marks.
const enchantments=Object.fromEntries(slots.map(slot=>[slot,Object.fromEntries(Object.entries(profile[slot]).map(([id,base])=>[id,{min:base,max:fixed.has(id)?base:Math.round(base*(1+0.4*mark/14))+(quality==='mythic'?1:0)}]))]));
return {policy:EQUIPMENT_POLICY,season,quality,mark,enchantments};
}
export function validateEquipmentEnchantments(template,piece,enchantments){
// Recompute limits: caller-supplied template bounds are never authority.
const bounds=equipmentTemplate(template).enchantments[piece];
if(!bounds||!enchantments||typeof enchantments!=='object'||Array.isArray(enchantments)||Object.keys(enchantments).length!==Object.keys(bounds).length)throw new TypeError('Choose the complete enchantment profile for this item.');
for(const [id,{min,max}] of Object.entries(bounds))if(!Number.isInteger(enchantments[id])||enchantments[id]<min||enchantments[id]>max)throw new TypeError('Enchantment exceeds the quality and MK limits: '+id);
return {...enchantments};
}
export function maximumEquipmentEnchantments(template,piece){
const bounds=equipmentTemplate(template).enchantments[piece];
if(!bounds)throw new TypeError('Unknown equipment slot.');
return Object.fromEntries(Object.entries(bounds).map(([id,range])=>[id,range.max]));
}