81 lines
2.4 KiB
PHP
81 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Game\Map;
|
|
|
|
use App\Game\WorldConfig;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
/**
|
|
* A world as a season (world.season): when the crown forts appear, where they stand, who
|
|
* holds them, when a holder wins, and what the next world is called.
|
|
*/
|
|
final class Season
|
|
{
|
|
public function __construct(private readonly WorldConfig $config) {}
|
|
|
|
/** When the crowns appear: crowns_after_days after the start, at world speed. */
|
|
public function crownsAt(CarbonImmutable $startedAt): CarbonImmutable
|
|
{
|
|
return $startedAt->addSeconds((int) round($this->config->get('world.season.crowns_after_days', 30) * 86400 / $this->config->speed()));
|
|
}
|
|
|
|
/** How long a holder must keep the crowns to win, at world speed. */
|
|
public function holdSeconds(): int
|
|
{
|
|
return max(1, (int) round($this->config->get('world.season.hold_days', 7) * 86400 / $this->config->speed()));
|
|
}
|
|
|
|
public function count(): int
|
|
{
|
|
return (int) $this->config->get('world.season.crowns', 0);
|
|
}
|
|
|
|
/** More than half of the crowns. */
|
|
public function needed(): int
|
|
{
|
|
return intdiv($this->count(), 2) + 1;
|
|
}
|
|
|
|
/**
|
|
* Where the crowns should stand: evenly round a ring of `radius` fields about 0|0.
|
|
*
|
|
* @return list<array{0: int, 1: int}>
|
|
*/
|
|
public function spots(): array
|
|
{
|
|
$radius = (float) $this->config->get('world.season.radius', 10);
|
|
$spots = [];
|
|
for ($i = 0; $i < $this->count(); $i++) {
|
|
$angle = 2 * M_PI * $i / $this->count() - M_PI / 2;
|
|
$spots[] = [(int) round($radius * cos($angle)), (int) round($radius * sin($angle))];
|
|
}
|
|
|
|
return $spots;
|
|
}
|
|
|
|
/**
|
|
* The player holding more than half of the crowns, if anyone does.
|
|
*
|
|
* @param iterable<?int> $owners the owner of every crown, null for barbarian
|
|
*/
|
|
public function holder(iterable $owners): ?int
|
|
{
|
|
$held = [];
|
|
foreach ($owners as $owner) {
|
|
if ($owner !== null) {
|
|
$held[$owner] = ($held[$owner] ?? 0) + 1;
|
|
}
|
|
}
|
|
arsort($held);
|
|
$top = array_key_first($held);
|
|
|
|
return $top !== null && $held[$top] >= $this->needed() ? (int) $top : null;
|
|
}
|
|
|
|
/** "w1" → "w2", "dev" → "dev2", "World 1" → "World 2". */
|
|
public static function next(string $name): string
|
|
{
|
|
return preg_match('/^(.*?)(\d+)$/', $name, $m) ? $m[1].((int) $m[2] + 1) : $name.'2';
|
|
}
|
|
}
|