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

34 lines
1.0 KiB
PHP
Raw Normal View History

<?php
use App\Game\Map\SpawnRing;
/** A random source that repeats the given values. */
function sequence(float ...$values): Closure
{
$i = 0;
return function () use ($values, &$i) {
return $values[$i++ % count($values)];
};
}
it('grows the ring with the number of player villages', function () {
$ring = new SpawnRing(gameConfig());
expect($ring->radius(0))->toBe(6.0)
->and($ring->radius(100))->toEqualWithDelta(6 + sqrt(100 / (M_PI * 0.08)), 1e-9)
->and($ring->radius(1000))->toBeGreaterThan($ring->radius(100));
});
it('picks a field on the ring', function () {
// Angle 0 and no offset: straight east of the centre at the start radius.
expect((new SpawnRing(gameConfig()))->pick(0, [], sequence(0.0, 0.5)))->toBe([506, 500]);
});
it('never picks a field next to another village', function () {
$ring = new SpawnRing(gameConfig());
expect($ring->pick(0, ['507|501' => true], sequence(0.0, 0.5)))->toBeNull()
->and($ring->pick(0, ['508|502' => true], sequence(0.0, 0.5)))->toBe([506, 500]);
});