Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function diceRollAction()
- {
- $messages = [];
- if ($this->request->isPost()) {
- do {
- if (!$this->auth->hasUserSession()) {
- $messages[] = 'Необходимо выполнить вход в систему.';
- break;
- }
- if (!$this->csrf->check($this->request->getPost())) {
- $messages[] = 'Неверный CSRF-токен. Попробуйте обновить страницу.';
- break;
- }
- #Vars
- $amount = $this->request->getPost('amount', null, '0.00000000');
- $currencyId = (int)$this->request->getPost('currencyId', null, '0');
- $number = (int)$this->request->getPost('number', null, 50);
- $direction = $this->request->getPost('direction', null, 'under');
- $clientSeed = $this->request->getPost('clientSeed');
- $this->db->begin();
- $form = new DiceRollForm();
- if (!$form->isValid($this->request->getPost())) {
- $this->db->rollback();
- $messages = array_merge($messages, formMessagesToArray($form));
- break;
- }
- $user = $this->auth->getUser();
- /** @var Balance $userBalance */
- $userBalance = $user->getBalanceByCurrency($currencyId);
- if (mathComp($userBalance->amount, $amount, 8) === -1) {
- $this->db->rollback();
- $messages[] = 'На вашем балансе недостаточно средств.';
- break;
- }
- $userBalance->amount = mathMinus("$userBalance->amount", $amount, 8);
- $diceRoll = new DiceRoll();
- $diceRoll->user_id = $user->id;
- $diceRoll->currency_id = $currencyId;
- $diceRoll->server_seed = $user->server_seed;
- $diceRoll->client_seed = $clientSeed;
- $diceRoll->amount = $amount;
- $diceRoll->direction = $direction;
- $diceRoll->prediction = $number;
- if (!$diceRoll->save()) {
- $this->db->rollback();
- $messages[] = 'Неизвестная ошибка. Пожалуйста попробуйте позже.';
- logger()->error(stringifyModelErrors($diceRoll));
- break;
- }
- if ($isWin = $diceRoll->isWin()) {
- $userBalance->amount = mathPlus(
- $userBalance->amount,
- mathMul(
- $amount,
- $diceRoll->getRatio(),
- 8
- )
- );
- }
- $data = [];
- $data['number'] = $diceRoll->getNumber();;
- $data['status'] = $isWin;
- $data['server_seed'] = $user->resetServerSeed();
- if (!$userBalance->save()) {
- $this->db->rollback();
- $messages[] = 'Неизвестная ошибка. Пожалуйста попробуйте позже.';
- logger()->error(stringifyModelErrors($userBalance));
- break;
- }
- if (!$user->save()) {
- $this->db->rollback();
- $messages[] = 'Неизвестная ошибка. Пожалуйста попробуйте позже.';
- logger()->error(stringifyModelErrors($userBalance));
- break;
- }
- $this->db->commit();
- $status = true;
- $data['balance'] = $userBalance->amount;
- } while (false);
- }
- $this->response->setJsonContent(
- [
- 'status' => $status ?? false,
- 'messages' => $messages,
- 'data' => $data ?? []
- ]
- )->send();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement