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

95 lines
3.8 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { Levy, UnitType } from '~/types/game'
const props = defineProps<{
troops: Partial<Record<UnitType, number>>
training: number
levy: Levy
/** Villagers a new levy can draft. */
villagers: number
secondsUntil: (iso: string) => number
busy: boolean
}>()
const emit = defineEmits<{ levy: [count: number]; endLevy: [] }>()
const home = computed(() => (Object.entries(props.troops) as [UnitType, number][]).filter(([, n]) => n > 0))
const draft = ref<number | null>(null)
const draftCount = computed(() => Math.min(props.villagers, Math.floor(Number(draft.value) || 0)))
function submitLevy() {
if (draftCount.value < 1) return
emit('levy', draftCount.value)
draft.value = null
}
</script>
<!-- Own units in the village, like the Die Stämme overview's unit box, and the levy. -->
<template>
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('village.troops.title') }}</h2>
<span v-if="training" class="badge badge-plain">
<IconUi name="clock" class="size-3.5" />
{{ $t('village.troops.training', { count: training }) }}
</span>
</div>
<div class="panel-body">
<ul v-if="home.length || levy.militia" class="divide-y divide-parchment-300">
<li v-for="[unit, count] in home" :key="unit" class="flex items-center gap-2 py-1 text-sm">
<IconUnit :unit="unit" class="size-5 text-wood-800" />
<span class="flex-1">{{ $t(`units.${unit}`) }}</span>
<span class="font-semibold tabular-nums">{{ count }}</span>
</li>
<li v-if="levy.militia" class="flex items-center gap-2 py-1 text-sm">
<IconUnit unit="militia" class="size-5 text-wood-800" />
<span class="flex-1">{{ $t('village.levy.militia') }}</span>
<span class="font-semibold tabular-nums">{{ levy.militia }}</span>
</li>
</ul>
<p v-else class="py-2 text-center text-sm text-ink-500">{{ $t('village.troops.none') }}</p>
<!-- Levy: draft villagers as militia for a fixed time. -->
<div class="mt-4 border-t-2 border-parchment-300 pt-3">
<div v-if="levy.militia && levy.until" class="flex items-center gap-2 text-sm">
<IconUi name="clock" class="size-3.5 text-ink-400" />
<span class="flex-1">
{{ $t('village.levy.returns_in') }}
<span class="font-semibold tabular-nums">{{ formatDuration(secondsUntil(levy.until)) }}</span>
</span>
<button type="button" class="btn btn-outline px-2.5 py-1" :disabled="busy" @click="emit('endLevy')">
{{ $t('village.levy.end') }}
</button>
</div>
<form v-else class="space-y-2" @submit.prevent="submitLevy">
<div class="flex items-center gap-2">
<label for="levy-count" class="flex-1 text-sm font-semibold">{{ $t('village.levy.title') }}</label>
<input
id="levy-count"
v-model.number="draft"
type="number"
min="0"
:max="villagers"
inputmode="numeric"
class="input w-16 px-1.5 py-0.5 text-right tabular-nums"
/>
<button
type="button"
class="btn-fill"
:title="$t('village.levy.all')"
:disabled="!villagers"
@click="draft = villagers"
>
({{ villagers }})
</button>
<button type="submit" class="btn btn-primary px-2.5 py-1" :disabled="busy || draftCount < 1">
{{ $t('village.levy.submit') }}
</button>
</div>
<p class="text-xs text-ink-500">{{ $t('village.levy.hint', { duration: formatDuration(levy.seconds) }) }}</p>
</form>
</div>
</div>
</section>
</template>