b9b4c01cf0
Signed-off-by: lopar <lopar.4ever@gmail.com>
205 lines
7.5 KiB
PHP
205 lines
7.5 KiB
PHP
<?php
|
||
# Date: 03.02.2021 (11:05)
|
||
|
||
namespace Battles;
|
||
|
||
use Battles\Models\Inventory;
|
||
use Battles\Models\User\Effects;
|
||
use Battles\Models\User\Stats;
|
||
use Exceptions\GameException;
|
||
|
||
class UserStats extends User
|
||
{
|
||
protected int $id;
|
||
protected int $strength;
|
||
protected int $dexterity;
|
||
protected int $intuition;
|
||
protected int $endurance;
|
||
protected int $intelligence;
|
||
protected int $wisdom;
|
||
protected int $health;
|
||
protected int $mana;
|
||
protected int $free_stat_points = 0;
|
||
protected int $level;
|
||
private const STAT_MAXIMUM_AMOUNT = 40;
|
||
private const ERROR_STAT_IS_MAXIMUM = 'Ошибка: Параметр достиг своего лимита!';
|
||
private const ERROR_STAT_UNKNOWN = 'Ошибка: Неизвестный параметр!';
|
||
private const STAT_NAMES_ARRAY = ['strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'];
|
||
|
||
//// Неизменяемые для игрока(!) переменные.
|
||
// Удар кулаком всегда 1-2.
|
||
protected int $minDamage = 1;
|
||
protected int $maxDamage = 2;
|
||
// Природная броня всегда 0.
|
||
|
||
// Динамически рассчитываемые
|
||
protected int $maxHealth;
|
||
protected int $maxMana;
|
||
|
||
/**
|
||
* UserStats constructor.
|
||
*
|
||
* @param $user
|
||
*/
|
||
public function __construct($user)
|
||
{
|
||
|
||
$data = Stats::getAll($user);
|
||
$this->id = $data->id;
|
||
$this->strength = $data->strength;
|
||
$this->dexterity = $data->dexterity;
|
||
$this->intuition = $data->intuition;
|
||
$this->endurance = $data->endurance;
|
||
$this->intelligence = $data->intelligence;
|
||
$this->wisdom = $data->wisdom;
|
||
$this->health = $data->health;
|
||
$this->mana = $data->mana;
|
||
$this->free_stat_points = $data->free_stat_points;
|
||
$this->level = $data->level;
|
||
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));
|
||
}
|
||
|
||
/**
|
||
* Отдаёт информацию о базовом(!) стате.
|
||
*
|
||
* @param string $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition',
|
||
* 'endurance', 'intelligence', 'wisdom'.
|
||
* @param int $isMainWindow - переключатель "главного окна". Если включить, дополнительно будет показывать ссылку
|
||
* на повышение стата на 1, при условии наличия свободных очков статов.
|
||
*
|
||
* @return string
|
||
* @throws GameException
|
||
*/
|
||
public function getStat(string $stat_name, int $isMainWindow = 0): string
|
||
{
|
||
if (!in_array($stat_name, self::STAT_NAMES_ARRAY)) {
|
||
throw new GameException(self::ERROR_STAT_UNKNOWN);
|
||
}
|
||
$stat = strval($this->$stat_name);
|
||
if ($this->free_stat_points && $isMainWindow && $this->$stat_name < self::STAT_MAXIMUM_AMOUNT) {
|
||
$rand = strval(mt_rand());
|
||
$stat .= " <a href='/main.php?edit=$rand&ups=$stat_name'>[+]</a>";
|
||
}
|
||
return $stat;
|
||
}
|
||
|
||
/**
|
||
* Повышает один из выбранных статов на 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, self::STAT_NAMES_ARRAY)) {
|
||
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 {
|
||
Stats::addOne($stat_name, $this->id);
|
||
}
|
||
}
|
||
|
||
public function getMaxWeight(): int
|
||
{
|
||
return max($this->strength * 5, 50);
|
||
}
|
||
|
||
/**
|
||
* @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;
|
||
}
|
||
|
||
public function getFullStats(): object
|
||
{
|
||
$stats = Stats::getAll($this->id);
|
||
$itemBonuses = Inventory::getBonuses($this->id);
|
||
$effectBonuses = Effects::getStatMods($this->id);
|
||
$obj = (object)[];
|
||
foreach (self::STAT_NAMES_ARRAY as $stat) {
|
||
$obj->$stat = max(0, $stats->$stat + $itemBonuses->{'item_' . $stat} + $effectBonuses->{'effect_' . $stat});
|
||
}
|
||
//$obj->strength = max(0, $stats->strength + $itemBonuses->item_strength + $effectBonuses->effect_strength);
|
||
//$obj->dexterity = max(0, $stats->dexterity + $itemBonuses->item_dexterity + $effectBonuses->effect_dexterity);
|
||
//$obj->intuition = max(0, $stats->intuition + $itemBonuses->item_intuition + $effectBonuses->effect_intuition);
|
||
//$obj->endurance = max(0, $stats->endurance + $itemBonuses->item_endurance + $effectBonuses->effect_endurance);
|
||
//$obj->intelligence = max(0, $stats->intelligence + $itemBonuses->item_intelligence + $effectBonuses->effect_intelligence);
|
||
//$obj->wisdom = max(0, $stats->wisdom + $itemBonuses->item_wisdom + $effectBonuses->effect_wisdom);
|
||
$obj->accuracy = max(0, $itemBonuses->item_accuracy);
|
||
$obj->evasion = max(0, $itemBonuses->item_evasion);
|
||
$obj->criticals = max(0, $itemBonuses->item_criticals);
|
||
$obj->min_physical_damage = max($this->minDamage, $this->minDamage + $itemBonuses->item_min_physical_damage);
|
||
$obj->max_physical_damage = max($this->maxDamage, $this->maxDamage + $itemBonuses->item_max_physical_damage);
|
||
return $obj;
|
||
}
|
||
|
||
public function levelUp(): string
|
||
{
|
||
$this->level += 1;
|
||
$this->free_stat_points += 2;
|
||
$this->save();
|
||
Chat::addSYSMessage('Внимание, вы получили ' . $this->level . 'уровень. Доступны очки распределения параметров.');
|
||
return 'Персонаж перешёл на ' . $this->level . 'уровень.';
|
||
}
|
||
|
||
/** Сохраняет в базу актуальные статы, здоровье, ману, свободные очки статов и уровень.
|
||
*
|
||
*/
|
||
private function save()
|
||
{
|
||
Stats::save([
|
||
$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
|
||
]);
|
||
}
|
||
} |