MC/website/control-panel/public/item-lore-model.mjs
2026-11-27 23:43:05 +00:00

23 lines
4.4 KiB
JavaScript

export const ITEM_QUALITY=Object.freeze({common:['Common','#ffffff','#cccccc'],uncommon:['Uncommon','#55ff55','#a6ffa6'],rare:['Rare','#5555ff','#aaaaff'],very_rare:['Very Rare','#aa00aa','#d580d5'],legendary:['Legendary','#ffaa00','#ffd580'],mythic:['Mythic','#ff55ff','#ffa6ff'],relic:['Relic','#fff2b0','#e3c66a']});
const uuid=/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i;
const clean=(value,max=160)=>typeof value==='string'?value.replace(/[\x00-\x1f\x7f]/g,' ').slice(0,max):'';
export const itemWords=value=>clean(value).replace(/^minecraft:/,'').replaceAll('_',' ').replace(/\b[a-z]/g,c=>c.toUpperCase());
export function itemRoman(number){if(!Number.isInteger(number)||number<0||number>3999)return '—';if(number===0)return '0';let result='';for(const [value,text]of [[1000,'M'],[900,'CM'],[500,'D'],[400,'CD'],[100,'C'],[90,'XC'],[50,'L'],[40,'XL'],[10,'X'],[9,'IX'],[5,'V'],[4,'IV'],[1,'I']])while(number>=value){result+=text;number-=value;}return result;}
export function itemIdentity(record){const kit=['kit','kits'].includes(record?.type)||typeof record?.listingId==='string';const id=kit?record.listingId||String(record.id||'').replace(/^kit-/,''):String(record?.id||'');if(kit?!uuid.test(id):! /^[1-9][0-9]{0,14}$/.test(id))return null;return {kind:kit?'kit':'item',id};}
export function itemHref(record){const identity=itemIdentity(record);return identity?'/item.html?kind='+identity.kind+'&id='+encodeURIComponent(identity.id):null;}
export function itemPublicHref(record){const href=itemHref(record);return href?'https://www.dunerealms.com'+href:null;}
export function safeItemArt(value,fallback='/assets/imperial-atelier-v1.webp'){
try{const url=new URL(value,'https://www.dunerealms.com');if(url.protocol==='https:'&&url.hostname==='www.dunerealms.com'&&url.port===''&&url.pathname.startsWith('/assets/')&&!url.username&&!url.password)return url.href;}catch{}return fallback;
}
export function itemMaterialIcon(value){const material=clean(value,80).toLowerCase().replace(/^minecraft:/,'');return /^[a-z0-9_]+$/.test(material)?'https://www.dunerealms.com/assets/minecraft-items/'+material+'.png':null;}
export function itemLoreModel(record){
const identity=itemIdentity(record),kit=identity?.kind==='kit',quality=Object.hasOwn(ITEM_QUALITY,record?.quality)?record.quality:null;
const mark=Number.isInteger(record?.equipmentMark)&&record.equipmentMark>=0&&record.equipmentMark<=14?record.equipmentMark:null;
const material=clean(record?.material,80).toLowerCase().replace(/^minecraft:/,'');
const lore=Array.isArray(record?.lore)?record.lore.slice(0,8).map(line=>clean(typeof line==='string'?line:line?.text,160)):[];
const enchants=record?.enchantments&&typeof record.enchantments==='object'&&!Array.isArray(record.enchantments)?Object.entries(record.enchantments).filter(([id,n])=>/^(?:minecraft:)?[a-z_]{1,64}$/.test(id)&&Number.isInteger(n)&&n>0&&n<=255).slice(0,20).map(([id,n])=>({name:itemWords(id),level:itemRoman(n)})):[];
const stats=Array.isArray(record?.stats)?record.stats.slice(0,12).filter(s=>s&&typeof s.label==='string'&&typeof s.value==='string').map(s=>({name:clean(s.label,48),value:clean(s.value,64)})):[];
let price=null;if(Number.isSafeInteger(record?.priceCents)&&record.priceCents>0)price=record.priceCents/100;else if(Number.isFinite(record?.price)&&record.price>0)price=record.price;
const organisationGear=['clan','company','politics'].includes(record?.organisationGear?.kind)&&/^[A-Za-z0-9_-]{1,16}$/.test(record.organisationGear.tag||'')?{kind:record.organisationGear.kind,tag:record.organisationGear.tag}:null;
return {organisationGear,nativeMetadata:record?.nativeMetadata===true,identity,href:itemHref(record),title:clean(record?.title||record?.name||record?.displayName,96)||itemWords(material)||'Registered item',quality,qualityLabel:quality?ITEM_QUALITY[quality][0]:'Quality not registered',mark,markLabel:quality==='relic'&&record.equipmentMark==='infinite'?'MK ∞':mark===null?'MK not registered':'MK '+itemRoman(mark),season:clean(record?.edition?.season||record?.season,32),material,owner:clean(record?.owner||record?.ownerName||record?.sellerName||record?.seller,64),lore,enchants,stats,price,quantity:Number.isSafeInteger(record?.quantity)&&record.quantity>0?record.quantity:null,icon:safeItemArt(record?.image,itemMaterialIcon(material)||'https://www.dunerealms.com/assets/minecraft-items/netherite_chestplate.png'),art:safeItemArt(record?.loreArt,'https://www.dunerealms.com/assets/imperial-atelier-v1.webp'),kit,purchaseAvailable:record?.purchaseAvailable===true};
}