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

175 lines
7.5 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { Building, BuildingEffect, Cost, Resources } from '~/types/game'
import { garrisonOf } from '~/diorama/scene'
const props = defineProps<{
building: Building
resources: Resources
canAfford: (cost: Cost) => boolean
busy: boolean
/** "Timber camp, copy 2" for a copy, in the server's rank. */
title: string
}>()
const emit = defineEmits<{
upgrade: [buildingId: number]
demolish: [buildingId: number]
priority: [buildingId: number, priority: number]
}>()
const { t } = useI18n()
const v = useVillageContext()
/** What goes on at it right now, for the close-up: orders running or waiting, units in training. */
const state = computed(() => {
const village = v.village.value
const orders = village?.queue.filter((o) => o.building_id === props.building.id) ?? []
const running = orders.find((o) => o.builder !== null)
return {
upgrading: running?.action === 'upgrade',
demolishing: running?.action === 'demolish',
queued: orders.length > 0,
training: !!village?.military.find((m) => m.building_id === props.building.id)?.queue.length,
garrison: village ? garrisonOf(village) : 0,
}
})
/** Effects side by side: now and one level up (null at max level). */
const rows = computed(() =>
props.building.effects.map((e) => ({ e, next: props.building.next_effects?.find((n) => n.key === e.key) ?? null })),
)
function value(e: BuildingEffect) {
switch (e.key) {
case 'production':
return t('village.resources.per_hour', { rate: formatRate(e.value) })
case 'wall_defense':
return formatSigned(e.value)
// A copy's training speed can fall below a single building's: the sign comes from the value.
case 'wall_bonus':
case 'build_speed':
case 'recruit_speed':
return `${formatSigned(e.value)}%`
case 'detection':
return t('village.effects.fields', { n: e.value })
default:
return String(e.value)
}
}
</script>
<!--
The top of a building's own screen: the building up close with its workers at work,
then what it is, what it does now and one level up, who works there, and its actions.
-->
<template>
<section class="grid gap-3 @3xl:grid-cols-[minmax(0,1fr)_minmax(0,1fr)]">
<div class="h-52 border border-wood-700 @3xl:h-auto @3xl:min-h-60">
<VillageCloseup :type="building.type" :level="building.level" :workers="building.workers.assigned" :state="state" />
</div>
<div class="space-y-2.5 text-sm">
<div>
<h1 class="font-display text-lg leading-tight font-bold text-wood-950">
{{ title }} <span class="font-normal">({{ $t('village.buildings.level_n', { level: building.level }) }})</span>
</h1>
<p v-if="isCopy(building.instance_share)" class="mt-0.5">
<span class="badge badge-gold">{{ $t('village.copy.this', { share: sharePercent(building.instance_share) }) }}</span>
</p>
<p class="mt-0.5 text-ink-700">{{ $t(`village.building_about.${building.type}`) }}</p>
</div>
<table v-if="rows.length" class="grid-table">
<thead>
<tr>
<th />
<th class="text-right">{{ $t('village.effects.now') }}</th>
<th class="text-right">{{ building.next_effects ? $t('village.buildings.level_n', { level: building.level + 1 }) : '' }}</th>
</tr>
</thead>
<tbody>
<tr v-for="{ e, next } in rows" :key="e.key">
<td class="whitespace-nowrap">
<span class="inline-flex items-center gap-1">
<IconResource v-if="e.resource" :resource="e.resource" class="size-4" />
{{ $t(`village.effects.${e.key}`) }}
</span>
</td>
<td class="text-right font-semibold whitespace-nowrap tabular-nums">{{ value(e) }}</td>
<td class="text-right whitespace-nowrap text-moss-700 tabular-nums">{{ next ? value(next) : '' }}</td>
</tr>
</tbody>
</table>
<div class="grid grid-cols-2 gap-3">
<div>
<p class="text-xs text-ink-500">{{ $t('village.buildings.workers') }}</p>
<p class="tabular-nums">{{ building.workers.assigned }}/{{ building.workers.required }}</p>
<UiBar class="mt-1" :value="building.staffing" :tone="building.staffing < 1 ? 'rose' : 'emerald'" />
</div>
<div>
<p class="text-xs text-ink-500">{{ $t('village.buildings.output') }}</p>
<p v-if="building.production" class="flex items-center gap-1 font-semibold whitespace-nowrap tabular-nums">
<IconResource :resource="building.production.resource" class="size-4" />
{{ $t('village.resources.per_hour', { rate: formatRate(building.production.per_hour) }) }}
</p>
<p v-else class="text-ink-400">–</p>
</div>
</div>
<div class="flex items-center justify-between gap-3">
<span class="text-ink-500">{{ $t('village.buildings.priority') }}</span>
<div class="inline-flex items-center border border-wood-600 bg-parchment-50">
<button
type="button"
class="btn btn-ghost p-1"
:aria-label="$t('village.buildings.priority_down')"
:disabled="busy || building.priority <= 0"
@click="emit('priority', building.id, building.priority - 1)"
>
<IconUi name="minus" class="size-4" />
</button>
<span class="w-7 text-center font-semibold tabular-nums">{{ building.priority }}</span>
<button
type="button"
class="btn btn-ghost p-1"
:aria-label="$t('village.buildings.priority_up')"
:disabled="busy || building.priority >= 9"
@click="emit('priority', building.id, building.priority + 1)"
>
<IconUi name="plus" class="size-4" />
</button>
</div>
</div>
<div v-if="building.upgrade" class="flex flex-wrap items-center justify-between gap-2 border border-wood-600/50 bg-parchment-100 p-2">
<span class="flex flex-wrap items-center gap-2.5">
<UiCost :cost="building.upgrade.cost" :have="resources" :villagers="building.upgrade.villagers" :idle="v.village.value?.population.idle" nowrap />
<span class="flex items-center gap-1 text-xs text-ink-500 tabular-nums">
<IconUi name="clock" class="size-3.5" />
{{ formatDuration(building.upgrade.seconds) }}
</span>
</span>
<button
type="button"
class="btn btn-primary"
:disabled="busy || !!building.upgrade.blocked || !canAfford(building.upgrade.cost)"
@click="emit('upgrade', building.id)"
>
<IconUi name="up" class="size-4" />
{{ $t('village.buildings.upgrade', { level: building.upgrade.target_level }) }}
</button>
<p v-if="building.upgrade.blocked" class="w-full text-xs font-semibold text-blood-700">{{ $t(`village.blocked.${building.upgrade.blocked}`) }}</p>
</div>
<p v-else class="text-ink-500">{{ $t('village.blocked.max_level') }}</p>
<div v-if="building.demolish" class="flex flex-wrap items-center justify-between gap-2 text-xs text-ink-500">
<span class="flex items-center gap-1.5">
{{ $t('village.buildings.refund') }}
<UiCost :cost="building.demolish.refund" nowrap />
</span>
<button type="button" class="btn btn-danger" :disabled="busy" @click="emit('demolish', building.id)">
{{ $t('village.buildings.demolish') }}
</button>
</div>
</div>
</section>
</template>