/** * 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; }
/** * @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()); } } }
/** * 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)); } }
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; }