Files
aeternalis/client/src/character-panel.ts
T

42 lines
6.0 KiB
TypeScript
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.
import type { Character } from './generated';
import { equipmentSlots, itemDetails, modifiersView, node, numberText, primaryStats, statLabel, statsBreakdown } from './character-stats';
type Send = (op:string,target?:string,x?:number,z?:number,slot?:string)=>void;
const root=node('section','','character-panel'),resources=node('p','','character-resources'),nav=node('div','','character-sections'),body=node('div');
root.append(resources,nav,body);
let selected='bag',latest:Character,sendIntent:Send,signature='';
const rings=new Map<string,string>();
for(const [key,label] of [['character','Character'],['equipment','Equipment'],['bag','Bag']]){const b=node('button',label);b.type='button';b.dataset.section=key;b.onclick=()=>{selected=key;signature='';render()};nav.append(b);}
function active(){return latest.entity.state==='alive'&&latest.remainingSeconds>0;}
function button(label:string,key:string,fn:()=>void,disabled=false){const b=node('button',label);b.type='button';b.dataset.focusKey=key;b.disabled=disabled;b.onclick=fn;return b;}
function request(op:string,target:string,slot=''){sendIntent(op,target,0,0,slot);}
function render(){
for(const b of nav.querySelectorAll('button'))b.setAttribute('aria-pressed',String(b.dataset.section===selected));
const next=JSON.stringify([selected,latest.entity.state,latest.remainingSeconds>0,latest.statPoints,latest.pendingVitalChoices,latest.allocatedStats,latest.stats,latest.items,latest.equipment,latest.inventory,latest.craftedStacks,latest.carryWeight,latest.gathering,latest.gatheringEffects,latest.consumables]);
if(next===signature)return;signature=next;
const focus=root.contains(document.activeElement)?(document.activeElement as HTMLElement).dataset.focusKey:undefined;
const opened=new Set(Array.from(body.querySelectorAll<HTMLDetailsElement>('details[open]')).map(d=>d.dataset.key));body.replaceChildren();
const c=latest;
if(selected==='character'){
body.append(node('p',`${c.statPoints??0} stat points available`,'stat-bank'),node('p','Each level adds 10 stat points, 5 stamina, and a choice of 5 maximum health or mana.','progression-note'));
if(c.pendingVitalChoices>0){const choices=node('div','','vital-choice');choices.append(node('p',`${c.pendingVitalChoices} level reward choice(s) remaining`),button('+5 health','choose-health',()=>request('choose-vital','health'),!active()),button('+5 mana','choose-mana',()=>request('choose-vital','mana'),!active()));body.append(choices);}
for(const key of primaryStats){const row=node('div','','allocated-stat'),label=node('div');label.append(node('strong',statLabel(key)),node('small',`${c.allocatedStats?.[key]??0} points allocated`,'progression-note'));const add=button('+1',`allocate-${key}`,()=>request('allocate-stat',key),!active()||!(c.statPoints>0));add.setAttribute('aria-label',`Allocate one point to ${statLabel(key)}`);row.append(label,node('b',numberText(c.stats?.[key]?.final)),add);body.append(row);}
const details=node('details','','stat-inspection');details.dataset.key='stats';details.append(node('summary','Inspect all stats'),statsBreakdown(c));body.append(details);
} else if(selected==='equipment'){
const grid=node('div','','equipment-grid');
for(const slot of equipmentSlots){const item=c.items?.find(i=>i.id===c.equipment?.[slot]),card=node('article','','equipment-slot');card.append(node('small',statLabel(slot)),node('strong',item?.name??'Empty'));if(item)card.append(modifiersView(item.modifiers),button('Unequip',`unequip-${slot}`,()=>request('unequip',slot),!active()));grid.append(card);}body.append(grid);
} else {
body.append(node('p',`Carry weight ${numberText(c.carryWeight)} / ${numberText(c.stats?.carryCapacity?.final)} (advisory)`,'progression-note'));
const held=new Set(Object.values(c.equipment??{})),grid=node('div','','gear-list');
for(const item of c.items??[])if(!held.has(item.id)){
const card=itemDetails(item,c),actions=node('div','','gear-actions');let slot=item.slot;
if(slot==='ring'){const label=node('label','Ring slot'),select=node('select');select.dataset.focusKey=`ring-${item.id}`;select.setAttribute('aria-label',`Ring slot for ${item.name}`);for(let n=1;n<=8;n++)select.append(new Option(`Ring ${n}${c.equipment?.[`ring${n}`]?' · Occupied':' · Empty'}`,`ring${n}`));select.value=rings.get(item.id)??equipmentSlots.find(s=>s.startsWith('ring')&&!c.equipment?.[s])??'ring1';slot=select.value;select.onchange=()=>{slot=select.value;rings.set(item.id,slot)};label.append(select);actions.append(label);}
actions.append(button('Equip',`equip-${item.id}`,()=>request('equip',item.id,slot),!active()||!item.equippable),button('Drop',`drop-${item.id}`,()=>request('drop',item.id),!active()));card.append(actions);grid.append(card);
}
for(const [key,count] of Object.entries(c.inventory??{}))if(count>0){const card=node('article','','stack-card');card.append(node('span',`${c.stackNames?.[key]??statLabel(key)} × ${count}`),button('Drop one',`drop-stack-${key}`,()=>request('drop',key),!active()));const consumable=c.consumables?.find(v=>v.id===key);if(consumable)card.append(node('p',`${consumable.description} · ${consumable.seconds}s`),modifiersView(consumable.modifiers),button('Use',`consume-${key}`,()=>request('consume',key),!active()));for(const batch of c.craftedStacks??[])if(batch.item===key)card.append(node('small',`${batch.quantity} crafted by ${batch.provenance.crafterName} in ${batch.provenance.worldId}`));grid.append(card);}
if(!grid.children.length)grid.append(node('p','Your bag is empty.'));body.append(grid);
}
for(const d of body.querySelectorAll<HTMLDetailsElement>('details'))d.open=opened.has(d.dataset.key);
if(focus)Array.from(body.querySelectorAll<HTMLElement>('[data-focus-key]')).find(e=>e.dataset.focusKey===focus)?.focus({preventScroll:true});
}
export function characterPanel(c:Character,send:Send){latest=c;sendIntent=send;resources.textContent=`Health ${numberText(c.entity.hp)} / ${numberText(c.entity.maxHp)} · Mana ${numberText(c.mana)} / ${numberText(c.stats?.manaPoints?.final)} · Stamina ${numberText(c.stamina)} / ${numberText(c.stats?.stamina?.final)}`;render();return root;}