2020-07-03 13:23:08 +00:00
|
|
|
<?php
|
2021-01-27 15:56:04 +00:00
|
|
|
|
2020-10-28 20:21:08 +00:00
|
|
|
namespace Battles;
|
2021-01-27 15:56:04 +00:00
|
|
|
|
2022-01-26 23:15:33 +00:00
|
|
|
use Battles\Database\Db;
|
2020-07-03 13:23:08 +00:00
|
|
|
|
|
|
|
class User
|
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
use Users;
|
2022-01-26 23:15:33 +00:00
|
|
|
private static ?self $_instance = null;
|
2022-08-09 19:57:43 +00:00
|
|
|
private ?UserProfile $profile = null;
|
|
|
|
private ?UserEffect $effect = null;
|
|
|
|
private ?UserStats $stats = null;
|
|
|
|
private ?UserInfo $userInfo = null;
|
|
|
|
private ?UserMoney $userMoney = null;
|
2022-01-26 23:15:33 +00:00
|
|
|
|
2021-03-10 21:38:14 +00:00
|
|
|
|
2020-07-03 13:49:39 +00:00
|
|
|
// Пока несуществующие, для совместимости.
|
2021-08-25 01:44:36 +00:00
|
|
|
protected int $experience = 0;
|
|
|
|
protected int $battle = 0;
|
|
|
|
protected int $zayavka = 0;
|
2020-07-04 10:49:43 +00:00
|
|
|
|
2022-01-26 23:15:33 +00:00
|
|
|
protected function __construct($user = null)
|
2020-07-03 13:23:08 +00:00
|
|
|
{
|
2022-01-26 23:15:33 +00:00
|
|
|
if (is_null($user)) {
|
|
|
|
$user = $_SESSION['uid'];
|
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
// Отсекаем 2.0000~
|
|
|
|
$col = ctype_digit(strval($user)) ? 'id' : 'login';
|
|
|
|
$query = "select * from users where $col = ?";
|
2022-01-26 23:15:33 +00:00
|
|
|
$user_query = Db::getInstance()->fetch($query, $user);
|
2020-07-03 13:23:08 +00:00
|
|
|
foreach ($this as $key => $value) {
|
|
|
|
if (isset($user_query[$key])) {
|
|
|
|
$this->$key = $user_query[$key];
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
|
|
|
|
$this->profileData = (object)[
|
|
|
|
$this->id,
|
|
|
|
$this->pass,
|
|
|
|
$this->email,
|
|
|
|
$this->realname,
|
|
|
|
$this->borndate,
|
|
|
|
$this->info,
|
|
|
|
$this->ip,
|
|
|
|
];
|
2020-07-03 13:23:08 +00:00
|
|
|
}
|
2020-07-03 16:37:24 +00:00
|
|
|
|
2022-01-26 23:15:33 +00:00
|
|
|
public static function getInstance($user = null): self
|
|
|
|
{
|
|
|
|
if (is_null(self::$_instance)) {
|
|
|
|
self::$_instance = new self($user);
|
|
|
|
}
|
|
|
|
return self::$_instance;
|
|
|
|
}
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
public function profile(): UserProfile
|
2020-08-29 18:14:33 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
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
|
|
|
}
|
2021-01-28 21:05:34 +00:00
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
public function effect(): UserEffect
|
2021-01-28 21:05:34 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
if (is_null($this->effect)) {
|
|
|
|
$this->effect = new UserEffect();
|
2021-01-28 21:05:34 +00:00
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
return $this->effect;
|
2021-01-28 21:05:34 +00:00
|
|
|
}
|
2021-02-01 16:42:52 +00:00
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
public function stats(): UserStats
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
if (is_null($this->stats)) {
|
|
|
|
$this->stats = new UserStats($this->id);
|
|
|
|
}
|
|
|
|
return $this->stats;
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
public function userInfo(): UserInfo
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
if (is_null($this->userInfo)) {
|
|
|
|
$this->userInfo = new UserInfo($this->id);
|
|
|
|
}
|
|
|
|
return $this->userInfo;
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
public function money(): UserMoney
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
if (is_null($this->userMoney)) {
|
|
|
|
$this->userMoney = new UserMoney($this->id, $this->money);
|
|
|
|
}
|
|
|
|
return $this->userMoney;
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
public function getId(): int
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
return $this->id;
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
public function getLogin(): string
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
return $this->login;
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getLevel(): int
|
|
|
|
{
|
|
|
|
return $this->level;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAlign(): int
|
|
|
|
{
|
|
|
|
return $this->align;
|
|
|
|
}
|
|
|
|
|
2021-08-25 01:44:36 +00:00
|
|
|
public function getClan(): ?string
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
|
|
|
return $this->clan;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-25 01:44:36 +00:00
|
|
|
* @param string|null $short_name Короткое название клана. Передать null для очистки.
|
2021-02-01 16:42:52 +00:00
|
|
|
*/
|
2021-08-25 01:44:36 +00:00
|
|
|
public function setClan(?string $short_name)
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
2021-08-26 14:44:14 +00:00
|
|
|
$this->clan = is_null($short_name) ? null : $short_name;
|
|
|
|
$this->saveUser();
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAdmin(): int
|
|
|
|
{
|
|
|
|
return $this->admin;
|
|
|
|
}
|
|
|
|
|
2021-08-25 01:44:36 +00:00
|
|
|
public function getRoom(): int
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
|
|
|
return $this->room;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $room
|
|
|
|
*/
|
2021-08-25 01:44:36 +00:00
|
|
|
public function setRoom($room)
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
|
|
|
$this->room = $room;
|
|
|
|
}
|
|
|
|
|
2021-08-25 01:44:36 +00:00
|
|
|
public function getBlock(): int
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
|
|
|
return $this->block;
|
|
|
|
}
|
|
|
|
|
2021-08-25 01:44:36 +00:00
|
|
|
public function getShadow(): string
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
|
|
|
return $this->shadow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $shadow
|
|
|
|
*/
|
|
|
|
public function setShadow($shadow): void
|
|
|
|
{
|
2021-03-10 21:38:14 +00:00
|
|
|
$shadows = [
|
|
|
|
'm01', 'm02', 'm03', 'm04', 'm05', 'm06', 'm07', 'm08', 'm09', 'm10',
|
|
|
|
'f01', 'f02', 'f03', 'f04', 'f05', 'f06', 'f07', 'f08', 'f09', 'f10',
|
|
|
|
];
|
2022-08-09 19:57:43 +00:00
|
|
|
if (in_array($shadow, $shadows) && $this->getShadow() === '0.png') {
|
2021-03-10 21:38:14 +00:00
|
|
|
$this->shadow = $shadow . '.png';
|
|
|
|
}
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getExperience(): int
|
|
|
|
{
|
|
|
|
return $this->experience;
|
|
|
|
}
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
/**
|
|
|
|
* @param int $experience
|
|
|
|
*/
|
|
|
|
public function addExperience(int $experience): void
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
$this->experience += $experience;
|
|
|
|
Db::getInstance()->execute('update users set experience = ? where id = ?', [$experience, $this->id]);
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 19:57:43 +00:00
|
|
|
public function getBattle(): int
|
2021-02-01 16:42:52 +00:00
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
return $this->battle;
|
2021-02-01 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getZayavka(): int
|
|
|
|
{
|
|
|
|
return $this->zayavka;
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:55:08 +00:00
|
|
|
public function setOnline()
|
|
|
|
{
|
2022-01-26 23:15:33 +00:00
|
|
|
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
|
|
|
|
2021-08-26 14:44:14 +00:00
|
|
|
/** Сохраняет в базу актуальные логин, пароль, email, имя, дату рождения, текст инфы, склонность, клан, образ, права админа.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function saveUser()
|
|
|
|
{
|
2022-08-09 19:57:43 +00:00
|
|
|
$query = 'update users set login = ?, align = ?, clan = ?, shadow = ?, admin = ? where id = ?';
|
2021-08-26 14:44:14 +00:00
|
|
|
$vals = [
|
|
|
|
$this->login, //set
|
|
|
|
$this->align,
|
|
|
|
$this->clan,
|
|
|
|
$this->shadow,
|
|
|
|
$this->admin,
|
|
|
|
$this->id //where
|
|
|
|
];
|
2022-01-26 23:15:33 +00:00
|
|
|
Db::getInstance()->execute($query, $vals);
|
2021-08-26 14:44:14 +00:00
|
|
|
}
|
2022-08-09 19:57:43 +00:00
|
|
|
|
2020-07-03 13:23:08 +00:00
|
|
|
}
|