game-village-politics-backend/app/Engine/Nobility.php

131 lines
4.9 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Engine;
use App\Game\Military\Conquest;
use App\Models\GameEvent;
use App\Models\Movement;
use App\Models\Player;
use App\Models\UnitStack;
use App\Models\Village;
use Carbon\CarbonImmutable;
/** Coins and the villages they pay for, loyalty and handing a conquered village over (combat.conquest). */
final class Nobility
{
private function __construct(private readonly Conquest $rules) {}
public static function of(Village $village): self
{
return new self(new Conquest($village->world->gameConfig()));
}
public function rules(): Conquest
{
return $this->rules;
}
/** The village's loyalty at $at, grown back since it last changed. */
public function loyalty(Village $village, CarbonImmutable $at): float
{
if (! $village->loyalty_at) {
return min((float) $village->loyalty, $this->rules->maxLoyalty());
}
$hours = ($at->getTimestampMs() - $village->loyalty_at->getTimestampMs()) / 3_600_000;
return $this->rules->loyalty((float) $village->loyalty, $hours);
}
/** Slots in use: every village beyond the first. Noblemen take none. */
public function slotsUsed(Player $player): int
{
return max(0, Village::where('player_id', $player->id)->count() - 1);
}
/** Slots the player's coins pay for and no village uses yet. */
public function freeSlots(Player $player): int
{
return max(0, Conquest::slots((int) $player->coins) - $this->slotsUsed($player));
}
/** Whether the player's coins pay for one more village. */
public function canTake(?int $playerId): bool
{
$player = $playerId ? Player::find($playerId) : null;
return $player !== null && Conquest::canTake((int) $player->coins, Village::where('player_id', $player->id)->count());
}
/**
* The village goes to the attacker. Its queues end without refunds, the old owner's
* groups from or to it are lost, and so is its support standing elsewhere.
*
* @return list<int> villages whose owners should hear about it
*/
public function handOver(Village $village, int $playerId, CarbonImmutable $at): array
{
$touched = $this->release($village);
$village->player_id = $playerId;
$village->setRelation('player', Player::find($playerId));
$village->loyalty = $this->rules->afterConquest();
$village->loyalty_at = $at;
// Raiders come for the new owner's village too.
Raiders::of($village)->ensurePlanned($at);
if ($village->crown) {
Seasons::recount($village, $at);
}
return $touched;
}
/**
* What a village loses with its owner: its queues end without refunds, the owner's
* groups from or to it are lost, and so is its support standing elsewhere.
*
* @return list<int> villages whose owners should hear about it
*/
public function release(Village $village): array
{
$touched = [];
foreach ($village->orders as $order) {
GameEvent::where('type', GameEvent::BUILD_ORDER_FINISHED)->where('subject_id', $order->id)->whereNull('processed_at')->delete();
$order->delete();
}
$village->setRelation('orders', collect());
foreach ($village->recruits as $order) {
GameEvent::where('type', GameEvent::RECRUIT_FINISHED)->where('subject_id', $order->id)->whereNull('processed_at')->delete();
$order->delete();
}
$village->setRelation('recruits', collect());
GameEvent::where('type', GameEvent::LEVY_ENDS)->where('village_id', $village->id)->whereNull('processed_at')->delete();
$village->militia = 0;
$village->levy_until = null;
$village->starving_since = null;
Hospital::clear($village);
// Its offers (with the goods they reserved) and its trade routes.
Market::release($village);
// The old owner's commands and merchants from here, and troops and merchants on their way home here.
$lost = Movement::where(fn ($q) => $q->where('origin_id', $village->id)->whereIn('type', [Movement::ATTACK, Movement::SUPPORT, Movement::TRADE]))
->orWhere(fn ($q) => $q->where('target_id', $village->id)->whereIn('type', [Movement::RETURN, Movement::TRADE_RETURN]))
->get();
foreach ($lost as $movement) {
GameEvent::whereIn('type', [GameEvent::MOVEMENT_ARRIVES, GameEvent::MOVEMENT_SPOTTED])
->where('subject_id', $movement->id)->whereNull('processed_at')->delete();
$touched[] = $movement->target_id;
$movement->delete();
}
// Its support standing in other villages.
$away = UnitStack::where('home_village_id', $village->id)->where('village_id', '!=', $village->id)->get();
foreach ($away as $stack) {
$touched[] = $stack->village_id;
$stack->delete();
}
return array_values(array_unique($touched));
}
}