2021-03-10 21:38:14 +00:00
|
|
|
|
<?php
|
|
|
|
|
# Date: 03.02.2021 (11:05)
|
|
|
|
|
|
|
|
|
|
namespace Battles;
|
|
|
|
|
|
2022-01-26 23:15:33 +00:00
|
|
|
|
use Battles\Database\Db;
|
2021-03-10 21:38:14 +00:00
|
|
|
|
use Exceptions\GameException;
|
|
|
|
|
|
|
|
|
|
class UserStats extends User
|
|
|
|
|
{
|
|
|
|
|
protected $strength;
|
|
|
|
|
protected $dexterity;
|
|
|
|
|
protected $intuition;
|
|
|
|
|
protected $endurance;
|
|
|
|
|
protected $intelligence;
|
|
|
|
|
protected $wisdom;
|
|
|
|
|
protected $health;
|
|
|
|
|
protected $mana;
|
|
|
|
|
protected $free_stat_points;
|
2023-01-24 17:04:15 +00:00
|
|
|
|
private Models\UserStats $db;
|
2021-03-10 21:38:14 +00:00
|
|
|
|
private const STAT_MAXIMUM_AMOUNT = 40;
|
|
|
|
|
private const ERROR_STAT_IS_MAXIMUM = 'Ошибка: Параметр достиг своего лимита!';
|
|
|
|
|
private const ERROR_STAT_UNKNOWN = 'Ошибка: Неизвестный параметр!';
|
|
|
|
|
|
|
|
|
|
//// Неизменяемые для игрока(!) переменные.
|
|
|
|
|
// Удар кулаком всегда 1-2.
|
2021-08-26 14:44:14 +00:00
|
|
|
|
protected int $minDamage = 1;
|
|
|
|
|
protected int $maxDamage = 2;
|
2021-03-10 21:38:14 +00:00
|
|
|
|
// Природная броня всегда 0.
|
|
|
|
|
// Зачем их три, если во всех формулах она одна?
|
2021-08-26 14:44:14 +00:00
|
|
|
|
protected int $headArmor = 0;
|
|
|
|
|
protected int $chestArmor = 0;
|
|
|
|
|
protected int $legArmor = 0;
|
2021-03-10 21:38:14 +00:00
|
|
|
|
// Динамически рассчитываемые
|
2021-08-26 14:44:14 +00:00
|
|
|
|
protected int $maxHealth;
|
|
|
|
|
protected int $maxMana;
|
2021-03-10 21:38:14 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* UserStats constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param $user
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($user)
|
|
|
|
|
{
|
|
|
|
|
parent::__construct($user);
|
|
|
|
|
$this->maxHealth = round(($this->endurance * 3) + ($this->endurance / 2) * ($this->level - 1) + ($this->endurance / 5) * (($this->level - 1) * ($this->level - 2) / 2));
|
|
|
|
|
$this->maxMana = round(($this->wisdom * 3) + ($this->wisdom / 2) * ($this->level - 1) + ($this->wisdom / 5) * (($this->level - 1) * ($this->level - 2) / 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Отдаёт информацию о базовом(!) стате.
|
|
|
|
|
*
|
2021-08-26 14:44:14 +00:00
|
|
|
|
* @param $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition',
|
2021-03-10 21:38:14 +00:00
|
|
|
|
* 'endurance', 'intelligence', 'wisdom'.
|
|
|
|
|
* @param int $isMainWindow - переключатель "главного окна". Если включить, дополнительно будет показывать ссылку
|
|
|
|
|
* на повышение стата на 1, при условии наличия свободных очков статов.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-08-22 18:51:00 +00:00
|
|
|
|
public function getStat($stat_name, int $isMainWindow = 0): string
|
2021-03-10 21:38:14 +00:00
|
|
|
|
{
|
|
|
|
|
if (!in_array($stat_name, ['strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'])) {
|
|
|
|
|
return self::ERROR_STAT_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
if ($this->free_stat_points && $isMainWindow && $this->$stat_name < self::STAT_MAXIMUM_AMOUNT) {
|
|
|
|
|
$this->$stat_name .= " <a href='/main.php?edit=" . mt_rand() . "&ups=$stat_name'>[+]</a>";
|
|
|
|
|
}
|
|
|
|
|
return $this->$stat_name;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Повышает один из выбранных статов на 1, но не выше self::STAT_MAXIMUM_AMOUNT при условии наличия свободных очков
|
|
|
|
|
* статов.
|
|
|
|
|
*
|
|
|
|
|
* @param string $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition',
|
|
|
|
|
* 'endurance', 'intelligence', 'wisdom'.
|
|
|
|
|
*
|
|
|
|
|
* @throws GameException
|
|
|
|
|
*/
|
|
|
|
|
public function addOnePointToStat(string $stat_name)
|
|
|
|
|
{
|
|
|
|
|
if (!in_array($stat_name, ['strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'])) {
|
|
|
|
|
throw new GameException(self::ERROR_STAT_UNKNOWN);
|
|
|
|
|
}
|
|
|
|
|
if ($this->free_stat_points <= 0 || $this->$stat_name >= self::STAT_MAXIMUM_AMOUNT) {
|
|
|
|
|
throw new GameException(self::ERROR_STAT_IS_MAXIMUM);
|
|
|
|
|
} else {
|
|
|
|
|
$query = "UPDATE users SET {$stat_name} = {$stat_name} + 1, free_stat_points = free_stat_points - 1 WHERE id = ?";
|
2022-01-26 23:15:33 +00:00
|
|
|
|
Db::getInstance()->execute($query, $this->id);
|
2023-01-24 17:04:15 +00:00
|
|
|
|
//$this->db = new \Battles\Models\UserStats(23);
|
|
|
|
|
$this->db->addPoint($stat_name);
|
2021-03-10 21:38:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 16:16:09 +00:00
|
|
|
|
public function getMaxWeight(): int
|
2021-03-10 21:38:14 +00:00
|
|
|
|
{
|
2022-01-25 16:16:09 +00:00
|
|
|
|
return $this->strength * 4;
|
2021-03-10 21:38:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getHealth()
|
|
|
|
|
{
|
|
|
|
|
return $this->health;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getMana()
|
|
|
|
|
{
|
|
|
|
|
return $this->mana;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getFreeStatPoints()
|
|
|
|
|
{
|
|
|
|
|
return $this->free_stat_points;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return float
|
|
|
|
|
*/
|
|
|
|
|
public function getMaxHealth(): float
|
|
|
|
|
{
|
|
|
|
|
return $this->maxHealth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return float
|
|
|
|
|
*/
|
|
|
|
|
public function getMaxMana(): float
|
|
|
|
|
{
|
|
|
|
|
return $this->maxMana;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getHeadArmor(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->headArmor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getChestArmor(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->chestArmor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getLegArmor(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->legArmor;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 16:16:09 +00:00
|
|
|
|
public function getFullStats(): object
|
|
|
|
|
{
|
2023-01-24 17:04:15 +00:00
|
|
|
|
return (new Models\UserStats($this->id))->getFull();
|
2021-03-11 17:43:20 +00:00
|
|
|
|
}
|
2021-03-10 21:38:14 +00:00
|
|
|
|
|
2021-08-26 14:44:14 +00:00
|
|
|
|
public function levelUp(): string
|
|
|
|
|
{
|
|
|
|
|
$this->level += 1;
|
|
|
|
|
$this->free_stat_points += 2;
|
|
|
|
|
$this->saveStats();
|
2022-06-10 21:36:30 +00:00
|
|
|
|
Chat::sendSys('Внимание, вы получили ' . $this->level . 'уровень. Доступны очки распределения параметров.');
|
2021-08-26 14:44:14 +00:00
|
|
|
|
return 'Персонаж перешёл на ' . $this->level . 'уровень.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Сохраняет в базу актуальные статы, здоровье, ману, свободные очки статов и уровень.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
private function saveStats()
|
|
|
|
|
{
|
2023-01-24 17:04:15 +00:00
|
|
|
|
//$this->db->save();
|
2021-08-26 14:44:14 +00:00
|
|
|
|
$query = 'update users set strength = ?, dexterity = ?, intuition = ?, endurance = ?,
|
|
|
|
|
intelligence = ?, wisdom = ?, health = ?, mana = ?, free_stat_points = ?,
|
|
|
|
|
level = ? where id = ?';
|
|
|
|
|
$vals = [
|
|
|
|
|
$this->strength, //set
|
|
|
|
|
$this->dexterity,
|
|
|
|
|
$this->intuition,
|
|
|
|
|
$this->endurance,
|
|
|
|
|
$this->intelligence,
|
|
|
|
|
$this->wisdom,
|
|
|
|
|
$this->health,
|
|
|
|
|
$this->mana,
|
|
|
|
|
$this->free_stat_points,
|
|
|
|
|
$this->level,
|
|
|
|
|
$this->id //where
|
|
|
|
|
];
|
2022-01-26 23:15:33 +00:00
|
|
|
|
Db::getInstance()->execute($query, $vals);
|
2021-08-26 14:44:14 +00:00
|
|
|
|
}
|
2021-03-10 21:38:14 +00:00
|
|
|
|
}
|