battles/classes/Battles/User.php

228 lines
5.4 KiB
PHP
Raw Normal View History

<?php
namespace Battles;
use Battles\Database\Db;
class User
{
use Users;
2022-12-16 23:20:43 +00:00
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;
2022-12-16 23:20:43 +00:00
private object $profileData;
2020-07-04 10:49:43 +00:00
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 = ?";
2022-12-16 23:20:43 +00:00
$userQuery = Db::getInstance()->fetch($query, $user);
foreach ($this as $key => $value) {
2022-12-16 23:20:43 +00:00
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
{
2022-12-16 23:20:43 +00:00
if (is_null(self::$instance)) {
self::$instance = new self($user);
}
2022-12-16 23:20:43 +00:00
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;
2020-07-10 08:08:00 +00:00
}
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;
}
/**
2022-12-16 23:20:43 +00:00
* @param string|null $shortName Короткое название клана. Передать null для очистки.
*/
2022-12-16 23:20:43 +00:00
public function setClan(?string $shortName)
{
2022-12-16 23:20:43 +00:00
$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;
}
2021-03-10 22:55:08 +00:00
public function setOnline()
{
Db::getInstance()->execute('update online set real_time = ? where user_id = ?', [time(), $this->getId()]);
2021-03-10 22:55:08 +00:00
}
2021-05-12 20:34:54 +00:00
/** Сохраняет в базу актуальные логин, пароль, 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);
}
2022-12-16 23:20:43 +00:00
}