Запись банковских логов в sqlite.
This commit is contained in:
parent
73642cc643
commit
10f3bab59d
@ -6,6 +6,13 @@
|
||||
*/
|
||||
namespace Battles;
|
||||
|
||||
use Config;
|
||||
use db;
|
||||
use Exceptions\GameException;
|
||||
use Krugozor\Database\Mysql\Exception;
|
||||
use SQLite3;
|
||||
use Throwable;
|
||||
|
||||
class Bank
|
||||
{
|
||||
public $user_id;
|
||||
@ -26,8 +33,8 @@ class Bank
|
||||
|
||||
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();
|
||||
$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];
|
||||
@ -35,7 +42,7 @@ class Bank
|
||||
}
|
||||
// Если ВДРУГ у человека нет счёта в банке - создаём.
|
||||
if (empty($this->user_id)) {
|
||||
\db::c()->query('INSERT INTO bank (user_id) VALUES (?i)', $row);
|
||||
db::c()->query('INSERT INTO bank (user_id) VALUES (?i)', $row);
|
||||
$this->user_id = $row;
|
||||
}
|
||||
}
|
||||
@ -49,7 +56,7 @@ class Bank
|
||||
*/
|
||||
private function bankCommission(int $amount): int
|
||||
{
|
||||
$bankCommission = round($amount * \Config::$bank_commission);
|
||||
$bankCommission = round($amount * Config::$bank_commission);
|
||||
if ($bankCommission < 1) {
|
||||
return 1;
|
||||
} else {
|
||||
@ -66,7 +73,7 @@ class Bank
|
||||
* @param int $senderId ID отправителя (ID игрока, если не указано иное).
|
||||
*
|
||||
* @return void
|
||||
* @throws \Krugozor\Database\Mysql\Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
private function bankLogs(int $receiverId, int $amount, string $operationType, int $senderId = 0): void
|
||||
{
|
||||
@ -83,8 +90,15 @@ class Bank
|
||||
$text .= " Комиссия: " . $this->bankCommission($amount);
|
||||
}
|
||||
|
||||
\db::c()->query('INSERT INTO `bank_logs` (sender_id, receiver_id, amount_result, type, text)
|
||||
VALUES (?i, ?i, ?i, "?s", "?s")', $senderId, $receiverId, $amount, $operationType, $text);
|
||||
$db = new SQLite3('databases/logs.sqlite');
|
||||
$logLine = $db->prepare("INSERT INTO bank_logs (sender_id, receiver_id, amount, type, text) VALUES (?, ?, ?, ?, ?)");
|
||||
$logLine->bindParam(1, $senderId, SQLITE3_INTEGER);
|
||||
$logLine->bindParam(2, $receiverId, SQLITE3_INTEGER);
|
||||
$logLine->bindParam(3, $amount, SQLITE3_INTEGER);
|
||||
$logLine->bindParam(4, $operationType, SQLITE3_TEXT);
|
||||
$logLine->bindParam(5, $text, SQLITE3_TEXT);
|
||||
$logLine->execute();
|
||||
$logLine->close();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,20 +108,20 @@ class Bank
|
||||
* @param int $amount сумма.
|
||||
*
|
||||
* @return int
|
||||
* @throws \Krugozor\Database\Mysql\Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendMoney(int $receiver, int $amount): int
|
||||
{
|
||||
$receiverWallet = \db::c()->query('SELECT money FROM bank WHERE user_id = ?i', $receiver)->fetch_object();
|
||||
$receiverWallet = db::c()->query('SELECT money FROM bank WHERE user_id = ?i', $receiver)->fetch_object();
|
||||
if ($amount <= 0) {
|
||||
throw new \Exceptions\GameException(self::ERROR_WRONG_AMOUNT);
|
||||
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
||||
}
|
||||
if (!$receiverWallet) {
|
||||
throw new \Exceptions\GameException(self::ERROR_NO_BANK_ACCOUNT);
|
||||
throw new GameException(self::ERROR_NO_BANK_ACCOUNT);
|
||||
}
|
||||
$amountWithComission = $amount + $this->bankCommission($amount);
|
||||
if ($amountWithComission > $this->money) {
|
||||
throw new \Exceptions\GameException(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
||||
throw new GameException(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
||||
}
|
||||
// Снимаем сумму с комиссией у отправителя
|
||||
$this->money -= $amountWithComission;
|
||||
@ -127,16 +141,16 @@ class Bank
|
||||
* @param int $amount сумма.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Krugozor\Database\Mysql\Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function depositMoney(int $amount): array
|
||||
{
|
||||
if ($amount <= 0) {
|
||||
throw new \Exceptions\GameException(self::ERROR_WRONG_AMOUNT);
|
||||
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
||||
}
|
||||
$wallet = \db::c()->query('SELECT money FROM users WHERE id = ?i', $this->user_id)->fetch_object();
|
||||
$wallet = db::c()->query('SELECT money FROM users WHERE id = ?i', $this->user_id)->fetch_object();
|
||||
if ($wallet->money < $amount) {
|
||||
throw new \Exceptions\GameException(self::ERROR_NO_MONEY_IN_WALLET);
|
||||
throw new GameException(self::ERROR_NO_MONEY_IN_WALLET);
|
||||
}
|
||||
// Забираем деньги из кошелька получателя
|
||||
$this->user->money -= $amount;
|
||||
@ -158,16 +172,16 @@ class Bank
|
||||
* @param int $amount сумма.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Krugozor\Database\Mysql\Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public function withdrawMoney(int $amount):array
|
||||
{
|
||||
if ($amount <= 0) {
|
||||
throw new \Exceptions\GameException(self::ERROR_WRONG_AMOUNT);
|
||||
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
||||
}
|
||||
$amountWithComission = $amount + $this->bankCommission($amount);
|
||||
if ($this->money < $amountWithComission) {
|
||||
throw new \Exceptions\GameException(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
||||
throw new GameException(self::ERROR_NO_MONEY_IN_BANK_ACCOUNT);
|
||||
}
|
||||
// Снимаем сумму с комиссией у отправителя
|
||||
$this->money -= $amountWithComission;
|
||||
@ -191,16 +205,16 @@ class Bank
|
||||
* @param string $operationType Тип операции. По умолчанию пусто. Если ввести, система запишет событие в банковский лог.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Krugozor\Database\Mysql\Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function setBankMoney(int $amount, int $user_id, string $operationType = ''): void
|
||||
{
|
||||
try {
|
||||
\db::c()->query('UPDATE bank SET money = ?i WHERE user_id = ?i', $amount, $user_id);
|
||||
db::c()->query('UPDATE bank SET money = ?i WHERE user_id = ?i', $amount, $user_id);
|
||||
if ($operationType) {
|
||||
(new Bank($user_id))->bankLogs(0, $amount, $operationType);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
} catch (Throwable $e) {
|
||||
echo "Не отработал запрос в БД в файле {$e->getFile()}({$e->getLine()})";
|
||||
}
|
||||
|
||||
@ -213,13 +227,13 @@ class Bank
|
||||
* @param int $user_id ID пользователя.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Krugozor\Database\Mysql\Exception
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function setWalletMoney(int $amount, int $user_id): void
|
||||
{
|
||||
try {
|
||||
\db::c()->query('UPDATE users SET money = ?i WHERE `id` = ?i', $amount, $user_id);
|
||||
} catch (\Throwable $e) {
|
||||
db::c()->query('UPDATE users SET money = ?i WHERE `id` = ?i', $amount, $user_id);
|
||||
} catch (Throwable $e) {
|
||||
echo "Не отработал запрос в БД в файле {$e->getFile()}({$e->getLine()})";
|
||||
}
|
||||
|
||||
|
BIN
databases/logs.sqlite
Normal file
BIN
databases/logs.sqlite
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user