示例#1
0
 /**
  * Добыча ресурсов
  * //TODO[Rottenwood]: Рефакторинг. Перенести в ObtainService
  * @param RoomResource $resource
  * @param EntityManager $em
  * @param array $result
  * @param UserService $userService
  * @param Item $resourceItem
  * @param Room $room
  * @return array
  */
 public function obtain(RoomResource $resource, EntityManager $em, array $result, UserService $userService, Item $resourceItem, Room $room) : array
 {
     if ($this->user->isBusy()) {
         $this->result->setWaitstate($this->user->getWaitstate());
         return [];
     }
     $userService->addWaitstate($this->user, $this->waitState);
     $this->reduceQuantity($em, $resource->getId(), $userService);
     $result['obtained'] = $this->quantityToObtain;
     $resourceQuantity = $resource->getQuantity();
     $result['resources'][$resourceItem->getId()] = $resourceQuantity;
     if ($resourceQuantity <= 0) {
         /** @var Grass[] $grassTypes */
         $grassTypes = $em->getRepository(Grass::class)->findAll();
         $grassType = $grassTypes[array_rand($grassTypes)];
         $room->setType($grassType);
         $result['typeChanged'] = true;
         $em->remove($resource);
     }
     return $result;
 }
示例#2
0
 /**
  * @param string $slot
  * @throws WrongSlot
  */
 public function setSlot($slot)
 {
     if (in_array($slot, Item::getAllSlotNames())) {
         $this->slot = $slot;
     } else {
         throw new WrongSlot($slot);
     }
 }
示例#3
0
 /**
  * Взять предмет
  * @param User $user
  * @param Item $item
  * @param int  $quantityToTake Сколько предметов взять
  */
 public function takeItem(User $user, Item $item, $quantityToTake = 1)
 {
     $inventoryItem = $this->inventoryItemRepository->findOneByUserAndItemId($user, $item->getId());
     if ($inventoryItem) {
         $quantity = $inventoryItem->getQuantity() + $quantityToTake;
         $inventoryItem->setQuantity($quantity);
     } else {
         $inventoryItem = new InventoryItem($user, $item, $quantityToTake);
         $this->inventoryItemRepository->persist($inventoryItem);
     }
     $this->inventoryItemRepository->flush($inventoryItem);
     $this->logger->info(sprintf('[%d]%s взял предмет: [%d]%s x %d шт. (всего %d)', $user->getId(), $user->getName(), $item->getId(), $item->getName(), $quantityToTake, isset($quantity) ? $quantity : $quantityToTake));
 }