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

55 lines
2.2 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { Cost, MarketState, Resources, TradePlan } from '~/types/game'
import type { MarketContext } from '~/composables/useMarket'
const props = defineProps<{
market: NonNullable<MarketState['market']>
stock: Resources
now: number
busy: boolean
/** Field to aim at, e.g. from the map. */
initialTarget: { x: number; y: number } | null
preview: MarketContext['preview']
send: MarketContext['send']
}>()
const target = ref(props.initialTarget ? `${props.initialTarget.x}|${props.initialTarget.y}` : '')
const plan = ref<TradePlan | null>(null)
const amounts = ref<Partial<Cost>>({})
const cargo = computed<Cost>(() => ({
wood: Math.max(0, Math.floor(Number(amounts.value.wood) || 0)),
clay: Math.max(0, Math.floor(Number(amounts.value.clay) || 0)),
iron: Math.max(0, Math.floor(Number(amounts.value.iron) || 0)),
}))
const total = computed(() => cargo.value.wood + cargo.value.clay + cargo.value.iron)
const fits = computed(
() => Math.ceil(total.value / props.market.carry) <= props.market.free && props.market.resources.every((r) => cargo.value[r] <= Math.floor(props.stock[r])),
)
const coords = computed(() => parseCoords(target.value))
const canSend = computed(() => !props.busy && !!plan.value && total.value > 0 && fits.value)
async function submit() {
const c = coords.value
if (!c || !canSend.value) return
if (await props.send(c.x, c.y, cargo.value)) amounts.value = {}
}
</script>
<!-- Send resources: the target field with what the server says about it, an amount per resource, the merchants it takes, Send. -->
<template>
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('market.send.title') }}</h2>
</div>
<form class="panel-body space-y-3" @submit.prevent="submit">
<MarketTarget id="market-send-target" v-model="target" v-model:plan="plan" :now="now" :preview="preview" />
<MarketAmounts id="market-send" v-model="amounts" :resources="market.resources" :carry="market.carry" :merchants="market.free" :stock="stock" />
<button type="submit" class="btn btn-primary" :disabled="!canSend">
<IconUi name="cart" class="size-4" />
{{ $t('market.send.submit') }}
</button>
</form>
</section>
</template>