2026-10-28 18:17:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Presenters;
|
|
|
|
|
|
2026-10-30 12:45:02 +00:00
|
|
|
use App\Engine\Hospital;
|
2026-10-28 20:39:48 +00:00
|
|
|
use App\Engine\Military;
|
2026-10-29 09:28:20 +00:00
|
|
|
use App\Engine\Nobility;
|
2026-10-29 09:05:55 +00:00
|
|
|
use App\Engine\Quests;
|
2026-10-28 18:17:34 +00:00
|
|
|
use App\Engine\Simulation;
|
2026-10-28 21:03:33 +00:00
|
|
|
use App\Engine\Starvation;
|
2026-10-28 18:45:02 +00:00
|
|
|
use App\Game\Map\Continent;
|
2026-10-29 09:28:20 +00:00
|
|
|
use App\Game\Military\Conquest;
|
2026-10-28 18:17:34 +00:00
|
|
|
use App\Models\Building;
|
|
|
|
|
use App\Models\BuildOrder;
|
2026-10-28 20:39:48 +00:00
|
|
|
use App\Models\RecruitOrder;
|
2026-10-28 22:56:55 +00:00
|
|
|
use App\Models\Report;
|
2026-10-30 09:47:41 +00:00
|
|
|
use App\Models\TribeInvite;
|
2026-10-28 18:17:34 +00:00
|
|
|
use App\Models\Village;
|
2026-10-30 12:45:02 +00:00
|
|
|
use App\Models\Wounded;
|
2026-10-28 18:17:34 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The `village.show` payload (VillageState in the client's app/types/game.ts).
|
|
|
|
|
* Every number is computed here; the client only projects resources and countdowns.
|
|
|
|
|
*/
|
|
|
|
|
final class VillagePresenter
|
|
|
|
|
{
|
|
|
|
|
public static function present(Village $village, CarbonImmutable $now): array
|
|
|
|
|
{
|
2026-10-30 12:07:09 +00:00
|
|
|
$sim = Simulation::of($village)->freeze();
|
2026-10-28 18:17:34 +00:00
|
|
|
$sim->settle($now);
|
2026-10-28 20:39:48 +00:00
|
|
|
$military = Military::of($village, $sim);
|
2026-10-28 21:03:33 +00:00
|
|
|
$starvation = Starvation::of($village, $sim);
|
2026-10-28 20:39:48 +00:00
|
|
|
$troops = $military->troopsAtHome();
|
2026-10-28 18:17:34 +00:00
|
|
|
$workforce = $sim->workforce();
|
|
|
|
|
$rules = $sim->rules();
|
|
|
|
|
$position = 0;
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => $village->id,
|
2026-10-28 18:45:02 +00:00
|
|
|
'world_id' => $village->world_id,
|
2026-10-28 18:17:34 +00:00
|
|
|
'name' => $village->name,
|
|
|
|
|
'x' => $village->x,
|
|
|
|
|
'y' => $village->y,
|
2026-10-29 00:51:37 +00:00
|
|
|
'continent' => Continent::of($village->x, $village->y, (int) $sim->config()->get('world.map.size', 1000)),
|
2026-10-28 18:45:02 +00:00
|
|
|
'points' => $village->points,
|
2026-10-28 21:16:45 +00:00
|
|
|
'space' => ['used' => $sim->spaceUsed(), 'max' => $sim->spaceMax()],
|
2026-10-29 15:20:52 +00:00
|
|
|
'specialisation' => self::specialisation($village, $sim),
|
2026-10-28 18:17:34 +00:00
|
|
|
'resources' => array_map(fn (float $n) => round($n, 4), $village->resources()),
|
|
|
|
|
'resources_at' => self::iso($village->resources_at),
|
|
|
|
|
'rates' => array_map(fn (float $n) => round($n, 4), $sim->rates()),
|
|
|
|
|
'storage_cap' => round($sim->storageCap()),
|
|
|
|
|
'population' => [
|
|
|
|
|
'food_budget' => round($workforce->foodBudget, 2),
|
|
|
|
|
'upkeep' => round($workforce->upkeep, 2),
|
|
|
|
|
'villagers' => $workforce->villagers,
|
|
|
|
|
'workers' => $workforce->workers(),
|
2026-10-28 20:39:48 +00:00
|
|
|
'soldiers' => array_sum($troops),
|
|
|
|
|
'training' => $village->recruits->sum(fn (RecruitOrder $o) => $o->count - $o->done),
|
2026-10-28 18:17:34 +00:00
|
|
|
],
|
2026-10-28 20:55:44 +00:00
|
|
|
'levy' => [
|
|
|
|
|
'militia' => $workforce->militia,
|
2026-10-28 21:03:33 +00:00
|
|
|
'until' => self::isoOrNull($village->levy_until),
|
2026-10-28 20:55:44 +00:00
|
|
|
// How long a new levy would last.
|
|
|
|
|
'seconds' => $military->levySeconds(),
|
|
|
|
|
],
|
2026-10-28 21:03:33 +00:00
|
|
|
'starvation' => [
|
|
|
|
|
// When the stock runs out at the current rates (null when it doesn't).
|
|
|
|
|
'food_empty_at' => self::isoOrNull($starvation->emptyAt()),
|
|
|
|
|
// Set while the stock is empty and upkeep exceeds income.
|
|
|
|
|
'since' => self::isoOrNull($village->starving_since),
|
|
|
|
|
'desertion_at' => self::isoOrNull($starvation->nextDesertion()),
|
|
|
|
|
],
|
2026-10-28 20:39:48 +00:00
|
|
|
'troops' => (object) $troops,
|
2026-10-30 12:45:02 +00:00
|
|
|
'hospital' => self::hospital($village, $sim),
|
2026-10-28 20:39:48 +00:00
|
|
|
'military' => self::military($village, $military, $now),
|
2026-10-28 22:56:55 +00:00
|
|
|
'movements' => RallyPresenter::movements($village, $now),
|
|
|
|
|
'support' => RallyPresenter::support($village),
|
|
|
|
|
'rally' => RallyPresenter::rally($village, $now),
|
2026-10-29 09:28:20 +00:00
|
|
|
'loyalty' => (int) floor(Nobility::of($village)->loyalty($village, $now)),
|
|
|
|
|
'conquest' => self::conquest($village),
|
|
|
|
|
// The player's villages in this world, for the switcher.
|
|
|
|
|
'villages' => Village::where('player_id', $village->player_id)->orderBy('id')->get(['id', 'name', 'x', 'y', 'points'])
|
|
|
|
|
->map(fn (Village $v) => ['id' => $v->id, 'name' => $v->name, 'x' => $v->x, 'y' => $v->y, 'points' => $v->points])->all(),
|
2026-10-29 09:05:55 +00:00
|
|
|
'quests' => Quests::summary($village),
|
2026-10-30 09:47:41 +00:00
|
|
|
// The player's tribe for the menu, and invites waiting for the badge.
|
|
|
|
|
'tribe' => self::tribe($village),
|
2026-10-29 15:31:34 +00:00
|
|
|
'season' => SeasonPresenter::present($village->world),
|
2026-10-28 22:56:55 +00:00
|
|
|
'unread_reports' => Report::where('player_id', $village->player_id)->whereNull('read_at')->count(),
|
2026-10-28 18:17:34 +00:00
|
|
|
'builders' => $sim->builders(),
|
|
|
|
|
'buildings' => $village->buildings->map(function (Building $b) use ($sim, $workforce, $rules) {
|
|
|
|
|
$output = $sim->output($b, $workforce);
|
2026-10-31 09:56:59 +00:00
|
|
|
$share = $sim->share($b);
|
2026-10-28 18:17:34 +00:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => $b->id,
|
|
|
|
|
'type' => $b->type,
|
|
|
|
|
'level' => $b->level,
|
2026-10-28 18:45:02 +00:00
|
|
|
'space' => $rules->space($b->type),
|
2026-10-28 18:17:34 +00:00
|
|
|
'priority' => $b->priority,
|
|
|
|
|
'workers' => ['assigned' => $workforce->assigned[$b->id] ?? 0, 'required' => $workforce->required[$b->id] ?? 0],
|
|
|
|
|
'staffing' => round($workforce->ratio($b->id), 4),
|
|
|
|
|
'production' => $output ? ['resource' => $output['resource'], 'per_hour' => round($output['per_hour'], 2)] : null,
|
2026-10-31 09:56:59 +00:00
|
|
|
// 1 for the first or only one of its type; a copy works at less.
|
|
|
|
|
'instance_share' => $share,
|
|
|
|
|
// What it does at this level and would do one level up, at its share, for its own screen.
|
|
|
|
|
'effects' => $rules->effects($b->type, $b->level, $share),
|
|
|
|
|
'next_effects' => $b->level < $rules->maxLevel($b->type) ? $rules->effects($b->type, $b->level + 1, $share) : null,
|
2026-10-28 18:17:34 +00:00
|
|
|
'upgrade' => $sim->upgradeOffer($b),
|
|
|
|
|
'demolish' => $sim->demolishOffer($b),
|
|
|
|
|
];
|
|
|
|
|
})->values()->all(),
|
|
|
|
|
'queue' => $village->orders->map(fn (BuildOrder $o) => [
|
|
|
|
|
'id' => $o->id,
|
|
|
|
|
'building_id' => $o->building_id,
|
|
|
|
|
'type' => $o->type,
|
|
|
|
|
'action' => $o->action,
|
|
|
|
|
'target_level' => $o->target_level,
|
2026-10-28 18:45:02 +00:00
|
|
|
'space' => $rules->space($o->type),
|
2026-10-28 18:17:34 +00:00
|
|
|
'cost' => $o->cost,
|
|
|
|
|
'position' => ++$position,
|
|
|
|
|
'builder' => $o->builder,
|
|
|
|
|
'starts_at' => $o->starts_at ? self::iso($o->starts_at) : null,
|
|
|
|
|
'finishes_at' => $o->finishes_at ? self::iso($o->finishes_at) : null,
|
|
|
|
|
])->values()->all(),
|
|
|
|
|
'buildable' => $sim->buildOptions(),
|
|
|
|
|
'server_time' => self::iso($now),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-30 12:45:02 +00:00
|
|
|
/**
|
|
|
|
|
* The Lazarett: beds as far as the hospitals are staffed, beds taken, the share of the
|
|
|
|
|
* fallen that comes back, and the wounded of each battle with when they stand again.
|
|
|
|
|
*/
|
|
|
|
|
private static function hospital(Village $village, Simulation $sim): array
|
|
|
|
|
{
|
|
|
|
|
$hospital = Hospital::of($village, $sim);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'beds' => $hospital->beds(),
|
|
|
|
|
'occupied' => $hospital->occupied(),
|
|
|
|
|
'share' => (float) $sim->config()->get('combat.hospital.share', 0),
|
|
|
|
|
'heal_seconds' => $hospital->rules()->healSeconds(),
|
|
|
|
|
'wounded' => $village->wounded->map(fn (Wounded $w) => ['id' => $w->id, 'units' => (object) $w->units, 'heals_at' => self::iso($w->heals_at)])->values()->all(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 20:39:48 +00:00
|
|
|
/**
|
|
|
|
|
* Every building that trains units: what it can train (with cost, time, upkeep, how
|
|
|
|
|
* many fit now, and missing requirements) and its own queue.
|
|
|
|
|
*/
|
|
|
|
|
private static function military(Village $village, Military $military, CarbonImmutable $now): array
|
|
|
|
|
{
|
|
|
|
|
$units = $military->units();
|
|
|
|
|
$out = [];
|
|
|
|
|
foreach ($village->buildings as $b) {
|
|
|
|
|
$trainable = $units->trainedAt($b->type);
|
|
|
|
|
if (! $trainable) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$out[] = [
|
|
|
|
|
'building_id' => $b->id,
|
|
|
|
|
'type' => $b->type,
|
|
|
|
|
'level' => $b->level,
|
|
|
|
|
'units' => array_map(function (string $unit) use ($military, $units, $b) {
|
|
|
|
|
$missing = $military->missingRequirements($unit);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'unit' => $unit,
|
|
|
|
|
'cost' => $units->cost($unit),
|
|
|
|
|
'seconds' => $military->secondsPerUnit($b, $unit),
|
|
|
|
|
'upkeep' => $units->upkeep($unit),
|
|
|
|
|
'max' => $missing ? 0 : $military->maxRecruitable($unit),
|
|
|
|
|
'blocked' => $missing ? 'requires' : null,
|
|
|
|
|
'requires' => $missing,
|
|
|
|
|
];
|
|
|
|
|
}, $trainable),
|
|
|
|
|
'queue' => $village->recruits->where('building_id', $b->id)->map(fn (RecruitOrder $o) => [
|
|
|
|
|
'id' => $o->id,
|
|
|
|
|
'unit' => $o->unit,
|
|
|
|
|
'count' => $o->count,
|
|
|
|
|
'done' => $o->doneAt($now),
|
|
|
|
|
'seconds_per_unit' => $o->seconds_per_unit,
|
|
|
|
|
'starts_at' => self::iso($o->starts_at),
|
|
|
|
|
'finishes_at' => self::iso($o->finishes_at),
|
|
|
|
|
])->values()->all(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-29 15:20:52 +00:00
|
|
|
/**
|
|
|
|
|
* The village's role, the space each role's buildings take, and what makes a role
|
|
|
|
|
* and what it gives, so the client can show how close the village is to one.
|
|
|
|
|
*/
|
|
|
|
|
private static function specialisation(Village $village, Simulation $sim): array
|
|
|
|
|
{
|
|
|
|
|
$spec = $sim->specialisation();
|
|
|
|
|
$config = $sim->config();
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'role' => $sim->role(),
|
|
|
|
|
'space' => (object) $spec->space($village->buildings->pluck('type')),
|
|
|
|
|
'share' => (float) $config->get('world.village.specialisation.share'),
|
|
|
|
|
'min_space' => (int) $config->get('world.village.specialisation.min_space'),
|
|
|
|
|
'roles' => array_map(fn (string $role) => [
|
|
|
|
|
'role' => $role,
|
|
|
|
|
'buildings' => $config->get("world.village.specialisation.roles.$role.buildings"),
|
|
|
|
|
'bonuses' => (object) $spec->bonuses($role),
|
|
|
|
|
], $spec->roles()),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-30 09:47:41 +00:00
|
|
|
/** @return array{tribe: array{id: int, name: string, tag: string, role: string}|null, invites: int} */
|
|
|
|
|
private static function tribe(Village $village): array
|
|
|
|
|
{
|
|
|
|
|
$membership = $village->player?->membership()->with('tribe')->first();
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'tribe' => $membership ? [...$membership->tribe->summary(), 'role' => $membership->role] : null,
|
|
|
|
|
'invites' => $village->player_id ? TribeInvite::where('player_id', $village->player_id)->count() : 0,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-31 09:50:31 +00:00
|
|
|
/** The player's coins, the villages they pay for, and what a coin costs. */
|
2026-10-29 09:28:20 +00:00
|
|
|
private static function conquest(Village $village): array
|
|
|
|
|
{
|
|
|
|
|
$nobility = Nobility::of($village);
|
|
|
|
|
$coins = (int) ($village->player->coins ?? 0);
|
|
|
|
|
$slots = Conquest::slots($coins);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'coins' => $coins,
|
2026-10-31 09:50:31 +00:00
|
|
|
// Villages beyond the first: the ones the coins pay for, and the ones held.
|
2026-10-29 09:28:20 +00:00
|
|
|
'slots' => $slots,
|
|
|
|
|
'used' => $village->player ? $nobility->slotsUsed($village->player) : 0,
|
2026-10-31 09:50:31 +00:00
|
|
|
// Coins in total for one more village.
|
|
|
|
|
'next_slot_coins' => Conquest::coinsFor($slots + 1),
|
2026-10-29 09:28:20 +00:00
|
|
|
'coin_cost' => $nobility->rules()->coinCost(),
|
|
|
|
|
'max_distance' => $nobility->rules()->maxDistance(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 18:17:34 +00:00
|
|
|
private static function iso(CarbonImmutable $at): string
|
|
|
|
|
{
|
|
|
|
|
return $at->utc()->format('Y-m-d\TH:i:s.v\Z');
|
|
|
|
|
}
|
2026-10-28 21:03:33 +00:00
|
|
|
|
|
|
|
|
private static function isoOrNull(?CarbonImmutable $at): ?string
|
|
|
|
|
{
|
|
|
|
|
return $at ? self::iso($at) : null;
|
|
|
|
|
}
|
2026-10-28 18:17:34 +00:00
|
|
|
}
|