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

77 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use App\Actions\SeedBarbarians;
use App\Actions\ShowMap;
use App\Game\Map\Continent;
use App\Models\GameEvent;
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, 41, 41);
expect($map)->toMatchArray([
'center' => ['x' => $village->x, 'y' => $village->y],
'x' => $village->x - 20,
'y' => $village->y - 20,
'w' => 41,
'h' => 41,
'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 => [$village->x, $village->y, true, owner($village)->name, Continent::of($village->x, $village->y)],
$other->id => [$other->x, $other->y, false, 'Bea', Continent::of($other->x, $other->y)],
]);
});
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);
});
it('lets barbarian villages grow over time, one building per step', function () {
config(['game.world.map.barbarians' => ['count' => 3, 'radius' => 20, 'max_level' => 3, 'growth' => ['every_hours' => 1, 'max_level' => 10]]]);
$this->travelTo(now()->startOfMinute());
$world = startVillage()->world_id;
$barbarians = fn () => Village::where('world_id', $world)->whereNull('player_id')->with('buildings')->get();
$levels = fn () => $barbarians()->sum(fn ($v) => $v->buildings->sum('level'));
$pending = fn () => GameEvent::where('world_id', $world)->where('type', GameEvent::BARBARIAN_GROWS)->whereNull('processed_at')->count();
$before = ['levels' => $levels(), 'points' => $barbarians()->sum('points')];
expect($pending())->toBe(3);
// Each first step lands within 0.5–1.5 h, the next one an hour later.
$this->travel(3)->hours();
$this->artisan('game:events --once')->assertSuccessful();
expect($levels())->toBeGreaterThanOrEqual($before['levels'] + 6)
->and($barbarians()->sum('points'))->toBeGreaterThan($before['points'])
->and($pending())->toBe(3);
});