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 <noreply@anthropic.com>
This commit is contained in:
parent
9a1f472436
commit
153e7eb19d
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// The worlds WorldSeeder makes when they don't exist yet, by slug: name and config
|
||||||
|
// overrides (worlds.config). A world that exists keeps its own config; the seeder never
|
||||||
|
// changes it.
|
||||||
|
return [
|
||||||
|
// Die Stämme's classic pace: building and production 4 times as fast, armies at half
|
||||||
|
// unit speed, so they march twice as fast (pace.md in the research repo).
|
||||||
|
'w1' => [
|
||||||
|
'name' => 'World 1',
|
||||||
|
'config' => ['world' => ['speed' => 4], 'movement' => ['unit_speed' => 0.5]],
|
||||||
|
],
|
||||||
|
'dev' => [
|
||||||
|
'name' => 'Dev world',
|
||||||
|
'config' => ['world' => ['speed' => 100]],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Engine\Nobility;
|
||||||
|
use App\Engine\Simulation;
|
||||||
|
use App\Engine\Starvation;
|
||||||
|
use App\Models\Village;
|
||||||
|
use App\Models\World;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
// World 1 runs at speed 4 with armies at unit speed 0.5, Die Stämme's classic pace
|
||||||
|
// (config/worlds.php, where WorldSeeder takes it from for a new World 1). The live w1
|
||||||
|
// row gets these two keys and keeps the rest of its config.
|
||||||
|
//
|
||||||
|
// Lazy values are worth world speed times the time passed, so every w1 village is
|
||||||
|
// settled first, at the old speed: its stock and loyalty, so what speed 1 earned isn't
|
||||||
|
// counted again at speed 4. Then its food timer follows the new rates. The world's own
|
||||||
|
// clock moves the same way: started_at moves up so the world days already played stay
|
||||||
|
// played, and the crowns still come after 30 world days, not at once. Timers already
|
||||||
|
// set keep their times: build orders under way, recruits, movements, levies, waves and
|
||||||
|
// events. Only new ones run at the new speed.
|
||||||
|
//
|
||||||
|
// A fresh database has no w1 when this runs, and a w1 that already runs at this pace
|
||||||
|
// is left alone, so running it again changes nothing.
|
||||||
|
public const PACE = ['world.speed' => 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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -8,22 +8,22 @@ use App\Models\World;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default world and a fast dev world. They start empty: villages, barbarian ones
|
* The default world and a fast dev world (config/worlds.php). They start empty:
|
||||||
* included, come with the players who join and the barbarian waves near them. Safe to
|
* villages, barbarian ones included, come with the players who join and the barbarian
|
||||||
* run on every deploy.
|
* waves near them. A world that exists keeps its config. Safe to run on every deploy.
|
||||||
*/
|
*/
|
||||||
class WorldSeeder extends Seeder
|
class WorldSeeder extends Seeder
|
||||||
{
|
{
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
$worlds = [
|
$worlds = [];
|
||||||
World::firstOrCreate(['slug' => 'w1'], ['name' => 'World 1', 'started_at' => now()]),
|
foreach (config('worlds') as $slug => $world) {
|
||||||
World::firstOrCreate(['slug' => 'dev'], [
|
$worlds[] = World::firstOrCreate(['slug' => $slug], [
|
||||||
'name' => 'Dev world',
|
'name' => $world['name'],
|
||||||
'config' => ['world' => ['speed' => 100]],
|
'config' => ($world['config'] ?? []) ?: null,
|
||||||
'started_at' => now(),
|
'started_at' => now(),
|
||||||
]),
|
]);
|
||||||
];
|
}
|
||||||
// Barbarian villages that lost their growth event get it back, and their garrison;
|
// Barbarian villages that lost their growth event get it back, and their garrison;
|
||||||
// player villages get a raid planned.
|
// player villages get a raid planned.
|
||||||
foreach ($worlds as $world) {
|
foreach ($worlds as $world) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,129 @@
|
||||||
|
<?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);
|
||||||
|
});
|
||||||
|
|
@ -13,13 +13,15 @@ use Database\Seeders\WorldSeeder;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Tests\TestCase;
|
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)
|
pest()->extend(TestCase::class)->use(RefreshDatabase::class)
|
||||||
->beforeEach(fn () => config([
|
->beforeEach(fn () => config([
|
||||||
'game.world.map.barbarians.per_join' => 0,
|
'game.world.map.barbarians.per_join' => 0,
|
||||||
'game.world.map.barbarians.waves.enabled' => false,
|
'game.world.map.barbarians.waves.enabled' => false,
|
||||||
'game.raids.enabled' => false,
|
'game.raids.enabled' => false,
|
||||||
'game.world.inactive_days' => 0,
|
'game.world.inactive_days' => 0,
|
||||||
|
'worlds.w1.config' => [],
|
||||||
]))
|
]))
|
||||||
->in('Feature');
|
->in('Feature');
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
->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 () {
|
it('is spotted once it comes within visibility plus detection', function () {
|
||||||
$travel = new TravelTime(gameConfig());
|
$travel = new TravelTime(gameConfig());
|
||||||
$seconds = $travel->seconds(['spear' => 1], 34.0, 'normal');
|
$seconds = $travel->seconds(['spear' => 1], 34.0, 'normal');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue