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

108 lines
5.3 KiB
PHP
Raw Permalink Normal View History

<?php
use App\Game\Buildings\BuildingRules;
use App\Game\ConfigLint;
use App\Game\GameRuleViolation;
use App\Game\Trade\TradeRules;
use App\Game\WorldConfig;
it('gives a market its merchants per level from the table, none without a market', function () {
$trade = new TradeRules(gameConfig());
expect($trade->merchants(0))->toBe(0)
->and($trade->merchants(1))->toBe(1)
->and($trade->merchants(11))->toBe(11)
->and($trade->merchants(12))->toBe(14)
->and($trade->merchants(25))->toBe(235)
->and(TradeRules::free(5, 3))->toBe(2)
->and(TradeRules::free(2, 3))->toBe(0);
});
it('shows the merchants among the market effects', function () {
$rules = new BuildingRules(gameConfig());
expect($rules->effects('market', 12))->toContain(['key' => 'merchants', 'value' => 14])
->and(collect($rules->effects('storage', 3))->pluck('key'))->not->toContain('merchants');
});
it('counts the merchants a shipment needs', function () {
$trade = new TradeRules(gameConfig());
expect($trade->needed(0))->toBe(0)
->and($trade->needed(1))->toBe(1)
->and($trade->needed(1000))->toBe(1)
->and($trade->needed(1001))->toBe(2)
->and($trade->needed(4500))->toBe(5);
});
it('moves merchants at their minutes per field, sped up by world and unit speed', function () {
expect((new TradeRules(gameConfig()))->seconds(5))->toBe(1800)
->and((new TradeRules(gameConfig()))->seconds(2.5))->toBe(900)
->and((new TradeRules(gameConfig(['world' => ['speed' => 100]])))->seconds(5))->toBe(18)
->and((new TradeRules(gameConfig(['world' => ['speed' => 2], 'movement' => ['unit_speed' => 0.5]])))->seconds(5))->toBe(1800)
->and((new TradeRules(gameConfig()))->seconds(0.001))->toBe(1);
});
it('keeps only traded resources in a cargo, whole and not negative', function () {
expect((new TradeRules(gameConfig()))->cargo(['wood' => '120', 'clay' => -5, 'food' => 300]))->toBe(['wood' => 120, 'clay' => 0, 'iron' => 0])
->and((new TradeRules(WorldConfig::make(['market' => ['resources' => ['wood']]])))->cargo(['wood' => 1, 'iron' => 1]))->toBe(['wood' => 1, 'clay' => 0, 'iron' => 0]);
});
it('sends on a route what the stock has, scaled down evenly to the free merchants', function () {
$trade = new TradeRules(gameConfig());
// Enough of everything.
expect($trade->routeCargo(['wood' => 500, 'clay' => 300, 'iron' => 0], ['wood' => 900.5, 'clay' => 900, 'iron' => 900], 1))
->toBe(['wood' => 500, 'clay' => 300, 'iron' => 0])
// Clay runs short: 500 of it; then 3500 > 2 merchants' 2000, scaled by 4/7.
->and($trade->routeCargo(['wood' => 3000, 'clay' => 1000, 'iron' => 0], ['wood' => 5000, 'clay' => 500.9, 'iron' => 100], 2))
->toBe(['wood' => 1714, 'clay' => 285, 'iron' => 0])
->and($trade->routeCargo(['wood' => 3000, 'clay' => 0, 'iron' => 0], ['wood' => 5000, 'clay' => 0, 'iron' => 0], 0))
->toBe(['wood' => 0, 'clay' => 0, 'iron' => 0])
->and($trade->routeCargo(['wood' => 3000, 'clay' => 0, 'iron' => 0], ['wood' => 0.7, 'clay' => 0, 'iron' => 0], 5))
->toBe(['wood' => 0, 'clay' => 0, 'iron' => 0]);
});
it('takes offers up to the ratio, of two different traded resources', function () {
$trade = new TradeRules(gameConfig());
$offer = function (string $give, int $giveAmount, string $want, int $wantAmount, int $count = 1) use ($trade) {
try {
$trade->checkOffer($give, $giveAmount, $want, $wantAmount, $count);
return 'ok';
} catch (GameRuleViolation $e) {
return $e->reason;
}
};
expect($offer('wood', 100, 'clay', 300))->toBe('ok')
->and($offer('wood', 300, 'clay', 100))->toBe('ok')
->and($offer('wood', 100, 'clay', 301))->toBe('ratio')
->and($offer('wood', 301, 'clay', 100))->toBe('ratio')
->and($offer('wood', 100, 'wood', 100))->toBe('same_resource')
->and($offer('wood', 100, 'food', 100))->toBe('not_found')
->and($offer('wood', 0, 'clay', 100))->toBe('amount')
->and($offer('wood', 100, 'clay', 100, 0))->toBe('count');
});
it('keeps routes between their shortest and longest interval', function () {
$trade = new TradeRules(gameConfig());
$trade->checkEvery(600);
$trade->checkEvery(604800);
expect(fn () => $trade->checkEvery(599))->toThrow(GameRuleViolation::class)
->and(fn () => $trade->checkEvery(604801))->toThrow(GameRuleViolation::class);
});
it('lints merchants that are missing a level, carry nothing or routes that never run', function () {
$short = gameConfig(['buildings' => ['market' => ['max_level' => 26]]]);
$zero = gameConfig(['buildings' => ['market' => ['merchants' => [0]]]]);
$market = gameConfig(['market' => ['carry' => 0, 'max_ratio' => 0.5, 'route' => ['min_every' => 700, 'max_every' => 600]]]);
expect(ConfigLint::check($short))->toContain('[market.merchants] must list one number per level, 26 in all.')
->and(ConfigLint::check($zero))->toContain('[market.merchants] must give every level at least 1 merchant.')
->and(ConfigLint::check($market))->toHaveCount(3)
->and(ConfigLint::check(gameConfig(['buildings' => ['market' => ['requires' => ['academy' => 9]]]])))
->toContain('[market] requires [academy] level 9, above its max level.');
});