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

76 lines
2.8 KiB
Vue

<script setup lang="ts">
import type { BuildOrder } from '~/types/game'
const props = defineProps<{
queue: BuildOrder[]
builders: number
secondsUntil: (iso: string) => number
busy: boolean
}>()
const emit = defineEmits<{ cancel: [orderId: number] }>()
const { t } = useI18n()
function describe(o: BuildOrder) {
const type = t(`buildings.${o.type}`)
if (o.action === 'build') return t('village.queue.build', { type })
if (o.action === 'upgrade') return t('village.queue.upgrade', { type, level: o.target_level })
if (o.target_level < 0) return t('village.queue.remove', { type })
return t('village.queue.demolish', { type, level: o.target_level })
}
/** Share of a running order that is done, from its server timestamps. */
function progress(o: BuildOrder) {
if (!o.starts_at || !o.finishes_at) return 0
const total = (Date.parse(o.finishes_at) - Date.parse(o.starts_at)) / 1000
return total > 0 ? 1 - props.secondsUntil(o.finishes_at) / total : 1
}
</script>
<template>
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('village.queue.title') }}</h2>
<span class="badge badge-plain">
<IconUi name="hammer" class="size-3.5" />
{{ $t('village.queue.builders', { count: builders }) }}
</span>
</div>
<div class="panel-body">
<p v-if="!queue.length" class="py-4 text-center text-sm text-ink-500">{{ $t('village.queue.empty') }}</p>
<ol v-else class="space-y-2.5">
<li
v-for="o in queue"
:key="o.id"
class="rounded-sm border-2 p-3"
:class="o.finishes_at ? 'border-gold-600 bg-parchment-50' : 'border-parchment-300 bg-parchment-100'"
>
<div class="flex items-center gap-2.5">
<IconBuildingTile :type="o.type" class="size-9" />
<div class="min-w-0 flex-1">
<p class="text-sm leading-snug font-medium">{{ describe(o) }}</p>
<p class="text-xs text-ink-500">
{{ o.builder ? $t('village.queue.builder_n', { n: o.builder }) : $t('village.queue.waiting') }}
</p>
</div>
<span v-if="o.finishes_at" class="flex items-center gap-1 text-sm font-semibold tabular-nums">
<IconUi name="clock" class="size-3.5 text-ink-400" />
{{ formatDuration(secondsUntil(o.finishes_at)) }}
</span>
<button
type="button"
class="btn btn-danger p-1.5"
:title="$t('village.queue.cancel')"
:aria-label="$t('village.queue.cancel')"
:disabled="busy"
@click="emit('cancel', o.id)"
>
<IconUi name="close" class="size-4" />
</button>
</div>
<UiBar v-if="o.finishes_at" class="mt-2.5" :value="progress(o)" />
</li>
</ol>
</div>
</section>
</template>