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

78 lines
3.8 KiB
PHP
Raw Permalink Normal View History

<?php
use App\Actions\SendTroops;
use App\Actions\ShowMap;
use App\Actions\Tribes;
use App\Engine\VillageFactory;
use App\Models\Tribe;
use App\Models\Village;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
function mapAround(Village $village): array
{
return app(ShowMap::class)(owner($village), $village->world_id, $village->x, $village->y, 41, 41);
}
function relationOn(array $map, Village $village): ?string
{
return collect($map['villages'])->firstWhere('id', $village->id)['relation'] ?? null;
}
it('colours villages by what their owner is to the viewer', function () {
$me = startVillage();
$mate = startVillage();
$ally = startVillage();
$foe = startVillage();
$stranger = startVillage();
// A free field near me: the other players settle at random and may take any given one.
$spot = collect(range(2, 10))->first(fn (int $d) => ! Village::where('world_id', $me->world_id)->where('x', $me->x + $d)->where('y', $me->y + 2)->exists());
$barbarian = app(VillageFactory::class)->create($me->world, null, 'Barbarian village', $me->x + $spot, $me->y + 2, ['headquarters' => 1]);
foreach ([[$me, 'AAA'], [$ally, 'BBB'], [$foe, 'CCC']] as [$v, $tag]) {
app(Tribes::class)->found(owner($v), $v->world_id, "Tribe $tag", $tag);
}
app(Tribes::class)->invite(owner($me), $me->world_id, $mate->player->name);
app(Tribes::class)->join(owner($mate), $mate->world_id, Tribe::where('tag', 'AAA')->value('id'));
app(Tribes::class)->propose(owner($me), $me->world_id, 'BBB', 'ally');
app(Tribes::class)->accept(owner($ally), $ally->world_id, Tribe::where('tag', 'AAA')->value('id'));
app(Tribes::class)->enemy(owner($foe), $foe->world_id, 'AAA');
$map = mapAround($me);
expect(relationOn($map, $me))->toBe('own')
->and(relationOn($map, $mate))->toBe('tribe')
->and(relationOn($map, $ally))->toBe('ally')
->and(relationOn($map, $foe))->toBe('enemy')
->and(relationOn($map, $stranger))->toBe('none')
->and(relationOn($map, $barbarian))->toBe('barbarian')
->and(collect($map['villages'])->firstWhere('id', $ally->id)['player']['tribe']['tag'])->toBe('BBB');
});
it('shows own groups with units and spotted incoming ones by size only', function () {
$me = startVillage();
$other = startVillage();
$barbarian = app(VillageFactory::class)->create($me->world, null, 'Barbarian village', $me->x + 3, $me->y, ['headquarters' => 1]);
$me->buildings()->create(['type' => 'rally_point', 'level' => 1]);
$me->troops()->create(['home_village_id' => $me->id, 'unit' => 'spear', 'count' => 60]);
app(SendTroops::class)(owner($me), $me->id, 'attack', $barbarian->x, $barbarian->y, ['spear' => 60], null, null);
$mine = mapAround($me)['movements'];
expect($mine)->toHaveCount(1)
->and($mine[0])->toMatchArray(['type' => 'attack', 'own' => true, 'raid' => false, 'size' => 'small', 'units' => ['spear' => 60]])
->and($mine[0]['from']['id'])->toBe($me->id)
->and($mine[0]['to']['id'])->toBe($barbarian->id)
->and(mapAround($other)['movements'])->toBe([]);
});
it('says until when an own command can be called back from the map', function () {
$me = startVillage();
$barbarian = app(VillageFactory::class)->create($me->world, null, 'Barbarian village', $me->x + 3, $me->y, ['headquarters' => 1]);
$me->buildings()->create(['type' => 'rally_point', 'level' => 1]);
$me->troops()->create(['home_village_id' => $me->id, 'unit' => 'spear', 'count' => 10]);
app(SendTroops::class)(owner($me), $me->id, 'attack', $barbarian->x, $barbarian->y, ['spear' => 10], null, null);
expect(mapAround($me)['movements'][0]['cancel_until'])->toBe('2026-10-29T12:10:00.000Z');
$this->travel(11)->minutes();
expect(mapAround($me)['movements'][0]['cancel_until'])->toBeNull();
});