game-village-politics-frontend/app/components/map/MarchCard.vue

67 lines
3.4 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
import type { UnitType } from '~/types/game'
import type { MapMovement } from '~/types/map'
const props = defineProps<{
march: MapMovement
/** The clock, for the countdowns. */
now: number
busy?: boolean
error?: string | null
}>()
const emit = defineEmits<{ close: []; cancel: []; center: [at: { x: number; y: number }] }>()
const { t } = useI18n()
const title = computed(() => {
const m = props.march
return t(`map.march.${m.raid ? 'raid' : m.type}`, { from: m.from.name, to: m.to.name })
})
const seconds = (iso: string) => Math.max(0, Math.ceil((Date.parse(iso) - props.now) / 1000))
const cancelLeft = computed(() => (props.march.cancel_until ? seconds(props.march.cancel_until) : 0))
const colour = computed(() => (props.march.raid ? MARCH_COLOURS.raid : MARCH_COLOURS[props.march.type]))
</script>
<!-- A group on the march, clicked on the map: where it comes from and goes, what it is, when it arrives, and calling it back. -->
<template>
<div class="w-64 rounded-sm border-2 border-wood-950 bg-parchment-50 text-sm text-ink-900 shadow-hard">
<div class="flex items-start gap-2 border-b border-wood-700 bg-parchment-200 px-3 py-1.5">
<span class="mt-1 inline-block h-1 w-4 shrink-0" :style="{ background: colour }" />
<p class="min-w-0 flex-1 font-semibold">{{ title }}</p>
<button type="button" class="text-ink-500 hover:text-blood-700" :aria-label="$t('common.close')" @click="emit('close')">
<IconUi name="close" class="size-4" />
</button>
</div>
<dl class="grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5 px-3 py-1.5 text-xs">
<dt class="text-ink-500">{{ $t('map.march.from') }}</dt>
<dd class="truncate">
<button type="button" class="hover:underline" @click="emit('center', march.from)">{{ march.from.name }} ({{ march.from.x }}|{{ march.from.y }})</button>
</dd>
<dt class="text-ink-500">{{ $t('map.march.to') }}</dt>
<dd class="truncate">
<button type="button" class="hover:underline" @click="emit('center', march.to)">{{ march.to.name }} ({{ march.to.x }}|{{ march.to.y }})</button>
</dd>
<template v-if="march.units">
<dt class="text-ink-500">{{ $t('map.march.troops') }}</dt>
<dd class="flex flex-wrap gap-x-2 gap-y-0.5">
<span v-for="[unit, count] in unitRows(march.units)" :key="unit" class="inline-flex items-center gap-1 tabular-nums">
<IconUnit :unit="unit as UnitType" class="size-3.5" />{{ count }}
</span>
</dd>
</template>
<template v-else>
<dt class="text-ink-500">{{ $t('map.march.size') }}</dt>
<dd>{{ $t(`map.march.sizes.${march.size}`) }}</dd>
</template>
<dt class="text-ink-500">{{ $t('map.march.arrives') }}</dt>
<dd class="tabular-nums">
{{ formatArrival(Date.parse(march.arrives_at), now, t) }} · <span class="font-semibold">{{ formatDuration(seconds(march.arrives_at)) }}</span>
</dd>
</dl>
<div v-if="cancelLeft > 0" class="flex items-center gap-2 border-t border-parchment-300 px-3 py-2">
<button type="button" class="btn btn-outline px-2 py-1 text-xs" :disabled="busy" @click="emit('cancel')">{{ $t('map.march.cancel') }}</button>
<span class="text-xs text-ink-500 tabular-nums">{{ $t('map.march.cancel_until', { time: formatDuration(cancelLeft) }) }}</span>
</div>
<p v-if="error" class="alert mx-3 mb-2" role="alert">{{ error }}</p>
</div>
</template>