138 lines
5.6 KiB
PHP
138 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Presenters;
|
|
|
|
use App\Engine\Military;
|
|
use App\Engine\Simulation;
|
|
use App\Game\Map\Continent;
|
|
use App\Models\Building;
|
|
use App\Models\BuildOrder;
|
|
use App\Models\RecruitOrder;
|
|
use App\Models\Village;
|
|
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
|
|
{
|
|
$sim = Simulation::of($village);
|
|
$sim->settle($now);
|
|
$military = Military::of($village, $sim);
|
|
$troops = $military->troopsAtHome();
|
|
$workforce = $sim->workforce();
|
|
$rules = $sim->rules();
|
|
$position = 0;
|
|
|
|
return [
|
|
'id' => $village->id,
|
|
'world_id' => $village->world_id,
|
|
'name' => $village->name,
|
|
'x' => $village->x,
|
|
'y' => $village->y,
|
|
'continent' => Continent::of($village->x, $village->y),
|
|
'points' => $village->points,
|
|
'space' => ['used' => $sim->spaceUsed(), 'max' => $village->space],
|
|
'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(),
|
|
'soldiers' => array_sum($troops),
|
|
'training' => $village->recruits->sum(fn (RecruitOrder $o) => $o->count - $o->done),
|
|
],
|
|
'troops' => (object) $troops,
|
|
'military' => self::military($village, $military, $now),
|
|
'builders' => $sim->builders(),
|
|
'buildings' => $village->buildings->map(function (Building $b) use ($sim, $workforce, $rules) {
|
|
$output = $sim->output($b, $workforce);
|
|
|
|
return [
|
|
'id' => $b->id,
|
|
'type' => $b->type,
|
|
'level' => $b->level,
|
|
'space' => $rules->space($b->type),
|
|
'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,
|
|
'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,
|
|
'space' => $rules->space($o->type),
|
|
'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),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
private static function iso(CarbonImmutable $at): string
|
|
{
|
|
return $at->utc()->format('Y-m-d\TH:i:s.v\Z');
|
|
}
|
|
}
|