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