/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return BetOptionUser the loaded model * @throws CHttpException */ public function loadModel($id) { $model = BetOptionUser::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
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)); } } }
/** * Handle bet placing (AJAX). */ public function actionDeleteBet() { //if (!YII_DEBUG && !Yii::app()->request->isAjaxRequest) { // throw new CHttpException('403', 'Forbidden access.'); //} header('Content-Type: application/json;charset=UTF-8'); if (isset($_POST['bet_id'])) { $bet_id = $_POST['bet_id']; } else { echo json_encode(array('success' => false, 'message' => 'Invalid request')); Yii::app()->end(); } //$bet_option_id, $points $bet = BetOptionUser::model()->findByPk($bet_id); $user = User::model()->findByPk(Yii::app()->user->id); if ($bet == NULL || $user == NULL) { echo json_encode(array('success' => false, 'message' => 'Bet or user not found', 'data' => NULL)); } else { try { $bet_option = $bet->betOption; $user->deleteBet($bet); echo json_encode(array('success' => true, 'message' => 'Bet deleted successfully', 'data' => array('bet_id' => $bet_option->id, 'user_points' => $user->points))); } catch (Exception $ex) { echo json_encode(array('success' => false, 'message' => $ex->getMessage(), 'data' => NULL)); } } Yii::app()->end(); }