Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.22 KB | None | 0 0
  1. public function diceRollAction()
  2.     {
  3.         $messages = [];
  4.  
  5.         if ($this->request->isPost()) {
  6.             do {
  7.                 if (!$this->auth->hasUserSession()) {
  8.                     $messages[] = 'Необходимо выполнить вход в систему.';
  9.                     break;
  10.                 }
  11.  
  12.                 if (!$this->csrf->check($this->request->getPost())) {
  13.                     $messages[] = 'Неверный CSRF-токен. Попробуйте обновить страницу.';
  14.                     break;
  15.                 }
  16.  
  17.                 #Vars
  18.                $amount     = $this->request->getPost('amount', null, '0.00000000');
  19.                 $currencyId = (int)$this->request->getPost('currencyId', null, '0');
  20.                 $number     = (int)$this->request->getPost('number', null, 50);
  21.                 $direction  = $this->request->getPost('direction', null, 'under');
  22.                 $clientSeed = $this->request->getPost('clientSeed');
  23.  
  24.                 $this->db->begin();
  25.  
  26.                 $form = new DiceRollForm();
  27.  
  28.                 if (!$form->isValid($this->request->getPost())) {
  29.                     $this->db->rollback();
  30.                     $messages = array_merge($messages, formMessagesToArray($form));
  31.                     break;
  32.                 }
  33.  
  34.                 $user = $this->auth->getUser();
  35.  
  36.                 /** @var Balance $userBalance */
  37.                 $userBalance = $user->getBalanceByCurrency($currencyId);
  38.  
  39.                 if (mathComp($userBalance->amount, $amount, 8) === -1) {
  40.                     $this->db->rollback();
  41.                     $messages[] = 'На вашем балансе недостаточно средств.';
  42.                     break;
  43.                 }
  44.  
  45.                 $userBalance->amount = mathMinus("$userBalance->amount", $amount, 8);
  46.  
  47.                 $diceRoll = new DiceRoll();
  48.  
  49.                 $diceRoll->user_id      = $user->id;
  50.                 $diceRoll->currency_id  = $currencyId;
  51.                 $diceRoll->server_seed  = $user->server_seed;
  52.                 $diceRoll->client_seed  = $clientSeed;
  53.                 $diceRoll->amount       = $amount;
  54.                 $diceRoll->direction    = $direction;
  55.                 $diceRoll->prediction   = $number;
  56.  
  57.                 if (!$diceRoll->save()) {
  58.                     $this->db->rollback();
  59.                     $messages[] = 'Неизвестная ошибка. Пожалуйста попробуйте позже.';
  60.                     logger()->error(stringifyModelErrors($diceRoll));
  61.                     break;
  62.                 }
  63.  
  64.                 if ($isWin = $diceRoll->isWin()) {
  65.                     $userBalance->amount = mathPlus(
  66.                         $userBalance->amount,
  67.                         mathMul(
  68.                             $amount,
  69.                             $diceRoll->getRatio(),
  70.                             8
  71.                         )
  72.                     );
  73.                 }
  74.  
  75.                 $data = [];
  76.  
  77.                 $data['number'] = $diceRoll->getNumber();;
  78.                 $data['status'] = $isWin;
  79.                 $data['server_seed'] = $user->resetServerSeed();
  80.  
  81.                 if (!$userBalance->save()) {
  82.                     $this->db->rollback();
  83.                     $messages[] = 'Неизвестная ошибка. Пожалуйста попробуйте позже.';
  84.                     logger()->error(stringifyModelErrors($userBalance));
  85.                     break;
  86.                 }
  87.  
  88.                 if (!$user->save()) {
  89.                     $this->db->rollback();
  90.                     $messages[] = 'Неизвестная ошибка. Пожалуйста попробуйте позже.';
  91.                     logger()->error(stringifyModelErrors($userBalance));
  92.                     break;
  93.                 }
  94.  
  95.                 $this->db->commit();
  96.  
  97.                 $status = true;
  98.                 $data['balance'] = $userBalance->amount;
  99.             } while (false);
  100.         }
  101.  
  102.         $this->response->setJsonContent(
  103.             [
  104.                 'status' => $status ?? false,
  105.                 'messages' => $messages,
  106.                 'data' => $data ?? []
  107.             ]
  108.         )->send();
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement