Рефакторинг, очистка, работа над ошибками, связанными с базой, отказ от глобальной переменной $user во многих файлах.
Singleton в некоторых местах вместо решения #42. Новые шаги для решения #16 и #52. Closes #42. Closes #32. Closes #31.
This commit is contained in:
+11
-14
@@ -8,7 +8,7 @@
|
||||
namespace Battles;
|
||||
|
||||
use Exceptions\GameException;
|
||||
use Battles\Database\DBPDO;
|
||||
use Battles\Database\Db;
|
||||
use Throwable;
|
||||
|
||||
class Bank
|
||||
@@ -16,7 +16,6 @@ class Bank
|
||||
public int $user_id = 0;
|
||||
private int $money = 0;
|
||||
private $user;
|
||||
private static DBPDO $db;
|
||||
|
||||
const ERROR_NO_MONEY_IN_WALLET = "Ошибка! Нет денег в кошельке!";
|
||||
const ERROR_NO_BANK_ACCOUNT = "Ошибка! Счёта не существует!";
|
||||
@@ -33,9 +32,8 @@ class Bank
|
||||
|
||||
public function __construct(int $user_id)
|
||||
{
|
||||
self::$db = DBPDO::INIT();
|
||||
$bank_row = self::$db->fetch('SELECT user_id, money FROM bank WHERE user_id = ?', $user_id);
|
||||
$this->user = self::$db->fetch('SELECT money FROM users WHERE id = ?', $user_id);
|
||||
$bank_row = Db::getInstance()->fetch('SELECT user_id, money FROM bank WHERE user_id = ?', $user_id);
|
||||
$this->user = Db::getInstance()->fetch('SELECT money FROM users WHERE id = ?', $user_id);
|
||||
foreach ($this as $key => $value) {
|
||||
if (isset($bank_row[$key])) {
|
||||
$this->$key = $bank_row[$key];
|
||||
@@ -70,9 +68,9 @@ class Bank
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function bankLogs(int $receiverId, int $amount, string $operationType, int $senderId = 0): void
|
||||
private function bankLogs(int $receiverId, int $amount, string $operationType, int $senderId = null): void
|
||||
{
|
||||
if (!$senderId) {
|
||||
if (is_null($senderId)) {
|
||||
$senderId = $this->user_id;
|
||||
}
|
||||
$text = self::LOG[$operationType];
|
||||
@@ -98,7 +96,7 @@ class Bank
|
||||
*/
|
||||
public function sendMoney(int $receiver, int $amount): int
|
||||
{
|
||||
$receiverWallet = self::$db->fetch('SELECT money FROM bank WHERE user_id = ?', $receiver);
|
||||
$receiverWallet = Db::getInstance()->ofetch('SELECT money FROM bank WHERE user_id = ?', $receiver);
|
||||
if ($amount <= 0) {
|
||||
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
||||
}
|
||||
@@ -134,8 +132,8 @@ class Bank
|
||||
if ($amount <= 0) {
|
||||
throw new GameException(self::ERROR_WRONG_AMOUNT);
|
||||
}
|
||||
$wallet = self::$db->fetch('SELECT money FROM users WHERE id = ?', $this->user_id);
|
||||
if ($wallet->money < $amount) {
|
||||
$walletMoney = Db::getInstance()->fetchColumn('SELECT money FROM users WHERE id = ?', $this->user_id);
|
||||
if ($walletMoney < $amount) {
|
||||
throw new GameException(self::ERROR_NO_MONEY_IN_WALLET);
|
||||
}
|
||||
// Забираем деньги из кошелька получателя
|
||||
@@ -195,7 +193,7 @@ class Bank
|
||||
public static function setBankMoney(int $amount, int $user_id, string $operationType = ''): void
|
||||
{
|
||||
try {
|
||||
self::$db->execute('UPDATE bank SET money = ? WHERE user_id = ?', [$amount, $user_id]);
|
||||
Db::getInstance()->execute('UPDATE bank SET money = ? WHERE user_id = ?', [$amount, $user_id]);
|
||||
if ($operationType) {
|
||||
GameLogs::addBankLog(0, 0, $amount, $operationType, self::LOG[$operationType]);
|
||||
}
|
||||
@@ -215,9 +213,8 @@ class Bank
|
||||
*/
|
||||
public static function setWalletMoney(int $amount, int $user_id): void
|
||||
{
|
||||
$u = new User($user_id);
|
||||
$u->setMoney($amount);
|
||||
$u->saveMoney();
|
||||
User::getInstance($user_id)->setMoney($amount);
|
||||
User::getInstance($user_id)->saveMoney();
|
||||
}
|
||||
|
||||
public function getMoney(): int
|
||||
|
||||
Reference in New Issue
Block a user