Первые телодвидения по #16
This commit is contained in:
+14
-20
@@ -8,10 +8,8 @@
|
||||
namespace Battles;
|
||||
|
||||
use Config;
|
||||
use db;
|
||||
use Exceptions\GameException;
|
||||
use Krugozor\Database\Mysql\Exception;
|
||||
use SQLite3;
|
||||
use Battles\Database\DBPDO;
|
||||
use Throwable;
|
||||
|
||||
class Bank
|
||||
@@ -19,6 +17,7 @@ class Bank
|
||||
public $user_id;
|
||||
private $money;
|
||||
private $user;
|
||||
private $db;
|
||||
|
||||
const ERROR_NO_MONEY_IN_WALLET = "Ошибка! Нет денег в кошельке!";
|
||||
const ERROR_NO_BANK_ACCOUNT = "Ошибка! Счёта не существует!";
|
||||
@@ -34,18 +33,14 @@ 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();
|
||||
$this->db = new DBPDO();
|
||||
$bank_row = $this->db->fetch('SELECT user_id, money FROM bank WHERE user_id = ?', $row);
|
||||
$this->user = $this->db->fetch('SELECT money FROM users WHERE id = ?', $row);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,7 +69,6 @@ class Bank
|
||||
* @param int $senderId ID отправителя (ID игрока, если не указано иное).
|
||||
*
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function bankLogs(int $receiverId, int $amount, string $operationType, int $senderId = 0): void
|
||||
{
|
||||
@@ -100,11 +94,11 @@ class Bank
|
||||
* @param int $amount сумма.
|
||||
*
|
||||
* @return int
|
||||
* @throws Exception
|
||||
* @throws GameException
|
||||
*/
|
||||
public function sendMoney(int $receiver, int $amount): int
|
||||
{
|
||||
$receiverWallet = db::c()->query('SELECT money FROM bank WHERE user_id = ?i', $receiver)->fetch_object();
|
||||
$receiverWallet = $this->db->fetch('SELECT money FROM bank WHERE user_id = ?', $receiver);
|
||||
if ($amount <= 0) {
|
||||
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
||||
}
|
||||
@@ -133,14 +127,14 @@ class Bank
|
||||
* @param int $amount сумма.
|
||||
*
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws GameException
|
||||
*/
|
||||
public function depositMoney(int $amount): array
|
||||
{
|
||||
if ($amount <= 0) {
|
||||
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
||||
}
|
||||
$wallet = db::c()->query('SELECT money FROM users WHERE id = ?i', $this->user_id)->fetch_object();
|
||||
$wallet = $this->db->fetch('SELECT money FROM users WHERE id = ?', $this->user_id);
|
||||
if ($wallet->money < $amount) {
|
||||
throw new GameException(self::ERROR_NO_MONEY_IN_WALLET);
|
||||
}
|
||||
@@ -164,7 +158,7 @@ class Bank
|
||||
* @param int $amount сумма.
|
||||
*
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws GameException
|
||||
*/
|
||||
public function withdrawMoney(int $amount): array
|
||||
{
|
||||
@@ -197,12 +191,12 @@ class Bank
|
||||
* @param string $operationType Тип операции. По умолчанию пусто. Если ввести, система запишет событие в банковский лог.
|
||||
*
|
||||
* @return void
|
||||
* @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 = new DBPDO();
|
||||
$db->execute('UPDATE bank SET money = ? WHERE user_id = ?', [$amount, $user_id]);
|
||||
if ($operationType) {
|
||||
(new Bank($user_id))->bankLogs(0, $amount, $operationType);
|
||||
}
|
||||
@@ -219,12 +213,12 @@ class Bank
|
||||
* @param int $user_id ID пользователя.
|
||||
*
|
||||
* @return void
|
||||
* @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);
|
||||
$db = new DBPDO();
|
||||
$db->execute('UPDATE users SET money = ? WHERE id = ?', [$amount, $user_id]);
|
||||
} catch (Throwable $e) {
|
||||
echo "Не отработал запрос в БД в файле {$e->getFile()}({$e->getLine()})";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user