import type { TribeListRow, TribePage } from '~/types/game' /** The player's tribe on one world: its page, every tribe of the world, and the changes. */ export function useTribe(worldId: number) { const page = ref(null) const tribes = ref(null) const error = ref(null) const busy = ref(false) const errorText = useErrorText() const v = useVillageContextOrNull() async function call(event: string, data: object = {}): Promise { busy.value = true error.value = null try { return await ws.send(event, { world_id: worldId, ...data }) } catch (e) { error.value = errorText(e) return null } finally { busy.value = false } } const load = async () => (page.value = (await call('tribe.show')) ?? page.value) const loadList = async () => (tribes.value = (await call('tribe.list')) ?? tribes.value) /** A change answers with the fresh page; the village header learns about it too. */ async function act(intent: string, data: object = {}) { const next = await call(`tribe.${intent}`, data) if (!next) return false page.value = next v?.refresh() if (tribes.value) loadList() return true } return { page, tribes, error, busy, load, loadList, act } }