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

70 lines
2.6 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { TradePlan } from '~/types/game'
const props = defineProps<{
id: string
now: number
preview: (x: number, y: number) => Promise<{ plan: TradePlan | null; refusal: string | null }>
}>()
/** The field as typed, "x|y". */
const target = defineModel<string>({ required: true })
/** What the server says about the field: the village there and the walk. */
const plan = defineModel<TradePlan | null>('plan', { default: null })
const { t } = useI18n()
const coords = computed(() => parseCoords(target.value))
const refusal = ref<string | null>(null)
// Asked again a moment after each change; answers for a field that changed meanwhile are dropped.
let timer: ReturnType<typeof setTimeout> | undefined
watch(
coords,
(c) => {
clearTimeout(timer)
plan.value = null
refusal.value = null
if (!c) return
timer = setTimeout(async () => {
const result = await props.preview(c.x, c.y)
if (coords.value?.x !== c.x || coords.value?.y !== c.y) return
plan.value = result.plan
refusal.value = result.refusal
}, 250)
},
{ immediate: true },
)
onBeforeUnmount(() => clearTimeout(timer))
</script>
<!-- The target field, typed like the rally point's, and what the server says about it: the village, the distance and the walk. -->
<template>
<div class="space-y-2">
<div class="flex flex-wrap items-center gap-x-3 gap-y-1">
<label :for="id" class="label mb-0 w-24">{{ $t('market.target') }}</label>
<input
:id="id"
v-model="target"
type="text"
inputmode="numeric"
autocomplete="off"
:placeholder="$t('market.target_placeholder')"
class="input w-32 tabular-nums"
/>
</div>
<dl v-if="plan" class="grid grid-cols-[auto_1fr] gap-x-4 gap-y-0.5 border border-parchment-300 bg-parchment-50 px-2 py-1.5 text-sm">
<dt class="text-ink-500">{{ $t('market.village') }}</dt>
<dd class="min-w-0 font-semibold">
{{ villageLabel(plan.target) }}
<span class="font-normal text-ink-500">{{ plan.target.player?.name }}</span>
</dd>
<dt class="text-ink-500">{{ $t('market.distance') }}</dt>
<dd class="tabular-nums">{{ $t('market.fields', { fields: plan.distance.toFixed(1) }) }}</dd>
<dt class="text-ink-500">{{ $t('market.duration') }}</dt>
<dd class="tabular-nums">{{ formatDuration(plan.seconds) }}</dd>
<dt class="text-ink-500">{{ $t('market.arrival') }}</dt>
<dd class="tabular-nums">{{ formatArrival(now + plan.seconds * 1000, now, t) }}</dd>
</dl>
<p v-else-if="refusal" class="text-sm text-blood-700">{{ refusal }}</p>
</div>
</template>