x, $village->y))->toBeLessThanOrEqual(3 * M_SQRT2 + 0.01) ->and($village->only('space', 'points'))->toBe(['space' => 40, 'points' => 21]) ->and($village->resources())->toBe(['wood' => 500.0, 'clay' => 500.0, 'iron' => 500.0, 'food' => 300.0]) ->and($village->buildings->map->only('type', 'level')->all())->toBe([ ['type' => 'headquarters', 'level' => 1], ['type' => 'storage', 'level' => 1], ['type' => 'farm', 'level' => 1], ]); }); it('returns the same village when joining again', function () { $village = startVillage(); expect(app(JoinWorld::class)(owner($village), $village->world_id)->id)->toBe($village->id); }); it('spawns players on the frontier, never right next to another player', function () { $villages = collect(range(1, 8))->map(fn () => startVillage(User::factory()->create())); 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); } } } }); 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(); }); it('lists worlds with their speed and the player village', function () { $village = startVillage(); $dev = World::where('slug', 'dev')->first(); $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], ]); }); it('shows a start village to its owner only', function () { $village = startVillage(); $state = shown($village); expect($state['space'])->toBe(['used' => 17, 'max' => 40]) ->and($state['continent'])->toBe(Continent::of($village->x, $village->y)) ->and($state['rates'])->toBe(['wood' => 5.0, 'clay' => 5.0, 'iron' => 5.0, 'food' => 6.0]) ->and($state['storage_cap'])->toEqual(1000) ->and($state['population'])->toEqual(['food_budget' => 30, 'upkeep' => 24, 'villagers' => 24, 'workers' => 5, 'soldiers' => 0, 'training' => 0]) ->and($state['builders'])->toBe(2) ->and(collect($state['buildable'])->firstWhere('type', 'headquarters')['blocked'])->toBe('max_per_village') ->and(collect($state['buildable'])->firstWhere('type', 'wall')['blocked'])->toBeNull() ->and(rule(fn () => app(ShowVillage::class)(User::factory()->create(), $village->id)))->toBe('not_found'); }); 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'); });