feat: raiders: incoming bands with their gathering countdown, raid reports with bounty, scattered raids

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-29 16:17:00 +01:00
parent f690e06919
commit 5935f395f7
10 changed files with 108 additions and 13 deletions

View File

@ -42,7 +42,7 @@ const luckAt = computed(() => Math.max(0, Math.min(100, ((props.report.luck + 25
<section v-for="s in sides" :key="s.key" class="rounded-sm border-2 border-wood-700 bg-parchment-50"> <section v-for="s in sides" :key="s.key" class="rounded-sm border-2 border-wood-700 bg-parchment-50">
<dl class="grid grid-cols-[6rem_1fr] gap-x-3 gap-y-0.5 border-b border-wood-700 bg-parchment-200 px-3 py-1.5"> <dl class="grid grid-cols-[6rem_1fr] gap-x-3 gap-y-0.5 border-b border-wood-700 bg-parchment-200 px-3 py-1.5">
<dt class="font-semibold">{{ s.label }}</dt> <dt class="font-semibold">{{ s.label }}</dt>
<dd>{{ s.side.village.player?.name ?? $t('common.barbarian') }}</dd> <dd>{{ s.side.village.player?.name ?? (s.key === 'attacker' && report.raid ? $t('reports.battle.raiders') : $t('common.barbarian')) }}</dd>
<dt class="text-ink-500">{{ s.place }}</dt> <dt class="text-ink-500">{{ s.place }}</dt>
<dd> <dd>
<NuxtLink :to="mapLink(s.side.village)" class="font-semibold text-blood-700 hover:underline">{{ villageLabel(s.side.village) }}</NuxtLink> <NuxtLink :to="mapLink(s.side.village)" class="font-semibold text-blood-700 hover:underline">{{ villageLabel(s.side.village) }}</NuxtLink>
@ -107,6 +107,13 @@ const luckAt = computed(() => Math.max(0, Math.min(100, ((props.report.luck + 25
<span class="text-ink-500 tabular-nums">{{ $t('reports.battle.haul_of', { loot: looted, capacity: report.capacity }) }}</span> <span class="text-ink-500 tabular-nums">{{ $t('reports.battle.haul_of', { loot: looted, capacity: report.capacity }) }}</span>
</div> </div>
<div v-if="report.bounty" class="flex flex-wrap items-center gap-x-4 gap-y-1">
<span class="font-semibold">{{ $t('reports.battle.bounty') }}</span>
<span v-for="r in (['wood', 'clay', 'iron'] as const)" :key="r" class="inline-flex items-center gap-1 tabular-nums">
<IconResource :resource="r" class="size-4" />{{ report.bounty[r] }}
</span>
</div>
<p v-if="report.loyalty" class="font-semibold" :class="report.loyalty.conquered ? 'text-blood-700' : 'text-ink-700'"> <p v-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>

View File

@ -17,6 +17,7 @@ const own = computed(() => props.movements.filter((m) => m.direction !== 'in'))
function title(m: Movement) { function title(m: Movement) {
if (m.direction === 'home') return t('village.movements.return_from', { from: villageLabel(m.from) }) if (m.direction === 'home') return t('village.movements.return_from', { from: villageLabel(m.from) })
if (m.direction === 'in') { 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_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) }) return t(m.type === 'attack' ? 'village.movements.attack_on' : 'village.movements.support_for', { target: villageLabel(m.to) })
@ -59,6 +60,9 @@ const canCancel = (m: Movement) => !!m.cancel_until && props.secondsUntil(m.canc
{{ title(m) }} {{ title(m) }}
</NuxtLink> </NuxtLink>
<span v-if="m.direction === 'in' && m.from.player" class="text-ink-500"> {{ $t('village.movements.by', { player: m.from.player.name }) }}</span> <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"> <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"> <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 }} <IconUnit :unit="unit as UnitType" class="size-3.5" />{{ count }}

View File

@ -31,7 +31,7 @@ function order(o: BuildOrder) {
<ul class="side-body"> <ul class="side-body">
<li v-for="m in incoming" :key="m.id" class="flex items-center gap-2 text-blood-700"> <li v-for="m in incoming" :key="m.id" class="flex items-center gap-2 text-blood-700">
<IconUi name="attack" class="size-4" /> <IconUi name="attack" class="size-4" />
<span class="min-w-0 flex-1 truncate">{{ villageLabel(m.from) }}</span> <span class="min-w-0 flex-1 truncate">{{ m.raid ? $t('village.side.raiders', { from: `(${m.from.x}|${m.from.y})` }) : villageLabel(m.from) }}</span>
<span class="font-semibold tabular-nums">{{ formatDuration(secondsUntil(m.arrives_at)) }}</span> <span class="font-semibold tabular-nums">{{ formatDuration(secondsUntil(m.arrives_at)) }}</span>
</li> </li>
</ul> </ul>

View File

@ -103,6 +103,8 @@ interface MockVillage {
levy_until?: number | null levy_until?: number | null
/** Support standing here, by home village id; it eats here. */ /** Support standing here, by home village id; it eats here. */
support?: Record<number, Units> support?: Record<number, Units>
/** When the next mock raid gathers against this village. */
next_raid_at?: number
} }
interface MockMovement { interface MockMovement {
@ -121,6 +123,8 @@ interface MockMovement {
cargo: Cost | null cargo: Cost | null
sent_at: number sent_at: number
arrives_at: number arrives_at: number
/** Raiders (user_id 0) gather until they march. */
marches_at?: number
} }
type MockReport = Report & { world_id: number; user_id: number } type MockReport = Report & { world_id: number; user_id: number }
@ -818,6 +822,11 @@ function battle(state: MockState, m: MockMovement, d: MockVillage, at: number) {
} }
const wall = d.buildings.find((b) => b.type === 'wall') const wall = d.buildings.find((b) => b.type === 'wall')
// A beaten band of raiders leaves half its attack in resources.
const raided = m.user_id === RAIDERS
const each = raided && !won ? Math.floor(attack / 6) : 0
const bounty: Cost | null = raided && !won ? { wood: each, clay: each, iron: each } : null
if (bounty) refund(d, bounty)
const report: BattleReport = { const report: BattleReport = {
won, won,
luck: 0, luck: 0,
@ -833,6 +842,8 @@ function battle(state: MockState, m: MockMovement, d: MockVillage, at: number) {
building: null, building: null,
loot, loot,
capacity, capacity,
raid: raided,
bounty,
} }
// 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)
@ -845,6 +856,42 @@ function battle(state: MockState, m: MockMovement, d: MockVillage, at: number) {
if (hasUnits(survivors)) turnBack(state, m, survivors, capacity > 0 ? loot : null, at) if (hasUnits(survivors)) turnBack(state, m, survivors, capacity > 0 ? loot : null, at)
} }
/** Raiders have no player. */
const RAIDERS = 0
/**
* Every few minutes the nearest made-up barbarian sends a small band at the village; it
* gathers two minutes before it marches. A crude stand-in for app/Engine/Raiders.php.
*/
function planRaid(state: MockState, v: MockVillage, now: number) {
if ((v.next_raid_at ?? 0) > now || (state.movements ?? []).some((m) => m.user_id === RAIDERS && m.target_id === v.id)) return
let camp: { id: number; x: number; y: number } | null = null
for (let r = 1; r <= 8 && !camp; r++) {
for (let dy = -r; dy <= r && !camp; dy++) {
for (let dx = -r; dx <= r && !camp; dx++) {
const b = Math.max(Math.abs(dx), Math.abs(dy)) === r ? barbarianAt(v.x + dx, v.y + dy) : null
if (b) camp = { id: b.id, x: v.x + dx, y: v.y + dy }
}
}
}
if (!camp) return
const marches = now + 120_000
const m = march(state, {
world_id: v.world_id,
user_id: RAIDERS,
type: 'attack',
mode: DEFAULT_MODE,
origin_id: camp.id,
target_id: v.id,
distance: fieldDistance(camp, v),
units: { spear: 6, sword: 2 },
cargo: null,
sent_at: marches,
})
m.marches_at = marches
v.next_raid_at = m.arrives_at + 600_000
}
/** A movement reaches its target, which was caught up to the arrival first. */ /** A movement reaches its target, which was caught up to the arrival first. */
function arrive(state: MockState, m: MockMovement) { function arrive(state: MockState, m: MockMovement) {
const at = m.arrives_at const at = m.arrives_at
@ -906,6 +953,8 @@ function movements(state: MockState, v: MockVillage, now: number): Movement[] {
sent_at: mine ? iso(m.sent_at) : null, sent_at: mine ? iso(m.sent_at) : null,
arrives_at: iso(m.arrives_at), arrives_at: iso(m.arrives_at),
cancel_until: mine && direction === 'out' && cancelUntil > now ? iso(cancelUntil) : null, cancel_until: mine && direction === 'out' && cancelUntil > now ? iso(cancelUntil) : null,
raid: m.user_id === RAIDERS,
marches_at: m.marches_at && m.marches_at > now ? iso(m.marches_at) : null,
} }
}) })
} }
@ -1054,7 +1103,11 @@ const handlers: Record<string, Handler> = {
return { village_id: v.id } return { village_id: v.id }
}, },
'village.show': (state, user, data, now) => view(state, ownVillage(state, user, data.village_id, now), now), 'village.show': (state, user, data, now) => {
const v = ownVillage(state, user, data.village_id, now)
planRaid(state, v, now)
return view(state, v, now)
},
'map.view': (state, user, data) => mapView(state, user, data), 'map.view': (state, user, data) => mapView(state, user, data),

View File

@ -19,10 +19,11 @@ watch(
const subject = computed(() => { const subject = computed(() => {
const r = report.value const r = report.value
if (!r) return '' return r ? reportSubject(r, t) : ''
if (r.type === 'support') return t('reports.title_support', { from: villageLabel(r.payload.from), to: villageLabel(r.payload.to) })
return t('reports.title_attack', { from: villageLabel(r.payload.attacker.village), to: villageLabel(r.payload.defender.village) })
}) })
const fight = computed(() => (report.value && report.value.type !== 'support' ? report.value.payload : null))
const battle = computed(() => (fight.value && !('scattered' in fight.value) ? fight.value : null))
const scattered = computed(() => (fight.value && 'scattered' in fight.value ? fight.value : null))
</script> </script>
<template> <template>
@ -35,8 +36,9 @@ const subject = computed(() => {
<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="report.type !== 'support'" :report="report.payload" :world-id="worldId" /> <ReportBattle v-if="battle" :report="battle" :world-id="worldId" />
<div v-else class="space-y-3 text-sm"> <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">
<p>{{ $t('reports.support.arrived', { to: villageLabel(report.payload.to) }) }}</p> <p>{{ $t('reports.support.arrived', { to: villageLabel(report.payload.to) }) }}</p>
<ReportUnits :units="report.payload.units" :lost="null" /> <ReportUnits :units="report.payload.units" :lost="null" />
</div> </div>

View File

@ -37,10 +37,7 @@ function toggleAll() {
selected.value = allSelected.value ? [] : (page.value?.reports.map((r) => r.id) ?? []) selected.value = allSelected.value ? [] : (page.value?.reports.map((r) => r.id) ?? [])
} }
function subject(r: Report) { const subject = (r: Report) => reportSubject(r, t)
if (r.type === 'support') return t('reports.title_support', { from: villageLabel(r.payload.from), to: villageLabel(r.payload.to) })
return t('reports.title_attack', { from: villageLabel(r.payload.attacker.village), to: villageLabel(r.payload.defender.village) })
}
const DOT = { won: 'bg-moss-600', losses: 'bg-gold-500', lost: 'bg-blood-700', info: 'bg-iron-400' } const DOT = { won: 'bg-moss-600', losses: 'bg-gold-500', lost: 'bg-blood-700', info: 'bg-iron-400' }
</script> </script>

View File

@ -143,6 +143,10 @@ export interface Movement {
arrives_at: string arrives_at: string
/** Until when the command can still be called back; null when it can't. */ /** Until when the command can still be called back; null when it can't. */
cancel_until: string | null cancel_until: string | null
/** Raiders from a barbarian camp: no player sent them. */
raid: boolean
/** While raiders still gather at their camp: when they march. */
marches_at: string | null
} }
export interface SupportGroup { export interface SupportGroup {
@ -201,6 +205,9 @@ export interface BattleReport {
capacity: number capacity: number
/** Noblemen survived a won attack: loyalty before and after, and whether the village changed hands. */ /** Noblemen survived a won attack: loyalty before and after, and whether the village changed hands. */
loyalty?: { before: number; after: number; conquered: boolean } | null loyalty?: { before: number; after: number; conquered: boolean } | null
/** Raiders from a barbarian camp attacked; a beaten band leaves a bounty in the village. */
raid?: boolean
bounty?: Cost | null
/** Only when scouts went along. Resources and buildings only when some got through. */ /** Only when scouts went along. Resources and buildings only when some got through. */
scouting?: { scouting?: {
sent: number sent: number
@ -234,8 +241,16 @@ interface ReportBase {
read: boolean read: boolean
} }
/** Raiders whose camp was beaten down before they arrived: no battle. */
export interface ScatteredRaid {
raid: true
scattered: true
attacker: { village: VillageRef; units: null; lost: null }
defender: { village: VillageRef; units: null; lost: null }
}
export type Report = export type Report =
| (ReportBase & { type: 'attack' | 'defense'; payload: BattleReport }) | (ReportBase & { type: 'attack' | 'defense'; payload: BattleReport | ScatteredRaid })
| (ReportBase & { type: 'support'; payload: SupportReport }) | (ReportBase & { type: 'support'; payload: SupportReport })
/** `report.index`, `report.delete` and `report.readAll`. */ /** `report.index`, `report.delete` and `report.readAll`. */

View File

@ -40,12 +40,21 @@ const lost = (side: BattleSide) => Object.values(side.lost ?? {}).reduce((a, b)
export function reportTone(r: Report): 'won' | 'losses' | 'lost' | 'info' { export function reportTone(r: Report): 'won' | 'losses' | 'lost' | 'info' {
if (r.type === 'support') return 'info' if (r.type === 'support') return 'info'
const p = r.payload const p = r.payload
if ('scattered' in p) return 'won'
const ownSide = r.type === 'attack' ? p.attacker : p.defender const ownSide = r.type === 'attack' ? p.attacker : p.defender
const won = r.type === 'attack' ? p.won : !p.won const won = r.type === 'attack' ? p.won : !p.won
if (!won) return 'lost' if (!won) return 'lost'
return lost(ownSide) > 0 ? 'losses' : 'won' return lost(ownSide) > 0 ? 'losses' : 'won'
} }
/** A report's subject line: who attacked or supported whom, raiders by name. */
export function reportSubject(r: Report, t: (key: string, values?: Record<string, unknown>) => string) {
if (r.type === 'support') return t('reports.title_support', { from: villageLabel(r.payload.from), to: villageLabel(r.payload.to) })
const p = r.payload
if ('scattered' in p) return t('reports.title_scattered', { from: villageLabel(p.attacker.village) })
return t(p.raid ? 'reports.title_raid' : 'reports.title_attack', { from: villageLabel(p.attacker.village), to: villageLabel(p.defender.village) })
}
/** "Today 13:30:05", "Tomorrow 08:00:00" or "31.10. 13:30:05" in the player's clock. */ /** "Today 13:30:05", "Tomorrow 08:00:00" or "31.10. 13:30:05" in the player's clock. */
export function formatArrival(ms: number, now: number, t: (key: string, values?: Record<string, unknown>) => string) { export function formatArrival(ms: number, now: number, t: (key: string, values?: Record<string, unknown>) => string) {
const at = new Date(ms) const at = new Date(ms)

View File

@ -19,9 +19,14 @@ export default {
unread: 'unread', unread: 'unread',
title_attack: '{from} attacks {to}', title_attack: '{from} attacks {to}',
title_support: '{from} supports {to}', title_support: '{from} supports {to}',
title_raid: 'Raiders from {from} attack {to}',
title_scattered: 'Raiders from {from} scattered',
scattered: 'The raiders from {from} scattered before they reached your village: their camp was too weak to send them.',
tone: { won: 'Won', losses: 'Won with losses', lost: 'Lost', info: 'Support' }, tone: { won: 'Won', losses: 'Won with losses', lost: 'Lost', info: 'Support' },
battle: { battle: {
attacker_won: 'The attacker has won', attacker_won: 'The attacker has won',
raiders: 'Raiders',
bounty: 'The beaten raiders left behind',
defender_won: 'The defender has won', defender_won: 'The defender has won',
time: 'Battle time', time: 'Battle time',
luck: 'Luck (for the attacker)', luck: 'Luck (for the attacker)',

View File

@ -63,6 +63,7 @@ export default {
construction: 'Construction', construction: 'Construction',
recruit: 'Recruit', recruit: 'Recruit',
incoming: 'Incoming attacks', incoming: 'Incoming attacks',
raiders: 'Raiders from {from}',
commands: 'Troop movements', commands: 'Troop movements',
rally: 'Rally point', rally: 'Rally point',
}, },
@ -208,6 +209,8 @@ export default {
support_for: 'Support for {target}', support_for: 'Support for {target}',
return_from: 'Return from {from}', return_from: 'Return from {from}',
attack_from: 'Attack from {from}', attack_from: 'Attack from {from}',
raid_from: 'Raiders from {from}',
gathering: 'Gathering at their camp, they march in {time}',
support_from: 'Support from {from}', support_from: 'Support from {from}',
by: 'by {player}', by: 'by {player}',
loot: 'Loot', loot: 'Loot',