136 lines
6.3 KiB
Vue
136 lines
6.3 KiB
Vue
|
|
<script setup lang="ts">
|
|||
|
|
import type { MarketOffers, MarketState, Resources, TradeResource, WorldOffer } from '~/types/game'
|
|||
|
|
import type { MarketContext } from '~/composables/useMarket'
|
|||
|
|
|
|||
|
|
const props = defineProps<{
|
|||
|
|
market: NonNullable<MarketState['market']>
|
|||
|
|
stock: Resources
|
|||
|
|
worldId: number
|
|||
|
|
busy: boolean
|
|||
|
|
offers: MarketContext['offers']
|
|||
|
|
accept: MarketContext['accept']
|
|||
|
|
}>()
|
|||
|
|
const { t } = useI18n()
|
|||
|
|
|
|||
|
|
const give = ref<TradeResource | null>(null)
|
|||
|
|
const want = ref<TradeResource | null>(null)
|
|||
|
|
const list = ref<MarketOffers | null>(null)
|
|||
|
|
const loading = ref(false)
|
|||
|
|
/** The count typed per offer; 1 until changed. */
|
|||
|
|
const counts = reactive<Record<number, number>>({})
|
|||
|
|
|
|||
|
|
async function load() {
|
|||
|
|
loading.value = true
|
|||
|
|
const filters = [give.value, want.value]
|
|||
|
|
const result = await props.offers(give.value, want.value)
|
|||
|
|
// Drop answers for filters that changed meanwhile.
|
|||
|
|
if (filters[0] === give.value && filters[1] === want.value) list.value = result
|
|||
|
|
loading.value = false
|
|||
|
|
}
|
|||
|
|
watch([give, want], load, { immediate: true })
|
|||
|
|
|
|||
|
|
const count = (o: WorldOffer) => Math.max(1, Math.min(o.count, Math.floor(Number(counts[o.id]) || 1)))
|
|||
|
|
/** Why an offer can't be taken that many times right now, or null. */
|
|||
|
|
function blocked(o: WorldOffer) {
|
|||
|
|
const n = count(o)
|
|||
|
|
if (o.want.amount * n > Math.floor(props.stock[o.want.resource])) return t('market.errors.resources')
|
|||
|
|
if (Math.ceil(o.want.amount / props.market.carry) * n > props.market.free) return t('market.errors.merchants')
|
|||
|
|
return null
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async function take(o: WorldOffer) {
|
|||
|
|
if (props.busy || blocked(o)) return
|
|||
|
|
if (await props.accept(o.id, count(o))) {
|
|||
|
|
delete counts[o.id]
|
|||
|
|
load()
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const mapLink = (o: WorldOffer) => ({ path: `/map/${props.worldId}`, query: { x: o.village.x, y: o.village.y } })
|
|||
|
|
</script>
|
|||
|
|
|
|||
|
|
<!-- The world's open offers, nearest first: what each gives and wants, how often, how far; take one or several. -->
|
|||
|
|
<template>
|
|||
|
|
<section class="panel">
|
|||
|
|
<div class="panel-head">
|
|||
|
|
<h2 class="panel-title">{{ $t('market.offers.title') }}</h2>
|
|||
|
|
<button type="button" class="btn btn-outline px-2 py-0.5 text-xs" :disabled="loading" @click="load">{{ $t('market.offers.refresh') }}</button>
|
|||
|
|
</div>
|
|||
|
|
<div class="panel-body space-y-2">
|
|||
|
|
<div class="flex flex-wrap items-center gap-x-4 gap-y-1.5 text-sm">
|
|||
|
|
<label class="flex items-center gap-1.5">
|
|||
|
|
<span class="font-semibold text-ink-700">{{ $t('market.offers.gives') }}</span>
|
|||
|
|
<select v-model="give" class="input w-auto py-0.5">
|
|||
|
|
<option :value="null">{{ $t('market.offers.any') }}</option>
|
|||
|
|
<option v-for="r in market.resources" :key="r" :value="r">{{ $t(`resources.${r}`) }}</option>
|
|||
|
|
</select>
|
|||
|
|
</label>
|
|||
|
|
<label class="flex items-center gap-1.5">
|
|||
|
|
<span class="font-semibold text-ink-700">{{ $t('market.offers.wants') }}</span>
|
|||
|
|
<select v-model="want" class="input w-auto py-0.5">
|
|||
|
|
<option :value="null">{{ $t('market.offers.any') }}</option>
|
|||
|
|
<option v-for="r in market.resources" :key="r" :value="r">{{ $t(`resources.${r}`) }}</option>
|
|||
|
|
</select>
|
|||
|
|
</label>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<p v-if="!list" class="text-sm text-ink-500">{{ $t('common.loading') }}</p>
|
|||
|
|
<p v-else-if="!list.offers.length" class="text-sm text-ink-500">{{ $t('market.offers.none') }}</p>
|
|||
|
|
<table v-else class="grid-table">
|
|||
|
|
<thead>
|
|||
|
|
<tr>
|
|||
|
|
<th>{{ $t('market.offers.gives') }}</th>
|
|||
|
|
<th>{{ $t('market.offers.wants') }}</th>
|
|||
|
|
<th class="hidden text-right @lg:table-cell">{{ $t('market.offers.count') }}</th>
|
|||
|
|
<th>{{ $t('market.village') }}</th>
|
|||
|
|
<th class="hidden text-right @xl:table-cell">{{ $t('market.duration') }}</th>
|
|||
|
|
<th class="text-right">{{ $t('market.offers.take') }}</th>
|
|||
|
|
</tr>
|
|||
|
|
</thead>
|
|||
|
|
<tbody>
|
|||
|
|
<tr v-for="o in list.offers" :key="o.id" :class="o.own ? 'bg-parchment-100' : ''">
|
|||
|
|
<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>
|
|||
|
|
<span class="ml-1 text-xs text-ink-500 @lg:hidden">×{{ o.count }}</span>
|
|||
|
|
</td>
|
|||
|
|
<td class="hidden text-right tabular-nums @lg:table-cell">{{ o.count }}</td>
|
|||
|
|
<td class="min-w-0">
|
|||
|
|
<NuxtLink :to="mapLink(o)" class="font-semibold hover:underline">{{ villageLabel(o.village) }}</NuxtLink>
|
|||
|
|
<span class="block text-xs text-ink-500">
|
|||
|
|
{{ o.village.player?.name }}
|
|||
|
|
<span class="tabular-nums @xl:hidden">· {{ formatDuration(o.seconds) }}</span>
|
|||
|
|
</span>
|
|||
|
|
</td>
|
|||
|
|
<td class="hidden text-right whitespace-nowrap tabular-nums @xl:table-cell">
|
|||
|
|
{{ formatDuration(o.seconds) }}
|
|||
|
|
<span class="block text-xs text-ink-500">{{ $t('market.fields', { fields: o.distance.toFixed(1) }) }}</span>
|
|||
|
|
</td>
|
|||
|
|
<td class="text-right">
|
|||
|
|
<span v-if="o.own" class="badge badge-plain">{{ $t('market.offers.own') }}</span>
|
|||
|
|
<span v-else class="inline-flex items-center justify-end gap-1">
|
|||
|
|
<input
|
|||
|
|
v-model.number="counts[o.id]"
|
|||
|
|
type="number"
|
|||
|
|
min="1"
|
|||
|
|
:max="o.count"
|
|||
|
|
inputmode="numeric"
|
|||
|
|
placeholder="1"
|
|||
|
|
class="input w-11 px-1 py-0.5 text-right tabular-nums"
|
|||
|
|
:aria-label="$t('market.offers.count_of', { village: villageLabel(o.village) })"
|
|||
|
|
/>
|
|||
|
|
<button type="button" class="btn btn-primary px-2 py-0.5" :disabled="busy || !!blocked(o)" :title="blocked(o) ?? undefined" @click="take(o)">
|
|||
|
|
{{ $t('market.offers.accept') }}
|
|||
|
|
</button>
|
|||
|
|
</span>
|
|||
|
|
</td>
|
|||
|
|
</tr>
|
|||
|
|
</tbody>
|
|||
|
|
</table>
|
|||
|
|
<p class="text-xs text-ink-500">{{ $t('market.offers.hint') }}</p>
|
|||
|
|
</div>
|
|||
|
|
</section>
|
|||
|
|
</template>
|