@@ -3,33 +3,35 @@
|
||||
|
||||
namespace Battles;
|
||||
|
||||
use Battles\Database\Db;
|
||||
use Battles\Models\Inventory;
|
||||
use Battles\Models\User\Effects;
|
||||
use Battles\Models\User\Stats;
|
||||
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;
|
||||
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 $headArmor = 0;
|
||||
protected int $chestArmor = 0;
|
||||
protected int $legArmor = 0;
|
||||
|
||||
// Динамически рассчитываемые
|
||||
protected int $maxHealth;
|
||||
protected int $maxMana;
|
||||
@@ -41,6 +43,19 @@ class UserStats extends 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));
|
||||
@@ -49,23 +64,25 @@ class UserStats extends User
|
||||
/**
|
||||
* Отдаёт информацию о базовом(!) стате.
|
||||
*
|
||||
* @param $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition',
|
||||
* 'endurance', 'intelligence', 'wisdom'.
|
||||
* @param int $isMainWindow - переключатель "главного окна". Если включить, дополнительно будет показывать ссылку
|
||||
* на повышение стата на 1, при условии наличия свободных очков статов.
|
||||
* @param string $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition',
|
||||
* 'endurance', 'intelligence', 'wisdom'.
|
||||
* @param int $isMainWindow - переключатель "главного окна". Если включить, дополнительно будет показывать ссылку
|
||||
* на повышение стата на 1, при условии наличия свободных очков статов.
|
||||
*
|
||||
* @return string
|
||||
* @throws GameException
|
||||
*/
|
||||
public function getStat($stat_name, int $isMainWindow = 0): string
|
||||
public function getStat(string $stat_name, int $isMainWindow = 0): string
|
||||
{
|
||||
if (!in_array($stat_name, ['strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'])) {
|
||||
return self::ERROR_STAT_UNKNOWN;
|
||||
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) {
|
||||
$this->$stat_name .= " <a href='/main.php?edit=" . mt_rand() . "&ups=$stat_name'>[+]</a>";
|
||||
$rand = strval(mt_rand());
|
||||
$stat .= " <a href='/main.php?edit=$rand&ups=$stat_name'>[+]</a>";
|
||||
}
|
||||
return $this->$stat_name;
|
||||
|
||||
return $stat;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,20 +96,19 @@ class UserStats extends User
|
||||
*/
|
||||
public function addOnePointToStat(string $stat_name)
|
||||
{
|
||||
if (!in_array($stat_name, ['strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'])) {
|
||||
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 {
|
||||
$query = "UPDATE users SET {$stat_name} = {$stat_name} + 1, free_stat_points = free_stat_points - 1 WHERE id = ?";
|
||||
Db::getInstance()->execute($query, $this->id);
|
||||
Stats::addOne($stat_name, $this->id);
|
||||
}
|
||||
}
|
||||
|
||||
public function getMaxWeight(): int
|
||||
{
|
||||
return $this->strength * 4;
|
||||
return max($this->strength * 5, 50);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,71 +151,21 @@ class UserStats extends User
|
||||
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;
|
||||
}
|
||||
|
||||
public function getFullStats(): object
|
||||
{
|
||||
$stats = Db::getInstance()->ofetch("
|
||||
select
|
||||
strength,
|
||||
dexterity,
|
||||
intuition,
|
||||
endurance,
|
||||
intelligence,
|
||||
wisdom
|
||||
from users where id = $this->id");
|
||||
$itemBonuses = Db::getInstance()->ofetch("
|
||||
select
|
||||
sum(add_strength) as item_strength,
|
||||
sum(add_dexterity) as item_dexterity,
|
||||
sum(add_intuition) as item_intuition,
|
||||
sum(add_endurance) as item_endurance,
|
||||
sum(add_intelligence) as item_intelligence,
|
||||
sum(add_wisdom) as item_wisdom,
|
||||
sum(add_accuracy) as item_accuracy,
|
||||
sum(add_evasion) as item_evasion,
|
||||
sum(add_criticals) as item_criticals,
|
||||
sum(add_min_physical_damage) as item_min_physical_damage,
|
||||
sum(add_max_physical_damage) as item_max_physical_damage
|
||||
from inventory where dressed_slot != 0 and owner_id = $this->id");
|
||||
$effectBonuses = Db::getInstance()->ofetch("
|
||||
select
|
||||
sum(mod_strength) as effect_strength,
|
||||
sum(mod_dexterity) as effect_dexterity,
|
||||
sum(mod_intuition) as effect_intuition,
|
||||
sum(mod_endurance) as effect_endurance,
|
||||
sum(mod_intelligence) as effect_intelligence,
|
||||
sum(mod_wisdom) as effect_wisdom
|
||||
from users_effects where owner_id = $this->id");
|
||||
$stats = Stats::getAll($this->id);
|
||||
$itemBonuses = Inventory::getBonuses($this->id);
|
||||
$effectBonuses = Effects::getStatMods($this->id);
|
||||
$obj = (object)[];
|
||||
$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);
|
||||
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);
|
||||
@@ -212,7 +178,7 @@ class UserStats extends User
|
||||
{
|
||||
$this->level += 1;
|
||||
$this->free_stat_points += 2;
|
||||
$this->saveStats();
|
||||
$this->save();
|
||||
Chat::addSYSMessage('Внимание, вы получили ' . $this->level . 'уровень. Доступны очки распределения параметров.');
|
||||
return 'Персонаж перешёл на ' . $this->level . 'уровень.';
|
||||
}
|
||||
@@ -220,12 +186,9 @@ class UserStats extends User
|
||||
/** Сохраняет в базу актуальные статы, здоровье, ману, свободные очки статов и уровень.
|
||||
*
|
||||
*/
|
||||
private function saveStats()
|
||||
private function save()
|
||||
{
|
||||
$query = 'update users set strength = ?, dexterity = ?, intuition = ?, endurance = ?,
|
||||
intelligence = ?, wisdom = ?, health = ?, mana = ?, free_stat_points = ?,
|
||||
level = ? where id = ?';
|
||||
$vals = [
|
||||
Stats::save([
|
||||
$this->strength, //set
|
||||
$this->dexterity,
|
||||
$this->intuition,
|
||||
@@ -237,7 +200,6 @@ class UserStats extends User
|
||||
$this->free_stat_points,
|
||||
$this->level,
|
||||
$this->id //where
|
||||
];
|
||||
Db::getInstance()->execute($query, $vals);
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user