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

133 lines
6.0 KiB
PHP
Raw Permalink Normal View History

<?php
use App\Actions\ClaimQuest;
use App\Actions\CollectRewards;
use App\Actions\QueueBuild;
use App\Actions\QueueDemolish;
use App\Actions\QueueUpgrade;
use App\Actions\RenameVillage;
use App\Actions\SendTroops;
use App\Actions\ShowMap;
use App\Actions\ShowQuests;
use App\Engine\VillageFactory;
use App\Game\Buildings\BuildingRules;
use App\Models\LevelReward;
use App\Models\Village;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
function questBook(Village $village): array
{
return app(ShowQuests::class)(owner($village), $village->id);
}
function questLine(Village $village, string $line): array
{
return collect(questBook($village)['lines'])->firstWhere('line', $line);
}
/** Queue an upgrade of the village's first building of $type and let it finish. */
function upgraded(Village $village, string $type): Village
{
$building = $village->buildings()->where('type', $type)->first();
app(QueueUpgrade::class)(owner($village), $village->id, $building->id);
test()->travel(1)->days();
test()->artisan('game:events --once')->assertSuccessful();
return $village->fresh();
}
it('leaves a level reward the first time a player reaches a level', function () {
$village = upgraded(startVillage(), 'headquarters');
$cost = (new BuildingRules($village->world->gameConfig()))->cost('headquarters', 2, 0);
expect(questBook($village)['rewards'])->toHaveCount(1)
->and(questBook($village)['rewards'][0])->toMatchArray([
'type' => 'headquarters', 'level' => 2,
'reward' => array_map(fn (int $n) => (int) round($n * 0.2), $cost),
'village' => ['id' => $village->id, 'name' => $village->name],
])
->and(shown($village)['quests'])->toBe(['claimable' => 1, 'rewards' => 1]);
});
it('pays a level back only once per player, type and level', function () {
$village = startVillage();
$village->update(['wood' => 500, 'clay' => 500, 'iron' => 500]);
app(QueueBuild::class)(owner($village), $village->id, 'timber_camp');
app(QueueBuild::class)(owner($village), $village->id, 'timber_camp');
$this->travel(1)->days();
$this->artisan('game:events --once')->assertSuccessful();
expect($village->buildings()->where('type', 'timber_camp')->count())->toBe(2)
->and(LevelReward::where('type', 'timber_camp')->count())->toBe(1);
});
it('pays nothing for demolishing', function () {
$village = upgraded(startVillage(), 'storage');
LevelReward::query()->delete();
app(QueueDemolish::class)(owner($village), $village->id, $village->buildings()->where('type', 'storage')->value('id'));
$this->travel(1)->days();
$this->artisan('game:events --once')->assertSuccessful();
expect(LevelReward::count())->toBe(0);
});
it('collects level rewards into the village, up to the storage', function () {
$village = upgraded(startVillage(), 'headquarters');
$before = shown($village)['resources'];
$reward = questBook($village)['rewards'][0]['reward'];
$state = shown(app(CollectRewards::class)(owner($village), $village->id));
expect($state['resources']['wood'])->toBe(min($state['storage_cap'], $before['wood'] + $reward['wood']))
->and($state['quests']['rewards'])->toBe(0)
->and(questBook($village)['rewards'])->toBe([]);
// Collected rewards are gone; collecting again changes nothing.
$again = shown(app(CollectRewards::class)(owner($village), $village->id));
expect($again['resources']['wood'])->toBe($state['resources']['wood']);
});
it('claims a completed quest once and opens the next one', function () {
$village = startVillage();
expect(rule(fn () => app(ClaimQuest::class)(owner($village), $village->id, 'hq_2')))->toBe('quest_open');
$village = upgraded($village, 'headquarters');
$village->update(['wood' => 0, 'clay' => 0, 'iron' => 0, 'resources_at' => now()]);
$state = shown(app(ClaimQuest::class)(owner($village), $village->id, 'hq_2'));
expect($state['resources'])->toMatchArray(['wood' => 120.0, 'clay' => 120.0, 'iron' => 100.0])
->and($state['quests']['claimable'])->toBe(0)
->and(questLine($village, 'construction'))->toMatchArray(['done' => 1])
->and(questLine($village, 'construction')['quest']['key'])->toBe('storage_farm_2')
->and(rule(fn () => app(ClaimQuest::class)(owner($village), $village->id, 'hq_2')))->toBe('quest_open');
});
it('counts renames, map views and attacks on barbarians', function () {
$village = startVillage();
app(RenameVillage::class)(owner($village), $village->id, 'Oakridge');
app(ShowMap::class)(owner($village), $village->world_id, null, null, 15, 15);
app(ShowMap::class)(owner($village), $village->world_id, null, null, 15, 15);
expect(questLine($village, 'village')['quest'])->toMatchArray(['key' => 'rename', 'complete' => true]);
app(ClaimQuest::class)(owner($village), $village->id, 'rename');
$this->assertDatabaseHas('player_counters', ['key' => 'map.viewed', 'value' => 1]);
$village->buildings()->create(['type' => 'rally_point', 'level' => 1]);
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'spear', 'count' => 5]);
$barbarian = app(VillageFactory::class)->create($village->world, null, 'Barbarian village', $village->x + 2, $village->y, ['headquarters' => 1, 'storage' => 1, 'farm' => 1]);
app(SendTroops::class)(owner($village), $village->id, 'attack', $barbarian->x, $barbarian->y, ['spear' => 5], null, null);
$this->assertDatabaseHas('player_counters', ['key' => 'attacks.barbarian', 'value' => 1]);
$this->assertDatabaseHas('player_counters', ['key' => 'attacks.sent', 'value' => 1]);
$this->assertDatabaseMissing('player_counters', ['key' => 'scouts.sent']);
});
it('previews what the next level of each building pays back', function () {
$village = startVillage();
$upcoming = collect(questBook($village)['upcoming'])->keyBy('type');
expect($upcoming->keys()->all())->toBe(['farm', 'headquarters', 'storage'])
->and($upcoming['headquarters']['level'])->toBe(2);
});