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

161 lines
6.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
import type { MapVillage } from '~/types/map'
const route = useRoute()
const router = useRouter()
const { view, error, busy, load } = useMap(Number(route.params.worldId))
useState<number | null>('world.current', () => null).value = Number(route.params.worldId)
const ZOOM = [36, 52, 72]
const zoom = ref(1)
const field = computed(() => ZOOM[zoom.value]!)
// The window fills the space the page gives it: odd sizes keep the centre field in the middle.
const box = ref<HTMLElement | null>(null)
const size = ref({ w: 15, h: 11 })
const odd = (n: number) => (n % 2 ? n : n - 1)
const clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n))
function measure() {
if (!box.value) return
const rect = box.value.getBoundingClientRect()
const height = Math.max(360, window.innerHeight - rect.top - 24)
size.value = {
w: clamp(odd(Math.floor((rect.width - 26) / field.value)), 5, 41),
h: clamp(odd(Math.floor((height - 26) / field.value)), 5, 41),
}
}
let observer: ResizeObserver | undefined
onMounted(() => {
measure()
observer = new ResizeObserver(measure)
if (box.value) observer.observe(box.value)
})
onBeforeUnmount(() => observer?.disconnect())
watch(field, measure)
/** Centre from the URL (?x=&y=), or null to open on the player's village. */
const center = computed(() => {
const x = Number(route.query.x)
const y = Number(route.query.y)
return Number.isFinite(x) && Number.isFinite(y) && route.query.x !== undefined ? { x, y } : null
})
watch([center, size], () => load(center.value, size.value.w, size.value.h), { immediate: true, deep: true })
function goTo(at: { x: number; y: number }) {
const max = (view.value?.size ?? 1000) - 1
router.replace({ query: { x: clamp(Math.round(at.x), 0, max), y: clamp(Math.round(at.y), 0, max) } })
}
function pan(dx: number, dy: number) {
const from = view.value?.center
if (from) goTo({ x: from.x + dx, y: from.y + dy })
}
// Jump-to form, prefilled with the centre.
const jumpX = ref<number | null>(null)
const jumpY = ref<number | null>(null)
watch(
() => view.value?.center,
(c) => {
if (c) {
jumpX.value = c.x
jumpY.value = c.y
}
},
)
function jump() {
if (jumpX.value !== null && jumpY.value !== null) goTo({ x: jumpX.value, y: jumpY.value })
}
const selected = ref<{ x: number; y: number } | null>(null)
const hovered = ref<MapVillage | null>(null)
const selectedVillage = computed(() =>
selected.value ? (view.value?.villages.find((v) => v.x === selected.value!.x && v.y === selected.value!.y) ?? null) : null,
)
const shown = computed(() => hovered.value ?? selectedVillage.value)
const counts = computed(() => {
const villages = view.value?.villages ?? []
return {
own: villages.filter((v) => v.own).length,
players: villages.filter((v) => v.player && !v.own).length,
barbarians: villages.filter((v) => !v.player).length,
}
})
const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.y / 100)}${Math.floor(view.value.center.x / 100)}` : ''))
</script>
<template>
<div class="px-4 py-5 lg:px-6">
<header class="mb-4 flex flex-wrap items-end justify-between gap-3">
<div>
<p class="text-xs font-medium tracking-wide text-ink-500 uppercase">{{ $t('map.subtitle') }}</p>
<h1 class="font-display text-2xl font-semibold sm:text-3xl">
{{ $t('map.title') }}
<span v-if="view" class="ml-2 text-lg font-normal text-ink-500 tabular-nums">{{ view.center.x }}|{{ view.center.y }} · {{ continent }}</span>
</h1>
</div>
<div class="flex flex-wrap items-center gap-2">
<form class="flex items-center gap-1.5" @submit.prevent="jump">
<label class="sr-only" for="jump-x">x</label>
<input id="jump-x" v-model.number="jumpX" class="input w-20 py-1.5 text-center tabular-nums" type="number" min="0" :max="(view?.size ?? 1000) - 1" />
<span class="font-semibold text-ink-500">|</span>
<label class="sr-only" for="jump-y">y</label>
<input id="jump-y" v-model.number="jumpY" class="input w-20 py-1.5 text-center tabular-nums" type="number" min="0" :max="(view?.size ?? 1000) - 1" />
<button type="submit" class="btn btn-primary">{{ $t('map.jump') }}</button>
</form>
<button type="button" class="btn btn-outline" @click="router.replace({ query: {} })">{{ $t('map.home') }}</button>
<div class="flex items-center rounded-lg border border-parchment-300 bg-white">
<button type="button" class="btn btn-ghost px-2.5" :disabled="zoom === 0" :aria-label="$t('map.zoom_out')" @click="zoom--">−</button>
<button type="button" class="btn btn-ghost px-2.5" :disabled="zoom === ZOOM.length - 1" :aria-label="$t('map.zoom_in')" @click="zoom++">+</button>
</div>
</div>
</header>
<p v-if="error" class="alert mb-4" role="alert">{{ error }}</p>
<div class="grid gap-5 xl:grid-cols-[1fr_20rem]">
<div ref="box" class="relative min-w-0">
<div class="panel overflow-hidden p-0">
<div class="flex justify-center overflow-hidden bg-[#a9c27d]">
<MapWorldMap
v-if="view"
:view="view"
:field="field"
:selected="selected"
@select="selected = $event"
@hover="hovered = $event"
@pan="pan"
/>
<p v-else class="py-24 text-sm text-ink-500">{{ $t('common.loading') }}</p>
</div>
</div>
<div class="mt-2 flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-ink-500">
<span>{{ $t('map.controls') }}</span>
<span v-if="busy">{{ $t('common.loading') }}</span>
</div>
</div>
<aside class="space-y-5">
<MapInfoCard :village="shown" :at="selected" @center="goTo" />
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('map.legend.title') }}</h2>
</div>
<ul class="panel-body space-y-2 text-sm">
<li class="flex items-center gap-2"><span class="size-3 rounded-sm bg-[#f2b705] ring-1 ring-ink-900/30" /> {{ $t('map.legend.own') }} <span class="ml-auto tabular-nums text-ink-500">{{ counts.own }}</span></li>
<li class="flex items-center gap-2"><span class="size-3 rounded-sm bg-[#c0392b] ring-1 ring-ink-900/30" /> {{ $t('map.legend.players') }} <span class="ml-auto tabular-nums text-ink-500">{{ counts.players }}</span></li>
<li class="flex items-center gap-2"><span class="size-3 rounded-sm bg-[#7d7d7d] ring-1 ring-ink-900/30" /> {{ $t('map.legend.barbarians') }} <span class="ml-auto tabular-nums text-ink-500">{{ counts.barbarians }}</span></li>
<li class="pt-1 text-xs text-ink-500">{{ $t('map.legend.lines') }}</li>
</ul>
</section>
</aside>
</div>
</div>
</template>