diff --git a/app/composables/useCurrentWorld.ts b/app/composables/useCurrentWorld.ts index bb979d8..6bea880 100644 --- a/app/composables/useCurrentWorld.ts +++ b/app/composables/useCurrentWorld.ts @@ -32,5 +32,16 @@ export function useCurrentWorld() { } } - return { current, enter } + /** Drop a remembered village that no longer exists (a world reset, a conquest). */ + function forget(villageId: number) { + if (current.value?.villageId !== villageId) return + current.value = { ...current.value, villageId: null } + try { + localStorage.setItem(KEY, JSON.stringify(current.value)) + } catch { + // nothing remembered, nothing to drop + } + } + + return { current, enter, forget } } diff --git a/app/composables/useVillage.ts b/app/composables/useVillage.ts index fdd7865..83198d7 100644 --- a/app/composables/useVillage.ts +++ b/app/composables/useVillage.ts @@ -1,4 +1,4 @@ -import type { BuildingType, Cost, MarchPlan, MovementType, Resource, Resources, TravelMode, Units, UnitType, VillageState, VillageUpdated } from '~/types/game' +import type { BuildingType, Cost, GameError, MarchPlan, MovementType, Resource, Resources, TravelMode, Units, UnitType, VillageState, VillageUpdated } from '~/types/game' import type { InjectionKey } from 'vue' const RESOURCES: Resource[] = ['wood', 'clay', 'iron', 'food'] @@ -15,6 +15,7 @@ export function useVillage(villageId: number) { const unreadReports = useUnreadReports() // Projections and countdowns run on the server's clock. const { now, sync } = useServerClock() + const { forget } = useCurrentWorld() async function run(event: string, data: object = {}) { busy.value = true @@ -26,6 +27,12 @@ export function useVillage(villageId: number) { unreadReports.value = state.unread_reports return true } catch (e) { + // The village is gone (the world was reset): forget it and pick a world again. + if (event === 'village.show' && (e as Partial)?.code === 'not_found') { + forget(villageId) + await navigateTo('/worlds') + return false + } error.value = errorText(e) return false } finally {