2020-07-03 10:26:38 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Author: lopiu
|
|
|
|
|
* Date: 03.07.2020
|
|
|
|
|
* Time: 07:24
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
$bank_row = db::c()->query('SELECT user_id, money FROM bank WHERE user_id = ?i', $row)->fetch_assoc();
|
2020-09-25 16:27:11 +00:00
|
|
|
|
$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)) {
|
|
|
|
|
db::c()->query('INSERT INTO bank (user_id) VALUES (?i)', $row);
|
|
|
|
|
$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
|
|
|
|
{
|
2020-10-27 23:41:42 +00:00
|
|
|
|
$bankCommission = round($amount * Config::$bank_commission);
|
|
|
|
|
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
|
|
|
|
*
|
2020-10-27 23:52:53 +00:00
|
|
|
|
* @param int $receiverId ID получателя.
|
|
|
|
|
* @param int $amount сумма.
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @param string $operationType тип банковской операции.
|
2020-10-27 23:52:53 +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
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 22:59:37 +00:00
|
|
|
|
db::c()->query('INSERT INTO `bank_logs` (sender_id, receiver_id, amount_result, type, text)
|
2020-10-27 22:48:16 +00:00
|
|
|
|
VALUES (?i, ?i, ?i, "?s", "?s")', $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 получателя.
|
2020-10-27 23:52:53 +00:00
|
|
|
|
* @param int $amount сумма.
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
|
|
|
|
* @return void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
2020-09-25 16:27:11 +00:00
|
|
|
|
public function sendMoney(int $receiver, int $amount): void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-10-27 22:48:16 +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) {
|
|
|
|
|
throw new Exception(self::ERROR_WRONG_AMOUNT);
|
|
|
|
|
}
|
2020-10-27 22:48:16 +00:00
|
|
|
|
if (!$receiverWallet) {
|
2020-09-25 16:27:11 +00:00
|
|
|
|
throw new Exception(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) {
|
|
|
|
|
throw new Exception(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
|
|
|
|
}
|
|
|
|
|
// Снимаем сумму с комиссией у отправителя
|
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-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
|
|
|
|
*
|
|
|
|
|
* @return void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
2020-09-25 16:27:11 +00:00
|
|
|
|
public function depositMoney(int $amount): void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
|
|
|
|
throw new Exception(self::ERROR_WRONG_AMOUNT);
|
|
|
|
|
}
|
|
|
|
|
$wallet = db::c()->query('SELECT money FROM users WHERE id = ?i', $this->user_id)->fetch_object();
|
|
|
|
|
if ($wallet->money < $amount) {
|
|
|
|
|
throw new Exception(self::ERROR_NO_MONEY_IN_WALLET);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Забираем деньги из кошелька получателя
|
|
|
|
|
//todo check it!
|
|
|
|
|
$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-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
|
|
|
|
*
|
|
|
|
|
* @return void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
2020-09-25 16:27:11 +00:00
|
|
|
|
public function withdrawMoney(int $amount): void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
|
|
|
|
throw new Exception(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) {
|
|
|
|
|
throw new Exception(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
|
|
|
|
}
|
|
|
|
|
// Снимаем сумму с комиссией у отправителя
|
|
|
|
|
$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
|
|
|
|
// Отдаём сумму в кошелёк получателя
|
|
|
|
|
//todo check it!
|
|
|
|
|
$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
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 10:07:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Установить количество денег на банковском счету.
|
|
|
|
|
*
|
2020-10-27 23:52:53 +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
|
2020-09-29 10:07:08 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
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-27 22:48:16 +00:00
|
|
|
|
db::c()->query('UPDATE bank SET money = ?i WHERE user_id = ?i', $amount, $user_id);
|
|
|
|
|
if ($operationType) {
|
2020-10-27 22:59:37 +00:00
|
|
|
|
(new Bank($user_id))->bankLogs(0, $amount, $operationType);
|
2020-10-27 22:48:16 +00:00
|
|
|
|
}
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
2020-09-29 10:07:08 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Установить количество денег на руках.
|
|
|
|
|
*
|
2020-10-27 23:52:53 +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
|
2020-09-29 10:07:08 +00:00
|
|
|
|
* @throws \Krugozor\Database\Mysql\Exception
|
|
|
|
|
*/
|
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
|
|
|
|
{
|
|
|
|
|
db::c()->query('UPDATE users SET money = ?i WHERE `id` = ?i', $amount, $user_id);
|
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
|
|
|
|
}
|