/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $user = \Auth::user();
     $match = Match::find(\Request::input('match'));
     $sheet = $user->getPrimarySheet();
     return \Response::json(['message' => 'You have already be ton this match!'], 422);
     // Status code here
     $this->validate($request, ['match' => 'required', 'team' => 'required', 'amount' => 'required|numeric|between:0,' . $sheet->amount]);
     $bet = new Bet();
     $bet->match_id = \Request::input('match');
     $bet->amount = \Request::input('amount');
     $bet->user()->associate($user);
     $bet->match()->associate($match);
     $bet->sheet()->associate($sheet);
     $bet->save();
     $sheet->amount = $sheet->amount - \Request::input('amount');
     $sheet->save();
     /*return \Auth::check() ? 'authenticated': "NO biiish!";*/
     /*return \Response::json($bet);*/
 }
Beispiel #2
0
 public function addTicket(Request $request)
 {
     if (\Cache::has('ticket.user.' . $this->user->id)) {
         return response()->json(['text' => 'Подождите...', 'type' => 'error']);
     }
     \Cache::put('ticket.user.' . $this->user->id, '', 0.02);
     $totalItems = $this->user->itemsCountByGame($this->game);
     if ($totalItems > self::MAX_ITEMS || 1 + $totalItems > self::MAX_ITEMS) {
         return response()->json(['text' => 'Максимальное кол-во предметов для депозита - ' . self::MAX_ITEMS, 'type' => 'error']);
     }
     if (!$request->has('id')) {
         return response()->json(['text' => 'Ошибка. Попробуйте обновить страницу.', 'type' => 'error']);
     }
     if ($this->game->status == Game::STATUS_PRE_FINISH || $this->game->status == Game::STATUS_FINISHED) {
         return response()->json(['text' => 'Дождитесь следующей игры!', 'type' => 'error']);
     }
     $id = $request->get('id');
     $ticket = Ticket::find($id);
     if (is_null($ticket)) {
         return response()->json(['text' => 'Ошибка.', 'type' => 'error']);
     } else {
         if ($this->user->money >= $ticket->price) {
             $ticketFrom = $this->lastTicket + 1;
             $ticketTo = $ticketFrom + $ticket->price * 100 - 1;
             $this->redis->set('last.ticket.' . $this->game->id, $ticketTo);
             $bet = new Bet();
             $bet->user()->associate($this->user);
             $bet->items = json_encode([$ticket]);
             $bet->itemsCount = 1;
             $bet->price = $ticket->price;
             $bet->from = $ticketFrom;
             $bet->to = $ticketTo;
             $bet->game()->associate($this->game);
             $bet->save();
             $bets = Bet::where('game_id', $this->game->id);
             $this->game->items = $bets->sum('itemsCount');
             $this->game->price = $bets->sum('price');
             if (count($this->game->users()) >= self::MIN_USERS) {
                 $this->game->status = Game::STATUS_PLAYING;
                 $this->game->started_at = Carbon::now();
             }
             if ($this->game->items >= self::MAX_ITEMSALL) {
                 $this->game->status = Game::STATUS_FINISHED;
                 $this->redis->publish(self::SHOW_WINNERS, true);
             }
             $this->game->save();
             $this->user->money = $this->user->money - $ticket->price;
             $this->user->save();
             $chances = $this->_getChancesOfGame($this->game);
             $returnValue = ['betId' => $bet->id, 'userId' => $this->user->steamid64, 'html' => view('includes.bet', compact('bet'))->render(), 'itemsCount' => $this->game->items, 'gamePrice' => $this->game->price, 'gameStatus' => $this->game->status, 'chances' => $chances];
             $this->redis->publish(self::NEW_BET_CHANNEL, json_encode($returnValue));
             return response()->json(['text' => 'Действие выполнено.']);
         } else {
             return response()->json(['text' => 'Недостаточно средств на вашем балансе.', 'type' => 'error']);
         }
     }
 }