Beispiel #1
0
 private function addResource(ResourceSource $resourceSource)
 {
     /** @var Island $island */
     $island = $resourceSource->getHexagon()->getIsland();
     /** @var ResourceType $resourceType */
     $resourceType = $resourceSource->getResourceType();
     $mainResourceStorage = $this->checkExistsAndGetResourceStorage($island, $resourceType);
     $resourceChanges = [];
     if ($this->managedToDeductRequiredResources($resourceSource, $resourceChanges)) {
         $mainResourceStorage->setCount($mainResourceStorage->getCount() + $resourceSource->getIncrement());
         $resourceChanges[] = ['resource_type' => $resourceType->getName(), 'amount' => '+' . $resourceSource->getIncrement()];
         $resourceSource->setLastTick(new \DateTime());
         $this->em->flush();
         return ['hexagon_id' => $resourceSource->getHexagon()->getId(), 'resource_changes' => $resourceChanges];
     } else {
         return null;
     }
 }
 private function constructionFinishCheck(ConstructionObject $constructionObject)
 {
     $newSources = [];
     $integrity = $constructionObject->getIntegrity();
     $maxIntegrity = $constructionObject->getConstructionType()->getMaxIntegrity();
     if ($integrity >= $maxIntegrity) {
         $constructionObject->setIntegrity($maxIntegrity);
         $integrity = $maxIntegrity;
     }
     if ($integrity > $constructionObject->getConstructionType()->getOperationalIntegrity() && count($constructionObject->getResourceSources()) === 0) {
         $requiredResources = new ArrayCollection();
         $producedResources = new ArrayCollection();
         /** @var Production $resourceProduction */
         foreach ($constructionObject->getConstructionType()->getProductions() as $resourceProduction) {
             if ($resourceProduction->getAmount() > 0) {
                 $producedResources->add($resourceProduction);
             } else {
                 $requiredResources->add($resourceProduction);
             }
         }
         /** @var Production $resourceProduction */
         foreach ($producedResources as $resourceProduction) {
             $source = new ResourceSource();
             $source->setHexagon($constructionObject->getHexagon());
             $source->setResourceType($resourceProduction->getResourceType());
             $source->setIncrement($resourceProduction->getAmount());
             $source->setTick($resourceProduction->getTick());
             $constructionObject->addResourceSource($source);
             /** @var Production $production */
             foreach ($requiredResources as $production) {
                 $requirement = new ResourceSourceRequirement();
                 $requirement->setResourceType($production->getResourceType());
                 $requirement->setAmount($production->getAmount() * -1);
                 $requirement->setResourceSource($source);
                 $source->addResourceSourceRequirement($requirement);
                 $this->em->persist($requirement);
             }
             $this->em->persist($source);
             $this->em->flush();
             $newSources[] = ['resource_source_id' => $source->getId(), 'tick' => $source->getTick(), 'resourceSource' => $source->jsonSerialize()];
         }
     }
     return $newSources;
 }