feat: reports and the simulator show the units archers and catapults shot before the melee as a row under the losses; a report whose noblemen brought loyalty to 0 without coins for another village says so (amber for the attacker); the mock sends both

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-31 10:57:07 +01:00
parent 836907a783
commit fdca4661fd
7 changed files with 101 additions and 11 deletions

View File

@ -1,7 +1,12 @@
<script setup lang="ts"> <script setup lang="ts">
import type { BattleReport, UnitType, VillageRef } from '~/types/game' import type { BattleReport, UnitType, VillageRef } from '~/types/game'
const props = defineProps<{ report: BattleReport; worldId: number }>() const props = defineProps<{
report: BattleReport
worldId: number
/** A defence report (also a supporter's): wording from the defending side. */
defense?: boolean
}>()
const { t } = useI18n() const { t } = useI18n()
const sides = computed(() => [ const sides = computed(() => [
@ -52,7 +57,12 @@ const woundedBack = computed(() => (props.report.wounded ? formatArrival(Date.pa
<div class="px-3 py-2"> <div class="px-3 py-2">
<p v-if="s.side.units === null" class="text-ink-500">{{ $t('reports.battle.no_info') }}</p> <p v-if="s.side.units === null" class="text-ink-500">{{ $t('reports.battle.no_info') }}</p>
<p v-else-if="!unitRows(s.side.units).length" class="text-ink-500">{{ $t('reports.battle.no_troops') }}</p> <p v-else-if="!unitRows(s.side.units).length" class="text-ink-500">{{ $t('reports.battle.no_troops') }}</p>
<ReportUnits v-else :units="s.side.units" :lost="s.side.lost" /> <ReportUnits
v-else
:units="s.side.units"
:lost="s.side.lost"
:shot="s.key === 'attacker' ? report.volley?.attacker : report.volley?.defender"
/>
</div> </div>
</section> </section>
@ -123,7 +133,15 @@ const woundedBack = computed(() => (props.report.wounded ? formatArrival(Date.pa
</span> </span>
</div> </div>
<p v-if="report.loyalty" class="font-semibold" :class="report.loyalty.conquered ? 'text-blood-700' : 'text-ink-700'"> <!-- Loyalty at 0 takes the village only while the attacker's coins pay for another one. -->
<p
v-if="report.loyalty?.no_coins && !report.loyalty.conquered"
class="border px-2 py-0.5 font-semibold"
:class="defense ? 'border-transparent px-0 text-ink-700' : 'border-gold-600 bg-gold-400 text-ink-900'"
>
{{ $t(defense ? 'reports.battle.no_coins_defense' : 'reports.battle.no_coins', { before: report.loyalty.before, after: report.loyalty.after }) }}
</p>
<p v-else-if="report.loyalty" class="font-semibold" :class="report.loyalty.conquered ? 'text-blood-700' : 'text-ink-700'">
{{ $t('reports.battle.loyalty', { before: report.loyalty.before, after: report.loyalty.after }) }} {{ $t('reports.battle.loyalty', { before: report.loyalty.before, after: report.loyalty.after }) }}
<template v-if="report.loyalty.conquered">{{ $t('reports.battle.conquered') }}</template> <template v-if="report.loyalty.conquered">{{ $t('reports.battle.conquered') }}</template>
</p> </p>

View File

@ -5,11 +5,14 @@ const props = defineProps<{
units: Partial<Record<string, number>> units: Partial<Record<string, number>>
/** null hides the losses row. */ /** null hides the losses row. */
lost: Record<string, number> | null lost: Record<string, number> | null
/** Of the losses, those shot by archers and catapults before the melee; a row only when any were. */
shot?: Record<string, number> | null
}>() }>()
const columns = computed(() => unitRows(props.units).map(([u]) => u)) const columns = computed(() => unitRows(props.units).map(([u]) => u))
const anyShot = computed(() => Object.values(props.shot ?? {}).some((n) => n > 0))
</script> </script>
<!-- A Die Stämme report table: one column per unit, quantity and losses below. --> <!-- A Die Stämme report table: one column per unit, quantity and losses below, and of those the ones shot before the melee. -->
<template> <template>
<div class="overflow-x-auto"> <div class="overflow-x-auto">
<table class="text-sm tabular-nums"> <table class="text-sm tabular-nums">
@ -33,6 +36,12 @@ const columns = computed(() => unitRows(props.units).map(([u]) => u))
{{ lost[unit] ?? 0 }} {{ lost[unit] ?? 0 }}
</td> </td>
</tr> </tr>
<tr v-if="shot && anyShot" class="border-t border-parchment-300">
<th class="pr-3 py-1 text-left text-xs font-semibold whitespace-nowrap text-ink-500">{{ $t('reports.battle.volley') }}</th>
<td v-for="unit in columns" :key="unit" class="px-1.5 py-1 text-center" :class="shot[unit] ? 'text-blood-700' : 'text-ink-400'">
{{ shot[unit] ?? 0 }}
</td>
</tr>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -125,7 +125,7 @@ async function run() {
<p class="text-ink-500 tabular-nums">{{ $t('village.simulator.power', result.power) }}</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"> <div v-for="side in (['attacker', 'defender'] as const)" :key="side">
<p class="mb-1 font-semibold">{{ $t(`reports.battle.${side}`) }}</p> <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" /> <ReportUnits v-if="unitRows(result[side].units).length" :units="result[side].units" :lost="result[side].lost" :shot="result.volley?.[side]" />
<p v-else class="text-ink-500">{{ $t('reports.battle.no_troops') }}</p> <p v-else class="text-ink-500">{{ $t('reports.battle.no_troops') }}</p>
</div> </div>
<p v-if="result.wall && result.wall.after < result.wall.before">{{ $t('reports.battle.wall', result.wall) }}</p> <p v-if="result.wall && result.wall.after < result.wall.before">{{ $t('reports.battle.wall', result.wall) }}</p>

View File

@ -40,6 +40,7 @@ import type {
Units, Units,
VillageRef, VillageRef,
VillageState, VillageState,
Volley,
WorldSummary, WorldSummary,
} from '~/types/game' } from '~/types/game'
import type { MapMovement, MapVillage, MapView } from '~/types/map' import type { MapMovement, MapVillage, MapView } from '~/types/map'
@ -913,6 +914,37 @@ function writeReport(state: MockState, userId: number, worldId: number, at: numb
/** Per unit: none of them. */ /** Per unit: none of them. */
const noLosses = (units: Units) => Object.fromEntries(unitEntries(units).map(([u]) => [u, 0])) const noLosses = (units: Units) => Object.fromEntries(unitEntries(units).map(([u]) => [u, 0]))
/** Units that shoot before the melee. */
const SHOOTERS = new Set(['archer', 'catapult'])
/**
* Of one side's losses, those the other side's archers and catapults shot before the melee:
* the shooters' share of that side's strength. Crude stand-in for the backend's volley phase.
*/
function shotOf(lost: Record<string, number>, shooters: Record<string, number>, strength: (unit: string) => number) {
const total = Object.entries(shooters).reduce((sum, [u, n]) => sum + strength(u) * n, 0)
const share = total > 0 ? Object.entries(shooters).reduce((sum, [u, n]) => sum + (SHOOTERS.has(u) ? strength(u) * n : 0), 0) / total : 0
return Object.fromEntries(Object.entries(lost).map(([u, n]) => [u, Math.floor(n * share)]).filter(([, n]) => Number(n) > 0))
}
/** Both sides' volley losses, or null when nobody was shot. */
function volleyOf(attacker: Record<string, number>, defender: Record<string, number>): Volley | null {
return Object.keys(attacker).length || Object.keys(defender).length ? { attacker, defender } : null
}
/**
* Noblemen that survived a won attack on a barbarian: loyalty falls, and at 0 the village would
* change hands if the coins paid for another village. The mock never takes made-up villages.
*/
function loyaltyHit(state: MockState, m: MockMovement): BattleReport['loyalty'] {
const nobles = m.units.nobleman ?? 0
if (!nobles) return null
const after = Math.max(0, 100 - 30 * nobles)
const home = state.villages.find((v) => v.id === m.origin_id)
const coins = home ? conquest(state, home) : null
return { before: 100, after, conquered: false, no_coins: after === 0 && !!coins && coins.slots <= coins.used }
}
/** Barbarians have no troops: the attack always wins and loots the made-up stock. */ /** Barbarians have no troops: the attack always wins and loots the made-up stock. */
function raid(state: MockState, m: MockMovement, at: number) { function raid(state: MockState, m: MockMovement, at: number) {
const capacity = unitSum(m.units, (u) => UNITS[u].carry) const capacity = unitSum(m.units, (u) => UNITS[u].carry)
@ -931,6 +963,8 @@ function raid(state: MockState, m: MockMovement, at: number) {
building: null, building: null,
loot, loot,
capacity, capacity,
loyalty: loyaltyHit(state, m),
volley: null,
}, },
}) })
turnBack(state, m, m.units, each > 0 ? loot : null, at) turnBack(state, m, m.units, each > 0 ? loot : null, at)
@ -977,6 +1011,10 @@ function battle(state: MockState, m: MockMovement, d: MockVillage, at: number) {
const attackLost = dead(m.units, attackRatio) const attackLost = dead(m.units, attackRatio)
const survivors = left(m.units, attackLost) const survivors = left(m.units, attackLost)
const volley = volleyOf(
shotOf(attackLost, counts(defenders), (u) => UNITS[u as UnitType]?.defense ?? 0),
shotOf(defenseLost, counts(m.units), (u) => UNITS[u as UnitType]?.attack ?? 0),
)
const capacity = won ? unitSum(survivors, (u) => UNITS[u].carry) : 0 const capacity = won ? unitSum(survivors, (u) => UNITS[u].carry) : 0
const loot: Cost = { wood: 0, clay: 0, iron: 0 } const loot: Cost = { wood: 0, clay: 0, iron: 0 }
for (const r of LOOTED) { for (const r of LOOTED) {
@ -1007,11 +1045,12 @@ function battle(state: MockState, m: MockMovement, d: MockVillage, at: number) {
capacity, capacity,
raid: raided, raid: raided,
bounty, bounty,
volley,
} }
// An attacker with no survivors learns nothing about the defence. // An attacker with no survivors learns nothing about the defence.
const seen: BattleReport = hasUnits(survivors) const seen: BattleReport = hasUnits(survivors)
? report ? report
: { ...report, defender: { village: report.defender.village, units: null, lost: null }, wall: null } : { ...report, defender: { village: report.defender.village, units: null, lost: null }, wall: null, volley: volley && { attacker: volley.attacker, defender: null } }
writeReport(state, m.user_id, m.world_id, at, { type: 'attack', payload: seen }) writeReport(state, m.user_id, m.world_id, at, { type: 'attack', payload: seen })
writeReport(state, d.user_id, d.world_id, at, { type: 'defense', payload: report }) writeReport(state, d.user_id, d.world_id, at, { type: 'defense', payload: report })
supporters.forEach((userId) => writeReport(state, userId, d.world_id, at, { type: 'defense', payload: report })) supporters.forEach((userId) => writeReport(state, userId, d.world_id, at, { type: 'defense', payload: report }))
@ -1445,13 +1484,19 @@ const handlers: Record<string, Handler> = {
const won = a > d const won = a > d
const ratio = won ? (d / a) ** 1.5 : a > 0 ? (a / d) ** 1.5 : 0 const ratio = won ? (d / a) ** 1.5 : a > 0 ? (a / d) ** 1.5 : 0
const dead = (units: Record<string, number>, loss: number) => Object.fromEntries(Object.entries(units).map(([u, n]) => [u, Math.min(n, Math.round(n * loss))])) const dead = (units: Record<string, number>, loss: number) => Object.fromEntries(Object.entries(units).map(([u, n]) => [u, Math.min(n, Math.round(n * loss))]))
const attackLost = dead(attack, won ? ratio : 1)
const defenseLost = dead(defense, won ? 1 : ratio)
return { return {
won, won,
attacker: { units: attack, lost: dead(attack, won ? ratio : 1) }, attacker: { units: attack, lost: attackLost },
defender: { units: defense, lost: dead(defense, won ? 1 : ratio) }, defender: { units: defense, lost: defenseLost },
wall: Number(data.wall) > 0 ? { before: Number(data.wall), after: Number(data.wall) } : null, wall: Number(data.wall) > 0 ? { before: Number(data.wall), after: Number(data.wall) } : null,
building: null, building: null,
power: { attack: a, defense: d }, power: { attack: a, defense: d },
volley: volleyOf(
shotOf(attackLost, defense, (u) => (u === 'militia' ? MILITIA_DEFENSE : UNITS[u as UnitType].defense)),
shotOf(defenseLost, attack, (u) => UNITS[u as UnitType].attack),
),
} }
}, },

View File

@ -36,7 +36,7 @@ const scattered = computed(() => (fight.value && 'scattered' in fight.value ? fi
<span class="text-sm whitespace-nowrap text-ink-700 tabular-nums">{{ formatArrival(Date.parse(report.happened_at), clock, t) }}</span> <span class="text-sm whitespace-nowrap text-ink-700 tabular-nums">{{ formatArrival(Date.parse(report.happened_at), clock, t) }}</span>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<ReportBattle v-if="battle" :report="battle" :world-id="worldId" /> <ReportBattle v-if="battle" :report="battle" :world-id="worldId" :defense="report.type === 'defense'" />
<p v-else-if="scattered" class="text-sm">{{ $t('reports.scattered', { from: villageLabel(scattered.attacker.village) }) }}</p> <p v-else-if="scattered" class="text-sm">{{ $t('reports.scattered', { from: villageLabel(scattered.attacker.village) }) }}</p>
<div v-else-if="report.type === 'support'" class="space-y-3 text-sm"> <div v-else-if="report.type === 'support'" class="space-y-3 text-sm">
<p>{{ $t('reports.support.arrived', { to: villageLabel(report.payload.to) }) }}</p> <p>{{ $t('reports.support.arrived', { to: villageLabel(report.payload.to) }) }}</p>

View File

@ -301,8 +301,13 @@ export interface BattleReport {
building: { type: BuildingType; before: number; after: number } | null building: { type: BuildingType; before: number; after: number } | null
loot: Cost loot: Cost
capacity: number capacity: number
/** Noblemen survived a won attack: loyalty before and after, and whether the village changed hands. */ /**
loyalty?: { before: number; after: number; conquered: boolean } | null * Noblemen survived a won attack: loyalty before and after, and whether the village changed
* hands. no_coins: loyalty reached 0 but the attacker's coins pay for no further village.
*/
loyalty?: { before: number; after: number; conquered: boolean; no_coins?: boolean } | null
/** Units shot by archers and catapults before the melee (already counted in `lost`). */
volley?: Volley | null
/** Raiders from a barbarian camp attacked; a beaten band leaves a bounty in the village. */ /** Raiders from a barbarian camp attacked; a beaten band leaves a bounty in the village. */
raid?: boolean raid?: boolean
bounty?: Cost | null bounty?: Cost | null
@ -327,6 +332,16 @@ export interface SimulatedBattle {
wall: { before: number; after: number } | null wall: { before: number; after: number } | null
building: { before: number; after: number } | null building: { before: number; after: number } | null
power: { attack: number; defense: number } power: { attack: number; defense: number }
volley?: Volley | null
}
/**
* Each side's units killed by the other side's archers and catapults before the melee, by unit;
* they are part of that side's `lost`. defender is null in an attacker's report that saw nothing.
*/
export interface Volley {
attacker: Record<string, number>
defender: Record<string, number> | null
} }
export interface SupportReport { export interface SupportReport {

View File

@ -38,6 +38,7 @@ export default {
destination: 'Destination', destination: 'Destination',
units: 'Quantity', units: 'Quantity',
losses: 'Losses', losses: 'Losses',
volley: 'Shot before the melee',
no_info: 'None of your troops came back, so nothing is known about the defenders.', no_info: 'None of your troops came back, so nothing is known about the defenders.',
no_troops: 'No troops in the village.', no_troops: 'No troops in the village.',
wall: 'Rams: wall damaged from level {before} to level {after}.', wall: 'Rams: wall damaged from level {before} to level {after}.',
@ -53,6 +54,8 @@ export default {
haul_of: '{loot} of {capacity}', haul_of: '{loot} of {capacity}',
loyalty: 'Loyalty dropped from {before} to {after}.', loyalty: 'Loyalty dropped from {before} to {after}.',
conquered: 'The village has been conquered.', conquered: 'The village has been conquered.',
no_coins: "Loyalty fell from {before} to {after}, but your coins don't pay for another village yet.",
no_coins_defense: "Loyalty fell from {before} to {after}, but the attacker's coins don't pay for another village, so the village holds.",
wounded: 'Carried into the hospital', wounded: 'Carried into the hospital',
wounded_back: 'they stand again {when}', wounded_back: 'they stand again {when}',
}, },