Add physical hub portals and shared housing entrances
This commit is contained in:
@@ -14,14 +14,15 @@ type InventoryView=Pick<HouseInventory,'inventory'|'items'> & {stackNames?:Recor
|
||||
function choices(v:InventoryView):[string,string][]{return [...v.items.map(i=>[i.id,i.name] as [string,string]),...Object.entries(v.inventory).filter(([,n])=>n>0).map(([id,n])=>[id,`${v.stackNames?.[id]??id} × ${n}`] as [string,string])];}
|
||||
|
||||
export function createHousingPanel(character:()=>Character|undefined,prefix:string,isHub:()=>boolean,onEnter:()=>void){
|
||||
const root=node('section'),open=button('Housing & keys',()=>{dialog.showModal();menu.opened();void refresh(true)});
|
||||
const root=node('section'),open=button('Housing & keys',()=>show());
|
||||
const dialog=node('dialog','','housing-dialog'),title=node('h2','Housing'),status=node('p'),body=node('div'),close=button('Close controls',()=>dialog.close());
|
||||
const toolbar=node('div','','house-toolbar');toolbar.hidden=true;toolbar.append(button('House controls',()=>show()),button('Return to hub',()=>void leave()));
|
||||
const retry=button('Retry pending action',()=>{if(pending)void action(pending,true)});retry.hidden=true;
|
||||
status.setAttribute('role','status');root.append(open);dialog.append(title,close,status,retry,body);document.body.append(dialog);
|
||||
let entranceContext=false;
|
||||
let pending:HouseRequest|undefined,state:HouseState|undefined,busy=false,loading=false,signature='',lastHouse='',selected='',latest:Character|undefined;
|
||||
const menu=registerMenu(dialog,()=>dialog.close());dialog.addEventListener('cancel',e=>{e.preventDefault();dialog.close()});
|
||||
function show(id=''){selected=id;if(!dialog.open)dialog.showModal();menu.opened();void refresh(true)}
|
||||
function show(id='',atEntrance=false){entranceContext=atEntrance;selected=id;if(!dialog.open)dialog.showModal();menu.opened();void refresh(true)}
|
||||
async function leave(){if(busy)return;if(character()?.houseId||state?.room)await action({op:'leave'});dialog.close();signature=''}
|
||||
async function action(r:Omit<HouseRequest,'requestId'>,retrying=false){
|
||||
if(busy)return;if(pending&&!retrying){status.textContent='Resolve the pending action before making another change.';return}busy=true;dialog.classList.add('housing-busy');status.textContent='Saving…';
|
||||
@@ -43,9 +44,9 @@ export function createHousingPanel(character:()=>Character|undefined,prefix:stri
|
||||
for(const key of c.items.filter(i=>i.houseId)){const row=node('p',key.name+' ');row.append(button('Destroy key',()=>confirmItemAction('Destroy '+key.name+'?','This takes three seconds. The key remains lootable until destruction completes.',()=>void action({op:'destroy-key',item:key.id}))));keys.append(row)}
|
||||
const progress=node('p');progress.dataset.keyProgress='';keys.append(progress,button('Cancel key destruction',()=>void action({op:'cancel-key'})));body.append(keys);
|
||||
if(!h){
|
||||
body.append(node('p',isHub()?'Create or enter a house after surviving your first world. A carried key grants owner rights.':'Return to a hub to enter housing. Keys can be destroyed here.'));
|
||||
body.append(node('p',isHub()?'After surviving your first world, walk to the shared housing entrance in this hub to enter a house. A carried key grants owner rights; guests need an invitation.':'Return to a hub to enter housing. Keys can be destroyed here.'));
|
||||
if(isHub()){const name=node('input');name.maxLength=80;name.placeholder='House name';body.append(label('New house name',name),button('Create house',()=>void action({op:'create',name:name.value})))}
|
||||
for(const entry of s.houses){const row=node('section');row.append(node('h3',entry.name),node('p',`${entry.state} · ${entry.role||'no access'} · ${entry.keys} valid keys`));if(isHub()&&entry.state==='active'&&entry.role)row.append(button('Enter',()=>void action({op:'enter',houseId:entry.id})));if(isHub()&&entry.creator&&entry.recoveryWorld&&entry.state!=='purged')row.append(button('Recover creator key',()=>void action({op:'recover',houseId:entry.id})));body.append(row)}return;
|
||||
for(const entry of s.houses){const row=node('section');row.append(node('h3',entry.name),node('p',`${entry.state} · ${entry.role||'no access'} · ${entry.keys} valid keys`));if(isHub()&&entranceContext&&entry.state==='active'&&entry.role)row.append(button('Enter through housing entrance',()=>void action({op:'enter',houseId:entry.id})));if(isHub()&&entry.creator&&entry.recoveryWorld&&entry.state!=='purged')row.append(button('Recover creator key',()=>void action({op:'recover',houseId:entry.id})));body.append(row)}return;
|
||||
}
|
||||
body.prepend(node('p','Private house room · click the floor to choose a position, or furniture to select it. No experience is earned here.'));
|
||||
const layout=node('div','','house-layout');layout.setAttribute('aria-label','House floor plan');
|
||||
@@ -79,5 +80,5 @@ export function createHousingPanel(character:()=>Character|undefined,prefix:stri
|
||||
}
|
||||
}
|
||||
setInterval(()=>{if(dialog.open&&!busy)void refresh();const a=character()?.destroyKey;const p=dialog.querySelector<HTMLElement>('[data-key-progress]');if(p)p.textContent=a?`Key destruction in progress · ${Math.max(0,a.finishesAt-Date.now()/1000).toFixed(1)}s remaining (server confirmed on completion)`:'No key destruction in progress.'},1000);
|
||||
return {root,toolbar,open:show,leave,destroyKey:(id:string)=>{show();void action({op:'destroy-key',item:id})},update(c:Character){latest=c;toolbar.hidden=!c.houseId;open.textContent=c.houseId?'Return to house controls':'Housing & keys';if(c.entity.state==='dead'||c.remainingSeconds<=0){dialog.close();return}if((c.houseId??'')!==lastHouse){lastHouse=c.houseId??'';signature='';if(c.houseId){onEnter();dialog.close()}if(dialog.open)void refresh(true)}}};
|
||||
return {root,toolbar,open:show,openEntrance:()=>show('',true),enter:(houseId:string,key:string)=>{show('',true);void action({op:'enter',houseId,item:key})},leave,destroyKey:(id:string)=>{show();void action({op:'destroy-key',item:id})},update(c:Character){latest=c;toolbar.hidden=!c.houseId;open.textContent=c.houseId?'Return to house controls':'Housing & keys';if(c.entity.state==='dead'||c.remainingSeconds<=0){dialog.close();return}if((c.houseId??'')!==lastHouse){lastHouse=c.houseId??'';signature='';if(c.houseId){onEnter();dialog.close()}if(dialog.open)void refresh(true)}}};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user