game-village-politics-backend/tests/Unit/Game/BuildingRulesTest.php

233 lines
12 KiB
PHP
Raw Normal View History

<?php
use App\Game\Buildings\BuildingRules;
it('costs the base at level 1 and grows by the factor per level', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->cost('timber_camp', 1))->toBe(['wood' => 50, 'clay' => 60, 'iron' => 40])
->and($rules->cost('timber_camp', 3))->toBe([
'wood' => (int) round(50 * 1.25 ** 2),
'clay' => (int) round(60 * 1.25 ** 2),
'iron' => (int) round(40 * 1.25 ** 2),
]);
});
it('makes later instances of a type more expensive', function () {
$rules = new BuildingRules(gameConfig());
// The n-th copy costs 2^n as much, at every level.
expect($rules->cost('farm', 1, 1))->toBe(['wood' => 90, 'clay' => 80, 'iron' => 60])
->and($rules->cost('farm', 1, 2))->toBe(['wood' => 180, 'clay' => 160, 'iron' => 120])
->and($rules->cost('timber_camp', 3, 1))->toBe(['wood' => 156, 'clay' => 188, 'iron' => 125])
->and($rules->cost('headquarters', 1, 3))->toBe($rules->cost('headquarters', 1));
});
it('lets copies of a type work at a falling share, ranked by level then id', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->instanceShares([
building(4, 'timber_camp', 3),
building(2, 'timber_camp', 5),
building(1, 'farm', 2),
building(7, 'timber_camp', 3),
building(9, 'storage'),
]))->toEqual([2 => 1.0, 4 => 0.5, 7 => 0.25, 1 => 1.0, 9 => 1.0])
->and($rules->instanceShare(0))->toBe(1.0)
->and($rules->instanceShare(3))->toBe(0.125)
->and($rules->instanceShares([]))->toBe([]);
});
it('swaps the shares when a copy is upgraded past the first', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->instanceShares([building(1, 'storage', 4), building(2, 'storage', 4)]))->toEqual([1 => 1.0, 2 => 0.5])
->and($rules->instanceShares([building(1, 'storage', 4), building(2, 'storage', 5)]))->toEqual([2 => 1.0, 1 => 0.5]);
});
it('gives every instance its full share when the world turns copies off', function () {
$rules = new BuildingRules(gameConfig(['economy' => ['instance_share' => 1.0]]));
expect($rules->instanceShares([building(1, 'iron_mine'), building(2, 'iron_mine')]))->toEqual([1 => 1.0, 2 => 1.0]);
});
it('builds as fast as Die Stämme, with the headquarters at the same level', function () {
$rules = new BuildingRules(gameConfig());
$time = fn (string $type, int $level, ?int $hq = null) => $rules->buildSeconds($type, $level, $hq ?? $level, 1.0);
// Die Stämme at world speed 1 (pace.md): timber camp 0:10, 0:10, 2:06, 11:15, 45:04,
// 3:23:10 and 12:05:12; the headquarters 2:12, 11:48 and 47:19 built from the level below.
expect([$time('timber_camp', 1), $time('timber_camp', 2), $time('timber_camp', 3), $time('timber_camp', 5), $time('timber_camp', 10)])
->toBe([10, 10, 126, 675, 2704])
->and($time('timber_camp', 20))->toBe(3 * 3600 + 23 * 60 + 10)
->and($time('timber_camp', 30))->toBe(12 * 3600 + 5 * 60 + 12)
->and([$time('headquarters', 3, 2), $time('headquarters', 5, 4), $time('headquarters', 10, 9)])->toBe([132, 708, 2839])
// Barracks 0:13, 0:12, 4:11; farm 2:47; iron mine 13:30; market 33:44.
->and([$time('barracks', 1), $time('barracks', 2), $time('barracks', 3)])->toBe([13, 12, 251])
->and([$time('farm', 3), $time('iron_mine', 5), $time('market', 5)])->toBe([167, 810, 2024]);
});
it('never builds faster than the floor, 30 s for the watchtower and 240 s for the wall', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->buildSeconds('timber_camp', 1, 1, 1.0))->toBe(10)
->and($rules->buildSeconds('wall', 1, 1, 1.0))->toBe(240)
->and($rules->buildSeconds('wall', 2, 20, 1.0))->toBe(240)
// 13200 × 0.0075 ÷ 1.05^20 is 37 s, ÷ 1.05^30 below 30.
->and($rules->buildSeconds('watchtower', 1, 20, 1.0))->toBe(37)
->and($rules->buildSeconds('watchtower', 1, 30, 1.0))->toBe(30)
->and($rules->buildSeconds('wall', 4, 4, 1.0))->toBe(1482);
});
it('speeds building up by 1.05 per headquarters level', function () {
$rules = new BuildingRules(gameConfig());
$flat = new BuildingRules(gameConfig(['economy' => ['build_time' => ['hq_factor' => 1.0]]]));
// Timber camp 10: 900 × 4.89346 = 4404 s without a headquarters.
expect($rules->buildSeconds('timber_camp', 10, 0, 1.0))->toBe(4404)
->and($rules->buildSeconds('timber_camp', 10, 1, 1.0))->toBe(4194)
->and($rules->buildSeconds('timber_camp', 10, 20, 1.0))->toBe((int) round(4404.1153 / 1.05 ** 20))
->and($flat->buildSeconds('timber_camp', 10, 20, 1.0))->toBe(4404)
->and($rules->hqSpeedUp(20))->toEqualWithDelta(2.6533, 0.0001);
});
it('divides build time by world speed and the headquarters staffing', function () {
$rules = new BuildingRules(gameConfig());
$w1 = new BuildingRules(gameConfig(['world' => ['speed' => 4]]));
$fast = new BuildingRules(gameConfig(['world' => ['speed' => 100]]));
expect($rules->buildSeconds('timber_camp', 3, 3, 0.5))->toBe(251)
// The floor is slowed by staffing too: an understaffed headquarters is slower at every level.
->and($rules->buildSeconds('timber_camp', 1, 1, 0.5))->toBe(20)
->and($w1->buildSeconds('timber_camp', 3, 3, 1.0))->toBe(31)
->and($w1->buildSeconds('timber_camp', 1, 1, 1.0))->toBe(3)
->and($w1->buildSeconds('wall', 1, 1, 1.0))->toBe(60)
->and($fast->buildSeconds('timber_camp', 1, 1, 1.0))->toBe(1);
});
it('takes the academy from its own table, and grows past the end of a table', function () {
$rules = new BuildingRules(gameConfig());
$high = new BuildingRules(gameConfig(['buildings' => ['timber_camp' => ['max_level' => 32]]]));
// 586800 × 0.0075, × 1.5 per level after.
expect($rules->buildSeconds('academy', 1, 0, 1.0))->toBe(4401)
->and($rules->buildSeconds('academy', 2, 0, 1.0))->toBe(6602)
->and($rules->buildSeconds('academy', 3, 0, 1.0))->toBe(9902)
->and($rules->timeFactor('timber_camp', 30))->toBe(208.951)
->and($high->timeFactor('timber_camp', 31))->toEqualWithDelta(208.951 ** 2 / 175.818, 0.0001)
->and($high->timeFactor('timber_camp', 32))->toEqualWithDelta(208.951 ** 3 / 175.818 ** 2, 0.0001);
});
it('takes half the time and refunds half the cost when demolishing', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->demolishSeconds('timber_camp', 3, 3, 1.0))->toBe(63)
->and($rules->demolishSeconds('timber_camp', 1, 1, 1.0))->toBe(5)
->and($rules->demolishRefund('timber_camp', 0))->toBe(['wood' => 25, 'clay' => 30, 'iron' => 20]);
});
it('rounds workers and scales output with world speed', function () {
$rules = new BuildingRules(gameConfig());
$fast = new BuildingRules(gameConfig(['world' => ['speed' => 2]]));
expect($rules->workers('timber_camp', 1))->toBe(2)
->and($rules->workers('timber_camp', 11))->toBe((int) round(2 * 1.12 ** 10))
->and($rules->workers('hiding_place', 5))->toBe(0)
->and($rules->output('timber_camp', 1))->toBe(['resource' => 'wood', 'per_hour' => 30.0])
->and($fast->output('farm', 1))->toBe(['resource' => 'food', 'per_hour' => 60.0])
->and($rules->output('storage', 3))->toBeNull();
});
it('gives storage capacity only to storage', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->capacity('storage', 1))->toBe(1000.0)
->and($rules->capacity('storage', 2))->toBe(1230.0)
->and($rules->capacity('farm', 1))->toBe(0.0);
});
it('takes building space per type at every level, and none for the wall', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->space('headquarters'))->toBe(9)
->and($rules->space('wall'))->toBe(0)
->and($rules->spaceUsed(['headquarters', 'storage', 'farm', 'hiding_place', 'wall']))->toBe(18);
});
it('gives points per building level', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->points('headquarters', 1))->toBe(10)
->and($rules->points('headquarters', 6))->toBe((int) round(10 * 1.2 ** 5))
->and($rules->points('rally_point', 1))->toBe(2);
});
it('adds builders at the configured headquarters levels', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->builders(0))->toBe(2)
->and($rules->builders(9))->toBe(2)
->and($rules->builders(10))->toBe(3)
->and($rules->builders(20))->toBe(4);
});
it('adds building space for every headquarters level', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->spaceMax(40, 1))->toBe(40)
->and($rules->spaceMax(40, 2))->toBe(45)
->and($rules->spaceMax(40, 30))->toBe(185);
});
it('lists what an instance does at a level', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->effects('timber_camp', 1))->toBe([['key' => 'production', 'value' => 30.0, 'resource' => 'wood']])
->and($rules->effects('storage', 2))->toBe([['key' => 'capacity', 'value' => 1230]])
->and($rules->effects('hiding_place', 1))->toBe([['key' => 'hides', 'value' => 150]])
->and($rules->effects('hospital', 2))->toBe([['key' => 'beds', 'value' => 24]])
->and($rules->effects('wall', 3))->toBe([['key' => 'wall_defense', 'value' => 60], ['key' => 'wall_bonus', 'value' => 12]])
->and($rules->effects('headquarters', 3))->toBe([['key' => 'build_speed', 'value' => 16], ['key' => 'builders', 'value' => 2], ['key' => 'space', 'value' => 50]])
->and($rules->effects('barracks', 6))->toBe([['key' => 'recruit_speed', 'value' => 25]])
->and($rules->effects('rally_point', 1))->toBe([]);
});
it('shows what a copy does at its share', function () {
$rules = new BuildingRules(gameConfig());
// A copy trains at half the speed of level 1 (−50%), at level 6 at 62.5% (−38%).
expect($rules->effects('timber_camp', 1, 0.5))->toBe([['key' => 'production', 'value' => 15.0, 'resource' => 'wood']])
->and($rules->effects('storage', 2, 0.5))->toBe([['key' => 'capacity', 'value' => 615]])
->and($rules->effects('hiding_place', 1, 0.25))->toBe([['key' => 'hides', 'value' => 38]])
->and($rules->effects('hospital', 2, 0.5))->toBe([['key' => 'beds', 'value' => 12]])
->and($rules->effects('barracks', 1, 0.5))->toBe([['key' => 'recruit_speed', 'value' => -50]])
->and($rules->effects('barracks', 6, 0.5))->toBe([['key' => 'recruit_speed', 'value' => -38]]);
});
it('feeds heads by farm level at world speed 1, on every world speed', function () {
$rules = new BuildingRules(gameConfig());
$fast = new BuildingRules(gameConfig(['world' => ['speed' => 100]]));
expect($rules->feeds('farm', 1))->toBe(30.0)
->and($rules->feeds('farm', 3))->toBe(30 * 1.25 ** 2)
->and($fast->feeds('farm', 3))->toBe(30 * 1.25 ** 2)
->and($fast->output('farm', 3)['per_hour'])->toBe(30 * 1.25 ** 2 * 100)
->and($rules->feeds('timber_camp', 5))->toBe(0.0)
->and($rules->isFarm('farm'))->toBeTrue()
->and($rules->isFarm('storage'))->toBeFalse();
});
it('needs idle villagers for the workers a level adds, and none for farms', function () {
$rules = new BuildingRules(gameConfig());
// Timber camp workers: round(2 × 1.12^(n−1)) = 2, 2, 3 at levels 1 to 3.
expect($rules->extraWorkers('timber_camp', 1))->toBe(2)
->and($rules->extraWorkers('timber_camp', 2))->toBe(0)
->and($rules->extraWorkers('timber_camp', 3))->toBe(1)
->and($rules->extraWorkers('barracks', 1))->toBe(3)
->and($rules->extraWorkers('hiding_place', 4))->toBe(0)
->and($rules->extraWorkers('timber_camp', 0))->toBe(0)
->and($rules->extraWorkers('farm', 1))->toBe(0)
->and($rules->extraWorkers('farm', 21))->toBe(0);
});