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

188 lines
8.5 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { BuildingType, MarchPlan, MovementType, TravelMode, Units, UnitType, VillageState } from '~/types/game'
const props = defineProps<{
village: VillageState
busy: boolean
/** Field to aim at, e.g. from the map. */
initialTarget: { x: number; y: number } | null
now: number
preview: (x: number, y: number, units: Units, mode: TravelMode) => Promise<MarchPlan | null>
/** Resolves false when the server refused. */
send: (type: Exclude<MovementType, 'return'>, x: number, y: number, units: Units, mode: TravelMode, catapultTarget: BuildingType | null) => Promise<boolean>
}>()
const { t } = useI18n()
// Like the Die Stämme rally point: the units at home in groups, each with an amount
// field and its (count); the target field; then Attack or Support.
const amounts = reactive<Units>({})
const target = ref(props.initialTarget ? `${props.initialTarget.x}|${props.initialTarget.y}` : '')
const mode = ref<TravelMode>(props.village.rally.default_mode)
const catapultTarget = ref<BuildingType>('headquarters')
const home = (unit: UnitType) => props.village.troops[unit] ?? 0
const picked = computed<Units>(() => {
const units: Units = {}
for (const unit of UNIT_ORDER) {
const n = Math.min(home(unit), Math.floor(Number(amounts[unit]) || 0))
if (n > 0) units[unit] = n
}
return units
})
const hasUnits = computed(() => Object.keys(picked.value).length > 0)
const hasTroops = computed(() => UNIT_ORDER.some((u) => home(u) > 0))
const coords = computed(() => parseCoords(target.value))
function all() {
for (const unit of UNIT_ORDER) amounts[unit] = home(unit) || undefined
}
// The server names the target and times the march; asked again a moment after each change.
const plan = ref<MarchPlan | null>(null)
let timer: ReturnType<typeof setTimeout> | undefined
watch(
[coords, picked, mode],
() => {
clearTimeout(timer)
const c = coords.value
if (!c) {
plan.value = null
return
}
timer = setTimeout(async () => {
const result = await props.preview(c.x, c.y, picked.value, mode.value)
// Drop answers for a target or mode that changed meanwhile.
if (coords.value?.x === c.x && coords.value?.y === c.y) plan.value = result
}, 250)
},
{ immediate: true, deep: true },
)
onBeforeUnmount(() => clearTimeout(timer))
const own = computed(() => !!plan.value?.target && plan.value.target.id === props.village.id)
const canSend = computed(
() => props.village.rally.has_rally_point && !props.busy && hasUnits.value && !!plan.value?.target && !own.value,
)
async function submit(type: 'attack' | 'support') {
const c = coords.value
if (!c || !canSend.value) return
const sent = await props.send(type, c.x, c.y, picked.value, mode.value, picked.value.catapult ? catapultTarget.value : null)
if (sent) UNIT_ORDER.forEach((u) => delete amounts[u])
}
function modeHint(m: VillageState['rally']['modes'][number]) {
const speed = t('village.rally.mode_speed', { percent: Math.round(m.speed * 100) })
const seen = m.visibility > 0 ? t('village.rally.mode_seen', { fields: m.visibility }) : t('village.rally.mode_unseen')
return `${speed}, ${seen}`
}
</script>
<template>
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('village.rally.send') }}</h2>
<button v-if="hasTroops" type="button" class="btn btn-outline" @click="all">
{{ $t('village.rally.all') }}
</button>
</div>
<form class="@container panel-body space-y-4" @submit.prevent="submit('attack')">
<p v-if="!village.rally.has_rally_point" class="alert">
{{ $t('village.rally.no_rally_point') }}
<NuxtLink :to="`/village/${village.id}/build`" class="font-semibold underline">{{ $t('village.rally.to_build') }}</NuxtLink>
</p>
<p v-if="village.rally.protected_until" class="text-sm text-ink-700">
{{ $t('village.rally.protected', { time: formatArrival(Date.parse(village.rally.protected_until), now, t) }) }}
</p>
<div class="grid gap-x-6 gap-y-3 @sm:grid-cols-2 @2xl:grid-cols-4">
<fieldset v-for="group in UNIT_GROUPS" :key="group.name">
<legend class="mb-1 text-xs font-semibold tracking-wide text-ink-500 uppercase">{{ $t(`village.rally.groups.${group.name}`) }}</legend>
<div v-for="unit in group.units" :key="unit" class="flex items-center gap-2 py-0.5" :class="home(unit) ? '' : 'opacity-50'">
<IconUnit :unit="unit" class="size-5 text-wood-800" :title="$t(`units.${unit}`)" />
<input
v-model.number="amounts[unit]"
type="number"
min="0"
:max="home(unit)"
inputmode="numeric"
class="input w-16 px-1.5 py-0.5 text-right tabular-nums"
:disabled="!home(unit)"
:aria-label="$t('village.rally.amount_of', { unit: $t(`units.${unit}`) })"
/>
<button
type="button"
class="btn-fill"
:disabled="!home(unit)"
@click="amounts[unit] = home(unit)"
>
({{ home(unit) }})
</button>
</div>
</fieldset>
</div>
<p v-if="!hasTroops" class="text-sm text-ink-500">{{ $t('village.rally.none_home') }}</p>
<div class="grid gap-3 border-t-2 border-parchment-300 pt-3 @md:grid-cols-[10rem_1fr]">
<label for="send-target" class="label self-center">{{ $t('village.rally.target') }}</label>
<input
id="send-target"
v-model="target"
type="text"
inputmode="numeric"
autocomplete="off"
:placeholder="$t('village.rally.target_placeholder')"
class="input w-32 tabular-nums"
/>
<label for="send-mode" class="label self-center">{{ $t('village.rally.mode') }}</label>
<div class="flex flex-wrap items-center gap-2">
<select id="send-mode" v-model="mode" class="input w-auto py-1">
<option v-for="m in village.rally.modes" :key="m.mode" :value="m.mode">{{ $t(`village.rally.modes.${m.mode}`) }}</option>
</select>
<span class="text-xs text-ink-500">{{ modeHint(village.rally.modes.find((m) => m.mode === mode) ?? village.rally.modes[0]!) }}</span>
</div>
<template v-if="picked.catapult">
<label for="send-catapult" class="label self-center">{{ $t('village.rally.catapult_target') }}</label>
<select id="send-catapult" v-model="catapultTarget" class="input w-auto py-1">
<option v-for="type in BUILDING_TYPES" :key="type" :value="type">{{ $t(`buildings.${type}`) }}</option>
</select>
</template>
</div>
<!-- What the server says about the target and the march. -->
<dl v-if="plan" class="grid grid-cols-[auto_1fr] gap-x-4 gap-y-1 rounded-sm border border-parchment-300 bg-parchment-50 px-3 py-2 text-sm">
<template v-if="plan.target">
<dt class="text-ink-500">{{ $t('village.rally.target') }}</dt>
<dd class="font-semibold">
{{ villageLabel(plan.target) }}
<span class="font-normal text-ink-500">{{ own ? $t('village.rally.own') : (plan.target.player?.name ?? $t('common.barbarian')) }}</span>
</dd>
<dt class="text-ink-500">{{ $t('village.rally.distance') }}</dt>
<dd class="tabular-nums">{{ $t('village.rally.fields', { fields: plan.distance.toFixed(1) }) }}</dd>
<template v-if="plan.seconds !== null && !own">
<dt class="text-ink-500">{{ $t('village.rally.duration') }}</dt>
<dd class="tabular-nums">{{ formatDuration(plan.seconds) }}</dd>
<dt class="text-ink-500">{{ $t('village.rally.arrival') }}</dt>
<dd class="tabular-nums">{{ formatArrival(now + plan.seconds * 1000, now, t) }}</dd>
</template>
</template>
<p v-else class="col-span-2 text-ink-700">{{ $t('village.rally.empty', { x: plan.x, y: plan.y }) }}</p>
</dl>
<div class="flex flex-wrap gap-2">
<button type="submit" class="btn btn-attack" :disabled="!canSend">
<IconUi name="attack" class="size-4" />
{{ $t('village.rally.attack') }}
</button>
<button type="button" class="btn btn-outline" :disabled="!canSend || !plan?.target?.player" @click="submit('support')">
<IconUi name="support" class="size-4" />
{{ $t('village.rally.support') }}
</button>
</div>
<p class="text-xs text-ink-500">{{ $t('village.rally.detection', { fields: village.rally.detection }) }}</p>
</form>
</section>
</template>