game-village-politics-frontend/app/components/game/VillageFrame.vue

151 lines
5.9 KiB
Vue

<script setup lang="ts">
const props = defineProps<{ villageId: number }>()
// One village state for every screen in the world: the village header, the village's
// own screens, the map and the reports all read it through useVillageContext().
const v = useVillage(props.villageId)
provideVillage(v)
v.refresh()
const { enter } = useCurrentWorld()
watch(v.village, (state) => {
if (state) enter(state.world_id, state.id)
})
// The quest book opens by itself when a quest is newly complete, like Die Stämme's.
// What the player has already seen is kept per world, so a reload doesn't reopen it.
const questBook = useQuestBookOpen()
watch(
() => v.village.value && { world: v.village.value.world_id, claimable: v.village.value.quests.claimable },
(now) => {
if (!now) return
const key = `vp.quests.${now.world}`
let seen = 0
try {
seen = Number(localStorage.getItem(key) ?? 0)
localStorage.setItem(key, String(now.claimable))
} catch {
return
}
if (now.claimable > seen) questBook.value = true
},
)
// On the village's own screens the village fills the screen and everything else docks
// over it; on the map and the reports the header sits above a framed page as before.
const route = useRoute()
const base = computed(() => `/village/${props.villageId}`)
const stage = computed(() => route.path.startsWith(base.value))
const overview = computed(() => route.path === base.value || route.path === `${base.value}/`)
const { selected, plot, clear } = useVillageSelection()
clear()
// Picking something in the village opens its card, which lives on the overview.
watch([selected, plot], ([b, p]) => {
if ((b || p) && stage.value && !overview.value) navigateTo(base.value)
})
const { t } = useI18n()
const screenTitle = computed(() => {
const s = VILLAGE_SCREENS.find((s) => s.path && route.path.startsWith(`${base.value}${s.path}`))
return s ? t(`village.screens.${s.name}`) : ''
})
const picked = computed(() => !!(selected.value || plot.value))
// One key per screen of the world, as the menu and quick bar titles show.
useShortcuts(() => {
const base = `/village/${props.villageId}`
const worldId = v.village.value?.world_id
return {
...Object.fromEntries(VILLAGE_SCREENS.map((s) => [s.name, `${base}${s.path}`])),
...(worldId ? { map: `/map/${worldId}`, reports: `/reports/${worldId}` } : {}),
}
})
</script>
<!-- The centred column: village header, thin notices, then the framed content. -->
<template>
<!-- The village's own screens: the village fills the screen, bars and windows dock over it. -->
<div v-if="stage">
<!-- One column's width wider than the screen, starting off its left edge: the village
centres in the part the right column leaves free, and the land still runs under it. -->
<div class="fixed inset-y-0 right-0 left-0 z-0 lg:-left-60">
<VillageDiorama
v-if="v.village.value"
v-model:selected="selected"
v-model:plot="plot"
fill
:village="v.village.value"
:seconds-until="v.secondsUntil"
/>
</div>
<div class="pointer-events-none fixed inset-0 z-10 flex flex-col">
<div class="pointer-events-auto shrink-0">
<GameMenuBar in-world wide />
<div class="space-y-1 px-1 pt-1 sm:px-2">
<GameVillageBar v-if="v.village.value && v.resources.value" :village="v.village.value" :resources="v.resources.value" />
<GameProtectionBar
v-if="v.village.value?.rally.protected_until"
:until="v.village.value.rally.protected_until"
:village-id="villageId"
:seconds-until="v.secondsUntil"
/>
<VillageFoodWarning
v-if="v.village.value"
:starvation="v.village.value.starvation"
:levy="v.village.value.levy"
:seconds-until="v.secondsUntil"
/>
<p v-if="v.error.value" class="alert" role="alert">{{ v.error.value }}</p>
</div>
</div>
<div class="flex min-h-0 flex-1 items-start gap-2 p-1 pb-14 sm:p-2 md:pb-2">
<!-- Left: the open screen in a window, or on the overview the card of what is picked. -->
<div v-if="overview" v-show="picked" class="pointer-events-auto max-h-full w-full overflow-y-auto sm:w-80">
<slot />
</div>
<UiWindow v-else class="w-full max-w-[66rem]" :title="screenTitle" :close-to="base">
<slot />
</UiWindow>
<!-- Right: the boxes with the numbers that matter at a glance. -->
<div
v-if="v.village.value"
class="pointer-events-auto ml-auto max-h-full w-60 shrink-0 overflow-y-auto"
:class="overview && !picked ? 'max-lg:w-full max-lg:self-end max-lg:max-h-[45dvh]' : 'hidden lg:block'"
>
<VillageOverviewSidebar :village="v.village.value" :base="base" :seconds-until="v.secondsUntil" />
</div>
</div>
</div>
<GameTabBar :village-id="villageId" />
<QuestBook v-if="v.village.value" />
</div>
<!-- Map and reports: the village header above a framed page. -->
<div v-else>
<div class="mx-auto max-w-[1100px] space-y-1 px-1 pt-1.5 sm:px-2">
<GameVillageBar v-if="v.village.value && v.resources.value" :village="v.village.value" :resources="v.resources.value" />
<GameProtectionBar
v-if="v.village.value?.rally.protected_until"
:until="v.village.value.rally.protected_until"
:village-id="villageId"
:seconds-until="v.secondsUntil"
/>
<VillageFoodWarning
v-if="v.village.value"
:starvation="v.village.value.starvation"
:levy="v.village.value.levy"
:seconds-until="v.secondsUntil"
/>
<p v-if="v.error.value" class="alert" role="alert">{{ v.error.value }}</p>
</div>
<main class="mx-auto max-w-[1100px] px-1 pt-1 pb-4 sm:px-2">
<div class="frame">
<slot />
</div>
</main>
<GameTabBar :village-id="villageId" />
<QuestBook v-if="v.village.value" />
</div>
</template>