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

161 lines
8.2 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 { Resources, VillageState } from '~/types/game'
const props = defineProps<{ village: VillageState; resources: Resources }>()
const v = useVillageContext()
const route = useRoute()
const base = computed(() => `/village/${props.village.id}`)
const incoming = computed(() => props.village.movements.filter((m) => m.direction === 'in'))
const attacks = computed(() => incoming.value.filter((m) => m.type === 'attack'))
const nextAttack = computed(() => attacks.value[0] ?? null)
const support = computed(() => incoming.value.length - attacks.value.length)
const isActive = (path: string) => route.path === `${base.value}${path}`
/** Another of the player's villages, on the same screen where the URL has one. */
const others = computed(() => props.village.villages)
const position = computed(() => others.value.findIndex((o) => o.id === props.village.id))
function switchTo(id: number) {
const path = route.path.startsWith(base.value) ? route.path.replace(base.value, `/village/${id}`) : `/village/${id}`
navigateTo(path)
}
function step(by: number) {
const list = others.value
switchTo(list[(position.value + by + list.length) % list.length]!.id)
}
const questBook = useQuestBookOpen()
/** Quests to claim plus level rewards to collect. */
const questsWaiting = computed(() => props.village.quests.claimable + props.village.quests.rewards)
/** The new name while the village is being renamed. */
const renaming = ref<string | null>(null)
async function rename() {
if (renaming.value !== null && (await v.rename(renaming.value))) renaming.value = null
}
</script>
<!--
Above the content, like the header of Die Stämme: the quick bar with the village's
screens, then one row with the village and its stock. Plain numbers in thin cells;
each resource leads to the workforce, where it is made.
-->
<template>
<div class="space-y-1 text-[13px]">
<nav class="hidden border border-wood-700 bg-parchment-200 md:flex" :aria-label="$t('village.screens.label')">
<NuxtLink
v-for="s in VILLAGE_SCREENS"
:key="s.name"
:to="`${base}${s.path}`"
class="quick"
:class="{ 'quick-active': isActive(s.path) }"
:aria-current="isActive(s.path) ? 'page' : undefined"
:title="$t('app.shortcut', { key: SHORTCUTS[s.name].toUpperCase() })"
>
<IconBuilding :type="s.icon" class="size-4 text-wood-800" />
{{ $t(`village.screens.${s.name}`) }}
</NuxtLink>
</nav>
<div class="flex flex-wrap items-stretch border border-wood-700 bg-parchment-200">
<!-- The quest book, with what waits in it. -->
<button
type="button"
class="flex items-center gap-1 border-r border-wood-700 px-2 py-1 font-semibold text-wood-900 hover:bg-parchment-100 hover:text-blood-700"
:class="questsWaiting ? 'bg-gold-400' : ''"
:title="$t('quests.open')"
@click="questBook = true"
>
<IconUi name="book" class="size-4" />
<span class="sr-only sm:not-sr-only">{{ $t('quests.open') }}</span>
<span v-if="questsWaiting" class="font-bold text-blood-700 tabular-nums">({{ questsWaiting }})</span>
</button>
<!-- Which village. -->
<div class="flex min-w-0 flex-1 items-center gap-1.5 px-2 py-1">
<form v-if="renaming !== null" class="flex items-center gap-1" @submit.prevent="rename">
<input v-model="renaming" type="text" maxlength="32" class="input w-40" :aria-label="$t('village.rename')" @keydown.esc="renaming = null" />
<button type="submit" class="btn btn-primary" :disabled="v.busy.value || !renaming.trim()">{{ $t('village.rename_save') }}</button>
<button type="button" class="btn btn-ghost" @click="renaming = null">{{ $t('village.rename_cancel') }}</button>
</form>
<template v-else>
<span v-if="others.length > 1" class="inline-flex shrink-0 items-center">
<button type="button" class="btn btn-ghost px-1" :title="$t('village.switcher.prev')" @click="step(-1)">‹<span class="sr-only">{{ $t('village.switcher.prev') }}</span></button>
<select
class="input w-auto max-w-48 py-0 font-bold"
:value="village.id"
:aria-label="$t('village.switcher.label')"
@change="switchTo(Number(($event.target as HTMLSelectElement).value))"
>
<option v-for="o in others" :key="o.id" :value="o.id">{{ o.name }}</option>
</select>
<button type="button" class="btn btn-ghost px-1" :title="$t('village.switcher.next')" @click="step(1)">›<span class="sr-only">{{ $t('village.switcher.next') }}</span></button>
</span>
<NuxtLink v-else :to="base" class="truncate font-bold text-wood-950 hover:text-blood-700">{{ village.name }}</NuxtLink>
<NuxtLink
:to="{ path: `/map/${village.world_id}`, query: { x: village.x, y: village.y } }"
class="shrink-0 font-bold text-wood-950 tabular-nums hover:text-blood-700"
:title="$t('village.on_map')"
>
({{ village.x }}|{{ village.y }}) {{ village.continent }}
</NuxtLink>
<span class="hidden shrink-0 text-ink-500 tabular-nums sm:inline">{{ $t('village.points', { points: village.points }) }}</span>
<button type="button" class="shrink-0 text-ink-400 hover:text-blood-700" :title="$t('village.rename')" @click="renaming = village.name">
<IconUi name="edit" class="size-3.5" />
<span class="sr-only">{{ $t('village.rename') }}</span>
</button>
</template>
</div>
<!-- The stock: four resources, the storage cap and the villagers, each in its own cell. -->
<div class="grid w-full grid-cols-5 border-t border-parchment-300 sm:flex sm:w-auto sm:border-t-0">
<NuxtLink v-for="r in (['wood', 'clay', 'iron', 'food'] as const)" :key="r" :to="`${base}/workforce`" class="cell flex hover:bg-parchment-100">
<GameResource :resource="r" :amount="resources[r]" :cap="village.storage_cap" :rate="village.rates[r]" />
</NuxtLink>
<span v-if="village.loyalty < 100" class="cell flex font-semibold text-blood-700" :title="`${$t('village.loyalty')}: ${$t('village.loyalty_about')}`">
{{ $t('village.loyalty') }} <span class="tabular-nums">{{ village.loyalty }}</span>
</span>
<span class="cell hidden lg:flex" :title="$t('village.resources.cap')">
<IconBuilding type="storage" class="size-4 text-wood-800" />
<span class="tabular-nums">{{ village.storage_cap }}</span>
</span>
<NuxtLink
:to="`${base}/workforce`"
class="cell flex hover:bg-parchment-100"
:title="`${$t('village.population.villagers')}: ${village.population.villagers} · ${$t('village.population.upkeep')}: ${village.population.upkeep}/${village.population.food_budget}`"
>
<IconUi name="users" class="size-4 text-wood-800" />
<span class="tabular-nums">{{ village.population.villagers }}</span>
</NuxtLink>
</div>
<!-- Incoming troops: red for attacks, green for support, with the next arrival. -->
<NuxtLink
v-if="incoming.length"
:to="`${base}/rally`"
class="flex items-center gap-2 border-l border-wood-700 px-2 font-bold"
:class="attacks.length ? 'bg-blood-700 text-parchment-50' : 'bg-moss-600 text-parchment-50'"
:title="$t('village.incoming', { count: attacks.length })"
>
<span v-if="attacks.length" class="inline-flex items-center gap-1"><IconUi name="attack" class="size-4" />{{ attacks.length }}</span>
<span v-if="support" class="inline-flex items-center gap-1"><IconUi name="support" class="size-4" />{{ support }}</span>
<span v-if="nextAttack" class="tabular-nums">{{ formatDuration(v.secondsUntil(nextAttack.arrives_at)) }}</span>
</NuxtLink>
</div>
</div>
</template>
<style scoped>
@reference "~/assets/css/main.css";
.quick {
@apply flex items-center gap-1.5 border-r border-parchment-300 px-3 py-1 font-semibold text-wood-900 last:border-r-0 hover:bg-parchment-100 hover:text-blood-700;
}
.quick-active {
@apply bg-parchment-50 text-blood-700;
}
.cell {
@apply items-center justify-center gap-1 border-l border-parchment-300 px-2 py-1 first:border-l-0 sm:first:border-l;
}
</style>