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

152 lines
7.7 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { Cost, MarketState, TradePlan, TradeRoute } from '~/types/game'
import type { MarketContext } from '~/composables/useMarket'
const props = defineProps<{
market: NonNullable<MarketState['market']>
routes: TradeRoute[]
worldId: number
now: number
busy: boolean
secondsUntil: (iso: string) => number
preview: MarketContext['preview']
create: MarketContext['routeCreate']
update: MarketContext['routeUpdate']
remove: MarketContext['routeDelete']
}>()
const { t } = useI18n()
const limits = computed(() => props.market.route)
const clampEvery = (s: number) => Math.min(limits.value.max_every, Math.max(limits.value.min_every, s))
const n = (v: unknown) => Math.max(0, Math.floor(Number(v) || 0))
const cost = (a: Partial<Cost>): Cost => ({ wood: n(a.wood), clay: n(a.clay), iron: n(a.iron) })
const sum = (c: Cost) => c.wood + c.clay + c.iron
const inRange = (s: number) => s >= limits.value.min_every && s <= limits.value.max_every
// A new route: target, amounts per run, interval.
const target = ref('')
const plan = ref<TradePlan | null>(null)
const amounts = ref<Partial<Cost>>({})
const every = ref(clampEvery(3600))
const full = computed(() => props.routes.length >= limits.value.max_routes)
const canCreate = computed(() => !props.busy && !full.value && !!plan.value && sum(cost(amounts.value)) > 0 && inRange(every.value))
async function submit() {
const c = parseCoords(target.value)
if (!c || !canCreate.value) return
if (await props.create(c.x, c.y, cost(amounts.value), every.value)) {
target.value = ''
amounts.value = {}
}
}
// One route at a time is edited in its own row.
const editing = ref<number | null>(null)
const editAmounts = ref<Partial<Cost>>({})
const editEvery = ref(3600)
function edit(r: TradeRoute) {
editing.value = r.id
editAmounts.value = { ...r.resources }
editEvery.value = r.every
}
async function save(r: TradeRoute) {
const resources = cost(editAmounts.value)
if (!sum(resources) || !inRange(editEvery.value)) return
editing.value = null
await props.update(r.id, { ...resources, every: editEvery.value })
}
const mapLink = (r: TradeRoute) => ({ path: `/map/${props.worldId}`, query: { x: r.to.x, y: r.to.y } })
</script>
<!-- Trade routes: each one's target, what it sends and how often, the next run and the last delivery; pause, edit, delete; and a new one. -->
<template>
<div class="grid gap-3 @4xl:grid-cols-[minmax(0,1fr)_minmax(0,22rem)]">
<section class="panel self-start">
<div class="panel-head">
<h2 class="panel-title">{{ $t('market.routes.title') }}</h2>
<span class="badge badge-plain tabular-nums">{{ $t('market.routes.used', { used: routes.length, max: limits.max_routes }) }}</span>
</div>
<div class="panel-body space-y-2">
<table v-if="routes.length" class="grid-table">
<thead>
<tr>
<th>{{ $t('market.routes.route') }}</th>
<th>{{ $t('market.routes.next') }}</th>
<th />
</tr>
</thead>
<tbody>
<template v-for="r in routes" :key="r.id">
<tr :class="r.paused ? 'bg-parchment-100 text-ink-500' : ''">
<td class="py-1">
<NuxtLink :to="mapLink(r)" class="font-semibold text-ink-900 hover:underline">{{ villageLabel(r.to) }}</NuxtLink>
<span class="ml-1 text-xs text-ink-500">{{ r.to.player?.name }}</span>
<span class="mt-0.5 flex flex-wrap items-center gap-x-2 text-xs text-ink-700">
<MarketCargo :cargo="r.resources" />
<span class="inline-flex items-center gap-0.5 whitespace-nowrap"><IconUi name="repeat" class="size-3.5" />{{ $t('market.routes.every_n', { every: formatInterval(r.every, t) }) }}</span>
</span>
</td>
<td class="py-1 text-sm">
<span v-if="r.paused" class="badge badge-gold">{{ $t('market.routes.paused') }}</span>
<span v-else class="font-semibold whitespace-nowrap tabular-nums">{{ formatDuration(secondsUntil(r.next_at)) }}</span>
<span v-if="r.paused_reason" class="block text-xs text-blood-700">{{ $t(`market.routes.reason.${r.paused_reason}`) }}</span>
<span class="mt-0.5 block text-xs text-ink-500">
<template v-if="r.last_sent_at">
{{ $t('market.routes.last', { when: formatArrival(Date.parse(r.last_sent_at), now, t) }) }}
<MarketCargo v-if="r.last_cargo && sum(r.last_cargo)" :cargo="r.last_cargo" />
<span v-else class="text-blood-700">{{ $t('market.routes.nothing_sent') }}</span>
</template>
<template v-else>{{ $t('market.routes.never') }}</template>
</span>
</td>
<td class="py-1 text-right">
<span class="inline-flex flex-wrap justify-end gap-1">
<button type="button" class="btn btn-outline px-1.5 py-0.5 text-xs" @click="update(r.id, { paused: !r.paused })">
{{ r.paused ? $t('market.routes.resume') : $t('market.routes.pause') }}
</button>
<button type="button" class="btn btn-outline px-1.5 py-0.5 text-xs" :aria-expanded="editing === r.id" @click="editing === r.id ? (editing = null) : edit(r)">
{{ $t('market.routes.edit') }}
</button>
<button type="button" class="btn btn-outline px-1.5 py-0.5 text-xs text-blood-700" @click="remove(r.id)">{{ $t('market.routes.delete') }}</button>
</span>
</td>
</tr>
<tr v-if="editing === r.id">
<td colspan="3" class="bg-parchment-100 py-2">
<form class="space-y-2" @submit.prevent="save(r)">
<MarketAmounts :id="`route-${r.id}`" v-model="editAmounts" :resources="market.resources" :carry="market.carry" :merchants="market.merchants" />
<MarketInterval :id="`route-${r.id}-every`" v-model="editEvery" :min="limits.min_every" :max="limits.max_every" />
<div class="flex gap-1.5">
<button type="submit" class="btn btn-primary" :disabled="!sum(cost(editAmounts)) || !inRange(editEvery)">{{ $t('market.routes.save') }}</button>
<button type="button" class="btn btn-outline" @click="editing = null">{{ $t('market.routes.cancel') }}</button>
</div>
</form>
</td>
</tr>
</template>
</tbody>
</table>
<p v-else class="text-sm text-ink-500">{{ $t('market.routes.none') }}</p>
<p class="text-xs text-ink-500">{{ $t('market.routes.hint') }}</p>
</div>
</section>
<section class="panel self-start">
<div class="panel-head">
<h2 class="panel-title">{{ $t('market.routes.create') }}</h2>
</div>
<form class="panel-body space-y-3" @submit.prevent="submit">
<p v-if="full" class="text-sm text-blood-700">{{ $t('market.routes.full', { max: limits.max_routes }) }}</p>
<MarketTarget id="route-target" v-model="target" v-model:plan="plan" :now="now" :preview="preview" />
<MarketAmounts id="route-new" v-model="amounts" :resources="market.resources" :carry="market.carry" :merchants="market.merchants" />
<MarketInterval id="route-every" v-model="every" :min="limits.min_every" :max="limits.max_every" />
<button type="submit" class="btn btn-primary" :disabled="!canCreate">
<IconUi name="repeat" class="size-4" />
{{ $t('market.routes.submit') }}
</button>
</form>
</section>
</div>
</template>