freeze(); $sim->settle($now); $military = Military::of($village, $sim); $starvation = Starvation::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, (int) $sim->config()->get('world.map.size', 1000)), 'points' => $village->points, 'space' => ['used' => $sim->spaceUsed(), 'max' => $sim->spaceMax()], 'specialisation' => self::specialisation($village, $sim), '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), ], 'levy' => [ 'militia' => $workforce->militia, 'until' => self::isoOrNull($village->levy_until), // How long a new levy would last. 'seconds' => $military->levySeconds(), ], '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()), ], 'troops' => (object) $troops, 'hospital' => self::hospital($village, $sim), 'military' => self::military($village, $military, $now), 'movements' => RallyPresenter::movements($village, $now), 'support' => RallyPresenter::support($village), 'rally' => RallyPresenter::rally($village, $now), '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(), 'quests' => Quests::summary($village), // The player's tribe for the menu, and invites waiting for the badge. 'tribe' => self::tribe($village), 'season' => SeasonPresenter::present($village->world), 'unread_reports' => Report::where('player_id', $village->player_id)->whereNull('read_at')->count(), '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, // What it does at this level and would do one level up, for its own screen. 'effects' => $rules->effects($b->type, $b->level), 'next_effects' => $b->level < $rules->maxLevel($b->type) ? $rules->effects($b->type, $b->level + 1) : 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), ]; } /** * 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(), ]; } /** * 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; } /** * 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()), ]; } /** @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, ]; } /** The player's coins, the villages they pay for, and what a coin costs. */ private static function conquest(Village $village): array { $nobility = Nobility::of($village); $coins = (int) ($village->player->coins ?? 0); $slots = Conquest::slots($coins); return [ 'coins' => $coins, // Villages beyond the first: the ones the coins pay for, and the ones held. 'slots' => $slots, 'used' => $village->player ? $nobility->slotsUsed($village->player) : 0, // Coins in total for one more village. 'next_slot_coins' => Conquest::coinsFor($slots + 1), 'coin_cost' => $nobility->rules()->coinCost(), 'max_distance' => $nobility->rules()->maxDistance(), ]; } private static function iso(CarbonImmutable $at): string { return $at->utc()->format('Y-m-d\TH:i:s.v\Z'); } private static function isoOrNull(?CarbonImmutable $at): ?string { return $at ? self::iso($at) : null; } }