// 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}
${title}

${text}

`; 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: '๐ŸŽฎ', };