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

99 lines
5.0 KiB
PHP
Raw Normal View History

<?php
use App\Actions\QueueBuild;
use App\Actions\QueueUpgrade;
use App\Actions\Recruit;
use App\Engine\Rally;
use App\Models\Village;
use Carbon\CarbonImmutable;
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-30 12:00:00')));
/** A start village with a farm big enough to staff everything, plus these level-1 instances. */
function villageWith(array $types): Village
{
$village = startVillage();
$village->buildings()->where('type', 'farm')->update(['level' => 10]);
foreach ($types as $type) {
$village->buildings()->create(['type' => $type, 'level' => 1]);
}
return $village->fresh();
}
it('lets a second timber camp cost double and produce half', function () {
$village = villageWith(['timber_camp']);
$option = collect(shown($village)['buildable'])->firstWhere('type', 'timber_camp');
expect($option)->toMatchArray(['cost' => ['wood' => 100, 'clay' => 120, 'iron' => 80], 'instance_share' => 0.5]);
app(QueueBuild::class)(owner($village), $village->id, 'timber_camp');
$state = shown($village);
expect($state['resources'])->toMatchArray(['wood' => 400.0, 'clay' => 380.0, 'iron' => 420.0])
// A third one would rank after the queued copy.
->and(collect($state['buildable'])->firstWhere('type', 'timber_camp'))
->toMatchArray(['cost' => ['wood' => 200, 'clay' => 240, 'iron' => 160], 'instance_share' => 0.25]);
$this->travel(90)->seconds();
$state = shown($village);
$camps = collect($state['buildings'])->where('type', 'timber_camp')->values();
expect($camps)->toHaveCount(2)
->and($camps[0])->toMatchArray(['instance_share' => 1.0, 'production' => ['resource' => 'wood', 'per_hour' => 30.0]])
->and($camps[1])->toMatchArray(['instance_share' => 0.5, 'production' => ['resource' => 'wood', 'per_hour' => 15.0]])
// A copy needs all its workers all the same.
->and($camps[1]['workers'])->toBe(['assigned' => 2, 'required' => 2])
->and($state['rates']['wood'])->toBe(5 + 30.0 + 15.0);
});
it('shows a copy\'s effects at its share, now and one level up', function () {
$state = shown(villageWith(['timber_camp', 'timber_camp']));
[$first, $copy] = collect($state['buildings'])->where('type', 'timber_camp')->values()->all();
expect($first['effects'])->toBe([['key' => 'production', 'value' => 30.0, 'resource' => 'wood']])
->and($copy['effects'])->toBe([['key' => 'production', 'value' => 15.0, 'resource' => 'wood']])
->and($copy['next_effects'])->toBe([['key' => 'production', 'value' => round(30 * 1.163 * 0.5, 1), 'resource' => 'wood']])
// Upgrading the copy costs double too.
->and($copy['upgrade']['cost'])->toBe(['wood' => 125, 'clay' => 150, 'iron' => 100])
->and(collect($state['buildings'])->firstWhere('type', 'headquarters')['instance_share'])->toBe(1.0);
});
it('adds half of a second storage to the cap, and swaps when the copy is upgraded past the first', function () {
$village = villageWith(['storage']);
expect(shown($village)['storage_cap'])->toEqual(1500);
$village->update(['wood' => 1000, 'clay' => 1000, 'iron' => 1000]);
$copy = $village->buildings()->where('type', 'storage')->get()->last();
app(QueueUpgrade::class)(owner($village), $village->id, $copy->id);
$this->travel(1)->hours();
$state = shown($village);
$storages = collect($state['buildings'])->where('type', 'storage')->values();
expect($storages->pluck('level')->all())->toBe([1, 2])
->and($storages->pluck('instance_share')->all())->toBe([0.5, 1.0])
->and($state['storage_cap'])->toEqual(1230 + 500)
// The cost follows the rank too: the old first now pays double, the new first single.
->and($storages[0]['upgrade']['cost'])->toBe(['wood' => 152, 'clay' => 127, 'iron' => 102])
->and($storages[1]['upgrade']['cost'])->toBe(['wood' => 97, 'clay' => 81, 'iron' => 65]);
});
it('trains at half the speed in a second barracks', function () {
$village = villageWith(['barracks', 'barracks']);
$village->update(['wood' => 1000, 'clay' => 1000, 'iron' => 1000]);
[$first, $copy] = $village->buildings->where('type', 'barracks')->values()->all();
$military = collect(shown($village)['military']);
$seconds = fn (int $id) => collect($military->firstWhere('building_id', $id)['units'])->firstWhere('unit', 'spear')['seconds'];
expect($seconds($first->id))->toBe(1020)
->and($seconds($copy->id))->toBe(2040);
app(Recruit::class)(owner($village), $village->id, $copy->id, 'spear', 2);
$queue = collect(shown($village)['military'])->firstWhere('building_id', $copy->id)['queue'];
expect($queue[0])->toMatchArray(['seconds_per_unit' => 2040, 'finishes_at' => '2026-10-30T13:08:00.000Z']);
});
it('gives a second hospital half its beds and a second hiding place half its hides', function () {
$village = villageWith(['hospital', 'hospital', 'hiding_place', 'hiding_place']);
expect(shown($village)['hospital']['beds'])->toBe(30)
->and(Rally::of($village)->hidden())->toBe(150 + 75.0);
});