From 153e7eb19df0fdb37b20856c6b2ac625364fdd6f Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Tue, 3 Nov 2026 12:14:21 +0100 Subject: [PATCH] feat: World 1 runs at speed 4 with armies at unit speed 0.5; the live world is settled at speed 1 first and keeps its played days and running timers Co-Authored-By: Claude Opus 5.5 --- config/worlds.php | 17 +++ ...6_11_03_210000_world_one_at_speed_four.php | 90 ++++++++++++ database/seeders/WorldSeeder.php | 20 +-- tests/Feature/WorldOnePaceTest.php | 129 ++++++++++++++++++ tests/Pest.php | 4 +- tests/Unit/Game/TravelTimeTest.php | 6 + 6 files changed, 255 insertions(+), 11 deletions(-) create mode 100644 config/worlds.php create mode 100644 database/migrations/2026_11_03_210000_world_one_at_speed_four.php create mode 100644 tests/Feature/WorldOnePaceTest.php diff --git a/config/worlds.php b/config/worlds.php new file mode 100644 index 0000000..be450d9 --- /dev/null +++ b/config/worlds.php @@ -0,0 +1,17 @@ + [ + 'name' => 'World 1', + 'config' => ['world' => ['speed' => 4], 'movement' => ['unit_speed' => 0.5]], + ], + 'dev' => [ + 'name' => 'Dev world', + 'config' => ['world' => ['speed' => 100]], + ], +]; diff --git a/database/migrations/2026_11_03_210000_world_one_at_speed_four.php b/database/migrations/2026_11_03_210000_world_one_at_speed_four.php new file mode 100644 index 0000000..721313a --- /dev/null +++ b/database/migrations/2026_11_03_210000_world_one_at_speed_four.php @@ -0,0 +1,90 @@ + 4, 'movement.unit_speed' => 0.5]; + + public function up(): void + { + $this->speedUp(now()->toImmutable()); + } + + public function down(): void + { + // What the villages earned at speed 4 can't be taken back: World 1 keeps its pace. + } + + /** Whether World 1 changed pace. */ + public function speedUp(CarbonImmutable $now): bool + { + return DB::transaction(function () use ($now) { + $world = World::where('slug', 'w1')->lockForUpdate()->first(); + if (! $world) { + return false; + } + $before = $world->gameConfig(); + if (collect(self::PACE)->every(fn ($value, string $key) => (float) $before->get($key) === (float) $value)) { + return false; + } + + $ids = Village::where('world_id', $world->id)->orderBy('id')->lockForUpdate()->pluck('id'); + foreach ($ids as $id) { + $this->settle(Village::find($id), $now); + } + + $config = $world->config ?? []; + foreach (self::PACE as $key => $value) { + Arr::set($config, $key, $value); + } + $world->config = $config; + if ($world->started_at?->lessThan($now)) { + $played = $now->getTimestampMs() - $world->started_at->getTimestampMs(); + $world->started_at = $now->subMilliseconds((int) round($played * $before->speed() / self::PACE['world.speed'])); + } + $world->save(); + + // Read again, so the village sees its world at the new pace. + foreach ($ids as $id) { + $village = Village::find($id); + Starvation::of($village, Simulation::of($village))->sync($now); + $village->save(); + } + + return true; + }); + } + + private function settle(Village $village, CarbonImmutable $now): void + { + Simulation::of($village)->settle($now); + if ($village->loyalty_at) { + $village->loyalty = Nobility::of($village)->loyalty($village, $now); + $village->loyalty_at = $now; + } + $village->save(); + } +}; diff --git a/database/seeders/WorldSeeder.php b/database/seeders/WorldSeeder.php index 41ba75b..658ea93 100644 --- a/database/seeders/WorldSeeder.php +++ b/database/seeders/WorldSeeder.php @@ -8,22 +8,22 @@ use App\Models\World; use Illuminate\Database\Seeder; /** - * The default world and a fast dev world. They start empty: villages, barbarian ones - * included, come with the players who join and the barbarian waves near them. Safe to - * run on every deploy. + * The default world and a fast dev world (config/worlds.php). They start empty: + * villages, barbarian ones included, come with the players who join and the barbarian + * waves near them. A world that exists keeps its config. Safe to run on every deploy. */ class WorldSeeder extends Seeder { public function run(): void { - $worlds = [ - World::firstOrCreate(['slug' => 'w1'], ['name' => 'World 1', 'started_at' => now()]), - World::firstOrCreate(['slug' => 'dev'], [ - 'name' => 'Dev world', - 'config' => ['world' => ['speed' => 100]], + $worlds = []; + foreach (config('worlds') as $slug => $world) { + $worlds[] = World::firstOrCreate(['slug' => $slug], [ + 'name' => $world['name'], + 'config' => ($world['config'] ?? []) ?: null, 'started_at' => now(), - ]), - ]; + ]); + } // Barbarian villages that lost their growth event get it back, and their garrison; // player villages get a raid planned. foreach ($worlds as $world) { diff --git a/tests/Feature/WorldOnePaceTest.php b/tests/Feature/WorldOnePaceTest.php new file mode 100644 index 0000000..0c22928 --- /dev/null +++ b/tests/Feature/WorldOnePaceTest.php @@ -0,0 +1,129 @@ + $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); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 04ce2ec..133d122 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -13,13 +13,15 @@ use Database\Seeders\WorldSeeder; use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; -// Feature tests start without barbarians, their waves, raids and the inactivity rule unless a test asks for them. +// Feature tests start without barbarians, their waves, raids and the inactivity rule unless a test asks for them, +// and the seeder makes World 1 at speed 1 instead of its live pace (config/worlds.php). pest()->extend(TestCase::class)->use(RefreshDatabase::class) ->beforeEach(fn () => config([ 'game.world.map.barbarians.per_join' => 0, 'game.world.map.barbarians.waves.enabled' => false, 'game.raids.enabled' => false, 'game.world.inactive_days' => 0, + 'worlds.w1.config' => [], ])) ->in('Feature'); diff --git a/tests/Unit/Game/TravelTimeTest.php b/tests/Unit/Game/TravelTimeTest.php index 7651a58..9f1ca68 100644 --- a/tests/Unit/Game/TravelTimeTest.php +++ b/tests/Unit/Game/TravelTimeTest.php @@ -24,6 +24,12 @@ it('speeds up with the travel mode, world speed and unit speed', function () { ->and($fast->seconds(['spear' => 1], 5.0, 'normal'))->toBe(108); }); +it('marches a spear 9 minutes a field on World 1, speed 4 with armies at half speed', function () { + $w1 = new TravelTime(gameConfig(['world' => ['speed' => 4], 'movement' => ['unit_speed' => 0.5]])); + + expect($w1->seconds(['spear' => 1], 5.0, 'normal'))->toBe(9 * 5 * 60); +}); + it('is spotted once it comes within visibility plus detection', function () { $travel = new TravelTime(gameConfig()); $seconds = $travel->seconds(['spear' => 1], 34.0, 'normal');