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

137 lines
5.7 KiB
Vue

<script setup lang="ts">
import type { SimulatedBattle, Units } from '~/types/game'
const props = defineProps<{
worldId: number
/** Own units at home, for "own troops". */
troops: Units
}>()
const errorText = useErrorText()
// The Die Stämme simulator: both sides' units, the wall and the modifiers, and the
// server runs the world's real battle rules on them. Nothing in the game changes.
const attack = reactive<Record<string, number | undefined>>({})
const defense = reactive<Record<string, number | undefined>>({})
const wall = ref(0)
const morale = ref(100)
const luck = ref(0)
const night = ref(false)
const building = ref(0)
const result = ref<SimulatedBattle | null>(null)
const error = ref<string | null>(null)
const busy = ref(false)
const DEFENDERS = [...UNIT_ORDER, 'militia'] as const
const counts = (units: Record<string, number | undefined>) =>
Object.fromEntries(Object.entries(units).filter(([, n]) => Number(n) > 0).map(([u, n]) => [u, Math.floor(Number(n))]))
function ownTroops() {
for (const unit of UNIT_ORDER) attack[unit] = props.troops[unit] || undefined
}
async function run() {
busy.value = true
error.value = null
try {
result.value = await ws.send<SimulatedBattle>('rally.simulate', {
world_id: props.worldId,
attack: counts(attack),
defense: counts(defense),
wall: wall.value,
morale: morale.value,
luck: luck.value,
night: night.value,
building: building.value,
})
} catch (e) {
error.value = errorText(e)
} finally {
busy.value = false
}
}
</script>
<template>
<details class="panel group">
<summary class="panel-head cursor-pointer list-none">
<h2 class="panel-title">{{ $t('village.simulator.title') }}</h2>
<IconUi name="plus" class="size-4 text-ink-500 group-open:hidden" />
<IconUi name="minus" class="hidden size-4 text-ink-500 group-open:block" />
</summary>
<form class="panel-body space-y-4" @submit.prevent="run">
<div class="grid gap-x-6 gap-y-2 sm:grid-cols-[auto_6rem_6rem]">
<span />
<span class="text-xs font-semibold text-ink-500 uppercase">{{ $t('reports.battle.attacker') }}</span>
<span class="text-xs font-semibold text-ink-500 uppercase">{{ $t('reports.battle.defender') }}</span>
<template v-for="unit in DEFENDERS" :key="unit">
<span class="flex items-center gap-2 text-sm">
<IconUnit :unit="unit" class="size-5 text-wood-800" />
{{ $t(`units.${unit}`) }}
</span>
<input
v-if="unit !== 'militia'"
v-model.number="attack[unit]"
type="number"
min="0"
inputmode="numeric"
class="input px-1.5 py-0.5 text-right tabular-nums"
:aria-label="`${$t('reports.battle.attacker')}: ${$t(`units.${unit}`)}`"
/>
<span v-else />
<input
v-model.number="defense[unit]"
type="number"
min="0"
inputmode="numeric"
class="input px-1.5 py-0.5 text-right tabular-nums"
:aria-label="`${$t('reports.battle.defender')}: ${$t(`units.${unit}`)}`"
/>
</template>
</div>
<div class="grid grid-cols-2 gap-3 border-t-2 border-parchment-300 pt-3 sm:grid-cols-5">
<label class="text-sm">
<span class="label">{{ $t('buildings.wall') }}</span>
<input v-model.number="wall" type="number" min="0" max="20" class="input px-1.5 py-0.5 tabular-nums" />
</label>
<label class="text-sm">
<span class="label">{{ $t('reports.battle.morale') }} %</span>
<input v-model.number="morale" type="number" min="30" max="100" class="input px-1.5 py-0.5 tabular-nums" />
</label>
<label class="text-sm">
<span class="label">{{ $t('village.simulator.luck') }} %</span>
<input v-model.number="luck" type="number" min="-25" max="25" class="input px-1.5 py-0.5 tabular-nums" />
</label>
<label class="text-sm">
<span class="label">{{ $t('village.simulator.building') }}</span>
<input v-model.number="building" type="number" min="0" max="30" class="input px-1.5 py-0.5 tabular-nums" />
</label>
<label class="flex items-center gap-2 self-end pb-1 text-sm">
<input v-model="night" type="checkbox" />
{{ $t('village.simulator.night') }}
</label>
</div>
<div class="flex flex-wrap gap-2">
<button type="submit" class="btn btn-primary" :disabled="busy">{{ $t('village.simulator.run') }}</button>
<button type="button" class="btn btn-outline" @click="ownTroops">{{ $t('village.simulator.own_troops') }}</button>
</div>
<p v-if="error" class="alert" role="alert">{{ error }}</p>
<div v-if="result" class="space-y-3 border-t-2 border-parchment-300 pt-3 text-sm">
<p class="font-display text-lg font-semibold" :class="result.won ? 'text-moss-700' : 'text-blood-700'">
{{ result.won ? $t('reports.battle.attacker_won') : $t('reports.battle.defender_won') }}
</p>
<p class="text-ink-500 tabular-nums">{{ $t('village.simulator.power', result.power) }}</p>
<div v-for="side in (['attacker', 'defender'] as const)" :key="side">
<p class="mb-1 font-semibold">{{ $t(`reports.battle.${side}`) }}</p>
<ReportUnits v-if="unitRows(result[side].units).length" :units="result[side].units" :lost="result[side].lost" />
<p v-else class="text-ink-500">{{ $t('reports.battle.no_troops') }}</p>
</div>
<p v-if="result.wall && result.wall.after < result.wall.before">{{ $t('reports.battle.wall', result.wall) }}</p>
<p v-if="result.building">{{ $t('village.simulator.building_after', result.building) }}</p>
</div>
</form>
</details>
</template>