2026-10-28 18:17:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use App\Actions\JoinWorld;
|
|
|
|
|
use App\Actions\ListWorlds;
|
2026-10-28 23:07:56 +00:00
|
|
|
use App\Actions\RenameVillage;
|
2026-10-28 18:17:34 +00:00
|
|
|
use App\Actions\ShowVillage;
|
2026-10-28 20:32:09 +00:00
|
|
|
use App\Game\Map\Continent;
|
2026-10-28 18:17:34 +00:00
|
|
|
use App\Models\User;
|
2026-10-29 00:51:37 +00:00
|
|
|
use App\Models\Village;
|
2026-10-28 18:17:34 +00:00
|
|
|
use App\Models\World;
|
2026-10-29 00:51:37 +00:00
|
|
|
use Database\Seeders\WorldSeeder;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
2026-10-28 21:26:25 +00:00
|
|
|
it('starts a village with HQ, storage and farm at level 1', function () {
|
2026-10-28 18:17:34 +00:00
|
|
|
$village = startVillage();
|
|
|
|
|
|
2026-10-29 00:51:37 +00:00
|
|
|
// The first village of a world lands on the frontier at radius 0 ± 3: right at 0|0.
|
|
|
|
|
expect(hypot($village->x, $village->y))->toBeLessThanOrEqual(3 * M_SQRT2 + 0.01)
|
2026-10-28 21:16:45 +00:00
|
|
|
->and($village->only('space', 'points'))->toBe(['space' => 40, 'points' => 21])
|
2026-10-28 18:17:34 +00:00
|
|
|
->and($village->resources())->toBe(['wood' => 500.0, 'clay' => 500.0, 'iron' => 500.0, 'food' => 300.0])
|
2026-10-28 18:45:02 +00:00
|
|
|
->and($village->buildings->map->only('type', 'level')->all())->toBe([
|
2026-10-28 21:26:25 +00:00
|
|
|
['type' => 'headquarters', 'level' => 1],
|
|
|
|
|
['type' => 'storage', 'level' => 1],
|
|
|
|
|
['type' => 'farm', 'level' => 1],
|
2026-10-28 18:17:34 +00:00
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns the same village when joining again', function () {
|
|
|
|
|
$village = startVillage();
|
|
|
|
|
|
|
|
|
|
expect(app(JoinWorld::class)(owner($village), $village->world_id)->id)->toBe($village->id);
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-29 00:51:37 +00:00
|
|
|
it('spawns players on the frontier, never right next to another player', function () {
|
|
|
|
|
$villages = collect(range(1, 8))->map(fn () => startVillage(User::factory()->create()));
|
2026-10-28 18:17:34 +00:00
|
|
|
|
2026-10-28 20:32:09 +00:00
|
|
|
foreach ($villages as $a) {
|
|
|
|
|
foreach ($villages as $b) {
|
|
|
|
|
if ($a->isNot($b)) {
|
|
|
|
|
expect(max(abs($a->x - $b->x), abs($a->y - $b->y)))->toBeGreaterThanOrEqual(2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-28 18:17:34 +00:00
|
|
|
});
|
|
|
|
|
|
2026-10-29 00:51:37 +00:00
|
|
|
it('starts a player in the quarter they picked', function () {
|
|
|
|
|
(new WorldSeeder)->run();
|
|
|
|
|
$world = World::where('slug', 'w1')->value('id');
|
|
|
|
|
// A few villages first, so the frontier has moved off 0|0.
|
|
|
|
|
collect(range(1, 6))->each(fn () => app(JoinWorld::class)(User::factory()->create(), $world));
|
|
|
|
|
|
|
|
|
|
$nw = app(JoinWorld::class)(User::factory()->create(), $world, 'nw');
|
|
|
|
|
$se = app(JoinWorld::class)(User::factory()->create(), $world, 'se');
|
|
|
|
|
|
|
|
|
|
expect($nw->x)->toBeLessThanOrEqual(0)->and($nw->y)->toBeLessThanOrEqual(0)
|
|
|
|
|
->and($se->x)->toBeGreaterThanOrEqual(0)->and($se->y)->toBeGreaterThanOrEqual(0)
|
|
|
|
|
->and(rule(fn () => app(JoinWorld::class)(User::factory()->create(), $world, 'up')))->toBe('direction');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('brings barbarian villages with every new player, close to their village', function () {
|
|
|
|
|
config(['game.world.map.barbarians.per_join' => 2]);
|
|
|
|
|
$village = startVillage();
|
|
|
|
|
|
|
|
|
|
$barbarians = Village::where('world_id', $village->world_id)->whereNull('player_id')->get();
|
|
|
|
|
expect($barbarians)->toHaveCount(2)
|
|
|
|
|
->and($barbarians->every(fn ($b) => hypot($b->x - $village->x, $b->y - $village->y) <= 4 + 1))->toBeTrue()
|
|
|
|
|
->and($barbarians->every(fn ($b) => $b->points > 0 && $b->buildings()->count() === 6))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-28 18:17:34 +00:00
|
|
|
it('lists worlds with their speed and the player village', function () {
|
|
|
|
|
$village = startVillage();
|
|
|
|
|
$dev = World::where('slug', 'dev')->first();
|
|
|
|
|
|
2026-10-30 10:27:14 +00:00
|
|
|
$worlds = app(ListWorlds::class)(owner($village));
|
|
|
|
|
$keys = array_flip(['id', 'name', 'slug', 'speed', 'village_id', 'fallen']);
|
|
|
|
|
expect($worlds[0])->toMatchArray(['players' => 1, 'villages' => 1, 'tribes' => 0, 'holder' => null, 'ended_at' => null, 'winner' => null])
|
|
|
|
|
->and(array_map(fn (array $w) => array_intersect_key($w, $keys), $worlds))->toBe([
|
|
|
|
|
['id' => $village->world_id, 'name' => 'World 1', 'slug' => 'w1', 'speed' => 1.0, 'village_id' => $village->id, 'fallen' => false],
|
|
|
|
|
['id' => $dev->id, 'name' => 'Dev world', 'slug' => 'dev', 'speed' => 100.0, 'village_id' => null, 'fallen' => false],
|
|
|
|
|
]);
|
2026-10-28 18:17:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('shows a start village to its owner only', function () {
|
|
|
|
|
$village = startVillage();
|
|
|
|
|
$state = shown($village);
|
|
|
|
|
|
2026-10-28 21:16:45 +00:00
|
|
|
expect($state['space'])->toBe(['used' => 17, 'max' => 40])
|
2026-10-28 20:32:09 +00:00
|
|
|
->and($state['continent'])->toBe(Continent::of($village->x, $village->y))
|
2026-10-28 20:28:16 +00:00
|
|
|
->and($state['rates'])->toBe(['wood' => 5.0, 'clay' => 5.0, 'iron' => 5.0, 'food' => 6.0])
|
2026-10-28 18:17:34 +00:00
|
|
|
->and($state['storage_cap'])->toEqual(1000)
|
2026-10-28 20:39:48 +00:00
|
|
|
->and($state['population'])->toEqual(['food_budget' => 30, 'upkeep' => 24, 'villagers' => 24, 'workers' => 5, 'soldiers' => 0, 'training' => 0])
|
2026-10-28 18:17:34 +00:00
|
|
|
->and($state['builders'])->toBe(2)
|
|
|
|
|
->and(collect($state['buildable'])->firstWhere('type', 'headquarters')['blocked'])->toBe('max_per_village')
|
2026-10-28 19:58:35 +00:00
|
|
|
->and(collect($state['buildable'])->firstWhere('type', 'wall')['blocked'])->toBeNull()
|
2026-10-28 18:17:34 +00:00
|
|
|
->and(rule(fn () => app(ShowVillage::class)(User::factory()->create(), $village->id)))->toBe('not_found');
|
|
|
|
|
});
|
2026-10-28 23:07:56 +00:00
|
|
|
|
|
|
|
|
it('renames an own village, trimmed, and refuses empty or long names', function () {
|
|
|
|
|
$village = startVillage();
|
|
|
|
|
|
|
|
|
|
app(RenameVillage::class)(owner($village), $village->id, " Iron Hill \n");
|
|
|
|
|
expect($village->fresh()->name)->toBe('Iron Hill')
|
|
|
|
|
->and(rule(fn () => app(RenameVillage::class)(owner($village), $village->id, ' ')))->toBe('name')
|
|
|
|
|
->and(rule(fn () => app(RenameVillage::class)(owner($village), $village->id, str_repeat('x', 33))))->toBe('name')
|
|
|
|
|
->and(rule(fn () => app(RenameVillage::class)(User::factory()->create(), $village->id, 'Mine')))->toBe('not_found');
|
|
|
|
|
});
|