49 lines
4.4 KiB
TypeScript
49 lines
4.4 KiB
TypeScript
import { api, el, type RecordCharacter } from './portal-shared';
|
||
import { statLabel } from './character-stats';
|
||
|
||
type Catalog={items:{id:string;name:string}[];titles:{id:string;name:string}[]};
|
||
export function playerContent(record:RecordCharacter,enabled:boolean,onQueued:()=>void){
|
||
const root=el('section','','player-content');root.append(el('h3','Manage items and titles'));
|
||
if(!enabled){root.append(el('p','A living character or survivor with an available world owner is required.','muted'));return root;}
|
||
const form=el('form'),operation=el('select'),choice=el('select'),quantity=el('input'),reason=el('textarea'),message=el('p','','error'),submit=el('button','Queue player change','button');
|
||
message.setAttribute('role','alert');submit.type='submit';submit.disabled=true;quantity.type='number';quantity.min='1';quantity.max='100';quantity.step='1';quantity.value='1';quantity.required=true;reason.required=true;reason.minLength=5;reason.maxLength=240;reason.rows=2;
|
||
for(const [value,label] of [['grant-item','Add item'],['remove-item','Remove item'],['grant-title','Add / restore title'],['revoke-title','Remove title']])operation.append(new Option(label,value));
|
||
function field(label:string,input:HTMLElement){const wrap=el('label',label);wrap.append(input);return wrap;}
|
||
const qtyLabel=field('Quantity',quantity),notice=el('p','','muted');
|
||
form.append(field('Player change',operation),field('Item or title',choice),qtyLabel,notice,field('Reason for player change',reason),message,submit);root.append(form);
|
||
let catalog:Catalog,busy=false,requestID=crypto.randomUUID(),lastBody='';
|
||
function updateQuantity(){const isItem=operation.value.endsWith('item'),instance=operation.value==='remove-item'&&record.character.view.items?.some(i=>i.id===choice.value);qtyLabel.hidden=!isItem;quantity.required=isItem;quantity.disabled=!!instance;if(instance)quantity.value='1';}
|
||
function options(){
|
||
if(!catalog)return;
|
||
const v=record.character.view;choice.replaceChildren();
|
||
switch(operation.value){
|
||
case 'grant-item':
|
||
for(const i of catalog.items)choice.append(new Option(`${i.name} (${i.id})`,i.id));
|
||
for(const id of ['axe','flint_and_steel','fishing_pole','wood','ore','herb','bandage'])choice.append(new Option(statLabel(id),id));
|
||
notice.textContent='Items go into the bag. Equipment requirements still apply when equipping.';break;
|
||
case 'remove-item':
|
||
for(const i of v.items??[]){const slot=Object.entries(v.equipment??{}).find(([,id])=>id===i.id)?.[0];choice.append(new Option(`${i.name}${slot?` [${statLabel(slot)}]`:''} · ${i.id}`,i.id));}
|
||
for(const [id,n] of Object.entries(v.inventory??{}))if(n>0)choice.append(new Option(`${statLabel(id)} · ${n} held`,id));
|
||
notice.textContent='Removes the selected instance or stack quantity. Equipped items are unequipped automatically.';break;
|
||
case 'grant-title':
|
||
for(const t of catalog.titles)choice.append(new Option(`${t.name} (${t.id})`,t.id));
|
||
notice.textContent='Bypasses achievement requirements. Restores revoked titles with their original effects. First claims held by another player remain reserved.';break;
|
||
case 'revoke-title':
|
||
for(const t of v.titles??[])choice.append(new Option(`${t.name} (${t.id})`,t.id));
|
||
notice.textContent='Admin exception to permanent titles: removes the account’s active title and effects, and blocks automatic re-awarding until an admin restores it.';break;
|
||
}
|
||
choice.required=true;choice.disabled=!choice.options.length;submit.disabled=!choice.options.length||busy;updateQuantity();
|
||
}
|
||
operation.onchange=options;choice.onchange=updateQuantity;
|
||
form.onsubmit=async event=>{
|
||
event.preventDefault();if(busy||!choice.value)return;
|
||
const value=operation.value.endsWith('item')?`${choice.value}:${quantity.value}`:choice.value;
|
||
const body={worldId:record.worldId,target:record.id,op:operation.value,value,reason:reason.value.trim()};
|
||
const serialized=JSON.stringify(body);if(lastBody&&lastBody!==serialized)requestID=crypto.randomUUID();lastBody=serialized;
|
||
busy=true;submit.disabled=true;message.textContent='';
|
||
try{await api('/api/control/commands',{id:requestID,...body});onQueued();}catch(e){message.textContent=(e as Error).message;}finally{busy=false;submit.disabled=!choice.options.length;}
|
||
};
|
||
void api<{catalog:Catalog}>('/api/control/catalog').then(data=>{catalog=data.catalog;options()}).catch(e=>{message.textContent=e.message});
|
||
return root;
|
||
}
|