2026-10-29 00:57:00 +00:00
|
|
|
import type { StartDirection, WorldSummary } from '~/types/game'
|
2026-10-28 16:57:44 +00:00
|
|
|
|
|
|
|
|
export function useWorld() {
|
|
|
|
|
const worlds = useState<WorldSummary[] | null>('worlds', () => null)
|
|
|
|
|
const error = ref<string | null>(null)
|
|
|
|
|
const busy = ref(false)
|
|
|
|
|
const errorText = useErrorText()
|
|
|
|
|
|
|
|
|
|
async function run<T>(fn: () => Promise<T>) {
|
|
|
|
|
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<WorldSummary[]>('world.index')))
|
|
|
|
|
|
2026-10-29 00:57:00 +00:00
|
|
|
/** 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)
|
2026-10-28 16:57:44 +00:00
|
|
|
|
|
|
|
|
return { worlds, error, busy, refresh, join }
|
|
|
|
|
}
|