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

98 lines
3.9 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { UiIconName } from '~/components/icon/Ui.vue'
const route = useRoute()
const router = useRouter()
const v = useVillageContext()
const m = useMarket(v)
const { t } = useI18n()
const village = computed(() => v.village.value!)
const state = computed(() => m.state.value)
// The market's parts are tabs, like the rally point's: send, the world's offers, own
// offers, trade routes, merchants under way.
type Tab = 'send' | 'offers' | 'mine' | 'routes' | 'trades'
const OTHERS: Tab[] = ['offers', 'mine', 'routes', 'trades']
const tab = computed<Tab>({
get: () => (OTHERS.includes(route.query.tab as Tab) ? (route.query.tab as Tab) : 'send'),
set: (value) => router.replace({ query: { ...route.query, tab: value === 'send' ? undefined : value } }),
})
const tabs = computed(() => {
const s = state.value
return [
{ key: 'send' as const, label: t('market.tabs.send'), icon: 'cart' as UiIconName },
{ key: 'offers' as const, label: t('market.tabs.offers'), icon: 'globe' as UiIconName },
{ key: 'mine' as const, label: t('market.tabs.mine'), icon: 'edit' as UiIconName, count: s?.offers.length },
{ key: 'routes' as const, label: t('market.tabs.routes'), icon: 'repeat' as UiIconName, count: s?.routes.length },
{ key: 'trades' as const, label: t('market.tabs.trades'), icon: 'clock' as UiIconName, count: s?.trades.length },
]
})
/** `?x=&y=` from the map fills in the send form's target. */
const initialTarget = computed(() => (route.query.x && route.query.y ? { x: Number(route.query.x), y: Number(route.query.y) } : null))
</script>
<!-- The market's work: send resources, the world's offers, own offers, trade routes, merchants under way. -->
<template>
<div class="space-y-3">
<div class="flex flex-wrap items-center justify-between gap-2">
<UiSubTabs v-model="tab" :tabs="tabs" />
<span v-if="state?.market" class="inline-flex items-center gap-1 text-sm whitespace-nowrap tabular-nums" :class="state.market.free ? 'text-ink-700' : 'font-semibold text-blood-700'" :title="$t('market.merchants_title', { carry: state.market.carry })">
<IconUi name="cart" class="size-4" />
{{ $t('market.merchants_free', { free: state.market.free, merchants: state.market.merchants }) }}
</span>
</div>
<p v-if="m.error.value" class="alert" role="alert">{{ m.error.value }}</p>
<p v-if="!state" class="text-sm text-ink-500">{{ $t('common.loading') }}</p>
<p v-else-if="!state.market" class="text-sm text-ink-500">{{ $t('market.no_market') }}</p>
<template v-else>
<MarketSend
v-if="tab === 'send'"
:key="`${route.query.x}|${route.query.y}`"
:market="state.market"
:stock="v.resources.value!"
:now="v.now.value"
:busy="m.busy.value"
:initial-target="initialTarget"
:preview="m.preview"
:send="m.send"
/>
<MarketWorldOffers
v-else-if="tab === 'offers'"
:market="state.market"
:stock="v.resources.value!"
:world-id="village.world_id"
:busy="m.busy.value"
:offers="m.offers"
:accept="m.accept"
/>
<MarketMyOffers
v-else-if="tab === 'mine'"
:market="state.market"
:offers="state.offers"
:stock="v.resources.value!"
:now="v.now.value"
:busy="m.busy.value"
:offer="m.offer"
:cancel="m.cancelOffer"
/>
<MarketRoutes
v-else-if="tab === 'routes'"
:market="state.market"
:routes="state.routes"
:world-id="village.world_id"
:now="v.now.value"
:busy="m.busy.value"
:seconds-until="v.secondsUntil"
:preview="m.preview"
:create="m.routeCreate"
:update="m.routeUpdate"
:remove="m.routeDelete"
/>
<MarketTrades v-else :market="state.market" :trades="state.trades" :world-id="village.world_id" :now="v.now.value" :seconds-until="v.secondsUntil" />
</template>
</div>
</template>