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
|
|
|
|
|
2022-01-26 23:15:33 +00:00
|
|
|
|
use Battles\Database\Db;
|
2021-01-26 15:50:57 +00:00
|
|
|
|
|
2020-07-03 10:26:38 +00:00
|
|
|
|
class Bank
|
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
|
private int $userId = 0;
|
2021-08-20 17:40:06 +00:00
|
|
|
|
private int $money = 0;
|
2022-08-09 19:57:43 +00:00
|
|
|
|
private string $error = '';
|
|
|
|
|
private string $status = '';
|
|
|
|
|
private int $comission = 0;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
|
private const ERROR_NO_MONEY_IN_WALLET = "Ошибка! Нет денег в кошельке!";
|
|
|
|
|
private const ERROR_NO_BANK_ACCOUNT = "Ошибка! Счёта не существует!";
|
|
|
|
|
private const ERROR_NO_MONEY_IN_BANK_ACCOUNT = "Ошибка! Нет денег на счету!";
|
|
|
|
|
private const ERROR_WRONG_AMOUNT = "Ошибка! Сумма должна быть положительной!";
|
|
|
|
|
private const ERROR_WRONG_ID = 'Неверный ID!';
|
|
|
|
|
private const LOG = [
|
2020-10-27 23:52:53 +00:00
|
|
|
|
'sendMoney' => 'Банк: Перевод средств на другой счёт.',
|
|
|
|
|
'receiveMoney' => 'Банк: Получение средств.',
|
|
|
|
|
'depositMoney' => 'Пополнение счёта.',
|
|
|
|
|
'withdrawMoney' => 'Снятие денег со счёта.',
|
|
|
|
|
'clanRegister' => 'Оплата стоимости регистрации клана.',
|
2021-08-25 01:44:36 +00:00
|
|
|
|
'sellShop' => 'Продажа товара в магазине.'
|
2020-10-27 23:52:53 +00:00
|
|
|
|
];
|
2020-07-03 10:26:38 +00:00
|
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
|
public function __construct(int $userId = null)
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
|
if (empty($userId)) {
|
|
|
|
|
$userId = User::getInstance()->getId();
|
2022-08-09 19:57:43 +00:00
|
|
|
|
}
|
2022-12-16 23:20:43 +00:00
|
|
|
|
$bankRow = Db::getInstance()->fetch('SELECT user_id, money FROM bank WHERE user_id = ?', $userId);
|
2020-07-03 10:26:38 +00:00
|
|
|
|
foreach ($this as $key => $value) {
|
2022-12-16 23:20:43 +00:00
|
|
|
|
if (isset($bankRow[$key])) {
|
|
|
|
|
$this->$key = $bankRow[$key];
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
|
*/
|
2022-08-09 19:57:43 +00:00
|
|
|
|
private function commission(int $amount): int
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2021-03-10 21:03:59 +00:00
|
|
|
|
$bankCommission = round($amount * GameConfigs::BANK_COMISSION);
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->comission = max(1, (int)$bankCommission);
|
|
|
|
|
return $this->comission;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Пишем банковское событие в лог в БД
|
2020-09-25 16:27:11 +00:00
|
|
|
|
*
|
2022-12-16 23:20:43 +00:00
|
|
|
|
* @param int $receiverId ID получателя.
|
|
|
|
|
* @param int $amount сумма.
|
2020-10-27 23:41:42 +00:00
|
|
|
|
* @param string $operationType тип банковской операции.
|
2022-12-16 23:20:43 +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
|
|
|
|
*/
|
2022-08-09 19:57:43 +00:00
|
|
|
|
private function addLog(int $receiverId, int $amount, string $operationType, ?int $senderId = null): void
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2022-01-26 23:15:33 +00:00
|
|
|
|
if (is_null($senderId)) {
|
2022-12-16 23:20:43 +00:00
|
|
|
|
$senderId = $this->userId;
|
2020-10-27 22:48:16 +00:00
|
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
|
if ($operationType === 'depositMoney' || $operationType === 'withdrawMoney') {
|
2022-12-16 23:20:43 +00:00
|
|
|
|
$receiverId = $this->userId;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$commText = $this->comission ? ' Комиссия: ' . $this->comission : '';
|
|
|
|
|
$text = self::LOG[$operationType] . $commText;
|
|
|
|
|
$this->status = $text;
|
|
|
|
|
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
|
|
|
|
*
|
2022-08-09 19:57:43 +00:00
|
|
|
|
* @param mixed $receiver ID получателя.
|
2022-12-16 23:20:43 +00:00
|
|
|
|
* @param mixed $amount Cумма.
|
2020-07-03 10:26:38 +00:00
|
|
|
|
*/
|
2022-08-09 19:57:43 +00:00
|
|
|
|
public function sendMoney($receiver, $amount)
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
|
if (!is_numeric($receiver)) {
|
|
|
|
|
$this->error = self::ERROR_WRONG_ID;
|
|
|
|
|
return;
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$rec = new self($receiver);
|
|
|
|
|
if (!is_numeric($amount) || $amount <= 0) {
|
|
|
|
|
$this->error = self::ERROR_WRONG_AMOUNT;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2022-12-16 23:20:43 +00:00
|
|
|
|
if (!$rec->userId) {
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->error = self::ERROR_NO_BANK_ACCOUNT;
|
|
|
|
|
}
|
|
|
|
|
$amountWithComission = $amount + $this->commission($amount);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amountWithComission > $this->money) {
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->error = self::ERROR_NO_MONEY_IN_BANK_ACCOUNT;
|
|
|
|
|
}
|
|
|
|
|
if ($this->error) {
|
|
|
|
|
return;
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
|
|
|
|
// Снимаем сумму с комиссией у отправителя
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->modify(-$amountWithComission);
|
2022-12-16 23:20:43 +00:00
|
|
|
|
$this->addLog($rec->userId, $this->money, 'sendMoney', $this->userId);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Отдаём сумму на счёт получателю
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$rec->modify($amount);
|
2022-12-16 23:20:43 +00:00
|
|
|
|
$rec->addLog($rec->userId, $rec->money, 'receiveMoney', $this->userId);
|
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-07-03 10:26:38 +00:00
|
|
|
|
*/
|
2022-08-09 19:57:43 +00:00
|
|
|
|
public function depositMoney(int $amount)
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->error = self::ERROR_WRONG_AMOUNT;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Забираем деньги из кошелька получателя
|
2022-12-16 23:20:43 +00:00
|
|
|
|
if (!User::getInstance($this->userId)->money()->spend($amount)) {
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->error = self::ERROR_NO_MONEY_IN_WALLET;
|
|
|
|
|
}
|
|
|
|
|
if ($this->error) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Отдаём сумму на счёт получателю
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->modify($amount);
|
|
|
|
|
$this->addLog(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
|
|
|
|
*
|
2020-07-03 10:26:38 +00:00
|
|
|
|
*/
|
2022-08-09 19:57:43 +00:00
|
|
|
|
public function withdrawMoney(int $amount)
|
2020-07-03 10:26:38 +00:00
|
|
|
|
{
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($amount <= 0) {
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->error = self::ERROR_WRONG_AMOUNT;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$amountWithComission = $amount + $this->commission($amount);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
if ($this->money < $amountWithComission) {
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->error = self::ERROR_NO_MONEY_IN_BANK_ACCOUNT;
|
|
|
|
|
}
|
|
|
|
|
if ($this->error) {
|
|
|
|
|
return;
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
|
|
|
|
// Снимаем сумму с комиссией у отправителя
|
2022-08-09 19:57:43 +00:00
|
|
|
|
$this->modify(-$amountWithComission);
|
|
|
|
|
$this->addLog(0, $this->money, 'withdrawMoney');
|
2020-09-25 16:27:11 +00:00
|
|
|
|
// Отдаём сумму в кошелёк получателя
|
2022-12-16 23:20:43 +00:00
|
|
|
|
User::getInstance($this->userId)->money()->earn($amount);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
|
private function save()
|
2020-09-25 16:27:11 +00:00
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
|
Db::getInstance()->execute('UPDATE bank SET money = ? WHERE user_id = ?', [$this->money, $this->userId]);
|
2020-09-25 16:27:11 +00:00
|
|
|
|
}
|
2020-09-29 10:07:08 +00:00
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
|
public function getMoney(): int
|
2020-09-25 16:27:11 +00:00
|
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
|
return $this->money;
|
2020-07-03 10:26:38 +00:00
|
|
|
|
}
|
2020-10-27 22:48:16 +00:00
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
|
public function modify(int $amount, string $logType = '')
|
2020-10-27 23:52:53 +00:00
|
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
|
if ($amount > 0) {
|
|
|
|
|
// add_money
|
|
|
|
|
$this->money += $amount;
|
|
|
|
|
$this->save();
|
|
|
|
|
} elseif ($amount < 0) {
|
|
|
|
|
// remove_money
|
|
|
|
|
if ($this->money < $amount) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->money -= $amount;
|
|
|
|
|
$this->save();
|
|
|
|
|
}
|
|
|
|
|
if ($logType && $amount !== 0) {
|
|
|
|
|
$this->addLog(0, $this->money, $logType);
|
|
|
|
|
}
|
2020-10-27 22:48:16 +00:00
|
|
|
|
}
|
2020-10-28 12:32:14 +00:00
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
|
public function getStatus(): string
|
2020-10-28 12:32:14 +00:00
|
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
|
if (!$this->error) {
|
|
|
|
|
return $this->status;
|
|
|
|
|
}
|
|
|
|
|
return $this->error;
|
2020-10-28 12:32:14 +00:00
|
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
|
}
|