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

130 lines
5.7 KiB
PHP
Raw Normal View History

<?php
use App\Actions\QueueUpgrade;
use App\Engine\Simulation;
use App\Engine\Starvation;
use App\Engine\VillageFactory;
use App\Game\ConfigLint;
use App\Game\Map\Season;
use App\Models\GameEvent;
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')));
function paceMigration(): object
{
return require database_path('migrations/2026_11_03_210000_world_one_at_speed_four.php');
}
function worldOne(): World
{
return World::where('slug', 'w1')->sole();
}
it('makes a new World 1 at speed 4 with armies at half speed', function () {
config(['worlds' => require config_path('worlds.php')]);
(new WorldSeeder)->run();
expect(worldOne()->config)->toBe(['world' => ['speed' => 4], 'movement' => ['unit_speed' => 0.5]])
->and(ConfigLint::check(worldOne()->gameConfig()))->toBe([])
->and(World::where('slug', 'dev')->value('config'))->toBe(['world' => ['speed' => 100]])
// Already at that pace: the migration leaves it alone.
->and(paceMigration()->speedUp(now()->toImmutable()))->toBeFalse();
});
it('sets the live World 1 to speed 4 and unit speed 0.5 and keeps the rest of its config', function () {
startVillage();
worldOne()->update(['config' => ['combat' => ['luck' => 0], 'world' => ['inactive_days' => 21]]]);
expect(paceMigration()->speedUp(now()->toImmutable()))->toBeTrue()
->and(worldOne()->config)->toBe(['combat' => ['luck' => 0], 'world' => ['inactive_days' => 21, 'speed' => 4], 'movement' => ['unit_speed' => 0.5]])
->and(World::where('slug', 'dev')->value('config'))->toBe(['world' => ['speed' => 100]]);
});
it('settles every village at speed 1 before the change, so its stock is not counted again at speed 4', function () {
$village = startVillage();
$barbarian = app(VillageFactory::class)->create($village->world, null, 'Barbarian village', $village->x + 3, $village->y, ['headquarters' => 1, 'storage' => 1, 'farm' => 1, 'timber_camp' => 1]);
Village::whereKey($village->id)->update(['loyalty' => 50, 'loyalty_at' => now()]);
// Two hours at 5 wood an hour, and 1 loyalty an hour.
$this->travel(2)->hours();
paceMigration()->speedUp(now()->toImmutable());
expect($village->fresh()->only(['wood', 'loyalty']))->toEqual(['wood' => 510.0, 'loyalty' => 52.0])
->and($barbarian->fresh()->wood)->toEqualWithDelta(500 + 2 * 35, 0.01);
// Then 20 wood and 4 loyalty an hour.
$this->travel(1)->hours();
$state = shown($village);
expect($state['resources']['wood'])->toBe(530.0)
->and($state['loyalty'])->toEqual(56);
});
it('sets the food timer for the new rates', function () {
// 10 light cavalry eat 30 heads: no villagers are left to work the farm.
$village = startVillage();
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'light_cavalry', 'count' => 10]);
$village = $village->fresh();
Starvation::of($village)->sync(now()->toImmutable());
$village->save();
$before = Simulation::of($village->fresh())->rates()['food'];
paceMigration()->speedUp(now()->toImmutable());
$village = $village->fresh();
$rate = Simulation::of($village)->rates()['food'];
$runsOut = GameEvent::where('type', GameEvent::FOOD_RUNS_OUT)->whereNull('processed_at')->sole()->fire_at;
expect($rate)->toEqualWithDelta($before * 4, 0.0001)
->and($runsOut->getTimestampMs())->toEqualWithDelta(now()->getTimestampMs() + $village->food / -$rate * 3_600_000, 1000);
});
it('keeps the timers already set and runs only new ones at speed 4', function () {
$village = startVillage();
$hq = $village->buildings->firstWhere('type', 'headquarters');
$hq->update(['level' => 5]);
Village::whereKey($village->id)->update(['wood' => 1000, 'clay' => 1000, 'iron' => 1000]);
app(QueueUpgrade::class)(owner($village), $village->id, $hq->id);
app(QueueUpgrade::class)(owner($village), $village->id, $hq->id);
// HQ 6 at HQ 5: 900 × 1.50819 ÷ 1.05^5 = 1064 s at speed 1. HQ 7 waits for it.
$event = GameEvent::where('type', GameEvent::BUILD_ORDER_FINISHED)->sole();
expect($event->fire_at->toDateTimeString())->toBe('2026-11-03 12:17:44');
$this->travel(10)->minutes();
paceMigration()->speedUp(now()->toImmutable());
expect($event->fresh()->fire_at->toDateTimeString())->toBe('2026-11-03 12:17:44');
// HQ 7 at HQ 6 starts then at speed 4: 900 × 2.15872 ÷ 1.05^6 ÷ 4 = 362 s.
$this->travelTo(CarbonImmutable::parse('2026-11-03 12:17:44'));
expect(collect(shown($village)['queue'])->sole()['finishes_at'])->toBe('2026-11-03T12:23:46.000Z');
});
it('moves the world clock so the days played stay played and the crowns do not come at once', function () {
startVillage();
worldOne()->update(['started_at' => now()->subDays(8)]);
paceMigration()->speedUp(now()->toImmutable());
// Eight world days played: 22 to go, 5.5 real days at speed 4.
$world = worldOne();
expect($world->started_at->toDateTimeString())->toBe('2026-11-01 12:00:00')
->and((new Season($world->gameConfig()))->crownsAt($world->started_at)->toDateTimeString())->toBe('2026-11-09 00:00:00');
});
it('changes nothing on a second run, and nothing without a World 1', function () {
paceMigration()->up();
expect(World::count())->toBe(0);
$village = startVillage();
$this->travel(1)->hours();
paceMigration()->speedUp(now()->toImmutable());
$settled = $village->fresh()->resources_at;
$started = worldOne()->started_at;
$this->travel(1)->hours();
expect(paceMigration()->speedUp(now()->toImmutable()))->toBeFalse()
->and($village->fresh()->resources_at)->toEqual($settled)
->and(worldOne()->started_at)->toEqual($started);
});