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

62 lines
2.9 KiB
PHP
Raw Permalink Normal View History

<?php
use App\Actions\Ranking;
use App\Actions\SendTroops;
use App\Engine\VillageFactory;
use App\Models\User;
use App\Models\Village;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
function ranking(Village $village, string $kind, ?int $page = null): array
{
return app(Ranking::class)->list(owner($village), $village->world_id, $kind, $page);
}
it('ranks players by the points of all their villages and villages by their own', function () {
$small = startVillage();
$big = startVillage();
$big->buildings()->where('type', 'headquarters')->update(['level' => 5]);
$big->update(['points' => 60]);
$second = app(VillageFactory::class)->create($big->world, $big->player, 'Second', $big->x + 9, $big->y, ['headquarters' => 1]);
$second->update(['points' => 10]);
$players = ranking($small, 'players');
expect($players['rows'])->toHaveCount(2)
->and($players['rows'][0])->toMatchArray(['rank' => 1, 'value' => 70, 'villages' => 2, 'own' => false])
->and($players['rows'][1])->toMatchArray(['rank' => 2, 'value' => $small->points, 'villages' => 1, 'own' => true])
->and($players['own'])->toBe(['rank' => 2, 'page' => 1]);
$villages = ranking($small, 'villages');
expect(array_column(array_column($villages['rows'], 'village'), 'name'))->toBe([$big->name, $small->name, 'Second']);
});
it('counts opponents defeated in attack and in defence by the food they ate', function () {
$attacker = startVillage();
$attacker->buildings()->create(['type' => 'rally_point', 'level' => 1]);
$attacker->troops()->create(['home_village_id' => $attacker->id, 'unit' => 'light_cavalry', 'count' => 50]);
$camp = app(VillageFactory::class)->create($attacker->world, null, 'Barbarian village', $attacker->x + 3, $attacker->y, ['headquarters' => 1]);
$camp->troops()->create(['home_village_id' => $camp->id, 'unit' => 'spear', 'count' => 10]);
app(SendTroops::class)(owner($attacker), $attacker->id, 'attack', $camp->x, $camp->y, ['light_cavalry' => 50], null, null);
$this->travel(1)->hours();
$this->artisan('game:events --once')->assertSuccessful();
// 10 spears at 1 food each.
expect(ranking($attacker, 'attack')['rows'][0])->toMatchArray(['rank' => 1, 'value' => 10, 'own' => true])
->and(ranking($attacker, 'defense')['rows'])->toBe([]);
});
it('opens on the page with the own row and refuses unknown kinds', function () {
$village = startVillage();
foreach (range(1, 30) as $i) {
$other = startVillage(User::factory()->create());
$other->update(['points' => 1000 + $i]);
}
expect(ranking($village, 'players'))->toMatchArray(['page' => 2, 'pages' => 2, 'own' => ['rank' => 31, 'page' => 2]])
->and(ranking($village, 'players', 1)['page'])->toBe(1)
->and(rule(fn () => ranking($village, 'gold')))->toBe('not_found');
});