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

77 lines
3.5 KiB
PHP

<?php
use App\Engine\Barbarians;
use App\Engine\VillageFactory;
use App\Models\Movement;
use App\Models\Report;
use App\Models\Village;
use App\Models\World;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
/** @param list<string> $types new level-1 instances */
function withBuildings(Village $village, array $types): Village
{
foreach ($types as $type) {
$village->buildings()->create(['type' => $type, 'level' => 1]);
}
return $village->fresh();
}
it('makes a village of camps a resource village whose camps produce a fifth more', function () {
$village = startVillage();
$village->buildings()->where('type', 'farm')->update(['level' => 10]);
$before = shown(withBuildings($village, ['timber_camp', 'clay_pit', 'iron_mine']));
expect($before['specialisation'])->toMatchArray(['role' => null, 'share' => 0.7, 'min_space' => 16])
->and((array) $before['specialisation']['space'])->toBe(['resources' => 12, 'military' => 0]);
$state = shown(withBuildings($village, ['timber_camp']));
$camp = collect($state['buildings'])->firstWhere('type', 'clay_pit');
// 30/h at level 1, full staffing.
expect($state['specialisation']['role'])->toBe('resources')
->and($camp['production']['per_hour'])->toBe(36.0)
->and($state['rates']['clay'])->toBe(5 + 36.0);
});
it('trains a fifth faster in a military village', function () {
$village = startVillage();
$village->buildings()->where('type', 'farm')->update(['level' => 10]);
$plain = shown(withBuildings($village, ['barracks']));
$seconds = collect($plain['military'][0]['units'])->firstWhere('unit', 'spear')['seconds'];
$state = shown(withBuildings($village, ['stable', 'workshop']));
expect($state['specialisation']['role'])->toBe('military')
->and(collect($state['military'][0]['units'])->firstWhere('unit', 'spear')['seconds'])->toBe((int) ceil($seconds / 1.2));
});
it('lets a military village defend a tenth harder', function () {
// A band of 40 swords: 1000 attack.
config([
'game.raids.enabled' => true,
'game.raids.attack' => ['base' => 1000, 'per_point' => 0],
'game.world.map.barbarians.garrison.profiles' => ['swordsmen' => ['sword' => 1.0]],
]);
$village = startVillage();
World::whereKey($village->world_id)->update(['config' => ['combat' => ['luck' => 0]]]);
$village->buildings()->where('type', 'farm')->update(['level' => 15]);
withBuildings($village, ['barracks', 'stable', 'workshop']);
$types = ['headquarters', 'storage', 'farm', 'timber_camp', 'clay_pit', 'iron_mine'];
$camp = app(VillageFactory::class)->create($village->world, null, 'Barbarian village', $village->x + 3, $village->y, array_fill_keys($types, 8));
Barbarians::of($camp)->fill();
$this->travel(3)->days();
$this->artisan('game:events --once')->assertSuccessful();
$raid = Movement::where('target_id', $village->id)->sole();
// Spears that lose to the band on their own (15 defence each) but hold with +10%.
$attack = $raid->units['sword'] * 25;
$spears = (int) ceil($attack / 16.5 + 0.01);
expect($spears * 15)->toBeLessThan($attack)->and($spears * 16.5)->toBeGreaterThan($attack);
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'spear', 'count' => $spears]);
$this->travelTo($raid->arrives_at);
$this->artisan('game:events --once')->assertSuccessful();
expect(Report::where('player_id', $village->player_id)->sole()->payload['won'])->toBeFalse();
});