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

104 lines
4.5 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
import type { Building, BuildingType } from '~/types/game'
const props = defineProps<{
buildings: Building[]
busy: boolean
}>()
const emit = defineEmits<{
select: [buildingId: number]
priority: [buildingId: number, priority: number]
}>()
const { t } = useI18n()
// Who works where: building and upgrading live in the construction table above. Copies are
// named in the server's rank, like everywhere else ("Storage, copy 2").
const named = computed(() => {
const byType = new Map<BuildingType, Building[]>()
for (const b of props.buildings) byType.set(b.type, [...(byType.get(b.type) ?? []), b])
const rank = new Map<number, number>()
for (const list of byType.values()) ranked(list).forEach((b, i) => rank.set(b.id, i))
return props.buildings.map((b) => {
const n = rank.get(b.id) ?? 0
const name = t(`buildings.${b.type}`)
return { b, name: n > 0 ? t('village.copy.name', { name, n: n + 1 }) : name }
})
})
</script>
<template>
<section class="panel overflow-hidden">
<div class="panel-head">
<h2 class="panel-title">{{ $t('village.buildings.title') }}</h2>
<span class="badge badge-plain">{{ buildings.length }}</span>
</div>
<div class="overflow-x-auto">
<table class="w-full min-w-[34rem] border-collapse text-sm">
<thead>
<tr class="border-b-2 border-wood-700 bg-parchment-100 text-left text-xs font-semibold text-ink-700">
<th class="px-3 py-1.5">{{ $t('village.buildings.type') }}</th>
<th class="px-2 py-1.5 text-right">{{ $t('village.buildings.level') }}</th>
<th class="w-40 px-2 py-1.5">{{ $t('village.buildings.workers') }}</th>
<th class="px-2 py-1.5">{{ $t('village.buildings.output') }}</th>
<th class="px-3 py-1.5 text-right">{{ $t('village.buildings.priority') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="{ b, name } in named" :key="b.id" class="border-b border-parchment-300 last:border-b-0 even:bg-parchment-100">
<td class="px-3 py-1">
<div class="flex items-center gap-2">
<IconBuildingTile :type="b.type" class="size-7" />
<button
type="button"
class="truncate text-left font-semibold hover:text-blood-700 hover:underline"
:title="$t('village.buildings.show')"
@click="emit('select', b.id)"
>
{{ name }}
</button>
</div>
</td>
<td class="px-2 py-1 text-right font-semibold tabular-nums">{{ b.level }}</td>
<td class="px-2 py-1">
<div class="flex items-center gap-2">
<span class="w-12 shrink-0 text-xs tabular-nums">{{ b.workers.assigned }}/{{ b.workers.required }}</span>
<UiBar :value="b.staffing" :tone="b.staffing < 1 ? 'rose' : 'emerald'" />
</div>
</td>
<td class="px-2 py-1">
<span v-if="b.production" class="flex items-center gap-1 tabular-nums whitespace-nowrap">
<IconResource :resource="b.production.resource" class="size-4" />
{{ $t('village.resources.per_hour', { rate: formatRate(b.production.per_hour) }) }}
</span>
<span v-else class="text-ink-400">–</span>
</td>
<td class="px-3 py-1 text-right">
<div class="inline-flex items-center rounded-sm border border-wood-600 bg-parchment-50">
<button
type="button"
class="btn btn-ghost rounded-r-none p-1"
:aria-label="$t('village.buildings.priority_down')"
:disabled="busy || b.priority <= 0"
@click="emit('priority', b.id, b.priority - 1)"
>
<IconUi name="minus" class="size-3.5" />
</button>
<span class="w-6 text-center font-semibold tabular-nums">{{ b.priority }}</span>
<button
type="button"
class="btn btn-ghost rounded-l-none p-1"
:aria-label="$t('village.buildings.priority_up')"
:disabled="busy || b.priority >= 9"
@click="emit('priority', b.id, b.priority + 1)"
>
<IconUi name="plus" class="size-3.5" />
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</template>