98 lines
4.1 KiB
PHP
98 lines
4.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use App\Actions\SendTroops;
|
||
|
|
use App\Engine\Barbarians;
|
||
|
|
use App\Engine\VillageFactory;
|
||
|
|
use App\Models\GameEvent;
|
||
|
|
use App\Models\Village;
|
||
|
|
use Carbon\CarbonImmutable;
|
||
|
|
use Database\Seeders\WorldSeeder;
|
||
|
|
|
||
|
|
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
|
||
|
|
|
||
|
|
/** A barbarian village with every start building at this level, garrison not filled. */
|
||
|
|
function camp(Village $near, int $level, int $dx = 3, int $dy = 0): Village
|
||
|
|
{
|
||
|
|
$types = ['headquarters', 'storage', 'farm', 'timber_camp', 'clay_pit', 'iron_mine'];
|
||
|
|
|
||
|
|
return app(VillageFactory::class)->create($near->world, null, 'Barbarian village', $near->x + $dx, $near->y + $dy, array_fill_keys($types, $level));
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @return array<string, int> the camp's own troops */
|
||
|
|
function garrisonOf(Village $camp): array
|
||
|
|
{
|
||
|
|
return $camp->fresh()->troops->where('home_village_id', $camp->id)->mapWithKeys(fn ($s) => [$s->unit => $s->count])->all();
|
||
|
|
}
|
||
|
|
|
||
|
|
function heads(Village $camp): int
|
||
|
|
{
|
||
|
|
return array_sum(garrisonOf($camp));
|
||
|
|
}
|
||
|
|
|
||
|
|
it('brings a garrison with every new barbarian village', function () {
|
||
|
|
config(['game.world.map.barbarians.per_join' => 1, 'game.world.map.barbarians.garrison.free_points' => 0]);
|
||
|
|
$village = startVillage();
|
||
|
|
$barbarian = Village::whereNull('player_id')->sole();
|
||
|
|
|
||
|
|
// 0.1 a point, in the village's profile.
|
||
|
|
expect(heads($barbarian))->toBe((int) floor(0.1 * $barbarian->points))
|
||
|
|
->and(garrisonOf($barbarian))->toEqual(Barbarians::of($barbarian)->full())
|
||
|
|
->and(array_keys(garrisonOf($barbarian)))->each->toBeIn(['spear', 'sword', 'archer', 'light_cavalry']);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('regroups a quarter of full strength every two hours after being wiped out', function () {
|
||
|
|
$village = startVillage();
|
||
|
|
$village->buildings()->create(['type' => 'rally_point', 'level' => 1]);
|
||
|
|
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'light_cavalry', 'count' => 100]);
|
||
|
|
$camp = camp($village, 8);
|
||
|
|
Barbarians::of($camp)->fill();
|
||
|
|
$full = garrisonOf($camp);
|
||
|
|
expect(array_sum($full))->toBeGreaterThan(4);
|
||
|
|
|
||
|
|
app(SendTroops::class)(owner($village), $village->id, 'attack', $camp->x, $camp->y, ['light_cavalry' => 100], null, null);
|
||
|
|
$this->travel(30)->minutes();
|
||
|
|
$this->artisan('game:events --once')->assertSuccessful();
|
||
|
|
expect(garrisonOf($camp))->toBe([])
|
||
|
|
->and(GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->whereNull('processed_at')->value('fire_at')->toDateTimeString())->toBe('2026-10-29 14:30:00');
|
||
|
|
|
||
|
|
$this->travel(2)->hours();
|
||
|
|
$this->artisan('game:events --once')->assertSuccessful();
|
||
|
|
$step = array_map(fn (int $n) => max(1, (int) ceil(0.25 * $n)), $full);
|
||
|
|
expect(garrisonOf($camp))->toEqual($step);
|
||
|
|
|
||
|
|
$this->travel(6)->hours();
|
||
|
|
$this->artisan('game:events --once')->assertSuccessful();
|
||
|
|
expect(garrisonOf($camp))->toEqual($full)
|
||
|
|
->and(GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->whereNull('processed_at')->exists())->toBeFalse();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('fields more men as the village grows', function () {
|
||
|
|
config(['game.world.map.barbarians.garrison.per_point' => 1, 'game.world.map.barbarians.garrison.max_food_share' => 10]);
|
||
|
|
$village = startVillage();
|
||
|
|
$camp = camp($village, 3);
|
||
|
|
Barbarians::of($camp)->fill();
|
||
|
|
$before = heads($camp);
|
||
|
|
GameEvent::create(['world_id' => $camp->world_id, 'village_id' => $camp->id, 'type' => GameEvent::BARBARIAN_GROWS, 'fire_at' => now()]);
|
||
|
|
|
||
|
|
$this->artisan('game:events --once')->assertSuccessful();
|
||
|
|
$grown = $camp->fresh()->points;
|
||
|
|
expect($grown)->toBeGreaterThan($camp->points);
|
||
|
|
|
||
|
|
$this->travel(2)->hours();
|
||
|
|
$this->artisan('game:events --once')->assertSuccessful();
|
||
|
|
expect(heads($camp))->toBe($grown - 30)->and(heads($camp))->toBeGreaterThan($before);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('gives barbarian villages from before garrisons theirs on deploy, once', function () {
|
||
|
|
$village = startVillage();
|
||
|
|
$camp = camp($village, 8);
|
||
|
|
expect(garrisonOf($camp))->toBe([]);
|
||
|
|
|
||
|
|
(new WorldSeeder)->run();
|
||
|
|
$full = garrisonOf($camp);
|
||
|
|
expect($full)->toEqual(Barbarians::of($camp->fresh())->full())->and($full)->not->toBe([]);
|
||
|
|
|
||
|
|
(new WorldSeeder)->run();
|
||
|
|
expect(garrisonOf($camp))->toEqual($full);
|
||
|
|
});
|