feat: one-off migration raises the highest farm of every player village that holds more heads than its farms feed now, to the lowest level that feeds them, with points and a refund for orders queued on it
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a2f4e01092
commit
6e022f7d1f
|
|
@ -71,6 +71,28 @@ final class Population
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lowest level of this farm, from its own up to its max, at which the villagers
|
||||||
|
* left after $soldierUpkeep cover the workers every building needs. The max when
|
||||||
|
* even that falls short.
|
||||||
|
*
|
||||||
|
* @param list<BuildingState> $buildings
|
||||||
|
*/
|
||||||
|
public function lowestFarmLevel(array $buildings, int $farmId, float $soldierUpkeep): int
|
||||||
|
{
|
||||||
|
$farm = current(array_filter($buildings, fn (BuildingState $b) => $b->id === $farmId));
|
||||||
|
$max = $this->rules->maxLevel($farm->type);
|
||||||
|
for ($level = $farm->level; $level < $max; $level++) {
|
||||||
|
$states = array_map(fn (BuildingState $b) => $b->id === $farmId ? new BuildingState($b->id, $b->type, $level, $b->priority) : $b, $buildings);
|
||||||
|
$workforce = $this->workforce($states, $soldierUpkeep);
|
||||||
|
if ($workforce->villagers >= array_sum($workforce->required)) {
|
||||||
|
return $level;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $max;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Farms first, then by priority (highest first). Buildings with the same priority
|
* Farms first, then by priority (highest first). Buildings with the same priority
|
||||||
* share what is left evenly, one villager at a time in id order.
|
* share what is left evenly, one villager at a time in id order.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Engine\Simulation;
|
||||||
|
use App\Engine\Starvation;
|
||||||
|
use App\Game\Economy\Population;
|
||||||
|
use App\Models\Building;
|
||||||
|
use App\Models\Village;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
// Population counts heads now, the same on every world speed (architecture.md,
|
||||||
|
// "Population"). Before, farms fed world speed times as many, so a village on a fast
|
||||||
|
// world could hold more heads than its farms feed today. Each such player village gets
|
||||||
|
// its highest farm raised to the lowest level that feeds its soldiers where they eat
|
||||||
|
// (at home, stationed support, training, wounded) and the workers its buildings need,
|
||||||
|
// at most the farm's max level. Orders queued on that farm no longer fit and are
|
||||||
|
// cancelled with a full refund, as after a catapult hit. Points follow the new level.
|
||||||
|
// A village that fits already is left alone, so running this again changes nothing.
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
// Test databases start empty.
|
||||||
|
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$this->raiseFarms(now()->toImmutable());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
// The old levels are gone: farms stay where they were raised to.
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return array<int, int> village id => the level its highest farm was raised to */
|
||||||
|
public function raiseFarms(CarbonImmutable $now): array
|
||||||
|
{
|
||||||
|
$raised = [];
|
||||||
|
$ids = Village::whereNotNull('player_id')->orderBy('id')->pluck('id');
|
||||||
|
foreach ($ids as $id) {
|
||||||
|
$level = DB::transaction(fn () => $this->raise(Village::whereKey($id)->lockForUpdate()->first(), $now));
|
||||||
|
if ($level !== null) {
|
||||||
|
$raised[$id] = $level;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $raised;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function raise(Village $village, CarbonImmutable $now): ?int
|
||||||
|
{
|
||||||
|
$sim = Simulation::of($village);
|
||||||
|
$rules = $sim->rules();
|
||||||
|
// The farm working at the full share: highest level, then oldest.
|
||||||
|
$farm = $village->buildings
|
||||||
|
->filter(fn (Building $b) => $rules->isFarm($b->type))
|
||||||
|
->sortBy([['level', 'desc'], ['id', 'asc']])
|
||||||
|
->first();
|
||||||
|
if (! $farm) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$level = (new Population($sim->config()))->lowestFarmLevel($sim->states(), $farm->id, $sim->soldierUpkeep());
|
||||||
|
if ($level <= $farm->level) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$farm->level = $level;
|
||||||
|
// Settled at the new rates: the village was fed under the old rule all along.
|
||||||
|
$sim->settle($now);
|
||||||
|
$queued = $village->orders->firstWhere('building_id', $farm->id);
|
||||||
|
if ($queued) {
|
||||||
|
$sim->cancel($queued);
|
||||||
|
}
|
||||||
|
$farm->save();
|
||||||
|
$village->points = $sim->points();
|
||||||
|
$sim->startWaitingOrders($now);
|
||||||
|
Starvation::of($village, $sim)->sync($now);
|
||||||
|
$village->save();
|
||||||
|
|
||||||
|
return $level;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue