import type { StartDirection, WorldSummary } from '~/types/game' export function useWorld() { const worlds = useState('worlds', () => null) const error = ref(null) const busy = ref(false) const errorText = useErrorText() async function run(fn: () => Promise) { busy.value = true error.value = null try { return await fn() } catch (e) { error.value = errorText(e) } finally { busy.value = false } } const refresh = () => run(async () => (worlds.value = await ws.send('world.index'))) /** Joins (or re-enters) a world, starting in the given quarter of the map, and returns the player's village there. */ const join = (worldId: number, direction: StartDirection = 'random') => run(async () => (await ws.send<{ village_id: number }>('world.join', { world_id: worldId, direction })).village_id) return { worlds, error, busy, refresh, join } }