battles/classes/Battles/UserStats.php

256 lines
9.1 KiB
PHP
Raw Normal View History

<?php
# Date: 03.02.2021 (11:05)
namespace Battles;
use Battles\Database\DBPDO;
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;
private const STAT_MAXIMUM_AMOUNT = 40;
private const ERROR_STAT_IS_MAXIMUM = 'Ошибка: Параметр достиг своего лимита!';
private const ERROR_STAT_UNKNOWN = 'Ошибка: Неизвестный параметр!';
//// Неизменяемые для игрока(!) переменные.
// Удар кулаком всегда 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;
/**
* 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));
}
/**
* Отдаёт информацию о базовом(!) стате.
*
* @param $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition',
* 'endurance', 'intelligence', 'wisdom'.
* @param int $isMainWindow - переключатель "главного окна". Если включить, дополнительно будет показывать ссылку
* на повышение стата на 1, при условии наличия свободных очков статов.
*
* @return string
*/
public function getStat($stat_name, int $isMainWindow = 0): string
{
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 = ?";
self::$db->execute($query, $this->id);
}
}
/**
* @return mixed
*/
public function getStrength()
{
return $this->strength;
}
/**
* @return mixed
*/
public function getDexterity()
{
return $this->dexterity;
}
/**
* @return mixed
*/
public function getIntuition()
{
return $this->intuition;
}
/**
* @return mixed
*/
public function getEndurance()
{
return $this->endurance;
}
/**
* @return mixed
*/
public function getIntelligence()
{
return $this->intelligence;
}
/**
* @return mixed
*/
public function getWisdom()
{
return $this->wisdom;
}
/**
* @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;
}
public function getFullStats()
{
$query = "
select
2021-05-12 20:34:54 +00:00
sum(greatest(strength + (ifnull((select sum(add_strength) from inventory where dressed_slot != 0 and owner_id = $this->id), 0)) + (ifnull((select sum(mod_strength) from users_effects where owner_id = $this->id), 0)), 0)) as strength,
sum(greatest(dexterity + (ifnull((select sum(add_dexterity) from inventory where dressed_slot != 0 and owner_id = $this->id), 0)) + (ifnull((select sum(mod_dexterity) from users_effects where owner_id = $this->id), 0)), 0)) as dexterity,
sum(greatest(intuition + (ifnull((select sum(add_intuition) from inventory where dressed_slot != 0 and owner_id = $this->id), 0)) + (ifnull((select sum(mod_intuition) from users_effects where owner_id = $this->id), 0)), 0)) as intuition,
sum(greatest(endurance + (ifnull((select sum(add_endurance) from inventory where dressed_slot != 0 and owner_id = $this->id), 0)) + (ifnull((select sum(mod_endurance) from users_effects where owner_id = $this->id), 0)), 0)) as endurance,
sum(greatest(intelligence + (ifnull((select sum(add_intelligence) from inventory where dressed_slot != 0 and owner_id = $this->id), 0)) + (ifnull((select sum(mod_intelligence) from users_effects where owner_id = $this->id), 0)), 0)) as intelligence,
sum(greatest(wisdom + (ifnull((select sum(add_wisdom) from inventory where dressed_slot != 0 and owner_id = $this->id), 0)) + (ifnull((select sum(mod_wisdom) from users_effects where owner_id = $this->id), 0)), 0)) as wisdom,
ifnull((select sum(add_accuracy) from inventory where dressed_slot != 0 and owner_id = $this->id), 0) as accuracy,
ifnull((select sum(add_evasion) from inventory where dressed_slot != 0 and owner_id = $this->id), 0) as evasion,
ifnull((select sum(add_criticals) from inventory where dressed_slot != 0 and owner_id = $this->id), 0) as criticals,
sum(greatest($this->minDamage + (ifnull((select sum(add_min_physical_damage) from inventory where dressed_slot != 0 and owner_id = $this->id), 0)), 0)) as min_physical_damage,
sum(greatest($this->maxDamage + (ifnull((select sum(add_max_physical_damage) from inventory where dressed_slot != 0 and owner_id = $this->id), 0)), 0)) as max_physical_damage
from users where id = $this->id";
return self::$db->ofetch($query);
}
public function levelUp(): string
{
$this->level += 1;
$this->free_stat_points += 2;
$this->saveStats();
Chat::addSYSMessage('Внимание, вы получили ' . $this->level . 'уровень. Доступны очки распределения параметров.');
return 'Персонаж перешёл на ' . $this->level . 'уровень.';
}
/** Сохраняет в базу актуальные статы, здоровье, ману, свободные очки статов и уровень.
*
*/
private function saveStats()
{
$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
];
DBPDO::$db->execute($query, $vals);
}
}