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

205 lines
9.7 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { MilitaryBuilding, RecruitOption, RecruitOrder, Resources, UnitType } from '~/types/game'
const props = defineProps<{
military: MilitaryBuilding[]
troops: Partial<Record<UnitType, number>>
/** Villagers left to train; every unit takes `upkeep` of them. */
villagers: number
resources: Resources
secondsUntil: (iso: string) => number
busy: boolean
/** Sends the orders; resolves false when the server refused one. */
recruit: (buildingId: number, orders: { unit: UnitType; count: number }[]) => Promise<boolean>
}>()
const emit = defineEmits<{ cancel: [orderId: number] }>()
/** Building whose tab is open, by id. */
const selected = defineModel<number | null>('selected', { default: null })
const { t } = useI18n()
// Like the Die Stämme barracks: the queue on top, then one row per unit with an amount
// field and its (max), and one button that recruits every filled-in row. Each military
// building instance has its own tab, since each one trains in its own queue.
const current = computed(() => props.military.find((m) => m.building_id === selected.value) ?? props.military[0] ?? null)
const v = useVillageContextOrNull()
/** The building's name as everywhere else: copies in the server's rank ("Barracks, copy 2"). */
function tabName(m: MilitaryBuilding) {
const village = v?.village.value
const building = village?.buildings.find((b) => b.id === m.building_id)
return village && building ? buildingName(village, building, t) : t(`buildings.${m.type}`)
}
const amounts = reactive<Partial<Record<UnitType, number>>>({})
watch(
() => current.value?.building_id,
() => Object.keys(amounts).forEach((k) => delete amounts[k as UnitType]),
)
/** Most of this unit the projected resources and the villagers allow right now. */
function max(o: RecruitOption) {
if (o.blocked) return 0
const byResources = (['wood', 'clay', 'iron'] as const).map((r) =>
o.cost[r] > 0 ? Math.floor(props.resources[r] / o.cost[r]) : Infinity,
)
return Math.max(0, Math.min(Math.floor(props.villagers / o.upkeep), ...byResources))
}
const orders = computed(() =>
(current.value?.units ?? [])
.map((o) => ({ unit: o.unit, count: Math.floor(Number(amounts[o.unit]) || 0) }))
.filter((o) => o.count > 0),
)
async function submit() {
if (!current.value || !orders.value.length) return
if (await props.recruit(current.value.building_id, orders.value)) {
Object.keys(amounts).forEach((k) => delete amounts[k as UnitType])
}
}
/** Units of an order still to come and the wait for the next one, from its finish time. */
function progress(o: RecruitOrder) {
const remaining = props.secondsUntil(o.finishes_at)
const left = Math.min(o.count - o.done, Math.ceil(remaining / o.seconds_per_unit))
return { done: o.count - left, next: left > 0 ? remaining - (left - 1) * o.seconds_per_unit : 0, remaining }
}
</script>
<template>
<section v-if="current" id="recruit" class="panel scroll-mt-20 overflow-hidden">
<div class="panel-head">
<div class="flex flex-wrap items-center gap-1" role="tablist">
<button
v-for="m in military"
:key="m.building_id"
type="button"
role="tab"
:aria-selected="m === current"
class="inline-flex items-center gap-1 border border-wood-800 px-2 py-0.5 text-sm font-semibold"
:class="m === current ? 'bg-wood-800 text-gold-400' : 'bg-parchment-200 text-wood-950 hover:bg-parchment-100'"
@click="selected = m.building_id"
>
<IconBuilding :type="m.type" class="size-4" />
{{ tabName(m) }}
<span class="font-normal">({{ m.level }})</span>
</button>
</div>
<span class="badge badge-plain">
<IconUi name="users" class="size-3.5" />
{{ $t('village.recruit.villagers', { count: villagers }) }}
</span>
</div>
<div v-if="current.queue.length" class="overflow-x-auto border-b-2 border-wood-700">
<table class="w-full min-w-[30rem] border-collapse text-sm">
<thead>
<tr class="border-b border-wood-700 bg-parchment-100 text-left text-xs font-semibold whitespace-nowrap text-ink-700">
<th class="px-3 py-1.5">{{ $t('village.recruit.training') }}</th>
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.next') }}</th>
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.all_done') }}</th>
<th class="w-10 px-3 py-1.5"><span class="sr-only">{{ $t('village.recruit.cancel') }}</span></th>
</tr>
</thead>
<tbody>
<tr v-for="(o, i) in current.queue" :key="o.id" class="border-b border-parchment-300 last:border-b-0" :class="i === 0 ? 'bg-parchment-50' : 'bg-parchment-100'">
<td class="px-3 py-1">
<div class="flex items-center gap-2">
<IconUnit :unit="o.unit" class="size-5 text-wood-800" />
<span class="font-semibold">{{ $t('village.recruit.order', { count: o.count, unit: $t(`units.${o.unit}`) }) }}</span>
<span class="text-xs text-ink-500 tabular-nums">{{ $t('village.recruit.done', { done: progress(o).done, count: o.count }) }}</span>
</div>
<UiBar v-if="i === 0" class="mt-1 mb-0.5" :value="progress(o).done / o.count" />
</td>
<td class="px-2 py-1 text-right tabular-nums whitespace-nowrap">{{ formatDuration(progress(o).next) }}</td>
<td class="px-2 py-1 text-right tabular-nums whitespace-nowrap">{{ formatDuration(progress(o).remaining) }}</td>
<td class="px-3 py-1 text-right">
<button
type="button"
class="btn btn-danger p-1"
:title="$t('village.recruit.cancel')"
:aria-label="$t('village.recruit.cancel')"
:disabled="busy"
@click="emit('cancel', o.id)"
>
<IconUi name="close" class="size-3.5" />
</button>
</td>
</tr>
</tbody>
</table>
</div>
<form class="overflow-x-auto" @submit.prevent="submit">
<table class="w-full min-w-[36rem] border-collapse text-sm">
<thead>
<tr class="border-b-2 border-wood-700 bg-parchment-100 text-left text-xs font-semibold whitespace-nowrap text-ink-700">
<th class="px-3 py-1.5">{{ $t('village.recruit.unit') }}</th>
<th class="px-2 py-1.5">{{ $t('village.recruit.cost') }}</th>
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.upkeep') }}</th>
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.time') }}</th>
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.at_home') }}</th>
<th class="px-3 py-1.5 text-right">{{ $t('village.recruit.amount') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="o in current.units" :key="o.unit" 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" :class="o.blocked ? 'opacity-60' : ''">
<IconUnit :unit="o.unit" class="size-6 text-wood-800" />
<span class="font-semibold whitespace-nowrap">{{ $t(`units.${o.unit}`) }}</span>
</div>
</td>
<template v-if="!o.blocked">
<td class="px-2 py-1"><UiCost :cost="o.cost" :have="resources" nowrap /></td>
<td class="px-2 py-1 text-right">
<span class="inline-flex items-center gap-1 text-xs tabular-nums whitespace-nowrap">
<IconResource resource="food" class="size-4" />
{{ $t('village.recruit.per_hour', { value: o.upkeep }) }}
</span>
</td>
<td class="px-2 py-1 text-right tabular-nums whitespace-nowrap">{{ formatDuration(o.seconds) }}</td>
<td class="px-2 py-1 text-right tabular-nums">{{ troops[o.unit] ?? 0 }}</td>
<td class="px-3 py-1">
<div class="flex items-center justify-end gap-1.5">
<input
v-model.number="amounts[o.unit]"
type="number"
min="0"
:max="max(o)"
inputmode="numeric"
class="input w-16 px-1.5 py-0.5 text-right tabular-nums"
:aria-label="$t('village.recruit.amount_of', { unit: $t(`units.${o.unit}`) })"
/>
<button
type="button"
class="btn-fill"
:title="$t('village.recruit.max_title')"
:disabled="!max(o)"
@click="amounts[o.unit] = max(o)"
>
({{ max(o) }})
</button>
</div>
</td>
</template>
<td v-else colspan="5" class="px-2 py-1 text-ink-500">
<span class="mr-2 text-xs">{{ $t('village.build.requires') }}</span>
<span v-for="r in o.requires" :key="r.type" class="mr-3 inline-flex items-center gap-1 whitespace-nowrap">
<IconBuildingTile :type="r.type" class="size-5" />
{{ $t('village.build.requirement', { type: $t(`buildings.${r.type}`), level: r.level }) }}
</span>
</td>
</tr>
</tbody>
</table>
<div class="flex items-center justify-end gap-3 border-t-2 border-wood-700 bg-parchment-100 px-3 py-2">
<button type="submit" class="btn btn-primary min-w-[7rem]" :disabled="busy || !orders.length">
<IconUnit unit="spear" class="size-4" />
{{ $t('village.recruit.submit') }}
</button>
</div>
</form>
</section>
</template>