127 lines
6.7 KiB
Vue
127 lines
6.7 KiB
Vue
<script setup lang="ts">
|
|
import type { MarketState, OwnOffer, Resources, TradeResource } from '~/types/game'
|
|
import type { MarketContext } from '~/composables/useMarket'
|
|
|
|
const props = defineProps<{
|
|
market: NonNullable<MarketState['market']>
|
|
offers: OwnOffer[]
|
|
stock: Resources
|
|
now: number
|
|
busy: boolean
|
|
offer: MarketContext['offer']
|
|
cancel: MarketContext['cancelOffer']
|
|
}>()
|
|
const { t } = useI18n()
|
|
|
|
const give = ref<TradeResource>(props.market.resources[0] ?? 'wood')
|
|
const want = ref<TradeResource>(props.market.resources[1] ?? 'clay')
|
|
const giveAmount = ref<number | ''>(1000)
|
|
const wantAmount = ref<number | ''>(1000)
|
|
const count = ref<number | ''>(1)
|
|
|
|
const n = (v: number | '') => Math.max(0, Math.floor(Number(v) || 0))
|
|
const merchants = computed(() => Math.ceil(n(giveAmount.value) / props.market.carry) * n(count.value))
|
|
const reserved = computed(() => n(giveAmount.value) * n(count.value))
|
|
/** Wanted per given, as the ratio line shows it. */
|
|
const ratio = computed(() => (n(giveAmount.value) ? n(wantAmount.value) / n(giveAmount.value) : 0))
|
|
const outOfRatio = computed(() => {
|
|
const g = n(giveAmount.value)
|
|
const w = n(wantAmount.value)
|
|
return !!g && !!w && (w > g * props.market.max_ratio || g > w * props.market.max_ratio)
|
|
})
|
|
const problem = computed(() => {
|
|
if (give.value === want.value) return t('market.errors.same_resource')
|
|
if (outOfRatio.value) return t('market.errors.ratio')
|
|
if (reserved.value > Math.floor(props.stock[give.value])) return t('market.errors.resources')
|
|
if (merchants.value > props.market.free) return t('market.errors.merchants')
|
|
return null
|
|
})
|
|
const canPost = computed(() => !props.busy && n(giveAmount.value) > 0 && n(wantAmount.value) > 0 && n(count.value) > 0 && !problem.value)
|
|
|
|
async function submit() {
|
|
if (!canPost.value) return
|
|
if (await props.offer({ resource: give.value, amount: n(giveAmount.value) }, { resource: want.value, amount: n(wantAmount.value) }, n(count.value))) count.value = 1
|
|
}
|
|
</script>
|
|
|
|
<!-- Own offers: post one (what to give, what for, how often; resources and merchants are held for it) and the open ones with Cancel. -->
|
|
<template>
|
|
<div class="grid gap-3 @4xl:grid-cols-[minmax(0,22rem)_minmax(0,1fr)]">
|
|
<section class="panel self-start">
|
|
<div class="panel-head">
|
|
<h2 class="panel-title">{{ $t('market.mine.create') }}</h2>
|
|
</div>
|
|
<form class="panel-body space-y-2.5 text-sm" @submit.prevent="submit">
|
|
<div class="grid grid-cols-[5.5rem_auto_1fr] items-center gap-x-2 gap-y-1.5">
|
|
<label for="offer-give" class="font-semibold text-ink-700">{{ $t('market.mine.give') }}</label>
|
|
<select id="offer-give" v-model="give" class="input w-auto py-0.5">
|
|
<option v-for="r in market.resources" :key="r" :value="r">{{ $t(`resources.${r}`) }}</option>
|
|
</select>
|
|
<input v-model.number="giveAmount" type="number" min="1" step="100" inputmode="numeric" class="input w-24 px-1.5 py-0.5 text-right tabular-nums" :aria-label="$t('market.mine.give_amount')" />
|
|
|
|
<label for="offer-want" class="font-semibold text-ink-700">{{ $t('market.mine.want') }}</label>
|
|
<select id="offer-want" v-model="want" class="input w-auto py-0.5">
|
|
<option v-for="r in market.resources" :key="r" :value="r">{{ $t(`resources.${r}`) }}</option>
|
|
</select>
|
|
<input v-model.number="wantAmount" type="number" min="1" step="100" inputmode="numeric" class="input w-24 px-1.5 py-0.5 text-right tabular-nums" :aria-label="$t('market.mine.want_amount')" />
|
|
|
|
<label for="offer-count" class="font-semibold text-ink-700">{{ $t('market.mine.count') }}</label>
|
|
<input id="offer-count" v-model.number="count" type="number" min="1" inputmode="numeric" class="input w-16 px-1.5 py-0.5 text-right tabular-nums" />
|
|
<span class="text-xs text-ink-500">{{ $t('market.mine.count_hint') }}</span>
|
|
</div>
|
|
|
|
<ul class="space-y-0.5 text-ink-700">
|
|
<li :class="outOfRatio ? 'text-blood-700' : ''">
|
|
{{ $t('market.mine.ratio', { ratio: ratio.toFixed(2), max: market.max_ratio }) }}
|
|
</li>
|
|
<li :class="merchants > market.free ? 'text-blood-700' : ''">
|
|
<IconUi name="cart" class="mr-1 inline size-4 align-[-3px]" />{{ $t('market.merchants_needed', { needed: merchants, merchants: market.free }) }}
|
|
</li>
|
|
<li :class="reserved > Math.floor(stock[give]) ? 'text-blood-700' : ''">
|
|
{{ $t('market.mine.reserved', { amount: reserved, resource: $t(`resources.${give}`) }) }}
|
|
</li>
|
|
</ul>
|
|
<p v-if="problem && n(giveAmount) && n(wantAmount)" class="text-blood-700">{{ problem }}</p>
|
|
<button type="submit" class="btn btn-primary" :disabled="!canPost">{{ $t('market.mine.submit') }}</button>
|
|
</form>
|
|
</section>
|
|
|
|
<section class="panel self-start">
|
|
<div class="panel-head">
|
|
<h2 class="panel-title">{{ $t('market.mine.open') }}</h2>
|
|
</div>
|
|
<div class="panel-body">
|
|
<table v-if="offers.length" class="grid-table">
|
|
<thead>
|
|
<tr>
|
|
<th>{{ $t('market.offers.gives') }}</th>
|
|
<th>{{ $t('market.offers.wants') }}</th>
|
|
<th class="text-right">{{ $t('market.offers.count') }}</th>
|
|
<th class="hidden text-right @lg:table-cell">{{ $t('market.mine.merchants') }}</th>
|
|
<th class="hidden @xl:table-cell">{{ $t('market.mine.posted') }}</th>
|
|
<th />
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-for="o in offers" :key="o.id">
|
|
<td class="whitespace-nowrap tabular-nums">
|
|
<span class="inline-flex items-center gap-1"><IconResource :resource="o.give.resource" class="size-4" :title="$t(`resources.${o.give.resource}`)" />{{ o.give.amount }}</span>
|
|
</td>
|
|
<td class="whitespace-nowrap tabular-nums">
|
|
<span class="inline-flex items-center gap-1"><IconResource :resource="o.want.resource" class="size-4" :title="$t(`resources.${o.want.resource}`)" />{{ o.want.amount }}</span>
|
|
</td>
|
|
<td class="text-right tabular-nums">{{ o.count }}</td>
|
|
<td class="hidden text-right tabular-nums @lg:table-cell">{{ o.count * o.merchants }}</td>
|
|
<td class="hidden whitespace-nowrap text-ink-700 tabular-nums @xl:table-cell">{{ formatArrival(Date.parse(o.created_at), now, t) }}</td>
|
|
<td class="text-right">
|
|
<button type="button" class="btn btn-outline px-2 py-0.5 text-xs" @click="cancel(o.id)">{{ $t('market.mine.cancel') }}</button>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p v-else class="text-sm text-ink-500">{{ $t('market.mine.none') }}</p>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
</template>
|