From e8c4382d7876171cca0fb96cbba0513331ac4cc5 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 28 Oct 2026 19:45:03 +0100 Subject: [PATCH] feat: map.view and barbarian villages seeded per world Co-Authored-By: Claude Opus 5.5 --- app/Actions/SeedBarbarians.php | 52 +++++++++++++++++ app/Actions/ShowMap.php | 65 +++++++++++++++++++++ app/Websocket/Controllers/MapController.php | 25 ++++++++ database/seeders/WorldSeeder.php | 20 ++++--- tests/Feature/MapTest.php | 47 +++++++++++++++ 5 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 app/Actions/SeedBarbarians.php create mode 100644 app/Actions/ShowMap.php create mode 100644 app/Websocket/Controllers/MapController.php create mode 100644 tests/Feature/MapTest.php diff --git a/app/Actions/SeedBarbarians.php b/app/Actions/SeedBarbarians.php new file mode 100644 index 0000000..1708685 --- /dev/null +++ b/app/Actions/SeedBarbarians.php @@ -0,0 +1,52 @@ +villages()->whereNull('player_id')->exists()) { + return 0; + } + + $config = $world->gameConfig()->get('world.map.barbarians'); + $centre = intdiv($world->gameConfig()->get('world.map.size', 1000), 2); + mt_srand(crc32($world->slug)); + + return DB::transaction(function () use ($world, $config, $centre) { + $taken = VillageFactory::takenSpots($world); + $created = 0; + for ($tries = 0; $created < $config['count'] && $tries < $config['count'] * 20; $tries++) { + $x = $centre + mt_rand(-$config['radius'], $config['radius']); + $y = $centre + mt_rand(-$config['radius'], $config['radius']); + // Leave the centre ring free for player spawns (even coordinates near the middle). + if (isset($taken["$x|$y"]) || (abs($x - $centre) < 6 && abs($y - $centre) < 6)) { + continue; + } + $taken["$x|$y"] = true; + $levels = []; + foreach (self::BUILDINGS as $type) { + $levels[$type] = mt_rand(0, $config['max_level']); + } + $this->villages->create($world, null, 'Barbarian village', $x, $y, $levels); + $created++; + } + + return $created; + }); + } +} diff --git a/app/Actions/ShowMap.php b/app/Actions/ShowMap.php new file mode 100644 index 0000000..1fb2609 --- /dev/null +++ b/app/Actions/ShowMap.php @@ -0,0 +1,65 @@ +gameConfig(); + $size = $config->get('world.map.size', 1000); + $max = $config->get('world.map.max_view', 41); + $w = max(1, min($max, $w)); + $h = max(1, min($max, $h)); + + if ($x === null || $y === null) { + $home = Village::whereIn('player_id', Player::where('user_id', $user->id)->where('world_id', $world->id)->select('id')) + ->orderBy('id')->first(); + $x = $home->x ?? intdiv($size, 2); + $y = $home->y ?? intdiv($size, 2); + } + $x = max(0, min($size - 1, $x)); + $y = max(0, min($size - 1, $y)); + $left = max(0, min($size - $w, $x - intdiv($w, 2))); + $top = max(0, min($size - $h, $y - intdiv($h, 2))); + + $villages = Village::where('world_id', $world->id) + ->whereBetween('x', [$left, $left + $w - 1]) + ->whereBetween('y', [$top, $top + $h - 1]) + ->with('player:id,user_id,name') + ->get(['id', 'player_id', 'name', 'x', 'y', 'points']) + ->map(fn (Village $v) => [ + 'id' => $v->id, + 'x' => $v->x, + 'y' => $v->y, + 'name' => $v->name, + 'points' => $v->points, + 'continent' => Continent::of($v->x, $v->y), + 'player' => $v->player ? ['id' => $v->player->id, 'name' => $v->player->name] : null, + 'own' => $v->player?->user_id === $user->id, + ])->values()->all(); + + return [ + 'world_id' => $world->id, + 'size' => $size, + 'center' => ['x' => $x, 'y' => $y], + 'x' => $left, + 'y' => $top, + 'w' => $w, + 'h' => $h, + 'villages' => $villages, + ]; + } +} diff --git a/app/Websocket/Controllers/MapController.php b/app/Websocket/Controllers/MapController.php new file mode 100644 index 0000000..a39ecfd --- /dev/null +++ b/app/Websocket/Controllers/MapController.php @@ -0,0 +1,25 @@ +play(fn () => app(ShowMap::class)( + auth()->user(), + (int) request('world_id'), + is_numeric($x) ? (int) $x : null, + is_numeric($y) ? (int) $y : null, + (int) (request('w') ?: 15), + (int) (request('h') ?: 15), + )); + } +} diff --git a/database/seeders/WorldSeeder.php b/database/seeders/WorldSeeder.php index dd60459..7240e28 100644 --- a/database/seeders/WorldSeeder.php +++ b/database/seeders/WorldSeeder.php @@ -2,19 +2,25 @@ namespace Database\Seeders; +use App\Actions\SeedBarbarians; use App\Models\World; use Illuminate\Database\Seeder; -/** The default world and a fast dev world. Safe to run on every deploy. */ +/** The default world and a fast dev world, with their barbarians. Safe to run on every deploy. */ class WorldSeeder extends Seeder { public function run(): void { - World::firstOrCreate(['slug' => 'w1'], ['name' => 'World 1', 'started_at' => now()]); - World::firstOrCreate(['slug' => 'dev'], [ - 'name' => 'Dev world', - 'config' => ['world' => ['speed' => 100]], - 'started_at' => now(), - ]); + $worlds = [ + World::firstOrCreate(['slug' => 'w1'], ['name' => 'World 1', 'started_at' => now()]), + World::firstOrCreate(['slug' => 'dev'], [ + 'name' => 'Dev world', + 'config' => ['world' => ['speed' => 100]], + 'started_at' => now(), + ]), + ]; + foreach ($worlds as $world) { + app(SeedBarbarians::class)($world); + } } } diff --git a/tests/Feature/MapTest.php b/tests/Feature/MapTest.php new file mode 100644 index 0000000..bdade4e --- /dev/null +++ b/tests/Feature/MapTest.php @@ -0,0 +1,47 @@ +create(['name' => 'Bea'])); + + $map = app(ShowMap::class)(owner($village), $village->world_id, null, null, 15, 15); + + expect($map)->toMatchArray(['center' => ['x' => 500, 'y' => 500], 'x' => 493, 'y' => 493, 'w' => 15, 'h' => 15, 'size' => 1000]) + ->and(collect($map['villages'])->keyBy('id')->sortKeys()->map(fn ($v) => [$v['x'], $v['y'], $v['own'], $v['player']['name'] ?? null, $v['continent']])->all()) + ->toBe([ + $village->id => [500, 500, true, owner($village)->name, 'K55'], + $other->id => [498, 498, false, 'Bea', 'K44'], + ]); +}); + +it('shows only the window around the given centre, kept inside the map', function () { + $village = startVillage(); + + $far = app(ShowMap::class)(owner($village), $village->world_id, 100, 100, 15, 15); + $edge = app(ShowMap::class)(owner($village), $village->world_id, 2, 998, 15, 999); + + expect($far['villages'])->toBe([]) + ->and($edge)->toMatchArray(['x' => 0, 'y' => 959, 'w' => 15, 'h' => 41]); +}); + +it('seeds barbarian villages once per world, away from the spawn centre', function () { + config(['game.world.map.barbarians' => ['count' => 20, 'radius' => 20, 'max_level' => 3]]); + $village = startVillage(); + $world = World::find($village->world_id); + + $barbarians = Village::where('world_id', $world->id)->whereNull('player_id')->get(); + expect($barbarians)->toHaveCount(20) + ->and($barbarians->every(fn ($v) => abs($v->x - 500) <= 20 && abs($v->y - 500) <= 20))->toBeTrue() + ->and($barbarians->every(fn ($v) => abs($v->x - 500) >= 6 || abs($v->y - 500) >= 6))->toBeTrue() + ->and($barbarians->every(fn ($v) => $v->points > 0 && $v->buildings()->count() === 6))->toBeTrue() + ->and(app(SeedBarbarians::class)($world))->toBe(0); + + $map = app(ShowMap::class)(owner($village), $world->id, 500, 500, 41, 41); + expect(collect($map['villages'])->whereNull('player'))->toHaveCount(20); +});