/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     if (ACL::checkUserPermission('bet.roulette') == false) {
         return Redirect::action('dashboard');
     }
     $param = Input::only('bettype', 'betnumber', 'amount', 'game_id');
     $retrieve = Wallet::where('account_id', Auth::user()->id)->first();
     $bet_type = array('straight', 'split', 'line', 'square', 'basket', 'doublestreet');
     if ($retrieve->credits >= $param['amount']) {
         if (Input::has('betnumber')) {
             if (count($param['betnumber']) >= 0 && count($param['betnumber']) <= 6) {
                 $retrieve = Wallet::where('account_id', Auth::user()->id)->first();
                 $index = count($param['betnumber']) - 1;
                 try {
                     $update = $retrieve->decrement('credits', (double) $param['amount']);
                     $fundinout = array('wallet_id' => $retrieve->id, 'onbehalf' => Auth::user()->id, 'credits' => $param['amount'], 'description' => 'Bet on Roulette amount of $' . (double) $param['amount'], 'fundtype' => 'bet');
                     $fundout = Fundinout::create($fundinout);
                     $bet = array('player_id' => Auth::user()->id, 'channel_id' => $param['game_id'], 'bet_number' => implode(',', $param['betnumber']), 'bet_amount' => $param['amount'], 'bet_type' => $bet_type[$index]);
                     Gamebets::create($bet);
                     $message = 'Bet has been successfully place.';
                     return Redirect::action('bet.roulette')->with('success', $message);
                 } catch (Exception $e) {
                     return false;
                 }
             } else {
                 return Redirect::action('bet.roulette')->with('error', 'You can only place maximum of 6 number.');
             }
         }
         if (Input::has('bettype')) {
             $param = Input::only('bettype', 'amount', 'game_id');
             $retrieve = Wallet::where('account_id', Auth::user()->id)->first();
             try {
                 $update = $retrieve->decrement('credits', (double) $param['amount']);
                 $fundinout = array('wallet_id' => $retrieve->id, 'onbehalf' => Auth::user()->id, 'credits' => $param['amount'], 'description' => 'Bet on Roulette amount of $' . (double) $param['amount'], 'fundtype' => 'bet');
                 $fundout = Fundinout::create($fundinout);
                 $bet = array('player_id' => Auth::user()->id, 'channel_id' => $param['game_id'], 'bet_number' => $param['bettype'], 'bet_amount' => $param['amount'], 'bet_type' => $param['bettype']);
                 Gamebets::create($bet);
                 $message = 'Bet has been successfully place.';
                 return Redirect::action('bet.roulette')->with('success', $message);
             } catch (Exception $e) {
                 return false;
             }
         }
     } else {
         return Redirect::action('bet.roulette')->with('error', 'Insufficient credits!');
     }
 }
 public function winnings()
 {
     //retrieve POST value
     $param = Input::only('gameid', 'winning_number');
     $rules = array('gameid' => 'required|exists:game_channel,id', 'winning_number' => 'required|integer|between:0,36');
     //custom error messaging
     $messages = array('gameid.exists' => 'Game doesn\'t exist');
     $validator = Validator::make($param, $rules, $messages);
     if ($validator->passes()) {
         $gamewinnings = array('game_id' => 1, 'channel_id' => $param['gameid'], 'winning_number' => $param['winning_number']);
         $winnings = Gamewinnings::create($gamewinnings);
         Gamechannel::where('id', $param['gameid'])->update(array('channel_status' => 0));
         $bets = Gamebets::with('channel', 'payout')->where('channel_id', $param['gameid'])->get();
         $bet_win = array();
         foreach ($bets as $row) {
             $win = false;
             switch ($row->payout->name) {
                 case "straight":
                     if ($row->bet_number == $param['winning_number']) {
                         $win = true;
                     }
                     break;
                 case "split":
                     $numbers = explode(",", $row->bet_number);
                     if (in_array($param['winning_number'], $numbers)) {
                         $win = true;
                     }
                     break;
                 case "line":
                     $numbers = explode(",", $row->bet_number);
                     if (in_array($param['winning_number'], $numbers)) {
                         $win = true;
                     }
                     break;
                 case "square":
                     $numbers = explode(",", $row->bet_number);
                     if (in_array($param['winning_number'], $numbers)) {
                         $win = true;
                     }
                     break;
                 case "basket":
                     $numbers = explode(",", $row->bet_number);
                     if (in_array($param['winning_number'], $numbers)) {
                         $win = true;
                     }
                     break;
                 case "1stcolumn":
                     $numbers_covered = explode(',', $row->payout->numbers_covered);
                     if (in_array($param['winning_number'], $numbers_covered)) {
                         $win = true;
                     }
                     break;
                 case "2ndcolumn":
                     $numbers_covered = explode(',', $row->payout->numbers_covered);
                     if (in_array($param['winning_number'], $numbers_covered)) {
                         $win = true;
                     }
                     break;
                 case "3rdcolumn":
                     $numbers_covered = explode(',', $row->payout->numbers_covered);
                     if (in_array($param['winning_number'], $numbers_covered)) {
                         $win = true;
                     }
                     break;
                 case "1dozen":
                     $numbers_covered = explode(',', $row->payout->numbers_covered);
                     if (in_array($param['winning_number'], $numbers_covered)) {
                         $win = true;
                     }
                     break;
                 case "2dozen":
                     $numbers_covered = explode(',', $row->payout->numbers_covered);
                     if (in_array($param['winning_number'], $numbers_covered)) {
                         $win = true;
                     }
                     break;
                 case "3dozen":
                     $numbers_covered = explode(',', $row->payout->numbers_covered);
                     if (in_array($param['winning_number'], $numbers_covered)) {
                         $win = true;
                     }
                     break;
                 case "even":
                     if ($param['winning_number'] % 2 == 0) {
                         $win = true;
                     }
                     break;
                 case "odd":
                     if ($param['winning_number'] % 2 != 0) {
                         $win = true;
                     }
                     break;
                 default:
                     $numbers_covered = explode(',', $row->payout->numbers_covered);
                     if (in_array($param['winning_number'], $numbers_covered)) {
                         $win = true;
                     }
             }
             if ($win == true) {
                 $update = array('bet_result' => 'win', 'bet_status' => 0);
                 Gamebets::where('id', $row->id)->update($update);
                 $this->player_winnings($row->payout->payout, $row->player_id, $row->bet_amount, $row->channel->channel_id);
                 $bet_win[] = $row->id;
             } elseif ($win == false) {
                 $update = array('bet_result' => 'loss', 'bet_status' => 0);
                 Gamebets::where('id', $row->id)->update($update);
             }
         }
         if (count($bet_win) > 0) {
             $player_winnings = Gamebets::with('payout', 'playerdetails')->whereIn('id', $bet_win)->get();
             if (!empty($player_winnings->first())) {
                 $response = array('betwin' => true, 'message' => 'List of players who won.', 'data' => $player_winnings->toArray());
             }
         } else {
             $response = array('betwin' => false, 'message' => 'No player has won this game.');
         }
     } else {
         //All error message in validation
         $messages = $validator->messages();
         //response if merchant doesnt have list of products.
         $response = array('betwin' => 404, 'error' => 'not_found', 'error_message' => $messages->all());
     }
     echo json_encode($response);
 }