setters and default values

This commit is contained in:
Igor Barkov (iwork) 2020-07-21 11:36:21 +03:00
parent 87bcfe5b30
commit c5f8f958d6

View File

@ -3,24 +3,24 @@
class User class User
{ {
public $id; public $id;
public $login; public $login = '<em>Некто</em>';
public $email; public $email = '<em>неизвестно</em>';
public $realname; public $realname;
public $borndate; public $borndate;
public $info; public $info;
public $level; public $level = 0;
public $align; public $align = 0;
public $clan; public $clan = 0;
public $money; public $money = 0;
public $strength; public $strength = 0;
public $dexterity; public $dexterity = 0;
public $intuition; public $intuition = 0;
public $endurance; public $endurance = 0;
public $intelligence; public $intelligence = 0;
public $wisdom; public $wisdom = 0;
public $ip; public $ip;
public $session_id; public $session_id;
public $admin; public $admin = 0;
public $enter_game; public $enter_game;
public $room; public $room;
public $block; public $block;
@ -33,6 +33,8 @@ class User
public $chestArmor = 0; public $chestArmor = 0;
public $legArmor = 0; public $legArmor = 0;
public const STAT_MAXIMUM_AMOUNT = 40; public const STAT_MAXIMUM_AMOUNT = 40;
private const ERROR_STAT_IS_MAXIMUM = 'Ошибка: Параметр достиг своего лимита!';
private const ERROR_STAT_UNKNOWN = 'Ошибка: Неизвестный параметр!';
// Пока несуществующие, для совместимости. // Пока несуществующие, для совместимости.
public $married = 'Someone или нет.'; public $married = 'Someone или нет.';
public $experience = 200; public $experience = 200;
@ -133,7 +135,7 @@ class User
echo $variables; echo $variables;
echo '</div><!-- column -->'; echo '</div><!-- column -->';
echo '</div><!-- stats-container -->'; echo '</div><!-- stats-container -->';
echo '<div class="debug">TODO: Сделать рассчёт здоровья и модификаторов. Вывести полоску здоровья когда будет от чего отталкиваться.</div>'; echo '<div class="debug">TODO: Сделать рассчёт модификаторов. Вывести полоску здоровья когда будет от чего отталкиваться.</div>';
echo '</div><!-- user-info -->'; echo '</div><!-- user-info -->';
} }
@ -188,6 +190,76 @@ class User
return $this->wisdom; return $this->wisdom;
} }
public function setStrength()
{
if ($this->strength <= self::STAT_MAXIMUM_AMOUNT && $this->stat_points > 0) {
db::c()->query('UPDATE users SET strength = strength + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i', $this->id);
} else {
throw new Exception(self::ERROR_STAT_IS_MAXIMUM);
}
}
public function setDexterity()
{
if ($this->dexterity <= self::STAT_MAXIMUM_AMOUNT && $this->stat_points > 0) {
db::c()->query('UPDATE users SET dexterity = dexterity + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i', $this->id);
} else {
throw new Exception(self::ERROR_STAT_IS_MAXIMUM);
}
}
public function setIntuition()
{
if ($this->intuition <= self::STAT_MAXIMUM_AMOUNT && $this->stat_points > 0) {
db::c()->query('UPDATE users SET intuition = intuition + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i', $this->id);
} else {
throw new Exception(self::ERROR_STAT_IS_MAXIMUM);
}
}
public function setEndurance()
{
if ($this->endurance <= self::STAT_MAXIMUM_AMOUNT && $this->stat_points > 0) {
db::c()->query('UPDATE users SET endurance = endurance + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i', $this->id);
} else {
throw new Exception(self::ERROR_STAT_IS_MAXIMUM);
}
}
public function setIntelligence()
{
if ($this->intelligence <= self::STAT_MAXIMUM_AMOUNT && $this->stat_points > 0) {
db::c()->query('UPDATE users SET intelligence = intelligence + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i', $this->id);
} else {
throw new Exception(self::ERROR_STAT_IS_MAXIMUM);
}
}
public function setWisdom()
{
if ($this->wisdom <= self::STAT_MAXIMUM_AMOUNT && $this->stat_points > 0) {
db::c()->query('UPDATE users SET wisdom = wisdom + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i', $this->id);
} else {
throw new Exception(self::ERROR_STAT_IS_MAXIMUM);
}
}
public function addOnePointToStat($stat_name)
{
$allowed = ['strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'];
if (in_array($stat_name, $allowed)) {
if ($this->stat_points > 0 && $this->$stat_name <= self::STAT_MAXIMUM_AMOUNT) {
$query = 'UPDATE users SET ?f = ?f + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i';
db::c()->query($query, $stat_name, $stat_name, $this->id);
} else {
throw new Exception(self::ERROR_STAT_IS_MAXIMUM);
}
} else {
throw new Exception(self::ERROR_STAT_UNKNOWN);
}
}
private function UserInfo() private function UserInfo()
{ {
echo '<div class="user-info-container">'; echo '<div class="user-info-container">';