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

122 lines
4.7 KiB
PHP
Raw 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\RecruitOrder;
use App\Models\UnitStack;
use App\Models\Village;
use Carbon\CarbonImmutable;
/** Coins, noble slots, 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);
}
/**
* Noble slots in use: noblemen standing anywhere, under way and in training, plus
* every village beyond the first.
*/
public function slotsUsed(Player $player): int
{
$nobles = $this->rules->nobleUnits();
if (! $nobles) {
return 0;
}
$villages = Village::where('player_id', $player->id)->pluck('id');
$standing = (int) UnitStack::whereIn('home_village_id', $villages)->whereIn('unit', $nobles)->sum('count');
$moving = Movement::where('player_id', $player->id)->pluck('units')
->sum(fn (array $units) => array_sum(array_intersect_key($units, array_flip($nobles))));
$training = RecruitOrder::whereIn('village_id', $villages)->whereIn('unit', $nobles)->get()
->sum(fn (RecruitOrder $o) => $o->count - $o->done);
return $standing + $moving + $training + max(0, $villages->count() - 1);
}
/** Noble slots the player's coins pay for and doesn't use yet. */
public function freeSlots(Player $player): int
{
return max(0, Conquest::slots((int) $player->coins) - $this->slotsUsed($player));
}
/**
* 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 = [];
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;
// The old owner's commands from here and troops on their way home here.
$lost = Movement::where(fn ($q) => $q->where('origin_id', $village->id)->whereIn('type', [Movement::ATTACK, Movement::SUPPORT]))
->orWhere(fn ($q) => $q->where('target_id', $village->id)->where('type', Movement::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();
}
$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 array_values(array_unique($touched));
}
}