Example #1
0
 /**
  * Initializes the vv4a balance of the base.
  * 
  * This step takes into account:
  * 
  * 1) Possible production of vv4a according to buildings on the base.
  * 2) Configured production amount.
  * 3) Energy balance of the base.
  */
 private function initVv4aBalance()
 {
     $this->balanceVv4a = 0.0;
     // 1) Possible production of vv4a according to buildings on the base.
     $producibleVv4a = 0.0;
     foreach ($this->base->getBuildingCounters() as $id => $counter) {
         $building = $this->buildingFinder->getById($id);
         $producibleVv4a += $counter * $building->getBalanceVv4a();
     }
     // 2) Configured production amount.
     // null means "produce as much as possible"
     // If there is something configured, just make sure it's not more than what
     // is possible according to the production buildings.
     if ($this->base->produced_vv4a !== null) {
         $vv4aToProduce = intval($this->base->produced_vv4a);
         $producibleVv4a = min($producibleVv4a, $vv4aToProduce);
     }
     // 4) Energy balance of the base
     if ($this->balanceEnergy < $producibleVv4a && intval($this->base->stored_energy) === 0) {
         $producibleVv4a = max(0, $this->balanceEnergy);
     }
     $this->balanceEnergy -= $producibleVv4a;
     $this->balanceSteel -= 2 * $producibleVv4a;
     $this->balanceVv4a += $producibleVv4a;
 }
Example #2
0
 /**
  * @TODO Currently, we can have more people employed than there are on the
  *       planet. Not sure how IW handled it, but maybe we can create strikes
  *       or drop production if there are less people than could be employed?
  */
 private function calculateEmployedPopulation()
 {
     $this->employedPopulation = 0;
     foreach ($this->base->getBuildingCounters() as $id => $counter) {
         /* @var $building \frontend\models\Building */
         $building = $this->buildingFinder->getById($id);
         if ($building->getBalancePeople() < 0) {
             $this->employedPopulation += $counter * \abs($building->getBalancePeople());
         }
     }
 }
 public function calculateBaseCapacity(Base $base)
 {
     $buildingCounters = $base->getBuildingCounters();
     $shelters = $this->buildingFinder->getByGroup('shelters');
     $baseCapacity = new Resources();
     foreach ($shelters as $id => $shelter) {
         $counter = $buildingCounters[$id];
         /* @var $shelter \frontend\behaviors\ShelterCapacityBehavior */
         $capacity = $shelter->calculateShelterCapacity($counter);
         $baseCapacity->chemicals += $capacity->chemicals;
         $baseCapacity->energy += $capacity->energy;
         $baseCapacity->ice += $capacity->ice;
         $baseCapacity->iron += $capacity->iron;
         $baseCapacity->population += $capacity->population;
         $baseCapacity->steel += $capacity->steel;
         $baseCapacity->vv4a += $capacity->vv4a;
         $baseCapacity->water += $capacity->water;
     }
     return $baseCapacity;
 }
Example #4
0
 /**
  * Creates the ConstructBuildingTask.
  */
 public function createTask()
 {
     $building = $this->buildingFinder->getById($this->buildingId);
     /* @var $user \common\models\User */
     $user = Yii::$app->user->identity;
     $base = $user->getBase($this->baseId);
     // TODO once the ConstructionQueueRule is implemented, use it to pass
     //      the currently appropriate factor.
     $base->payFor($building, 1.0);
     $now = $this->getTimeComponent()->getStartTime();
     $finished = clone $now;
     $finished->add($building->getCostTime());
     $taskModel = new Task();
     $taskModel->data = ['baseId' => $this->baseId, 'buildingId' => $this->buildingId];
     $taskModel->dateTimeFinished = $finished;
     $taskModel->type = ConstructBuildingTask::className();
     $taskModel->user_id = $user->id;
     if (!$taskModel->save()) {
         throw new Exception('Failed to create task: ' . print_r($taskModel->firstErrors, true));
     }
 }
Example #5
0
 /**
  * @param \common\models\Base $base
  * @return \frontend\objects\Resources
  */
 public function calculateStorage(Base $base)
 {
     $energy = 0;
     $chemicals = 0;
     $iceAndWater = 0;
     $warehouses = $this->buildingFinder->getByGroup('warehouses');
     $buildingCounters = $base->getBuildingCounters();
     foreach ($warehouses as $id => $building) {
         $counter = $buildingCounters[$id];
         // TODO: Refactor. Each group of buildings could attach specific behaviors
         //       Those could extend the building with needed functionality.
         //       Warehouses: getStorageCapacity( $nWarehouses )
         //       Shelters: getShelterCapacity( $nWarehouses )
         for ($i = 1; i <= $counter; ++$i) {
             $energy += ($i * ($i - 2) + 2) * $building->getStorageEnergy();
             $chemicals += ($i * ($i - 2) + 2) * $building->getStorageChemicals();
             $iceAndWater += ($i * ($i - 2) + 2) * $building->getStorageIceAndWater();
         }
     }
     return new Resources(['iron' => PHP_INT_MAX, 'steel' => PHP_INT_MAX, 'chemicals' => $chemicals, 'vv4a' => PHP_INT_MAX, 'population' => PHP_INT_MAX, 'ice' => $iceAndWater, 'water' => $iceAndWater, 'energy' => $energy, 'credits' => PHP_INT_MAX]);
 }
Example #6
0
 private function convertTasksToInfo($aConstructionTasks)
 {
     /* @var $user \common\models\User */
     $user = \Yii::$app->user->identity;
     $aConstructionInfo = [];
     /* @var $taskModel \common\models\Task */
     foreach ($aConstructionTasks as $taskModel) {
         $baseId = $taskModel->data['baseId'];
         $base = $user->getBase($baseId);
         $buildingId = $taskModel->data['buildingId'];
         $building = $this->buildingFinder->getById($buildingId);
         $now = $this->getTimeComponent()->getStartTime();
         /* @var $remaining \DateInterval */
         $remaining = $taskModel->dateTimeFinished->diff($now);
         $info = new ConstructionInfo();
         $info->base = $base->getLabel();
         $info->building = $building->getName();
         // TODO introduce constant for DateInterval format
         $info->countdown = $remaining->format('%H:%I:%S');
         $info->finished = $taskModel->finished;
         $aConstructionInfo[] = $info;
     }
     return $aConstructionInfo;
 }