Esempio n. 1
0
 /**
  * @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_can_start_repairing_building(GamerHasEnoughCash $gamerHasEnoughCash, Building $building, Gamer $gamer, BuildingRepairRepository $buildingRepairRepository, BuildingRepairPrice $buildingRepairPrice, ReduceCash $reduceCash, EventDispatcher $eventDispatcher, BuildingRepository $buildingRepository)
 {
     $amount = 3600;
     $type = BuildingRepair::TYPE_FAST_ID;
     $buildingRepairPrice->countPrice($building, $type)->willReturn($amount);
     $building->getGamer()->willReturn($gamer);
     $gamerHasEnoughCash->isSatisfiedBy($gamer, $amount)->willReturn(true);
     $buildingRepairRepository->countActiveBuildings($building)->willReturn(0);
     $buildingRepairPrice->getTimeConstant($type)->willReturn(0.8);
     $building->getCurrentCondition()->willReturn(50);
     $building->setStatus(Building::INACTIVE)->shouldBeCalled();
     $buildingRepository->update()->shouldBeCalled();
     $buildingRepairRepository->save(Argument::type('Mm\\AppBundle\\Entity\\BuildingRepair'))->shouldBeCalled();
     $reduceCash->reduce($gamer, $amount)->shouldBeCalled();
     $eventDispatcher->dispatch(BuildingRepairStarted::NAME, Argument::type('Mm\\Model\\Event\\BuildingRepairStarted'))->shouldBeCalled();
     $this->repair($building, $type);
 }
Esempio n. 3
0
 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);
 }