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

137 lines
6.8 KiB
PHP
Raw Normal View History

<?php
use App\Actions\SendTroops;
use App\Engine\Abandon;
use App\Models\GameEvent;
use App\Models\Movement;
use App\Models\Report;
use App\Models\Village;
use App\Models\World;
use App\Models\Wounded;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\DB;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
/**
* A defender with 20 swordsmen, a farm big enough to staff everything and, unless
* $hospital is 0, a hospital at that level; and an attacker with a rally point.
*/
function wardVillages(int $hospital = 1, array $world = []): array
{
$attacker = startVillage();
$attacker->buildings()->create(['type' => 'rally_point', 'level' => 1]);
$defender = startVillage();
$defender->buildings()->where('type', 'farm')->update(['level' => 10]);
if ($hospital > 0) {
$defender->buildings()->create(['type' => 'hospital', 'level' => $hospital]);
}
$defender->troops()->create(['home_village_id' => $defender->id, 'unit' => 'sword', 'count' => 20]);
World::whereKey($defender->world_id)->update(['config' => array_replace_recursive(['combat' => ['beginner_protection' => 0, 'luck' => 0]], $world)]);
return [$attacker->fresh(), $defender->fresh()];
}
/** Attack with $spears spearmen and let the battle happen; returns the arrival time. */
function strike(Village $attacker, Village $defender, int $spears): CarbonImmutable
{
$attacker->troops()->create(['home_village_id' => $attacker->id, 'unit' => 'spear', 'count' => $spears]);
app(SendTroops::class)(owner($attacker), $attacker->id, 'attack', $defender->x, $defender->y, ['spear' => $spears], null, null);
$arrives = Movement::where('target_id', $defender->id)->sole()->arrives_at;
test()->travelTo($arrives);
test()->artisan('game:events --once')->assertSuccessful();
return $arrives;
}
it('brings half the fallen defenders back wounded; they eat meanwhile and stand at home after four hours', function () {
[$attacker, $defender] = wardVillages();
$at = strike($attacker, $defender, 60);
// 600 attack against 1000 defence: the defenders lose ((0.6 − 0.1) / 0.9)^1.5 of 20 → 8, 4 of them wounded.
$defense = Report::where('player_id', $defender->player_id)->sole()->payload;
expect($defense['defender']['lost'])->toBe(['sword' => 8])
->and($defense['wounded'])->toBe(['units' => ['sword' => 4], 'heals_at' => $at->addHours(4)->utc()->format('Y-m-d\TH:i:s.v\Z')])
->and(Report::where('player_id', $attacker->player_id)->sole()->payload['wounded'])->toBeNull();
$state = shown($defender);
expect((array) $state['troops'])->toBe(['sword' => 12])
->and($state['hospital'])->toMatchArray(['beds' => 20, 'occupied' => 4, 'share' => 0.5, 'heal_seconds' => 14400])
->and((array) $state['hospital']['wounded'][0]['units'])->toBe(['sword' => 4])
// Farm 10 feeds 0.8 × 223.5 = 178.8 heads: the 12 swordsmen and 4 wounded leave 162 villagers.
->and($state['population']['villagers'])->toBe(162);
$this->travel(4)->hours();
$this->artisan('game:events --once')->assertSuccessful();
$state = shown($defender);
expect((array) $state['troops'])->toBe(['sword' => 16])
->and($state['hospital'])->toMatchArray(['occupied' => 0, 'wounded' => []])
->and($state['population']['villagers'])->toBe(162);
});
it('takes in no more wounded than it has free beds', function () {
[$attacker, $defender] = wardVillages(1, ['buildings' => ['hospital' => ['beds' => ['base' => 3]]]]);
strike($attacker, $defender, 60);
expect(Report::where('player_id', $defender->player_id)->sole()->payload['wounded']['units'])->toBe(['sword' => 3])
->and(shown($defender)['hospital'])->toMatchArray(['beds' => 3, 'occupied' => 3]);
});
it('has no wounded without a hospital', function () {
[$attacker, $defender] = wardVillages(0);
strike($attacker, $defender, 60);
expect(Report::where('player_id', $defender->player_id)->sole()->payload['wounded'])->toBeNull()
->and((array) shown($defender)['troops'])->toBe(['sword' => 12])
->and(shown($defender)['hospital'])->toMatchArray(['beds' => 0, 'occupied' => 0, 'wounded' => []]);
});
it('takes in only the own fallen, not the support', function () {
[$attacker, $defender] = wardVillages();
$ally = startVillage();
$defender->troops()->create(['home_village_id' => $ally->id, 'unit' => 'sword', 'count' => 20]);
// 1500 attack against 2000 defence: ((0.75 − 0.1) / 0.9)^1.5 of each stack → 12 of 20 fall.
strike($attacker, $defender->fresh(), 150);
$defense = Report::where('player_id', $defender->player_id)->sole()->payload;
expect($defense['defender']['lost'])->toBe(['sword' => 24])
->and($defense['wounded']['units'])->toBe(['sword' => 6]);
});
it('counts the volley\'s dead with the fallen: in the reports, the hospital and the ranking', function () {
[$attacker, $defender] = wardVillages();
$attacker->troops()->create(['home_village_id' => $attacker->id, 'unit' => 'archer', 'count' => 30]);
app(SendTroops::class)(owner($attacker), $attacker->id, 'attack', $defender->x, $defender->y, ['archer' => 30], null, null);
$this->travelTo(Movement::where('target_id', $defender->id)->sole()->arrives_at);
$this->artisan('game:events --once')->assertSuccessful();
// 30 archers shoot 3 food at the swordsmen's 20 before the melee: 3 fall. The 17 left
// hold 680 against 450 and lose ((450 / 680 − 0.1) / 0.9)^1.5 of themselves: 8 more.
$defense = Report::where('player_id', $defender->player_id)->sole()->payload;
expect($defense['volley'])->toBe(['attacker' => ['archer' => 0], 'defender' => ['sword' => 3]])
->and($defense['defender']['lost'])->toBe(['sword' => 11])
->and($defense['wounded']['units'])->toBe(['sword' => 5])
->and((array) shown($defender)['troops'])->toBe(['sword' => 9])
// No archer came back, so the attacker learns nothing of the defence, its volley neither.
->and(Report::where('player_id', $attacker->player_id)->sole()->payload['volley'])->toBe(['attacker' => ['archer' => 0], 'defender' => null])
->and(DB::table('player_counters')->where('player_id', $attacker->player_id)->where('key', 'defeated.attack')->value('value'))->toBe(11)
->and(DB::table('player_counters')->where('player_id', $defender->player_id)->where('key', 'defeated.defense')->value('value'))->toBe(30);
});
it('loses its wounded when the village turns barbarian', function () {
[$attacker, $defender] = wardVillages();
strike($attacker, $defender, 60);
expect(Wounded::count())->toBe(1);
$village = Village::whereKey($defender->id)->first();
Abandon::village($village, now()->toImmutable());
$village->save();
expect(Wounded::count())->toBe(0)
->and(GameEvent::where('type', GameEvent::WOUNDED_HEALED)->whereNull('processed_at')->count())->toBe(0);
});