// LifeTown Online โ UI helpers shared across modules.
import { bus } from '../state';
import { audio } from '../audio';
export function $(sel: string): HTMLElement { return document.querySelector(sel) as HTMLElement; }
export function $$(sel: string): HTMLElement[] { return [...document.querySelectorAll(sel)] as any; }
export function el(tag: string, cls?: string, html?: string): HTMLElement {
const e = document.createElement(tag);
if (cls) e.className = cls;
if (html != null) e.innerHTML = html;
return e;
}
let toastN = 0;
export function toast(title: string, text = '', icon = 'โจ', kind: '' | 'gold' | 'coral' = '', onClick?: () => void) {
const box = $('#toasts');
const t = el('div', `toast ${kind}`);
t.innerHTML = `${icon}
`;
if (onClick) t.addEventListener('click', () => { onClick(); t.remove(); });
box.appendChild(t);
toastN++;
setTimeout(() => { t.remove(); toastN--; }, 5200);
while (box.children.length > 4) box.firstChild!.remove();
}
bus.on('srv:notify', (m: any) => {
toast(m.title || '', m.text || '', m.icon || 'โจ', m.title === 'Promotion!' ? 'gold' : '',
m.action?.type === 'visit' ? () => netVisit(m.action.home) : undefined);
});
bus.on('srv:achievement', (m: any) => {
audio.achievement();
toast(`๐ Achievement โ ${m.name}`, m.desc, '๐', 'gold');
});
import { net } from '../net';
function netVisit(home: string) { net.send({ t: 'zone', id: home }); }
export function openDialog(title: string, buildBody: (body: HTMLElement, close: () => void) => void) {
const modal = $('#dialog');
$('#dialog-title').textContent = title;
const body = $('#dialog-body');
body.innerHTML = '';
modal.classList.remove('hidden');
const close = () => modal.classList.add('hidden');
($('#dialog-close') as HTMLElement).onclick = close;
buildBody(body, close);
audio.click();
}
export function closeDialog() { $('#dialog').classList.add('hidden'); }
export function fmtMoney(n: number): string {
return n >= 1e6 ? `${(n / 1e6).toFixed(2)}M` : n.toLocaleString('en-US');
}
/** emoji icon per furniture category */
export const CAT_EMOJI: Record = {
bed: '๐๏ธ', sofa: '๐๏ธ', table: '๐ชต', chair: '๐ช', tv: '๐บ', pc: '๐ฅ๏ธ',
fridge: '๐ง', stove: '๐ณ', shower: '๐ฟ', toilet: '๐ฝ', sink: '๐ฐ',
wardrobe: '๐๏ธ', shelf: '๐', plant: '๐ชด', decor: '๐ฏ๏ธ', light: '๐ก',
rug: '๐งถ', paint: '๐ผ๏ธ', elec: '๐ฎ',
};