Beispiel #1
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));
     }
 }
Beispiel #2
0
 public function init()
 {
     parent::init();
     $this->_dirtyModels = new \SplObjectStorage();
     $this->_dirtyModelsInfo = [];
     // whenever a base is loaded, update its stock
     //    Event::on( Base::className(), Base::EVENT_AFTER_FIND,
     //      function ($event) {
     //        $this->updateStock( $event->sender );
     //      }
     //    );
     // whenever a user is loaded, execute the user's finished tasks
     //    Event::on( User::className(), User::EVENT_AFTER_FIND,
     //      function ($event) {
     //        $this->executeFinishedTasks( $event->sender );
     //      }
     //    );
     Event::on(AbstractTask::className(), AbstractTask::EVENT_MODEL_MODIFIED, function (ModelModifiedEvent $event) {
         $this->addDirtyModel($event->model);
     });
     Event::on(ConstructBuildingTask::className(), ConstructBuildingTask::EVENT_BEFORE_BUILDING_CONSTRUCTED, function (ConstructBuildingTaskEvent $event) {
         $this->updateStock($event->base, $event->time);
     });
 }