/** * @param BuildingRepair $buildingRepair * @param int $time */ public function repair(BuildingRepair $buildingRepair, $time = 60) { $building = $buildingRepair->getBuilding(); $currentCondition = $building->getCurrentCondition(); $startedCondition = $buildingRepair->getStartedCondition(); $type = $buildingRepair->getType(); $timeConstant = $this->buildingRepairPrice->getTimeConstant($type); $updateTimes = $timeConstant * (100 - $startedCondition) / $time; $step = (100 - $startedCondition) / $updateTimes; $building->setCurrentCondition($currentCondition + $step); $this->buildingRepository->update(); }
function it_should_repair_building(BuildingRepository $buildingRepository, BuildingRepair $buildingRepair, Building $building, BuildingRepairPrice $buildingRepairPrice) { $time = 60; $type = BuildingRepair::TYPE_VERY_FAST_ID; $timeConstant = BuildingRepair::TYPE_VERY_FAST_TIME_CONSTANT; $startedCondition = 10; $currentCondition = 20; $buildingRepair->getBuilding()->willReturn($building); $building->getCurrentCondition()->willReturn($currentCondition); $buildingRepair->getType()->willReturn($type); $buildingRepair->getStartedCondition()->willReturn($startedCondition); $buildingRepairPrice->getTimeConstant($type)->willReturn($timeConstant); $updateTimes = BuildingRepair::TYPE_VERY_FAST_TIME_CONSTANT * (100 - $startedCondition) / $time; $step = (100 - $startedCondition) / $updateTimes; $building->setCurrentCondition($currentCondition + $step)->shouldBeCalled(); $buildingRepository->update()->shouldBeCalled(); $this->repair($buildingRepair, $time); }
/** * Start repair a building * * @param Building $building * @param $type * @return int */ public function repair(Building $building, $type) { $amount = $this->buildingRepairPrice->countPrice($building, $type); if (!$this->gamerHasEnoughCash->isSatisfiedBy($building->getGamer(), $amount)) { throw new NotEnoughCashException(); } if ($this->buildingRepairRepository->countActiveBuildings($building) > 0) { throw new BuildingAlreadyRepairing(); } $duration = $this->buildingRepairPrice->getTimeConstant($type) * (100 - $building->getCurrentCondition()); $buildingRepair = new BuildingRepair(); $buildingRepair->setBuilding($building); $buildingRepair->setStatus(BuildingRepair::STATUS_ACTIVE); $buildingRepair->setType($type); $buildingRepair->setValidDuration($duration); $buildingRepair->setStartedCondition($building->getCurrentCondition()); $this->buildingRepairRepository->save($buildingRepair); $building->setStatus(Building::INACTIVE); $this->buildingRepository->update(); $this->reduceCash->reduce($building->getGamer(), $amount); $event = new BuildingRepairStarted($building); $this->eventDispatcher->dispatch($event::NAME, $event); }