160 lines
5.5 KiB
PHP
160 lines
5.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Game\Trade;
|
||
|
|
|
||
|
|
use App\Game\Buildings\BuildingRules;
|
||
|
|
use App\Game\GameRuleViolation;
|
||
|
|
use App\Game\WorldConfig;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The market's rules (architecture.md, "Market", config/game/market.php): merchants per
|
||
|
|
* market level, what one carries, how long they travel, fair offers, and how much a
|
||
|
|
* trade route sends when the stock or the merchants run short.
|
||
|
|
*/
|
||
|
|
final class TradeRules
|
||
|
|
{
|
||
|
|
/** The building type whose level gives the merchants. */
|
||
|
|
public const BUILDING = 'market';
|
||
|
|
|
||
|
|
/** Every cargo has these keys; resources the market doesn't trade stay 0. */
|
||
|
|
public const CARGO = ['wood', 'clay', 'iron'];
|
||
|
|
|
||
|
|
public function __construct(private readonly WorldConfig $config) {}
|
||
|
|
|
||
|
|
/** Merchants a village has with its market at $level (0 without a market). */
|
||
|
|
public function merchants(int $level): int
|
||
|
|
{
|
||
|
|
return $this->config->hasBuilding(self::BUILDING) ? (new BuildingRules($this->config))->merchants(self::BUILDING, $level) : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Merchants that are neither under way nor reserved by open offers. */
|
||
|
|
public static function free(int $merchants, int $busy): int
|
||
|
|
{
|
||
|
|
return max(0, $merchants - $busy);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Resources one merchant carries. */
|
||
|
|
public function carry(): int
|
||
|
|
{
|
||
|
|
return (int) $this->config->get('market.carry', 1000);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Merchants a shipment of $amount resources needs. */
|
||
|
|
public function needed(int $amount): int
|
||
|
|
{
|
||
|
|
return $amount > 0 ? (int) ceil($amount / max(1, $this->carry())) : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function minutesPerField(): float
|
||
|
|
{
|
||
|
|
return (float) $this->config->get('market.minutes_per_field', 6);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Seconds merchants take for $distance fields, sped up by the world and unit speed like troops. */
|
||
|
|
public function seconds(float $distance): int
|
||
|
|
{
|
||
|
|
$pace = $this->config->speed() * $this->config->get('movement.unit_speed', 1.0);
|
||
|
|
|
||
|
|
return max(1, (int) round($this->minutesPerField() * $distance * 60 / $pace));
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @return list<string> */
|
||
|
|
public function resources(): array
|
||
|
|
{
|
||
|
|
return array_values(array_intersect($this->config->get('market.resources', []), self::CARGO));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function maxRatio(): float
|
||
|
|
{
|
||
|
|
return (float) $this->config->get('market.max_ratio', 1);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function minEvery(): int
|
||
|
|
{
|
||
|
|
return (int) $this->config->get('market.route.min_every', 600);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function maxEvery(): int
|
||
|
|
{
|
||
|
|
return (int) $this->config->get('market.route.max_every', 604800);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function maxRoutes(): int
|
||
|
|
{
|
||
|
|
return (int) $this->config->get('market.route.max_routes', 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The traded resources the player asked for, whole and not negative, as a full cargo.
|
||
|
|
*
|
||
|
|
* @param array<string, mixed> $amounts
|
||
|
|
* @return array{wood: int, clay: int, iron: int}
|
||
|
|
*/
|
||
|
|
public function cargo(array $amounts): array
|
||
|
|
{
|
||
|
|
$traded = $this->resources();
|
||
|
|
$cargo = [];
|
||
|
|
foreach (self::CARGO as $resource) {
|
||
|
|
$cargo[$resource] = in_array($resource, $traded, true) ? max(0, (int) ($amounts[$resource] ?? 0)) : 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $cargo;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* An offer gives $count times $giveAmount of one traded resource for $wantAmount of
|
||
|
|
* another, asking at most max_ratio times what it gives and giving at most max_ratio
|
||
|
|
* times what it asks.
|
||
|
|
*/
|
||
|
|
public function checkOffer(string $give, int $giveAmount, string $want, int $wantAmount, int $count): void
|
||
|
|
{
|
||
|
|
$traded = $this->resources();
|
||
|
|
if (! in_array($give, $traded, true) || ! in_array($want, $traded, true)) {
|
||
|
|
throw new GameRuleViolation('not_found', 'The market doesn\'t trade that.');
|
||
|
|
}
|
||
|
|
if ($give === $want) {
|
||
|
|
throw new GameRuleViolation('same_resource', 'Trade one resource for another.');
|
||
|
|
}
|
||
|
|
if ($giveAmount < 1 || $wantAmount < 1) {
|
||
|
|
throw new GameRuleViolation('amount', 'Give and ask for at least 1.');
|
||
|
|
}
|
||
|
|
if ($count < 1) {
|
||
|
|
throw new GameRuleViolation('count', 'Offer it at least once.');
|
||
|
|
}
|
||
|
|
$ratio = $this->maxRatio();
|
||
|
|
if ($wantAmount > $ratio * $giveAmount || $giveAmount > $ratio * $wantAmount) {
|
||
|
|
throw new GameRuleViolation('ratio', 'That offer is too lopsided.');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function checkEvery(int $every): void
|
||
|
|
{
|
||
|
|
if ($every < $this->minEvery() || $every > $this->maxEvery()) {
|
||
|
|
throw new GameRuleViolation('every', 'A route runs between '.$this->minEvery().' and '.$this->maxEvery().' seconds apart.');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* What one run of a trade route sends: of each resource what the route asks for, as
|
||
|
|
* far as the stock has it, scaled down evenly to what the free merchants carry.
|
||
|
|
*
|
||
|
|
* @param array<string, int> $amounts what the route sends each time
|
||
|
|
* @param array<string, float> $stock
|
||
|
|
* @return array{wood: int, clay: int, iron: int}
|
||
|
|
*/
|
||
|
|
public function routeCargo(array $amounts, array $stock, int $freeMerchants): array
|
||
|
|
{
|
||
|
|
$cargo = $this->cargo($amounts);
|
||
|
|
foreach ($cargo as $resource => $amount) {
|
||
|
|
$cargo[$resource] = min($amount, max(0, (int) floor($stock[$resource] ?? 0)));
|
||
|
|
}
|
||
|
|
$total = array_sum($cargo);
|
||
|
|
$capacity = max(0, $freeMerchants) * $this->carry();
|
||
|
|
if ($total > $capacity) {
|
||
|
|
$cargo = array_map(fn (int $n) => (int) floor($n * $capacity / $total), $cargo);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $cargo;
|
||
|
|
}
|
||
|
|
}
|