Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\modules\tour\models;
- use Yii;
- use yii\base\InvalidParamException;
- /**
- * This is the model class for table "{{%tour_playoff}}".
- *
- * @property integer $id
- * @property integer $part
- * @property integer $position
- * @property integer $user_id
- * @property integer $tour_id
- * @property integer $team_id
- * @property integer $score
- */
- class TourPlayoff extends \yii\db\ActiveRecord
- {
- public $round;
- private $tour_id;
- public $height;
- public $position;
- private $_data;
- public $data;
- public $colPlayers;
- /**
- * @param int $tour_id
- */
- public function setTourId($tour_id)
- {
- $this->tour_id = $tour_id;
- }
- /**
- * @return int
- */
- public function getTourId()
- {
- return $this->tour_id;
- }
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%tour_playoff}}';
- }
- public function getData()
- {
- $this->_data = TourPlayoff::find()->with(['team', 'user', 'profile'])->asArray()->where(['tour_id' => $this->tour_id])->orderBy(['position' => ''])->all();
- $array = $this->getRound();
- $this->round = $array['round'];
- $this->colPlayers = $array['countPart1'];
- for ($stage = 0; $stage <= $array['round'] ; $stage++) {
- for ($iteration = 0; $iteration < $array['countPart1']; $iteration++) {
- foreach ($this->_data as $key => $var) {
- if ($iteration == $stage) {
- if ($var['part'] == $iteration + 1) {
- $this->data[$iteration]['position'][] = $var['position'];
- $this->data[$iteration][$var['position']]['name'] = $var['team']['name'];
- }
- }
- }
- }
- }
- return $this->data;
- }
- private function getRound() {
- $countPart1 = TourPlayoff::find()->where(['tour_id' => $this->getTourId(), 'part' => 1])->count();
- $round = 1;
- for($i = $countPart1; $i > 1; $i = $i / 2) {
- $round++;
- }
- return [
- 'round' => $round,
- 'countPart1' => $countPart1,
- ];
- }
- public function splitInHalf($stageInside)
- {
- if ($stageInside != 0) {
- $this->colPlayers = $this->colPlayers / 2;
- }
- }
- public static function checkNumberPlayersInPlayoff($number)
- {
- if(!in_array($number, [2, 4, 8, 16, 32, 64, 128, 256]))
- throw new InvalidParamException('Неправильное кол-во игроков для Playoff');
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['part', 'tour_id'], 'required'],
- [['id', 'part', 'position', 'user_id', 'tour_id', 'team_id', 'score'], 'integer'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'part' => 'Part',
- 'position' => 'Position',
- 'user_id' => 'User ID',
- 'tour_id' => 'Tour ID',
- 'team_id' => 'Team ID',
- 'score' => 'Score',
- ];
- }
- public function getHeight($iteration, $stage)
- {
- $this->position = $iteration + 1;
- if ($stage < 2) $this->height = 70;
- if ($stage >= 2) $this->height = 70 + $iteration * 20;
- }
- public function getUser()
- {
- return $this->hasOne(\app\modules\user\models\User::className(), ['id' => 'user_id']);
- }
- public function getProfile()
- {
- return $this->hasOne(\app\modules\user\models\Profile::className(), ['user_id' => 'user_id']);
- }
- public function getTeam()
- {
- return $this->hasOne(\app\modules\team\models\Team::className(), ['id' => 'team_id']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement