game-village-politics-frontend/app/components/market/Amounts.vue

54 lines
2.2 KiB
Vue

<script setup lang="ts">
import type { Cost, Resources, TradeResource } from '~/types/game'
const props = defineProps<{
id: string
resources: TradeResource[]
carry: number
/** Merchants to count against: free ones for a send, all of them for a route. */
merchants: number
/** The projected stock: fill buttons take what it and the merchants allow. None on routes. */
stock?: Resources | null
}>()
/** Amounts per resource; empty fields count as 0. */
const amounts = defineModel<Partial<Cost>>({ required: true })
const amount = (r: TradeResource) => Math.max(0, Math.floor(Number(amounts.value[r]) || 0))
const total = computed(() => props.resources.reduce((n, r) => n + amount(r), 0))
/** Merchants that many resources take, one load of `carry` each. */
const needed = computed(() => Math.ceil(total.value / props.carry))
/** The most of a resource the stock holds and the merchants carry next to the other amounts. */
function most(r: TradeResource) {
if (!props.stock) return 0
const room = props.merchants * props.carry - (total.value - amount(r))
return Math.max(0, Math.min(Math.floor(props.stock[r]), room))
}
</script>
<!-- One amount field per resource, a "(max)" fill button by stock and merchants, and the merchants it takes. -->
<template>
<div class="space-y-1.5">
<div class="flex flex-wrap gap-x-5 gap-y-1.5">
<div v-for="r in resources" :key="r" class="flex items-center gap-1.5">
<IconResource :resource="r" class="size-5" :title="$t(`resources.${r}`)" />
<input
:id="`${id}-${r}`"
v-model.number="amounts[r]"
type="number"
min="0"
inputmode="numeric"
class="input w-20 px-1.5 py-0.5 text-right tabular-nums"
:aria-label="$t('market.amount_of', { resource: $t(`resources.${r}`) })"
/>
<button v-if="stock" type="button" class="btn-fill" :disabled="!most(r)" @click="amounts[r] = most(r)">({{ most(r) }})</button>
</div>
</div>
<p class="text-sm" :class="needed > merchants ? 'text-blood-700' : 'text-ink-700'">
<IconUi name="cart" class="mr-1 inline size-4 align-[-3px]" />
{{ $t(stock ? 'market.merchants_needed' : 'market.merchants_per_run', { needed, merchants }) }}
</p>
</div>
</template>