155 lines
7.5 KiB
PHP
155 lines
7.5 KiB
PHP
<?php
|
||
|
||
use App\Actions\CancelOrder;
|
||
use App\Actions\JoinWorld;
|
||
use App\Actions\Levy;
|
||
use App\Actions\QueueBuild;
|
||
use App\Actions\QueueDemolish;
|
||
use App\Actions\QueueUpgrade;
|
||
use App\Actions\Recruit;
|
||
use App\Models\User;
|
||
use App\Models\Village;
|
||
use App\Models\World;
|
||
use Carbon\CarbonImmutable;
|
||
use Database\Seeders\WorldSeeder;
|
||
|
||
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-11-03 12:00:00')));
|
||
|
||
/** A start village on the speed 100 dev world. */
|
||
function devVillage(): Village
|
||
{
|
||
(new WorldSeeder)->run();
|
||
|
||
return app(JoinWorld::class)(User::factory()->create(), World::where('slug', 'dev')->value('id'));
|
||
}
|
||
|
||
/** A start village (24 villagers, 5 of them working) with extra buildings and soldiers at home. */
|
||
function crowded(array $buildings = [], array $troops = []): Village
|
||
{
|
||
$village = startVillage();
|
||
foreach ($buildings as [$type, $level]) {
|
||
$village->buildings()->create(['type' => $type, 'level' => $level]);
|
||
}
|
||
foreach ($troops as $unit => $count) {
|
||
$village->troops()->create(['home_village_id' => $village->id, 'unit' => $unit, 'count' => $count]);
|
||
}
|
||
|
||
return $village->fresh();
|
||
}
|
||
|
||
it('holds the same heads on the speed 100 dev world, with food made and eaten 100 times as fast', function () {
|
||
$fast = shown(devVillage());
|
||
$slow = shown(startVillage());
|
||
|
||
expect($fast['population'])->toEqual($slow['population'])
|
||
->and($fast['population'])->toMatchArray(['food_budget' => 30, 'upkeep' => 24, 'villagers' => 24, 'idle' => 19])
|
||
->and($fast['rates']['food'])->toEqual(100 * $slow['rates']['food'])
|
||
->and($fast['rates']['food'])->toEqual(600);
|
||
});
|
||
|
||
it('trains no more on the dev world than its heads allow, and starves at world speed', function () {
|
||
$village = devVillage();
|
||
$barracks = $village->buildings()->create(['type' => 'barracks', 'level' => 1]);
|
||
|
||
expect(rule(fn () => app(Recruit::class)(owner($village), $village->id, $barracks->id, 'spear', 25)))->toBe('villagers');
|
||
|
||
// 10 light cavalry eat 30 heads, all that a farm 1 feeds: no villagers are left, the
|
||
// farm makes a tenth of its 3000 food/h and the riders eat 3000.
|
||
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'light_cavalry', 'count' => 10]);
|
||
$state = shown($village->fresh());
|
||
$rate = 3000 * exp(-2.3) - 3000;
|
||
expect($state['population'])->toMatchArray(['villagers' => 0, 'upkeep' => 30])
|
||
->and($state['rates']['food'])->toEqualWithDelta($rate, 0.01)
|
||
->and(CarbonImmutable::parse($state['starvation']['food_empty_at'])->getTimestampMs())
|
||
->toEqualWithDelta(now()->getTimestampMs() + 300 / -$rate * 3_600_000, 1000);
|
||
|
||
// 300 food last 400 s. The grace is a day at speed 1, 864 s at speed 100; then just
|
||
// enough riders desert.
|
||
$this->travel(7)->minutes();
|
||
$this->artisan('game:events --once')->assertSuccessful();
|
||
$this->travel(864)->seconds();
|
||
$this->artisan('game:events --once')->assertSuccessful();
|
||
$state = shown($village);
|
||
expect($state['troops']->light_cavalry)->toBeLessThan(10)
|
||
->and($state['rates']['food'])->toBeGreaterThanOrEqual(0);
|
||
});
|
||
|
||
it('refuses a new building without idle villagers for its workers, and the queue reserves them', function () {
|
||
// 24 villagers − 17 spearmen = 7, the start buildings need 5: 2 are idle.
|
||
$village = crowded(troops: ['spear' => 17]);
|
||
$state = shown($village);
|
||
$options = collect($state['buildable'])->keyBy('type');
|
||
|
||
expect($state['population'])->toMatchArray(['villagers' => 7, 'idle' => 2, 'reserved' => 0])
|
||
->and($options['barracks'])->toMatchArray(['villagers' => 3, 'blocked' => 'villagers'])
|
||
->and($options['timber_camp'])->toMatchArray(['villagers' => 2, 'blocked' => null])
|
||
->and($options['farm'])->toMatchArray(['villagers' => 0, 'blocked' => null])
|
||
->and($options['hiding_place'])->toMatchArray(['villagers' => 0, 'blocked' => null])
|
||
->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'barracks')))->toBe('villagers');
|
||
|
||
app(QueueBuild::class)(owner($village), $village->id, 'timber_camp');
|
||
$state = shown($village);
|
||
$options = collect($state['buildable'])->keyBy('type');
|
||
expect($state['population'])->toMatchArray(['idle' => 0, 'reserved' => 2])
|
||
->and($state['queue'][0]['villagers'])->toBe(2)
|
||
->and($options['clay_pit']['blocked'])->toBe('villagers')
|
||
->and($options['farm']['blocked'])->toBeNull()
|
||
->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'clay_pit')))->toBe('villagers');
|
||
|
||
// A farm needs no idle villagers: it brings more than it employs.
|
||
app(QueueBuild::class)(owner($village), $village->id, 'farm');
|
||
|
||
// Cancelling frees the reserved workers at once.
|
||
app(CancelOrder::class)(owner($village), $village->id, $state['queue'][0]['id']);
|
||
expect(shown($village)['population'])->toMatchArray(['idle' => 2, 'reserved' => 0]);
|
||
});
|
||
|
||
it('refuses an upgrade without idle villagers until the farm grows', function () {
|
||
// A level 2 timber camp needs 2 more: 24 − 17 spearmen = 7 villagers for 7 workers.
|
||
$village = crowded([['timber_camp', 2]], ['spear' => 17]);
|
||
$state = shown($village);
|
||
$camp = collect($state['buildings'])->firstWhere('type', 'timber_camp');
|
||
$farm = collect($state['buildings'])->firstWhere('type', 'farm');
|
||
|
||
// Level 3 needs round(2 × 1.12²) = 3 workers, one more.
|
||
expect($state['population']['idle'])->toBe(0)
|
||
->and($camp['upgrade'])->toMatchArray(['target_level' => 3, 'villagers' => 1, 'blocked' => 'villagers'])
|
||
->and($farm['upgrade'])->toMatchArray(['target_level' => 2, 'villagers' => 0, 'blocked' => null])
|
||
->and(rule(fn () => app(QueueUpgrade::class)(owner($village), $village->id, $camp['id'])))->toBe('villagers');
|
||
|
||
// Farm 2 feeds 37.5 heads: 30 − 17 = 13 villagers for 7 workers.
|
||
app(QueueUpgrade::class)(owner($village), $village->id, $farm['id']);
|
||
$this->travel(144)->seconds();
|
||
$state = shown($village);
|
||
expect($state['population'])->toMatchArray(['villagers' => 13, 'idle' => 6])
|
||
->and(collect($state['buildings'])->firstWhere('type', 'timber_camp')['upgrade']['blocked'])->toBeNull();
|
||
|
||
app(QueueUpgrade::class)(owner($village), $village->id, $camp['id']);
|
||
expect(shown($village)['population'])->toMatchArray(['idle' => 5, 'reserved' => 1]);
|
||
});
|
||
|
||
it('frees the workers of a demolished building when the demolition finishes', function () {
|
||
// Barracks 1 needs 3: 24 − 16 spearmen = 8 villagers for 8 workers.
|
||
$village = crowded([['barracks', 1]], ['spear' => 16]);
|
||
$barracks = collect(shown($village)['buildings'])->firstWhere('type', 'barracks');
|
||
|
||
app(QueueDemolish::class)(owner($village), $village->id, $barracks['id']);
|
||
expect(shown($village)['population']['idle'])->toBe(0);
|
||
|
||
$this->travel(5)->minutes();
|
||
$state = shown($village);
|
||
expect(collect($state['buildings'])->firstWhere('type', 'barracks'))->toBeNull()
|
||
->and($state['population']['idle'])->toBe(3)
|
||
->and(collect($state['buildable'])->firstWhere('type', 'timber_camp')['blocked'])->toBeNull();
|
||
});
|
||
|
||
it('does not count drafted militia as idle', function () {
|
||
$village = startVillage();
|
||
app(Levy::class)(owner($village), $village->id, 20);
|
||
$state = shown($village);
|
||
|
||
expect($state['population']['idle'])->toBe(0)
|
||
->and(collect($state['buildable'])->firstWhere('type', 'timber_camp')['blocked'])->toBe('villagers')
|
||
->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'timber_camp')))->toBe('villagers');
|
||
});
|