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

176 lines
8.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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('divides build time by world speed, headquarters level and staffing', function () {
$rules = new BuildingRules(gameConfig());
$fast = new BuildingRules(gameConfig(['world' => ['speed' => 100]]));
expect($rules->buildSeconds('timber_camp', 1, 1, 1.0))->toBe(90)
->and($rules->buildSeconds('timber_camp', 2, 1, 1.0))->toBe(108)
->and($rules->buildSeconds('timber_camp', 1, 11, 1.0))->toBe(60)
->and($rules->buildSeconds('timber_camp', 1, 1, 0.5))->toBe(180)
->and($fast->buildSeconds('timber_camp', 1, 1, 1.0))->toBe(1);
});
it('takes half the time and refunds half the cost when demolishing', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->demolishSeconds('timber_camp', 2, 1, 1.0))->toBe(54)
->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' => 10], ['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);
});