mutate($user, $villageId, function (Simulation $sim, Village $village, CarbonImmutable $now) use ($type) { if (! $sim->config()->hasBuilding($type)) { throw new GameRuleViolation('not_found', "Unknown building type [$type]."); } $rules = $sim->rules(); $max = $rules->maxPerVillage($type); if ($max !== null && $sim->instances($type) >= $max) { throw new GameRuleViolation('max_per_village', 'Only one of these per village.'); } if (! $sim->meetsRequirements($type)) { throw new GameRuleViolation('requires', 'Requirements not met.'); } if (! $sim->spaceFits($type)) { throw new GameRuleViolation('no_space', 'Not enough building space.'); } $cost = $rules->cost($type, 0, $sim->instanceIndex($type)); $sim->pay($cost); $sim->enqueue([ 'building_id' => null, 'type' => $type, 'action' => BuildOrder::BUILD, 'target_level' => 0, 'cost' => $cost, ], $now); }); } }