DanKor
Опытный
- Сообщения
- 244
- Реакции
- 52
DanKor
- 244
- 52
Код:
public function newBet(){
if ($this->user->skins == 1){
$data = $this->redis->lrange('bets.list', 0, -1);
foreach ($data as $newBetJson) {
$newBet = json_decode($newBetJson, true);
$user = User::find($newBet['userid']);
if (is_null($user)) continue;
if ($this->game->id < $newBet['gameid']) continue;
if ($this->game->id >= $newBet['gameid']) $newBet['gameid'] = $this->game->id;
if ($this->game->status == Game::STATUS_PRE_FINISH || $this->game->status == Game::STATUS_FINISHED) {
$this->_responseMessageToSite('Ваша ставка пойдёт на следующую игру.', $user->steamid64);
$this->redis->lrem('bets.list', 0, $newBetJson);
$newBet['gameid'] = $newBet['gameid'] + 1;
$this->redis->rpush('bets.list', json_encode($newBet));
continue;
}
$ticketFrom = $this->lastTicket + 1;
$ticketTo = $ticketFrom + ($newBet['price'] * self::TICKETS_RATE) - 1;
$this->redis->set('last.ticket', $ticketTo);
$bet = new Bet();
$bet->user()->associate($user);
$bet->items = json_encode($newBet['items']);
$bet->itemsCount = count($newBet['items']);
$bet->price = $newBet['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()) >= 2 || $this->game->items >= 100) {
$this->game->status = Game::STATUS_PLAYING;
$this->game->started_at = Carbon::now();
}
if ($this->game->items >= 100) {
$this->game->status = Game::STATUS_FINISHED;
$this->redis->publish(self::SHOW_WINNERS, true);
}
$this->game->save();
$chances = $this->_getChancesOfGame($this->game);
$returnValue = [
'betId' => $bet->id,
'userId' => $user->steam64,
'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));
$this->redis->lrem('bets.list', 0, $newBetJson);
}
return $this->_responseSuccess();
}
else if ($this->user->skins == 0){
$data = $this->redis->lrange('bets.list', 0, -1);
foreach($data as $newBetJson) {
$newBet = json_decode($newBetJson, true);
$user = User::find($newBet['userid']);
if(is_null($user)) continue;
$user->money = $user->money + $newBet['price'];
$user->save();
$this->redis->lrem('bets.list', 0, $newBetJson);
}
return $this->_responseSuccess();
}
}
Последнее редактирование: