game-village-politics-frontend/app/pages/village/[id].vue

128 lines
4.8 KiB
Vue

<script setup lang="ts">
const route = useRoute()
const v = useVillage(Number(route.params.id))
const { village, resources, error, busy } = v
/** Building selected in the diorama, by id; dropped when the building is gone. */
const selected = ref<number | null>(null)
const selectedBuilding = computed(() => village.value?.buildings.find((b) => b.id === selected.value) ?? null)
watch(selectedBuilding, (b) => {
if (!b) selected.value = null
})
const spaceShare = computed(() => (village.value ? village.value.space.used / village.value.space.max : 0))
const currentWorld = useState<number | null>('world.current', () => null)
watch(village, (v) => {
if (v) currentWorld.value = v.world_id
})
v.refresh()
function showInVillage(id: number) {
selected.value = id
window.scrollTo({ top: 0, behavior: 'smooth' })
}
</script>
<template>
<div v-if="village && resources">
<VillageResourceBar class="sticky top-0 z-30" :village="village" :resources="resources" />
<section class="border-b-2 border-wood-700">
<!-- Village header: a flat parchment strip above the land, so nothing covers the view. -->
<div class="flex flex-wrap items-center gap-x-6 gap-y-2 border-b-2 border-wood-700 bg-parchment-200 px-4 py-2.5 sm:px-6">
<div class="min-w-0">
<p class="text-xs font-semibold tracking-wide text-ink-500 uppercase">{{ $t('village.title') }}</p>
<h1 class="truncate font-display text-xl font-semibold text-wood-900 sm:text-2xl" style="font-variant: small-caps">
{{ village.name }}
</h1>
</div>
<NuxtLink
:to="{ path: `/map/${village.world_id}`, query: { x: village.x, y: village.y } }"
class="inline-flex items-center gap-1 rounded-sm border border-wood-600/50 bg-parchment-50 px-2 py-1 text-sm text-ink-700 hover:border-blood-700 hover:text-blood-700"
:title="$t('village.on_map')"
>
<IconUi name="pin" class="size-3.5" />
<span class="font-semibold tabular-nums">{{ village.x }}|{{ village.y }}</span>
<span class="text-ink-500">· {{ village.continent }} · {{ $t('village.points', { points: village.points }) }}</span>
</NuxtLink>
<div class="w-full sm:ml-auto sm:w-64">
<div class="mb-1 flex justify-between gap-3 text-xs">
<span class="font-semibold text-ink-700">{{ $t('village.diorama.space') }}</span>
<span class="font-semibold tabular-nums">
{{ $t('village.diorama.space_value', { used: village.space.used, max: village.space.max }) }}
</span>
</div>
<UiBar :value="spaceShare" :tone="spaceShare >= 1 ? 'rose' : 'emerald'" />
</div>
</div>
<div class="relative">
<div class="h-[clamp(260px,82vw,460px)] sm:h-[clamp(340px,62vh,760px)]">
<VillageDiorama v-model:selected="selected" :village="village" />
</div>
<p
class="pointer-events-none absolute bottom-3 left-1/2 hidden -translate-x-1/2 rounded-sm border border-wood-700 bg-parchment-50 px-3 py-1 text-xs text-ink-700 sm:block"
>
{{ $t('village.diorama.hint') }}
</p>
<VillageBuildingCard
v-if="selectedBuilding"
class="mx-4 my-4 lg:absolute lg:top-4 lg:right-6 lg:z-10 lg:m-0 lg:w-80"
:building="selectedBuilding"
:resources="resources"
:can-afford="v.canAfford"
:busy="busy"
@upgrade="v.upgrade"
@demolish="v.demolish"
@priority="v.setPriority"
@close="selected = null"
/>
</div>
</section>
<div class="space-y-6 px-4 py-6 sm:px-6">
<p v-if="error" class="alert" role="alert">{{ error }}</p>
<div class="grid gap-6 lg:grid-cols-[minmax(0,1fr)_22rem] 2xl:grid-cols-[minmax(0,1fr)_26rem]">
<VillageBuildForm
:options="village.buildable"
:space="village.space"
:resources="resources"
:can-afford="v.canAfford"
:busy="busy"
@build="v.build"
/>
<div class="space-y-6">
<VillageBuildQueue
:queue="village.queue"
:builders="village.builders"
:seconds-until="v.secondsUntil"
:busy="busy"
@cancel="v.cancel"
/>
<VillagePopulationPanel :population="village.population" />
</div>
</div>
<VillageBuildingTable
:buildings="village.buildings"
:resources="resources"
:can-afford="v.canAfford"
:busy="busy"
@select="showInVillage"
@upgrade="v.upgrade"
@demolish="v.demolish"
@priority="v.setPriority"
/>
</div>
</div>
<div v-else class="px-4 py-16 text-center">
<p v-if="error" class="alert inline-block" role="alert">{{ error }}</p>
<p v-else class="text-sm text-ink-500">{{ $t('common.loading') }}</p>
</div>
</template>