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

226 lines
12 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 { DockItem } from '~/components/ui/Dock.vue'
import type { BuildOrder, Movement, UnitType, VillageRole, VillageState } from '~/types/game'
const props = defineProps<{
village: VillageState
/** The village page path, for the links to its screens. */
base: string
secondsUntil: (iso: string) => number
}>()
const { t } = useI18n()
const production = computed(() => (['wood', 'clay', 'iron', 'food'] as const).map((r) => ({ r, rate: props.village.rates[r] })))
const troops = computed(() => (Object.entries(props.village.troops) as [UnitType, number][]).filter(([, n]) => n > 0))
/** The wounded in the hospital and when the first of them stand again. */
const wounded = computed(() => {
const list = props.village.hospital.wounded
if (!list.length) return null
return { count: props.village.hospital.occupied, next: list[0]!.heals_at }
})
const idle = computed(() => props.village.population.villagers - props.village.population.workers - props.village.levy.militia)
const spaceShare = computed(() => props.village.space.used / props.village.space.max)
const incoming = computed(() => props.village.movements.filter((m) => m.direction === 'in' && m.type === 'attack'))
const commands = computed(() => props.village.movements.filter((m) => m.direction !== 'in').length)
/** Own groups under way, out and home, soonest first; they can be called back for a while. */
const own = computed(() => props.village.movements.filter((m) => m.direction !== 'in'))
const v = useVillageContextOrNull()
const command = (m: Movement) =>
m.direction === 'home'
? t('village.movements.return_from', { from: villageLabel(m.from) })
: t(m.type === 'attack' ? 'village.movements.attack_on' : 'village.movements.support_for', { target: villageLabel(m.to) })
const canCancel = (m: Movement) => !!m.cancel_until && props.secondsUntil(m.cancel_until) > 0
const spec = computed(() => props.village.specialisation)
const season = computed(() => props.village.season)
const roleSpace = computed(() => spec.value.roles.reduce((sum, r) => sum + (spec.value.space[r.role] ?? 0), 0))
const roleShare = (role: VillageRole) => (roleSpace.value ? Math.round((100 * (spec.value.space[role] ?? 0)) / roleSpace.value) : 0)
// Every box folds and moves (UiDock); the incoming attacks only while there are any.
const cards = computed<DockItem[]>(() => [
{ key: 'incoming', title: t('village.side.incoming'), alarm: true, show: incoming.value.length > 0 },
{ key: 'commands', title: t('village.movements.own'), show: own.value.length > 0 },
{ key: 'production', title: t('village.side.production') },
{ key: 'units', title: t('village.side.units') },
{ key: 'population', title: t('village.population.title') },
{ key: 'season', title: t('village.season.title') },
{ key: 'role', title: t('village.role.title') },
{ key: 'construction', title: t('village.side.construction') },
])
const bonuses = computed(() => Object.entries(spec.value.roles.find((r) => r.role === spec.value.role)?.bonuses ?? {}))
function order(o: BuildOrder) {
const type = t(`buildings.${o.type}`)
if (o.action === 'build') return type
return `${type} ${o.target_level < 1 ? '×' : `→ ${o.target_level}`}`
}
</script>
<!-- The column beside the village: small boxes with the numbers that matter at a glance. -->
<template>
<aside class="p-2 text-sm">
<UiDock scope="overview" :cards="cards">
<template #incoming>
<ul class="side-body">
<li v-for="m in incoming" :key="m.id" class="flex items-center gap-2 text-blood-700">
<IconUi name="attack" class="size-4" />
<span class="min-w-0 flex-1 truncate">{{ m.raid ? $t('village.side.raiders', { from: `(${m.from.x}|${m.from.y})` }) : villageLabel(m.from) }}</span>
<span class="font-semibold tabular-nums">{{ formatDuration(secondsUntil(m.arrives_at)) }}</span>
</li>
</ul>
</template>
<template #commands>
<ul class="side-body">
<li v-for="m in own" :key="m.id">
<div class="flex items-center gap-2">
<IconUi :name="m.type === 'return' ? 'return' : m.type" class="size-4 shrink-0" />
<NuxtLink
:to="{ path: `/map/${village.world_id}`, query: { x: (m.direction === 'home' ? m.from : m.to).x, y: (m.direction === 'home' ? m.from : m.to).y } }"
class="min-w-0 flex-1 truncate hover:underline"
:title="command(m)"
>
{{ command(m) }}
</NuxtLink>
<span class="font-semibold tabular-nums">{{ formatDuration(secondsUntil(m.arrives_at)) }}</span>
</div>
<div v-if="v && canCancel(m)" class="flex justify-end pb-0.5">
<button type="button" class="btn btn-outline px-1.5 py-0 text-xs tabular-nums" :disabled="v.busy.value" @click="v.cancelMovement(m.id)">
{{ $t('village.movements.cancel') }} · {{ formatDuration(secondsUntil(m.cancel_until!)) }}
</button>
</div>
</li>
</ul>
</template>
<template #production>
<ul class="side-body">
<li v-for="{ r, rate } in production" :key="r" class="flex items-center gap-2">
<IconResource :resource="r" class="size-4" />
<span class="flex-1">{{ $t(`resources.${r}`) }}</span>
<span class="font-semibold tabular-nums" :class="rate < 0 ? 'text-blood-700' : ''">
{{ $t('village.resources.per_hour', { rate: formatRate(rate) }) }}
</span>
</li>
</ul>
</template>
<template #units>
<div class="side-body">
<ul v-if="troops.length || village.levy.militia">
<li v-for="[unit, count] in troops" :key="unit" class="flex items-center gap-2">
<IconUnit :unit="unit" class="size-4 text-wood-800" />
<span class="font-semibold tabular-nums">{{ count }}</span>
<span class="truncate">{{ $t(`units.${unit}`) }}</span>
</li>
<li v-if="village.levy.militia" class="flex items-center gap-2">
<IconUnit unit="militia" class="size-4 text-wood-800" />
<span class="font-semibold tabular-nums">{{ village.levy.militia }}</span>
<span>{{ $t('village.levy.militia') }}</span>
</li>
</ul>
<p v-else class="text-ink-500">{{ $t('village.troops.none') }}</p>
<p v-if="village.population.training" class="text-xs text-ink-500">{{ $t('village.troops.training', { count: village.population.training }) }}</p>
<p v-if="wounded" class="text-xs text-ink-500">{{ $t('village.troops.wounded_back', { count: wounded.count, time: formatDuration(secondsUntil(wounded.next)) }) }}</p>
<p v-if="commands" class="text-xs text-ink-500">{{ $t('village.side.commands') }}: {{ commands }}</p>
<div class="side-actions">
<NuxtLink :to="`${base}/recruit`" class="btn btn-outline">{{ $t('village.side.recruit') }}</NuxtLink>
<NuxtLink :to="`${base}/rally`" class="btn btn-outline">{{ $t('village.side.rally') }}</NuxtLink>
</div>
</div>
</template>
<template #population>
<dl class="grid grid-cols-[1fr_auto] gap-x-2 gap-y-1 px-2.5 py-1.5">
<dt>{{ $t('village.population.villagers') }}</dt>
<dd class="text-right font-semibold tabular-nums">{{ village.population.villagers }}</dd>
<dt>{{ $t('village.population.workers') }}</dt>
<dd class="text-right tabular-nums">{{ village.population.workers }}</dd>
<dt>{{ $t('village.population.idle') }}</dt>
<dd class="text-right tabular-nums">{{ idle }}</dd>
<dt>{{ $t('village.population.upkeep') }}</dt>
<dd class="text-right tabular-nums">{{ village.population.upkeep }}/{{ village.population.food_budget }}</dd>
</dl>
</template>
<template #season>
<div class="side-body">
<template v-if="season.winner">
<p class="font-semibold">{{ $t('village.season.won', { player: season.winner.name }) }}</p>
<div class="side-actions">
<NuxtLink to="/worlds" class="btn btn-outline">{{ $t('village.season.next') }}</NuxtLink>
</div>
</template>
<p v-else-if="!season.crowns.length">{{ $t('village.season.crowns_in', { time: formatSpan(secondsUntil(season.crowns_at), t) }) }}</p>
<template v-else>
<ul>
<li v-for="c in season.crowns" :key="c.id" class="flex items-center gap-2">
<NuxtLink :to="{ path: `/map/${village.world_id}`, query: { x: c.x, y: c.y } }" class="tabular-nums hover:underline">({{ c.x }}|{{ c.y }})</NuxtLink>
<span class="min-w-0 flex-1 truncate text-right">{{ c.player?.name ?? $t('village.season.barbarians') }}</span>
</li>
</ul>
<p v-if="season.holder && season.ends_at" class="font-semibold">
{{ $t('village.season.holds', { player: season.holder.name, time: formatSpan(secondsUntil(season.ends_at), t) }) }}
</p>
<p v-else>{{ $t('village.season.nobody', { needed: season.needed }) }}</p>
</template>
<p v-if="!season.winner" class="text-xs text-ink-500">
{{ $t('village.season.rule', { needed: season.needed, count: season.count, time: formatSpan(season.hold_seconds, t) }) }}
</p>
</div>
</template>
<template #role>
<div class="side-body">
<p class="font-semibold">{{ spec.role ? $t(`village.role.${spec.role}`) : $t('village.role.none') }}</p>
<ul v-if="spec.role" class="text-moss-700">
<li v-for="[key, value] in bonuses" :key="key">{{ $t(`village.role.bonus.${key}`, { value: Math.round(value * 100) }) }}</li>
</ul>
<ul>
<li v-for="r in spec.roles" :key="r.role" class="flex items-center gap-2">
<span class="flex-1">{{ $t(`village.role.kinds.${r.role}`) }}</span>
<span class="tabular-nums">{{ $t('village.role.space', { space: spec.space[r.role] ?? 0, share: roleShare(r.role) }) }}</span>
</li>
</ul>
<p v-if="!spec.role" class="text-xs text-ink-500">{{ $t('village.role.hint', { min: spec.min_space, share: Math.round(spec.share * 100) }) }}</p>
</div>
</template>
<template #construction>
<div class="side-body">
<ol v-if="village.queue.length">
<li v-for="o in village.queue" :key="o.id" class="flex items-center gap-2">
<IconBuilding :type="o.type" class="size-4 text-wood-800" />
<span class="min-w-0 flex-1 truncate">{{ order(o) }}</span>
<span class="tabular-nums" :class="o.finishes_at ? 'font-semibold' : 'text-ink-500'">
{{ o.finishes_at ? formatDuration(secondsUntil(o.finishes_at)) : $t('village.queue.waiting') }}
</span>
</li>
</ol>
<p v-else class="text-ink-500">{{ $t('village.queue.empty') }}</p>
<div class="pt-1">
<div class="mb-1 flex justify-between text-xs text-ink-500">
<span>{{ $t('village.diorama.space') }}</span>
<span class="tabular-nums">{{ village.space.used }}/{{ village.space.max }}</span>
</div>
<UiBar :value="spaceShare" :tone="spaceShare >= 1 ? 'rose' : 'emerald'" />
</div>
<div class="side-actions">
<NuxtLink :to="`${base}/build`" class="btn btn-outline">{{ $t('village.screens.build') }}</NuxtLink>
</div>
</div>
</template>
</UiDock>
</aside>
</template>
<style scoped>
@reference "~/assets/css/main.css";
.side-body {
@apply space-y-0.5 px-1.5 py-1;
}
/* Links to the screens as small buttons on one line, never wrapped. */
.side-actions {
@apply flex flex-nowrap gap-1 pt-1 whitespace-nowrap;
}
</style>