game-village-politics-backend/tests/Feature/MapTest.php

48 lines
2.2 KiB
PHP
Raw Normal View History

<?php
use App\Actions\SeedBarbarians;
use App\Actions\ShowMap;
use App\Models\User;
use App\Models\Village;
use App\Models\World;
it('opens on the player village and marks it as their own', function () {
$village = startVillage();
$other = startVillage(User::factory()->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' => 939, 'w' => 15, 'h' => 61]);
});
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);
});