import { api, el, gameURL, type World } from './portal-shared'; import { registerMenu } from './menu-focus'; import './travel.css'; type Transfer = { id: string; source: string; sourceKind: string; destination: string; route: string; status: string; acceptedWorld: string }; type Departure = { worldId: string; route: 'hub' | 'escape' | 'portal'; beforeTravel?: () => void; afterTravel?: (departed: boolean) => void }; let open = false; export async function showTravel(source?: Departure) { if (open) return; open = true; const dialog = el('dialog', '', 'travel-dialog'); const heading = el('h2', 'Travel'), description = el('p'), status = el('p'); status.setAttribute('role', 'status'); const label = el('label', 'Destination'), select = el('select'); label.append(select); const close = el('button', 'Close'), refresh = el('button', 'Refresh destinations'), go = el('button', 'Travel'); const account = el('a','Account'); account.href='/'; const footer = el('footer'); footer.append(close, refresh, account, go); dialog.append(heading, description, label, status, footer); document.body.append(dialog); let transfer: Transfer | null = null, busy = false, prepared = false; const requestId = crypto.randomUUID(); const menu = registerMenu(dialog, () => { dialog.close(); dialog.remove(); menu.dispose(); open = false; if (prepared && source) location.href = '/'; }); close.onclick = menu.close; dialog.oncancel = event => { event.preventDefault(); menu.close(); }; dialog.showModal(); menu.opened(); async function load() { go.disabled = true; try { const [pending, worlds] = await Promise.all([api('/api/travel'), api('/api/worlds')]); transfer = pending ?? transfer; if (!source && !transfer) { description.textContent='No journey is waiting to finish.'; status.textContent='Refresh your account to continue your character.'; return; } const fromHub = transfer ? transfer.sourceKind === 'hub' : source?.route === 'hub'; description.textContent = transfer ? 'Your character and belongings are saved in transit. Choose an available destination to finish your journey.' : source?.route === 'portal' ? 'Stand beside the victory portal to enter a hub. Your character, belongings and progression travel with you.' : fromHub ? 'Switch hub instances or enter a survival world. Once there, you need an escape stone or a Harbinger victory to return.' : 'Consumes one escape stone to reach a hub. Escaping does not count as surviving this world.'; const previous = select.value || transfer?.destination; select.replaceChildren(); for (const w of worlds.filter(w => w.available && !w.closed && w.living < w.capacity && ['hub', 'gathering', 'cataclysm'].includes(w.phase) && (fromHub || w.kind === 'hub') && (transfer || w.id !== source?.worldId))) { select.append(new Option(`${w.id} · ${w.kind === 'hub' ? 'Hub' : w.phase === 'cataclysm' ? 'Collapsing world' : 'Survival world'} · ${w.living}/${w.capacity}`, w.id)); } if ([...select.options].some(o => o.value === previous)) select.value = previous!; go.textContent = transfer ? 'Finish travel' : source?.route === 'escape' ? 'Use escape stone & travel' : 'Enter portal'; go.disabled = !select.options.length || (!source && !transfer); status.textContent = select.options.length ? '' : 'No destination is available. Your saved character remains safe in transit if departure already completed.'; } catch (e) { status.textContent = (e as Error).message; } } refresh.onclick = () => { if (!busy) void load(); }; go.onclick = async () => { if (busy) return; busy = true; go.disabled = refresh.disabled = close.disabled = true; select.disabled = true; try { source?.beforeTravel?.(); if (!transfer) { if (!source) throw new Error('No pending journey. Refresh your account.'); transfer = await api(`/worlds/${source.worldId}/api/travel/prepare`, { requestId, destination: select.value, route: source.route }); } prepared = true; if (transfer.status !== 'accepted') transfer = await api(`/worlds/${select.value}/api/travel/accept`, { transferId: transfer.id }); location.href = gameURL(transfer.acceptedWorld); } catch (e) { // Recover either side of a lost HTTP response from durable server state. const message = (e as Error).message; try { if (transfer) { const saved = await api(`/api/travel?id=${encodeURIComponent(transfer.id)}`); if (saved?.status === 'accepted') { location.href = gameURL(saved.acceptedWorld); return; } } await load(); if (transfer) prepared = true; } catch { /* Keep the current transfer ID available for retry. */ } status.textContent = `${message} You can retry or return to your account to resume travel.`; } finally { busy = false; source?.afterTravel?.(prepared); go.disabled = !select.options.length || (!source && !transfer); refresh.disabled = close.disabled = select.disabled = false; if(prepared && !dialog.isConnected) location.href='/'; } }; await load(); }