Example #1
0
 public function afterInsert()
 {
     $user = $this->getUser();
     $universe = $this->getUniverseComponent();
     // 1) Find a celestial body for the player and reset its attributes
     //    to default values.
     $celestialBody = $universe->resetCelestialBody($universe->chooseCelestialBodyForNewPlayer());
     if (!$celestialBody->save()) {
         throw new Exception('Failed to save celestial body. Errors: ' . print_r($celestialBody->errors, true));
     }
     $base = new Base();
     $base->id = $celestialBody->id;
     $base->user_id = $user->id;
     $base->name = Yii::t('app', "{username}'s colony", ['username' => $user->name]);
     // TODO make configurable
     $base->stored_iron = 5000;
     $base->stored_steel = 2000;
     $base->stored_chemicals = 5000;
     $base->stored_vv4a = 0;
     $base->stored_ice = 5000;
     $base->stored_water = 5000;
     $base->stored_energy = 5000;
     $base->stored_people = 500;
     $base->stored_credits = 5000;
     //$base->stored_last_update = ;
     if (!$base->save()) {
         throw new \Exception('Failed to save base. Errors: ' . print_r($base->errors, true));
     }
 }
Example #2
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 #3
0
 public static function _prepareDataSelect($collections, $key, $value, $addFirst = true)
 {
     $data = [];
     if ($addFirst) {
         $data[0] = 'Chọn khu vực';
     }
     return parent::_prepareDataSelect($collections, $key, $value, $data);
 }
Example #4
0
 public function attach($owner)
 {
     if (!$owner instanceof Base) {
         $behaviorClass = $this->className();
         $ownerClass = Base::className();
         throw new InvalidConfigException("'{$behaviorClass}' must only be attached to '{$ownerClass}' instances.");
     }
     parent::attach($owner);
 }
Example #5
0
 /**
  * 获取聊天记录
  *
  * @param $sendId
  * @param $receiveId
  *
  * @return mixed
  */
 public function getMessageHistory($sendId, $receiveId)
 {
     $table = 'user_message';
     $messageModel = \common\models\Base::getInstance($table);
     $where = "receive_user_id={$sendId} and send_user_id={$receiveId} and status=2";
     $handle = $messageModel->Query()->where($where)->select('*');
     $list = $handle->all();
     \Yii::$app->db->createCommand("update bhy_{$table} set status=1 where {$where}")->execute();
     return $list;
 }
Example #6
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 #8
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 #9
0
 /**
  * 发送红包
  * @param $sendId
  * @param $receiveId
  * @param $money
  * @param $bri_message
  * @return bool|string
  * @throws \Exception
  */
 public function sendBribery($sendId, $receiveId, $money, $bri_message)
 {
     $tran = \Yii::$app->db->beginTransaction();
     $model = Base::getInstance('user_bribery');
     $model->send_user_id = $sendId;
     $model->receive_user_id = $receiveId;
     $model->money = $money;
     $model->create_time = time();
     $model->status = 0;
     $model->bri_message = $bri_message;
     if ($model->insert(true)) {
         $id = \Yii::$app->db->lastInsertID;
         if (User::getInstance()->changeBalance($sendId, $money)) {
             $tran->commit();
         }
         return $id;
     }
     $tran->rollBack();
     return false;
 }
Example #10
0
 /**
  * 添加评论
  * @param $data
  * @return bool
  */
 public function addComment($data)
 {
     $tran = \Yii::$app->db->beginTransaction();
     $comment = \common\models\Base::getInstance("user_comment");
     $comment->user_id = \common\util\Cookie::getInstance()->getCookie('bhy_id')->value;
     $comment->content = $data['content'];
     $comment->dynamic_id = $data['dynamicId'];
     $comment->private = $data['private'];
     $comment->create_time = $data['create_time'];
     $flag = $comment->save();
     $id = Yii::$app->db->lastInsertID;
     $dynamic = \common\models\Base::getInstance("user_dynamic")->findOne($data['dynamicId']);
     $dynamic->comment_num = $dynamic->comment_num + 1;
     if ($flag && $dynamic->save()) {
         $tran->commit();
         return $id;
     }
     $tran->rollBack();
     return false;
 }
Example #11
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getBases()
 {
     return $this->hasMany(Base::className(), ['user_id' => 'id'])->inverseOf('user');
 }
Example #12
0
 public static function _prepareDataSelect($collections, $key, $value)
 {
     $data[0] = 'Chọn cảm biến';
     return parent::_prepareDataSelect($collections, $key, $value, $data);
 }
Example #13
0
 /**
  * Updates the base's stock to the given time.
  * 
  * @param Base $base
  * @param \DateTime $time
  */
 public function updateStock($base, $time = null)
 {
     if ($time === null) {
         $time = $this->getTimeComponent()->getStartTime();
     }
     $productionCalculator = Yii::createObject(ProductionCalculator::className());
     $storageCalculator = Yii::createObject(StorageCalculator::className());
     $populationCalculator = Yii::createObject(PopulationCalculator::className());
     $updateStockTask = new UpdateStockTask(['from' => $base->getDateTimeStoredLastUpdate(), 'population' => $populationCalculator->run($base), 'production' => $productionCalculator->calculateProduction($base), 'stock' => $base->getStock(), 'storage' => $storageCalculator->calculateStorage($base), 'to' => $time]);
     $updateStockTask->execute();
     $base->setDateTimeStoredLastUpdate($time);
     $base->setStock($updateStockTask->stock);
     $this->addDirtyModel($base);
 }
Example #14
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getBase()
 {
     return $this->hasOne(Base::className(), ['id' => 'base_id']);
 }
Example #15
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getNik0()
 {
     return $this->hasOne(Base::className(), ['nik' => 'nik']);
 }
Example #16
0
 /**
  * 用户操作日志
  */
 public function userLog($log)
 {
     $userLog = \common\models\Base::getInstance('user_log');
     $userLog->user_id = $log['user_id'];
     $userLog->type = $log['type'];
     $userLog->create_time = $log['time'];
     $userLog->ip = ip2long($_SERVER["REMOTE_ADDR"]);
     return $userLog->insert(false);
 }
Example #17
0
 public static function _prepareDataSelect($collections, $key, $value)
 {
     $data[null] = 'Chọn cấp độ người dùng';
     return parent::_prepareDataSelect($collections, $key, $value, $data);
 }
Example #18
0
 public function addApply($user_id, $data)
 {
     $apply = Base::getInstance('user_rendezvous_apply');
     $apply->rendezvous_id = $data['rendezvous_id'];
     $apply->user_id = $user_id;
     $apply->create_time = YII_BEGIN_TIME;
     $apply->phone = $data['mobile'];
     isset($data['msg']) ? $apply->message = $data['msg'] : true;
     return $apply->insert(false);
 }