From 6e022f7d1f845b9ab0f29843cdbacb86fdd17f0f Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Tue, 3 Nov 2026 09:41:12 +0100 Subject: [PATCH] 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 --- app/Game/Economy/Population.php | 22 +++++ ...26_11_03_180000_farms_feed_their_heads.php | 83 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 database/migrations/2026_11_03_180000_farms_feed_their_heads.php diff --git a/app/Game/Economy/Population.php b/app/Game/Economy/Population.php index 78d953e..a47a3ba 100644 --- a/app/Game/Economy/Population.php +++ b/app/Game/Economy/Population.php @@ -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 $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 * share what is left evenly, one villager at a time in id order. diff --git a/database/migrations/2026_11_03_180000_farms_feed_their_heads.php b/database/migrations/2026_11_03_180000_farms_feed_their_heads.php new file mode 100644 index 0000000..daf1023 --- /dev/null +++ b/database/migrations/2026_11_03_180000_farms_feed_their_heads.php @@ -0,0 +1,83 @@ +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 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; + } +};