battles/classes/Battles/User.php
Ivor Barhansky 0398425205 Code smell.
2022-12-17 03:03:56 +02:00

228 lines
5.4 KiB
PHP

<?php
namespace Battles;
use Battles\Database\Db;
class User
{
use Users;
private static ?self $instance = null;
private ?UserProfile $profile = null;
private ?UserEffect $effect = null;
private ?UserStats $stats = null;
private ?UserInfo $userInfo = null;
private ?UserMoney $userMoney = null;
// Пока несуществующие, для совместимости.
protected int $experience = 0;
protected int $battle = 0;
protected int $zayavka = 0;
private object $profileData;
protected function __construct($user = null)
{
if (is_null($user)) {
$user = $_SESSION['uid'];
}
// Отсекаем 2.0000~
$col = ctype_digit(strval($user)) ? 'id' : 'login';
$query = "select * from users where $col = ?";
$userQuery = Db::getInstance()->fetch($query, $user);
foreach ($this as $key => $value) {
if (isset($userQuery[$key])) {
$this->$key = $userQuery[$key];
}
}
$this->profileData = (object)[
$this->id,
$this->pass,
$this->email,
$this->realname,
$this->borndate,
$this->info,
$this->ip,
];
}
public static function getInstance($user = null): self
{
if (is_null(self::$instance)) {
self::$instance = new self($user);
}
return self::$instance;
}
public function profile(): UserProfile
{
if (is_null($this->profile)) {
$this->profile = new UserProfile(
$this->id,
$this->pass,
$this->email,
$this->realname,
$this->borndate,
$this->info,
$this->ip,
);
}
return $this->profile;
}
public function effect(): UserEffect
{
if (is_null($this->effect)) {
$this->effect = new UserEffect();
}
return $this->effect;
}
public function stats(): UserStats
{
if (is_null($this->stats)) {
$this->stats = new UserStats($this->id);
}
return $this->stats;
}
public function userInfo(): UserInfo
{
if (is_null($this->userInfo)) {
$this->userInfo = new UserInfo($this->id);
}
return $this->userInfo;
}
public function money(): UserMoney
{
if (is_null($this->userMoney)) {
$this->userMoney = new UserMoney($this->id, $this->money);
}
return $this->userMoney;
}
public function getId(): int
{
return $this->id;
}
public function getLogin(): string
{
return $this->login;
}
public function getLevel(): int
{
return $this->level;
}
public function getAlign(): int
{
return $this->align;
}
public function getClan(): ?string
{
return $this->clan;
}
/**
* @param string|null $shortName Короткое название клана. Передать null для очистки.
*/
public function setClan(?string $shortName)
{
$this->clan = is_null($shortName) ? null : $shortName;
$this->saveUser();
}
public function getAdmin(): int
{
return $this->admin;
}
public function getRoom(): int
{
return $this->room;
}
/**
* @param mixed $room
*/
public function setRoom($room)
{
$this->room = $room;
}
public function getBlock(): int
{
return $this->block;
}
public function getShadow(): string
{
return $this->shadow;
}
/**
* @param mixed $shadow
*/
public function setShadow($shadow): void
{
$shadows = [
'm01', 'm02', 'm03', 'm04', 'm05', 'm06', 'm07', 'm08', 'm09', 'm10',
'f01', 'f02', 'f03', 'f04', 'f05', 'f06', 'f07', 'f08', 'f09', 'f10',
];
if (in_array($shadow, $shadows) && $this->getShadow() === '0.png') {
$this->shadow = $shadow . '.png';
}
}
public function getExperience(): int
{
return $this->experience;
}
/**
* @param int $experience
*/
public function addExperience(int $experience): void
{
$this->experience += $experience;
Db::getInstance()->execute('update users set experience = ? where id = ?', [$experience, $this->id]);
}
public function getBattle(): int
{
return $this->battle;
}
public function getZayavka(): int
{
return $this->zayavka;
}
public function setOnline()
{
Db::getInstance()->execute('update online set real_time = ? where user_id = ?', [time(), $this->getId()]);
}
/** Сохраняет в базу актуальные логин, пароль, email, имя, дату рождения, текст инфы, склонность, клан, образ, права админа.
*
*/
public function saveUser()
{
$query = 'update users set login = ?, align = ?, clan = ?, shadow = ?, admin = ? where id = ?';
$vals = [
$this->login, //set
$this->align,
$this->clan,
$this->shadow,
$this->admin,
$this->id //where
];
Db::getInstance()->execute($query, $vals);
}
}