176 lines
6.5 KiB
PHP
176 lines
6.5 KiB
PHP
<?php
|
||
/**
|
||
* Author: lopiu
|
||
* Date: 03.07.2020
|
||
* Time: 07:24
|
||
*/
|
||
|
||
class Bank
|
||
{
|
||
public $user_id;
|
||
public $money;
|
||
private $user;
|
||
|
||
public const BANK_COMISSION = 0.05; //5%
|
||
|
||
const ERROR_NO_MONEY_IN_WALLET = "Ошибка! Нет денег в кошельке!";
|
||
const ERROR_NO_BANK_ACCOUNT = "Ошибка! Счёта не существует!";
|
||
const ERROR_NO_MONEY_IN_BANK_ACCOUNT = "Ошибка! Нет денег на счету!";
|
||
const ERROR_WRONG_AMOUNT = "Ошибка! Сумма должна быть положительной!";
|
||
const LOG_SEND = "Банк: Перевод средств на другой счёт.";
|
||
const LOG_RECEIVE = "Банк: Получение средств.";
|
||
const LOG_DEPOSIT = "Пополнение счёта.";
|
||
const LOG_WITHDRAW = "Снятие денег со счёта.";
|
||
|
||
public function __construct($row)
|
||
{
|
||
$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();
|
||
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;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Комиссия: self::BANK_COMISSION от переводимой суммы, но не менее 1 кр.
|
||
*
|
||
* @param $amount
|
||
*
|
||
* @return int
|
||
*/
|
||
private function bankComission($amount)
|
||
{
|
||
$bankComission = round($amount * self::BANK_COMISSION);
|
||
if ($bankComission < 1) {
|
||
return 1;
|
||
} else {
|
||
return (int)$bankComission;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Пишем банковское событие в лог в БД
|
||
*
|
||
* @param $receiverId - user_id получателя
|
||
* @param $amount
|
||
* @param $operationType
|
||
*
|
||
* @throws \Krugozor\Database\Mysql\Exception
|
||
*/
|
||
private function bankLogs($receiverId, $amount, $operationType)
|
||
{
|
||
$text = '';
|
||
if ($operationType === "sendMoney") {
|
||
$text = self::LOG_SEND . " Комиссия: " . $this->bankComission($amount);
|
||
} elseif ($operationType === "depositMoney") {
|
||
$receiverId = $this->user_id;
|
||
$text = self::LOG_DEPOSIT;
|
||
} elseif ($operationType === "withdrawMoney") {
|
||
$receiverId = $this->user_id;
|
||
$text = self::LOG_WITHDRAW . " Комиссия: " . $this->bankComission($amount);
|
||
}
|
||
|
||
db::c()->query('INSERT INTO `bank_logs` (sender_id, receiver_id, amount, type, text)
|
||
VALUES (?i, ?i, ?i, "?s", "?s")', $this->user_id, $receiverId, $amount, $operationType, $text);
|
||
|
||
}
|
||
|
||
/**
|
||
* Перевод денег между бансковскими счетами игроков с банковской комиссией.
|
||
*
|
||
* @param int $receiver
|
||
* @param int $amount
|
||
*
|
||
* @return void
|
||
* @throws \Krugozor\Database\Mysql\Exception
|
||
*/
|
||
public function sendMoney(int $receiver, int $amount): void
|
||
{
|
||
if ($amount <= 0) {
|
||
throw new Exception(self::ERROR_WRONG_AMOUNT);
|
||
}
|
||
if (!db::c()->query('SELECT 1 FROM bank WHERE user_id = ?i', $receiver)) {
|
||
throw new Exception(self::ERROR_NO_BANK_ACCOUNT);
|
||
}
|
||
$amountWithComission = $amount + $this->bankComission($amount);
|
||
if ($amountWithComission > $this->money) {
|
||
throw new Exception(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
||
}
|
||
$this->money -= $amountWithComission;
|
||
// Снимаем сумму с комиссией у отправителя
|
||
$this->setBankMoney($this->money, $this->user_id);
|
||
// Отдаём сумму на счёт получателю
|
||
$this->setBankMoney($amount, $receiver);
|
||
$this->bankLogs($receiver, $amount, "sendMoney");
|
||
}
|
||
|
||
/**
|
||
* Пополнение банковского счёта игрока
|
||
*
|
||
* @param int $amount - сумма
|
||
*
|
||
* @return void
|
||
* @throws \Krugozor\Database\Mysql\Exception
|
||
*/
|
||
public function depositMoney(int $amount): void
|
||
{
|
||
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);
|
||
}
|
||
// Забираем деньги из кошелька получателя
|
||
//todo check it!
|
||
$this->user->money -= $amount;
|
||
$this->setWalletMoney($this->user->money, $this->user_id);
|
||
// Отдаём сумму на счёт получателю
|
||
$this->money += $amount;
|
||
$this->setBankMoney($this->money, $this->user_id);
|
||
$this->bankLogs(0, $amount, "depositMoney");
|
||
}
|
||
|
||
/**
|
||
* Снятие денег с банковского счёта игрока с банковской комиссией.
|
||
*
|
||
* @param int $amount - сумма
|
||
*
|
||
* @return void
|
||
* @throws \Krugozor\Database\Mysql\Exception
|
||
*/
|
||
public function withdrawMoney(int $amount): void
|
||
{
|
||
if ($amount <= 0) {
|
||
throw new Exception(self::ERROR_WRONG_AMOUNT);
|
||
}
|
||
$amountWithComission = $amount + $this->bankComission($amount);
|
||
if ($this->money < $amountWithComission) {
|
||
throw new Exception(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
||
}
|
||
// Снимаем сумму с комиссией у отправителя
|
||
$this->money -= $amountWithComission;
|
||
$this->setBankMoney($this->money, $this->user_id);
|
||
// Отдаём сумму в кошелёк получателя
|
||
//todo check it!
|
||
$this->user->money += $amount;
|
||
$this->setWalletMoney($this->user->money, $this->user_id);
|
||
$this->bankLogs(0, $amount, "withdrawMoney");
|
||
}
|
||
|
||
private function setBankMoney(int $amount, int $user_id): void
|
||
{
|
||
db::c()->query('UPDATE bank SET money = ?i WHERE `id` = ?i', $amount, $user_id);
|
||
}
|
||
private function setWalletMoney(int $amount, int $user_id): void
|
||
{
|
||
db::c()->query('UPDATE users SET money = ?i WHERE `id` = ?i', $amount, $user_id);
|
||
}
|
||
} |