2020-07-03 10:26:38 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Author: lopiu
|
|
|
|
|
* Date: 03.07.2020
|
|
|
|
|
* Time: 07:24
|
|
|
|
|
*/
|
2021-01-26 15:59:05 +00:00
|
|
|
|
|
2020-10-28 20:21:08 +00:00
|
|
|
|
namespace Battles;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
|
2021-01-26 15:50:57 +00:00
|
|
|
|
use Config;
|
|
|
|
|
use db;
|
|
|
|
|
use Exceptions\GameException;
|
|
|
|
|
use Krugozor\Database\Mysql\Exception;
|
|
|
|
|
use SQLite3;
|
|
|
|
|
use Throwable;
|
|
|
|
|
|
2020-07-03 10:26:38 +00:00
|
|
|
|
class Bank
|
|
|
|
|
{
|
|
|
|
|
public $user_id;
|
2020-10-28 12:32:14 +00:00
|
|
|
|
private $money;
|
2020-09-25 16:27:11 +00:00
|
|
|
|
private $user;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
|
|
|
|
|
const ERROR_NO_MONEY_IN_WALLET = "Ошибка! Нет денег в кошельке!";
|
|
|
|
|
const ERROR_NO_BANK_ACCOUNT = "Ошибка! Счёта не существует!";
|
|
|
|
|
const ERROR_NO_MONEY_IN_BANK_ACCOUNT = "Ошибка! Нет денег на счету!";
|
|
|
|
|
const ERROR_WRONG_AMOUNT = "Ошибка! Сумма должна быть положительной!";
|
2020-10-27 23:52:53 +00:00
|
|
|
|
const LOG = [
|
|
|
|
|
'sendMoney' => 'Банк: Перевод средств на другой счёт.',
|
|
|
|
|
'receiveMoney' => 'Банк: Получение средств.',
|
|
|
|
|
'depositMoney' => 'Пополнение счёта.',
|
|
|
|
|
'withdrawMoney' => 'Снятие денег со счёта.',
|
|
|
|
|
'clanRegister' => 'Оплата стоимости регистрации клана.',
|
|
|
|
|
];
|
2020-07-03 10:26:38 +00:00
|
|
|
|
|
|
|
|
|
public function __construct($row)
|
|
|
|
|
{
|
2021-01-26 15:50:57 +00:00
|
|
|
|
$bank_row = db::c()->query('SELECT user_id, money FROM bank WHERE user_id = ?i', $row)->fetch_assoc();
|
|
|
|
|
$this->user = db::c()->query('SELECT money FROM users WHERE id = ?i', $row)->fetch_object();
|
2020-07-03 10:26:38 +00:00
|
|
|
|
foreach ($this as $key => $value) {
|
|
|
|
|
if (isset($bank_row[$key])) {
|
|
|
|
|
$this->$key = $bank_row[$key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Если ВДРУГ у человека нет счёта в банке - создаём.
|
|
|
|
|
if (empty($this->user_id)) {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
db::c()->query('INSERT INTO bank (user_id) VALUES (?i)', $row);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
$this->user_id = $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* Комиссия: процент от переводимой суммы, но не менее 1 кр. Задаётся в config.php.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @param int $amount сумма.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2020-10-27 23:52:53 +00:00
|
|
|
|
private function bankCommission(int $amount): int
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2021-01-26 15:50:57 +00:00
|
|
|
|
$bankCommission = round($amount * Config::$bank_commission);
|
2020-10-27 23:41:42 +00:00
|
|
|
|
if ($bankCommission < 1) {
|
2020-07-03 10:26:38 +00:00
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
2020-10-27 23:41:42 +00:00
|
|
|
|
return (int)$bankCommission;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Пишем банковское событие в лог в БД
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2021-01-26 15:59:05 +00:00
|
|
|
|
* @param int $receiverId ID получателя.
|
|
|
|
|
* @param int $amount сумма.
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @param string $operationType тип банковской операции.
|
2021-01-26 15:59:05 +00:00
|
|
|
|
* @param int $senderId ID отправителя (ID игрока, если не указано иное).
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @return void
|
2021-01-26 15:50:57 +00:00
|
|
|
|
* @throws Exception
|
2020-07-03 10:26:38 +00:00
|
|
|
|
*/
|
2020-10-27 23:52:53 +00:00
|
|
|
|
private function bankLogs(int $receiverId, int $amount, string $operationType, int $senderId = 0): void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-10-27 22:48:16 +00:00
|
|
|
|
if (!$senderId) {
|
|
|
|
|
$senderId = $this->user_id;
|
|
|
|
|
}
|
2020-10-27 23:52:53 +00:00
|
|
|
|
$text = self::LOG[$operationType];
|
2020-10-27 23:41:42 +00:00
|
|
|
|
if ($operationType == "sendMoney") {
|
2020-10-27 23:52:53 +00:00
|
|
|
|
$text .= " Комиссия: " . $this->bankCommission($amount);
|
2020-10-27 23:41:42 +00:00
|
|
|
|
} elseif ($operationType == "depositMoney") {
|
2020-07-03 10:26:38 +00:00
|
|
|
|
$receiverId = $this->user_id;
|
2020-10-27 23:41:42 +00:00
|
|
|
|
} elseif ($operationType == "withdrawMoney") {
|
2020-07-03 10:26:38 +00:00
|
|
|
|
$receiverId = $this->user_id;
|
2020-10-27 23:52:53 +00:00
|
|
|
|
$text .= " Комиссия: " . $this->bankCommission($amount);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2021-01-27 11:30:11 +00:00
|
|
|
|
GameLogs::addBankLog($senderId,$receiverId,$amount,$operationType,$text);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* Перевод денег между банковскими счетами игроков с банковской комиссией.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @param int $receiver ID получателя.
|
2021-01-26 15:59:05 +00:00
|
|
|
|
* @param int $amount сумма.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-10-28 20:21:08 +00:00
|
|
|
|
* @return int
|
2021-01-26 15:50:57 +00:00
|
|
|
|
* @throws Exception
|
2020-07-03 10:26:38 +00:00
|
|
|
|
*/
|
2020-10-28 20:21:08 +00:00
|
|
|
|
public function sendMoney(int $receiver, int $amount): int
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2021-01-26 15:50:57 +00:00
|
|
|
|
$receiverWallet = db::c()->query('SELECT money FROM bank WHERE user_id = ?i', $receiver)->fetch_object();
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
2020-10-27 22:48:16 +00:00
|
|
|
|
if (!$receiverWallet) {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
throw new GameException(self::ERROR_NO_BANK_ACCOUNT);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-10-27 23:41:42 +00:00
|
|
|
|
$amountWithComission = $amount + $this->bankCommission($amount);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amountWithComission > $this->money) {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
throw new GameException(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
|
|
|
|
// Снимаем сумму с комиссией у отправителя
|
2020-10-27 22:48:16 +00:00
|
|
|
|
$this->money -= $amountWithComission;
|
2020-09-29 08:59:58 +00:00
|
|
|
|
self::setBankMoney($this->money, $this->user_id);
|
2020-10-27 22:48:16 +00:00
|
|
|
|
$this->bankLogs($receiver, $this->money, "sendMoney");
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Отдаём сумму на счёт получателю
|
2020-10-27 22:48:16 +00:00
|
|
|
|
$receiverWallet->money += $amount;
|
|
|
|
|
self::setBankMoney($receiverWallet->money, $receiver);
|
|
|
|
|
$this->bankLogs($receiver, $receiverWallet->money, "receiveMoney");
|
2020-10-28 20:21:08 +00:00
|
|
|
|
// Возвращаем изменившиеся значения
|
|
|
|
|
return $this->money;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Пополнение банковского счёта игрока
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @param int $amount сумма.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-10-28 20:21:08 +00:00
|
|
|
|
* @return array
|
2021-01-26 15:50:57 +00:00
|
|
|
|
* @throws Exception
|
2020-07-03 10:26:38 +00:00
|
|
|
|
*/
|
2020-10-28 20:21:08 +00:00
|
|
|
|
public function depositMoney(int $amount): array
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
2021-01-26 15:50:57 +00:00
|
|
|
|
$wallet = db::c()->query('SELECT money FROM users WHERE id = ?i', $this->user_id)->fetch_object();
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($wallet->money < $amount) {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
throw new GameException(self::ERROR_NO_MONEY_IN_WALLET);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Забираем деньги из кошелька получателя
|
|
|
|
|
$this->user->money -= $amount;
|
2020-09-29 08:59:58 +00:00
|
|
|
|
self::setWalletMoney($this->user->money, $this->user_id);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Отдаём сумму на счёт получателю
|
|
|
|
|
$this->money += $amount;
|
2020-09-29 08:59:58 +00:00
|
|
|
|
self::setBankMoney($this->money, $this->user_id);
|
2020-10-27 22:48:16 +00:00
|
|
|
|
$this->bankLogs(0, $this->money, "depositMoney");
|
2020-10-28 20:21:08 +00:00
|
|
|
|
// Возвращаем изменившиеся значения
|
|
|
|
|
return [
|
|
|
|
|
'walletMoney' => $this->user->money,
|
|
|
|
|
'bankMoney' => $this->money
|
|
|
|
|
];
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Снятие денег с банковского счёта игрока с банковской комиссией.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @param int $amount сумма.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2020-10-28 20:21:08 +00:00
|
|
|
|
* @return array
|
2021-01-26 15:50:57 +00:00
|
|
|
|
* @throws Exception
|
2020-07-03 10:26:38 +00:00
|
|
|
|
*/
|
2021-01-26 15:59:05 +00:00
|
|
|
|
public function withdrawMoney(int $amount): array
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-10-27 23:41:42 +00:00
|
|
|
|
$amountWithComission = $amount + $this->bankCommission($amount);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($this->money < $amountWithComission) {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
throw new GameException(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
|
|
|
|
// Снимаем сумму с комиссией у отправителя
|
|
|
|
|
$this->money -= $amountWithComission;
|
2020-09-29 08:59:58 +00:00
|
|
|
|
self::setBankMoney($this->money, $this->user_id);
|
2020-10-27 22:48:16 +00:00
|
|
|
|
$this->bankLogs(0, $this->money, "withdrawMoney");
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Отдаём сумму в кошелёк получателя
|
|
|
|
|
$this->user->money += $amount;
|
2020-09-29 08:59:58 +00:00
|
|
|
|
self::setWalletMoney($this->user->money, $this->user_id);
|
2020-10-28 20:21:08 +00:00
|
|
|
|
// Возвращаем изменившиеся значения
|
|
|
|
|
return [
|
|
|
|
|
'walletMoney' => $this->user->money,
|
|
|
|
|
'bankMoney' => $this->money
|
|
|
|
|
];
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 10:07:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Установить количество денег на банковском счету.
|
|
|
|
|
*
|
2021-01-26 15:59:05 +00:00
|
|
|
|
* @param int $amount сумма.
|
|
|
|
|
* @param int $user_id ID пользователя.
|
2020-10-27 22:48:16 +00:00
|
|
|
|
* @param string $operationType Тип операции. По умолчанию пусто. Если ввести, система запишет событие в банковский лог.
|
2020-09-29 10:07:08 +00:00
|
|
|
|
*
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @return void
|
2021-01-26 15:50:57 +00:00
|
|
|
|
* @throws Exception
|
2020-09-29 10:07:08 +00:00
|
|
|
|
*/
|
2020-10-27 22:48:16 +00:00
|
|
|
|
public static function setBankMoney(int $amount, int $user_id, string $operationType = ''): void
|
2020-09-25 16:27:11 +00:00
|
|
|
|
{
|
2020-10-28 20:21:08 +00:00
|
|
|
|
try {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
db::c()->query('UPDATE bank SET money = ?i WHERE user_id = ?i', $amount, $user_id);
|
2020-10-28 20:21:08 +00:00
|
|
|
|
if ($operationType) {
|
|
|
|
|
(new Bank($user_id))->bankLogs(0, $amount, $operationType);
|
|
|
|
|
}
|
2021-01-26 15:50:57 +00:00
|
|
|
|
} catch (Throwable $e) {
|
2020-10-28 20:21:08 +00:00
|
|
|
|
echo "Не отработал запрос в БД в файле {$e->getFile()}({$e->getLine()})";
|
2020-10-27 22:48:16 +00:00
|
|
|
|
}
|
2020-10-28 20:21:08 +00:00
|
|
|
|
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
2020-09-29 10:07:08 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Установить количество денег на руках.
|
|
|
|
|
*
|
2021-01-26 15:59:05 +00:00
|
|
|
|
* @param int $amount сумма.
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @param int $user_id ID пользователя.
|
2020-09-29 10:07:08 +00:00
|
|
|
|
*
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @return void
|
2021-01-26 15:50:57 +00:00
|
|
|
|
* @throws Exception
|
2020-09-29 10:07:08 +00:00
|
|
|
|
*/
|
2020-09-29 08:59:58 +00:00
|
|
|
|
public static function setWalletMoney(int $amount, int $user_id): void
|
2020-09-25 16:27:11 +00:00
|
|
|
|
{
|
2020-10-28 20:21:08 +00:00
|
|
|
|
try {
|
2021-01-26 15:50:57 +00:00
|
|
|
|
db::c()->query('UPDATE users SET money = ?i WHERE `id` = ?i', $amount, $user_id);
|
|
|
|
|
} catch (Throwable $e) {
|
2020-10-28 20:21:08 +00:00
|
|
|
|
echo "Не отработал запрос в БД в файле {$e->getFile()}({$e->getLine()})";
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-10-27 22:48:16 +00:00
|
|
|
|
|
2020-10-28 12:32:14 +00:00
|
|
|
|
public function getMoney()
|
2020-10-27 23:52:53 +00:00
|
|
|
|
{
|
2020-10-27 22:48:16 +00:00
|
|
|
|
return $this->money;
|
|
|
|
|
}
|
2020-10-28 12:32:14 +00:00
|
|
|
|
|
|
|
|
|
public function setMoney($amount)
|
|
|
|
|
{
|
|
|
|
|
$this->money = $amount;
|
|
|
|
|
}
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|