MC/website/web/kits.js

8 lines
5.4 KiB
JavaScript

import {kitDirectory,kitAvailable} from './kit-directory.js';
function enchantmentDetails(quality){
const profiles={common:['Standard vanilla kit: no enchantments'],uncommon:['All armour: Protection I, Unbreaking I','Boots: Feather Falling I'],rare:['All armour: Protection II, Unbreaking II','Helmet: Respiration I, Aqua Affinity I','Boots: Feather Falling II, Depth Strider I'],very_rare:['All armour: Protection III, Unbreaking III, Mending I','Helmet: Respiration II, Aqua Affinity I','Leggings: Swift Sneak II','Boots: Feather Falling III, Depth Strider II'],legendary:['All armour: Protection IV, Unbreaking III, Mending I, Thorns III','Helmet: Respiration III, Aqua Affinity I','Leggings: Swift Sneak III','Boots: Feather Falling IV, Depth Strider III, Soul Speed III']};
const box=el('section');box.className='kit-enchantment-profile';box.append(el('h3','Enchantment profile'));const list=el('ul');for(const line of profiles[quality]||[])list.append(el('li',line));box.append(list,el('p','Fixed by kit quality. Preview: game-side activation is pending.'));return box;
}
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n},categories=[['all','All Kits'],['personal_sale','Player Kits'],['clan','Clan Kits'],['official_sale','Official Kits'],['faction_sale','Faction Kits'],['server_sale','Server Kits']],art={personal_sale:'clan-atelier-v1.webp',official_sale:'imperial-atelier-v1.webp',faction_sale:'clan-atelier-v1.webp',server_sale:'imperial-atelier-v1.webp'};let selected='all',rows=[],page=1;const quality=document.getElementById('kit-quality'),availability=document.getElementById('kit-availability'),pages=document.getElementById('kit-pages');const menu=document.getElementById('kit-categories'),status=document.getElementById('kit-status'),results=document.getElementById('kit-listings'),search=document.getElementById('kit-search'),sort=document.getElementById('kit-sort');for(const[key,label]of categories){const b=el('button',label);b.type='button';b.dataset.kind=key;b.setAttribute('aria-pressed',String(key===selected));b.onclick=()=>{selected=key;page=1;render()};menu.append(b)}
function render(){for(const b of menu.children)b.setAttribute('aria-pressed',String(b.dataset.kind===selected));const model=kitDirectory(rows,{query:search.value,category:selected,quality:quality.value,availability:availability.value,sort:sort.value,page});page=model.page;const filtered=model.rows;pages.replaceChildren();if(model.pages>1){for(const[label,step]of [['Previous',-1],['Next',1]]){const button=el('button',label);button.type='button';button.disabled=step<0?page===1:page===model.pages;button.onclick=()=>{page+=step;render();};pages.append(button);}pages.append(el('span','Page '+page+' of '+model.pages));}results.replaceChildren();status.textContent=model.total+' published '+(model.total===1?'design':'designs');if(!filtered.length){const empty=el('article');empty.className='kit-empty';empty.append(el('h2',rows.length?'No matching designs':'No published designs yet'),el('p',rows.length?'Try another name, rarity or availability filter.':'This category is ready for its first commission.'));results.append(empty);return;}for(const r of filtered){const card=el('article');card.className='kit-store-card kit-collectible-listing';card.dataset.quality=r.quality;const image=el('img');image.src='assets/'+art[r.commerceKind];image.alt='Illustrated stillsuit atelier';image.loading='lazy';const visual=el('div');visual.className='kit-store-art';const swatch=el('span');swatch.className='kit-store-colour';swatch.textContent=/^#[A-Fa-f0-9]{6}$/.test(r.color||'')?r.color.toUpperCase():'Colour not recorded';if(/^#[A-Fa-f0-9]{6}$/.test(r.color||''))swatch.style.setProperty('--kit-swatch',r.color);visual.append(image,swatch);card.append(visual,el('small',categories.find(c=>c[0]===(r.shopType==='clan'?'clan':r.commerceKind))?.[1]+' · '+r.quality.replaceAll('_',' ')),el('h2',r.name),el('p','Designed by '+r.sellerName));const lore=el('div');for(const line of r.lore)lore.append(el('p',line));card.append(lore,el('strong',(r.priceCents/100).toFixed(2)+' DM'),el('p',r.completedSales+' completed sales'));const buy=el('a','Buy Kit →');buy.className='kit-store-link';buy.href='https://cp.dunerealms.com/?kit-listing='+encodeURIComponent(r.listingId)+'#realm/kits-exchange';const details=el('details'),summary=el('summary','Kit details');details.append(summary,el('p','Four-piece stillsuit · '+r.quality.replaceAll('_',' ')),el('p','Colour: '+r.color),el('p','Managed by '+(r.ownerName||r.sellerName)),enchantmentDetails(r.quality));card.append(details);if(!kitAvailable(r)){const note=el('p','Published design · in-game delivery opens later.');note.className='kit-delivery-note';card.append(note);}else card.append(buy);results.append(card)}}search.oninput=sort.onchange=quality.onchange=availability.onchange=()=>{page=1;render();};document.getElementById('kit-clear').onclick=()=>{search.value=quality.value=availability.value='';sort.value='sales';selected='all';page=1;render();};async function load(){status.textContent='Loading published kit designs…';try{const response=await fetch('/api/kits/catalogue',{cache:'no-store'}),data=await response.json();if(!response.ok||!Array.isArray(data.listings))throw Error(data.message||'The kit catalogue is temporarily unavailable.');rows=data.listings;render()}catch(e){status.textContent=e.message;results.replaceChildren();const retry=el('button','Retry catalogue');retry.type='button';retry.onclick=load;results.append(retry);}}void load();