travelTo(CarbonImmutable::parse('2026-11-03 12:00:00')); config(['game.world.map.barbarians.waves.enabled' => true, 'game.world.map.barbarians.waves.per_player' => 2]); }); /** The world's barbarian villages, in id order. */ function waveBarbarians(World $world) { return Village::where('world_id', $world->id)->whereNull('player_id')->orderBy('id')->get(); } it('settles new barbarian villages near the player once a wave is due, and plans the next', function () { $village = startVillage(); $world = $village->world->fresh(); $first = $world->barbarians_at; expect($first->between(now()->addDays(5), now()->addDays(9)))->toBeTrue(); $this->travelTo($first->subSecond()); $this->artisan('game:events --once')->assertSuccessful(); expect(waveBarbarians($world))->toHaveCount(0); $this->travelTo($first); $this->artisan('game:events --once')->assertSuccessful(); $new = waveBarbarians($world); expect($new)->toHaveCount(2) ->and($new->every(fn (Village $b) => hypot($b->x - $village->x, $b->y - $village->y) <= 8 + 1))->toBeTrue() ->and($new->every(fn (Village $b) => $b->points > 0 && $b->buildings()->count() === 6))->toBeTrue() ->and($new->every(fn (Village $b) => Barbarians::of($b)->current() == Barbarians::of($b)->full()))->toBeTrue() ->and(GameEvent::where('type', GameEvent::BARBARIAN_GROWS)->whereIn('village_id', $new->pluck('id'))->count())->toBe(2); $next = $world->fresh()->barbarians_at; expect($next->between($first->addDays(5), $first->addDays(9)))->toBeTrue(); // Ticking again at the same moment spawns nothing twice. Waves::tick(now()->toImmutable()); expect(waveBarbarians($world))->toHaveCount(2) ->and($world->fresh()->barbarians_at)->toEqual($next); }); it('brings 1.5 villages per player on average, never on a taken field', function () { config(['game.world.map.barbarians.waves.per_player' => 1.5]); $village = startVillage(); $world = $village->world->fresh(); $counts = []; for ($wave = 0; $wave < 6; $wave++) { $before = waveBarbarians($world)->count(); $this->travelTo($world->fresh()->barbarians_at); Waves::tick(now()->toImmutable()); $counts[] = waveBarbarians($world)->count() - $before; } $spots = Village::where('world_id', $world->id)->get()->map(fn (Village $v) => "$v->x|$v->y"); expect($counts)->each->toBeIn([1, 2]) ->and($spots->unique())->toHaveCount($spots->count()); }); it('leaves out players who have not been seen within inactive_days', function () { config(['game.world.inactive_days' => 14]); $active = startVillage(); $idle = startVillage(); $idle->player->update(['last_active_at' => now()->subDays(20)]); $world = $active->world->fresh(); $this->travelTo($world->barbarians_at); expect(Waves::wave($world->id, now()->toImmutable()))->toBe([$active->id]) ->and(waveBarbarians($world))->toHaveCount(2); }); it('brings no wave to an ended world', function () { $village = startVillage(); $world = $village->world->fresh(); $at = $world->barbarians_at; $world->update(['ended_at' => now(), 'winner_player_id' => $village->player_id]); $this->travelTo($at->addDay()); Waves::tick(now()->toImmutable()); expect(waveBarbarians($world))->toHaveCount(0) ->and($world->fresh()->barbarians_at)->toEqual($at) ->and(Waves::wave($world->id, now()->toImmutable()))->toBe([]); }); it('draws the same wave when it is replayed', function () { $village = startVillage(); $world = $village->world->fresh(); $this->travelTo($world->barbarians_at); $run = function () use ($world) { Waves::wave($world->id, now()->toImmutable()); return [waveBarbarians($world)->map(fn (Village $v) => "$v->x|$v->y")->all(), $world->fresh()->barbarians_at]; }; DB::beginTransaction(); $once = $run(); DB::rollBack(); expect($once[0])->toHaveCount(2) ->and(waveBarbarians($world))->toHaveCount(0) ->and($run())->toEqual($once); }); it('plans the first wave on seed and after a reset, and none while waves are off', function () { config(['game.world.map.barbarians.waves.enabled' => false]); (new WorldSeeder)->run(); Waves::tick(now()->toImmutable()); expect(World::whereNotNull('barbarians_at')->count())->toBe(0); config(['game.world.map.barbarians.waves.enabled' => true]); (new WorldSeeder)->run(); $w1 = World::where('slug', 'w1')->first(); $dev = World::where('slug', 'dev')->first(); expect($w1->barbarians_at->between(now()->addDays(5), now()->addDays(9)))->toBeTrue() // The speed-100 dev world: every 1.2 to 2.16 hours. ->and($dev->barbarians_at->between(now()->addMinutes(72), now()->addMinutes(130)))->toBeTrue(); $this->travel(1)->day(); $this->artisan('game:reset-world', ['slug' => 'w1', '--force' => true])->assertSuccessful(); expect($w1->fresh()->barbarians_at)->toBeNull(); Waves::tick(now()->toImmutable()); expect($w1->fresh()->barbarians_at->between(now()->addDays(5), now()->addDays(9)))->toBeTrue(); });