toImmutable(); $this->engine->catchUp($villageId, $now); return DB::transaction(function () use ($user, $villageId, $change, $now) { $village = Village::whereKey($villageId)->lockForUpdate()->first(); if (! $village || $village->player?->user_id !== $user->id) { throw new GameRuleViolation('not_found', 'Village not found.'); } $sim = Simulation::of($village); $sim->settle($now); Military::of($village, $sim)->settle($now); $change($sim, $village, $now); $village->save(); return $village; }); } protected function building(Village $village, int $buildingId): Building { return $village->buildings->firstWhere('id', $buildingId) ?? throw new GameRuleViolation('not_found', 'Building not found.'); } }