game-village-politics-backend/tests/Feature/RaidTest.php

149 lines
6.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use App\Engine\Barbarians;
use App\Engine\VillageFactory;
use App\Models\GameEvent;
use App\Models\Movement;
use App\Models\Report;
use App\Models\Village;
use Carbon\CarbonImmutable;
use Database\Seeders\WorldSeeder;
beforeEach(function () {
$this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00'));
config([
'game.raids.enabled' => true,
// One profile, so the band is known: 0.7 × 10 + 0.3 × 25 = 14.5 attack a head.
'game.world.map.barbarians.garrison.profiles' => ['spearmen' => ['spear' => 0.7, 'sword' => 0.3]],
]);
});
/** A barbarian camp at full strength 3 fields east of the village. */
function raidCamp(Village $near, int $dx = 3): Village
{
$types = ['headquarters', 'storage', 'farm', 'timber_camp', 'clay_pit', 'iron_mine'];
$camp = app(VillageFactory::class)->create($near->world, null, 'Barbarian village', $near->x + $dx, $near->y, array_fill_keys($types, 8));
Barbarians::of($camp)->fill();
return $camp->fresh();
}
function pendingRaid(Village $village): ?CarbonImmutable
{
return GameEvent::where('type', GameEvent::RAID_PLANNED)->where('village_id', $village->id)->whereNull('processed_at')->value('fire_at');
}
/** Past beginner protection, with the band announced. */
function announced(Village $village): Movement
{
test()->travel(3)->days();
test()->artisan('game:events --once')->assertSuccessful();
return Movement::where('target_id', $village->id)->sole();
}
function arrive(Movement $movement): void
{
test()->travelTo($movement->arrives_at);
test()->artisan('game:events --once')->assertSuccessful();
}
it('plans the first raid for when beginner protection ends', function () {
$village = startVillage();
expect(pendingRaid($village)->toDateTimeString())->toBe('2026-11-01 12:00:00');
});
it('announces a band from the nearest camp hours before it arrives, and plans the next raid', function () {
$village = startVillage();
raidCamp($village, 6);
$camp = raidCamp($village);
$movement = announced($village);
// 50 + 1.5 × 21 points = 81.5 attack: 6 heads. They gather 3 h, then march 3 fields at 22 min.
expect($movement->origin_id)->toBe($camp->id)
->and($movement->player_id)->toBeNull()
->and($movement->units)->toBe(['spear' => 4, 'sword' => 2])
->and($movement->arrives_at->toDateTimeString())->toBe('2026-11-01 16:06:00');
$incoming = shown($village)['movements'][0];
expect($incoming)->toMatchArray(['type' => 'attack', 'direction' => 'in', 'raid' => true, 'marches_at' => '2026-11-01T15:00:00.000Z', 'arrives_at' => '2026-11-01T16:06:00.000Z'])
->and($incoming['units'])->toBeNull();
$next = pendingRaid($village);
expect($next->greaterThanOrEqualTo($movement->arrives_at->addHours(18)))->toBeTrue()
->and($next->lessThanOrEqualTo($movement->arrives_at->addHours(30)))->toBeTrue();
});
it('loots an undefended village and carries the loot back to its camp', function () {
$village = startVillage();
$camp = raidCamp($village);
$movement = announced($village);
$campWood = $camp->fresh()->wood;
arrive($movement);
$report = Report::where('player_id', $village->player_id)->sole();
// 4 spears and 2 swords carry 4 × 25 + 2 × 15 = 130.
expect($report->type)->toBe('defense')
->and($report->payload['won'])->toBeTrue()
->and($report->payload['raid'])->toBeTrue()
->and(array_sum($report->payload['loot']))->toBe(130);
$home = $movement->fresh();
expect($home->type)->toBe('return')->and($home->target_id)->toBe($camp->id);
arrive($home);
expect($camp->fresh()->wood)->toBeGreaterThan($campWood)
->and(Barbarians::of($camp->fresh())->current())->toEqual(Barbarians::of($camp->fresh())->full());
});
it('pays a bounty when the village beats the band', function () {
$village = startVillage();
raidCamp($village);
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'sword', 'count' => 20]);
$movement = announced($village);
$wood = shown($village)['resources']['wood'];
arrive($movement);
$report = Report::where('player_id', $village->player_id)->sole();
// 4 × 10 + 2 × 25 = 90 attack → 45, a third each.
expect($report->payload['won'])->toBeFalse()
->and($report->payload['bounty'])->toBe(['wood' => 15, 'clay' => 15, 'iron' => 15])
->and($report->payload['attacker']['lost'])->toBe(['spear' => 4, 'sword' => 2])
->and(Movement::count())->toBe(0)
->and(shown($village)['resources']['wood'])->toBeGreaterThan($wood + 14);
});
it('sends a weaker band from a weakened camp, and none from a beaten one', function () {
$village = startVillage();
$camp = raidCamp($village);
$movement = announced($village);
$camp->troops()->update(['count' => 0]);
$camp->troops()->where('unit', 'spear')->update(['count' => 6]);
arrive($movement);
// 6 of 10 spears and no swords: 6 of 10 food → the band at 60%.
$report = Report::where('player_id', $village->player_id)->sole();
expect($report->payload['attacker']['units'])->toBe(['spear' => 2, 'sword' => 1]);
// Once the band is back, the camp is beaten down to nothing.
arrive($movement->fresh());
$camp->troops()->delete();
$this->travelTo(pendingRaid($village));
$this->artisan('game:events --once')->assertSuccessful();
arrive(Movement::where('target_id', $village->id)->where('type', 'attack')->sole());
$scattered = Report::where('player_id', $village->player_id)->latest('id')->first();
expect($scattered->payload)->toMatchArray(['raid' => true, 'scattered' => true])
->and(Movement::where('target_id', $village->id)->where('type', 'attack')->exists())->toBeFalse();
});
it('plans raids for villages from before raids on deploy', function () {
$village = startVillage();
GameEvent::where('type', GameEvent::RAID_PLANNED)->delete();
(new WorldSeeder)->run();
expect(pendingRaid($village)->toDateTimeString())->toBe('2026-11-01 12:00:00');
(new WorldSeeder)->run();
expect(GameEvent::where('type', GameEvent::RAID_PLANNED)->count())->toBe(1);
});