Advertisement
Guest User

Untitled

a guest
Feb 4th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\modules\tour\models;
  4.  
  5. use Yii;
  6. use yii\base\InvalidParamException;
  7.  
  8. /**
  9.  * This is the model class for table "{{%tour_playoff}}".
  10.  *
  11.  * @property integer $id
  12.  * @property integer $part
  13.  * @property integer $position
  14.  * @property integer $user_id
  15.  * @property integer $tour_id
  16.  * @property integer $team_id
  17.  * @property integer $score
  18.  */
  19. class TourPlayoff extends \yii\db\ActiveRecord
  20. {
  21.     public $round;
  22.     private $tour_id;
  23.     public $height;
  24.     public $position;
  25.     private $_data;
  26.     public $data;
  27.     public $colPlayers;
  28.  
  29.     /**
  30.      * @param int $tour_id
  31.      */
  32.     public function setTourId($tour_id)
  33.     {
  34.         $this->tour_id = $tour_id;
  35.     }
  36.  
  37.     /**
  38.      * @return int
  39.      */
  40.     public function getTourId()
  41.     {
  42.         return $this->tour_id;
  43.     }
  44.  
  45.     /**
  46.      * @inheritdoc
  47.      */
  48.     public static function tableName()
  49.     {
  50.         return '{{%tour_playoff}}';
  51.     }
  52.  
  53.     public function getData()
  54.     {
  55.         $this->_data = TourPlayoff::find()->with(['team', 'user', 'profile'])->asArray()->where(['tour_id' => $this->tour_id])->orderBy(['position' => ''])->all();
  56.        
  57.         $array = $this->getRound();
  58.         $this->round = $array['round'];
  59.         $this->colPlayers = $array['countPart1'];
  60.  
  61.         for ($stage = 0; $stage <= $array['round'] ; $stage++) {
  62.             for ($iteration = 0; $iteration < $array['countPart1']; $iteration++) {
  63.                 foreach ($this->_data as $key => $var) {
  64.                     if ($iteration == $stage) {
  65.                         if ($var['part'] == $iteration + 1) {
  66.                             $this->data[$iteration]['position'][] = $var['position'];
  67.                             $this->data[$iteration][$var['position']]['name'] = $var['team']['name'];
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.        
  74.         return $this->data;
  75.     }
  76.  
  77.     private function getRound() {
  78.         $countPart1 = TourPlayoff::find()->where(['tour_id' => $this->getTourId(), 'part' => 1])->count();
  79.         $round = 1;
  80.  
  81.         for($i = $countPart1; $i > 1; $i = $i / 2) {
  82.             $round++;
  83.         }
  84.         return [
  85.             'round' => $round,
  86.             'countPart1' => $countPart1,
  87.         ];
  88.     }
  89.  
  90.     public function splitInHalf($stageInside)
  91.     {
  92.         if ($stageInside != 0) {
  93.             $this->colPlayers = $this->colPlayers / 2;
  94.         }
  95.     }
  96.  
  97.     public static function checkNumberPlayersInPlayoff($number)
  98.     {
  99.         if(!in_array($number, [2, 4, 8, 16, 32, 64, 128, 256]))
  100.             throw new InvalidParamException('Неправильное кол-во игроков для Playoff');
  101.     }
  102.  
  103.     /**
  104.      * @inheritdoc
  105.      */
  106.     public function rules()
  107.     {
  108.         return [
  109.             [['part', 'tour_id'], 'required'],
  110.             [['id', 'part', 'position', 'user_id', 'tour_id', 'team_id', 'score'], 'integer'],
  111.         ];
  112.     }
  113.  
  114.     /**
  115.      * @inheritdoc
  116.      */
  117.     public function attributeLabels()
  118.     {
  119.         return [
  120.             'id' => 'ID',
  121.             'part' => 'Part',
  122.             'position' => 'Position',
  123.             'user_id' => 'User ID',
  124.             'tour_id' => 'Tour ID',
  125.             'team_id' => 'Team ID',
  126.             'score' => 'Score',
  127.         ];
  128.     }
  129.  
  130.  
  131.     public function getHeight($iteration, $stage)
  132.     {
  133.         $this->position = $iteration + 1;
  134.         if ($stage < 2) $this->height = 70;
  135.         if ($stage >= 2) $this->height = 70 + $iteration * 20;
  136.     }
  137.  
  138.  
  139.     public function getUser()
  140.     {
  141.         return $this->hasOne(\app\modules\user\models\User::className(), ['id' => 'user_id']);
  142.     }
  143.  
  144.     public function getProfile()
  145.     {
  146.         return $this->hasOne(\app\modules\user\models\Profile::className(), ['user_id' => 'user_id']);
  147.     }
  148.  
  149.     public function getTeam()
  150.     {
  151.         return $this->hasOne(\app\modules\team\models\Team::className(), ['id' => 'team_id']);
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement