예제 #1
0
 public function placeBet($bet_option, $points)
 {
     //date_default_timezone_set('Asia/Ho_Chi_Minh');
     Yii::log(date('Y-m-d H:i:s'));
     Yii::log('User: '******' Points bet: ' . $points . ' . Points in hand: ' . $this->points, CLogger::LEVEL_INFO);
     if ($points > $this->points) {
         Yii::log('Bet rejected. Points bet (' . $points . ') is greater than user has(' . $this->points . ')', CLogger::LEVEL_INFO);
         throw new Exception('Points you bet (' . $points . ') is greater than you have(' . $this->points . ')');
     }
     if ($bet_option->event->bet_until < date('Y-m-d H:i:s')) {
         // need to check if over bet time
         Yii::log('Now is ' . date('Y-m-d H:i:s') . ' which is over match bet until time(' . $bet_option->event->bet_until . ')');
         throw new Exception('Match is closed for betting');
     } else {
         $bet_option_user = new BetOptionUser();
         $bet_option_user->user_id = $this->id;
         $bet_option_user->bet_option_id = $bet_option->id;
         $bet_option_user->bet_points = $points;
         //TODO: need to use transaction here
         $this->points -= $points;
         //echo $this->validate();
         //echo $bet_option_user->validate();
         if ($this->validate() && $bet_option_user->validate()) {
             $this->save();
             $bet_option_user->save();
             Yii::app()->user->setState('user', $this);
             Yii::log('Bet accepted. User points is deducted ' . $points . ' points. Now user points is ' . $this->points);
             return $bet_option_user;
         } else {
             throw new Exception(CHtml::errorSummary($this) + CHtml::errorSummary($bet_option_user));
         }
     }
 }