Рефакторинг, очистка, работа над ошибками, связанными с базой, отказ от глобальной переменной $user во многих файлах.
Singleton в некоторых местах вместо решения #42. Новые шаги для решения #16 и #52. Closes #42. Closes #32. Closes #31.
This commit is contained in:
+23
-19
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace Battles;
|
||||
|
||||
use Battles\Database\DBPDO;
|
||||
use Battles\Database\Db;
|
||||
|
||||
class User
|
||||
{
|
||||
private static ?self $_instance = null;
|
||||
|
||||
protected int $id = 0;
|
||||
protected string $login = '';
|
||||
protected ?string $pass = null;
|
||||
@@ -29,26 +31,20 @@ class User
|
||||
protected int $battle = 0;
|
||||
protected int $in_tower = 0; // Скорее башню похороним чем запустим...
|
||||
protected int $zayavka = 0;
|
||||
protected static DBPDO $db;
|
||||
|
||||
public const INFO_CHAR_LIMIT = 1500;
|
||||
/**
|
||||
* @var User Переменная инициализируемая при запуске, хранящая объект текущего пользователя.
|
||||
*/
|
||||
public static User $current;
|
||||
|
||||
/**
|
||||
* @param int|string $user
|
||||
*/
|
||||
public function __construct($user)
|
||||
protected function __construct($user = null)
|
||||
{
|
||||
self::$db = DBPDO::INIT();
|
||||
if (is_null($user)) {
|
||||
$user = $_SESSION['uid'];
|
||||
}
|
||||
$query = 'select * from users where login = ?';
|
||||
if (is_numeric($user)) {
|
||||
$query = 'select * from users where id = ?';
|
||||
$user = (int)$user;
|
||||
}
|
||||
$user_query = self::$db->fetch($query, $user);
|
||||
$user_query = Db::getInstance()->fetch($query, $user);
|
||||
foreach ($this as $key => $value) {
|
||||
if (isset($user_query[$key])) {
|
||||
$this->$key = $user_query[$key];
|
||||
@@ -56,6 +52,14 @@ class User
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance($user = null): self
|
||||
{
|
||||
if (is_null(self::$_instance)) {
|
||||
self::$_instance = new self($user);
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @param int $type
|
||||
@@ -66,13 +70,13 @@ class User
|
||||
public static function addUserEffect(int $userId, int $type, string $name, int $time, string $json_modifiers_list = null)
|
||||
{
|
||||
$mods = json_decode($json_modifiers_list);
|
||||
self::$db->execute('INSERT INTO users_effects (owner_id, type, name, remaining_time, mod_strength, mod_dexterity, mod_intuition, mod_endurance, mod_intelligence, mod_wisdom) VALUES (?,?,?,?,?,?,?,?,?,?)', [$userId, $type, $name, $time, $mods->str ?? null, $mods->dex ?? null, $mods->int ?? null, $mods->end ?? null, $mods->intel ?? null, $mods->wis ?? null]);
|
||||
Db::getInstance()->execute('INSERT INTO users_effects (owner_id, type, name, remaining_time, mod_strength, mod_dexterity, mod_intuition, mod_endurance, mod_intelligence, mod_wisdom) VALUES (?,?,?,?,?,?,?,?,?,?)', [$userId, $type, $name, $time, $mods->str ?? null, $mods->dex ?? null, $mods->int ?? null, $mods->end ?? null, $mods->intel ?? null, $mods->wis ?? null]);
|
||||
}
|
||||
|
||||
public static function removeUserEffect(int $userId, int $type): bool
|
||||
{
|
||||
if (self::$db->fetch('SELECT 1 FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type])) {
|
||||
self::$db->execute('DELETE FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type]);
|
||||
if (Db::getInstance()->fetchColumn('SELECT 1 FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type])) {
|
||||
Db::getInstance()->execute('DELETE FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -157,12 +161,12 @@ class User
|
||||
|
||||
public function setMoney(int $money)
|
||||
{
|
||||
$this->money = $money < 0 ? 0 : $money;
|
||||
$this->money = max($money, 0);
|
||||
}
|
||||
|
||||
public function saveMoney()
|
||||
{
|
||||
self::$db->execute('update users set money = ? where id = ?', [$this->money, $this->id]);
|
||||
Db::getInstance()->execute('update users set money = ? where id = ?', [$this->money, $this->id]);
|
||||
}
|
||||
|
||||
public function getAdmin(): int
|
||||
@@ -229,7 +233,7 @@ class User
|
||||
|
||||
public function setOnline()
|
||||
{
|
||||
self::$db->execute('update online set real_time = ? where user_id = ?', [time(), $this->getId()]);
|
||||
Db::getInstance()->execute('update online set real_time = ? where user_id = ?', [time(), $this->getId()]);
|
||||
}
|
||||
|
||||
public function setInjury(int $type): bool
|
||||
@@ -285,6 +289,6 @@ class User
|
||||
$this->admin,
|
||||
$this->id //where
|
||||
];
|
||||
DBPDO::$db->execute($query, $vals);
|
||||
Db::getInstance()->execute($query, $vals);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user