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

94 lines
4.9 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
import type { Movement, UnitType, VillageRef } from '~/types/game'
const props = defineProps<{
movements: Movement[]
worldId: number
now: number
secondsUntil: (iso: string) => number
busy: boolean
}>()
const emit = defineEmits<{ cancel: [movementId: number] }>()
const { t } = useI18n()
const incoming = computed(() => props.movements.filter((m) => m.direction === 'in'))
const own = computed(() => props.movements.filter((m) => m.direction !== 'in'))
function title(m: Movement) {
if (m.direction === 'home') return t('village.movements.return_from', { from: villageLabel(m.from) })
if (m.direction === 'in') {
if (m.raid) return t('village.movements.raid_from', { from: villageLabel(m.from) })
return t(m.type === 'attack' ? 'village.movements.attack_from' : 'village.movements.support_from', { from: villageLabel(m.from) })
}
return t(m.type === 'attack' ? 'village.movements.attack_on' : 'village.movements.support_for', { target: villageLabel(m.to) })
}
/** The other end of the movement, for the map link. */
const other = (m: Movement): VillageRef => (m.direction === 'out' ? m.to : m.from)
const icon = (m: Movement) => (m.type === 'return' ? 'return' : m.type)
const canCancel = (m: Movement) => !!m.cancel_until && props.secondsUntil(m.cancel_until) > 0
</script>
<!-- Commands under way, like the Die Stämme rally point's troop movements: incoming first, red for attacks. -->
<template>
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('village.movements.title') }}</h2>
</div>
<div class="panel-body space-y-4">
<div v-for="list in [{ key: 'incoming', rows: incoming }, { key: 'own', rows: own }]" :key="list.key">
<h3 class="mb-1 text-xs font-semibold tracking-wide text-ink-500 uppercase">{{ $t(`village.movements.${list.key}`) }}</h3>
<table v-if="list.rows.length" class="w-full text-sm">
<thead class="sr-only">
<tr>
<th>{{ $t('reports.subject') }}</th>
<th>{{ $t('village.movements.arrival') }}</th>
<th>{{ $t('village.movements.arrives_in') }}</th>
<th />
</tr>
</thead>
<tbody class="divide-y divide-parchment-300">
<tr v-for="m in list.rows" :key="m.id" :class="m.direction === 'in' && m.type === 'attack' ? 'text-blood-700' : ''">
<td class="py-1.5 pr-2">
<div class="flex items-start gap-2">
<IconUi :name="icon(m)" class="mt-0.5 size-4 shrink-0" />
<div class="min-w-0">
<NuxtLink
:to="{ path: `/map/${worldId}`, query: { x: other(m).x, y: other(m).y } }"
class="font-semibold hover:underline"
>
{{ title(m) }}
</NuxtLink>
<span v-if="m.direction === 'in' && m.from.player" class="text-ink-500"> {{ $t('village.movements.by', { player: m.from.player.name }) }}</span>
<div v-if="m.marches_at && secondsUntil(m.marches_at) > 0" class="text-xs text-ink-700">
{{ $t('village.movements.gathering', { time: formatDuration(secondsUntil(m.marches_at)) }) }}
</div>
<div v-if="m.units" class="mt-0.5 flex flex-wrap gap-x-2.5 gap-y-0.5 text-xs text-ink-700">
<span v-for="[unit, count] in unitRows(m.units)" :key="unit" class="inline-flex items-center gap-1 tabular-nums">
<IconUnit :unit="unit as UnitType" class="size-3.5" />{{ count }}
</span>
<span v-if="m.cargo" class="inline-flex items-center gap-1 tabular-nums" :title="$t('village.movements.loot')">
<template v-for="r in (['wood', 'clay', 'iron'] as const)" :key="r">
<IconResource :resource="r" class="size-3.5" />{{ m.cargo[r] }}
</template>
</span>
</div>
</div>
</div>
</td>
<td class="py-1.5 pr-2 whitespace-nowrap text-ink-700 tabular-nums">{{ formatArrival(Date.parse(m.arrives_at), now, t) }}</td>
<td class="py-1.5 pr-2 text-right font-semibold whitespace-nowrap tabular-nums">{{ formatDuration(secondsUntil(m.arrives_at)) }}</td>
<td class="py-1.5 text-right">
<button v-if="canCancel(m)" type="button" class="btn btn-outline px-2 py-0.5 text-xs" :disabled="busy" @click="emit('cancel', m.id)">
{{ $t('village.movements.cancel') }}
</button>
</td>
</tr>
</tbody>
</table>
<p v-else class="text-sm text-ink-500">{{ $t(list.key === 'incoming' ? 'village.movements.none_incoming' : 'village.movements.none') }}</p>
</div>
</div>
</section>
</template>