From c5f8f958d684f2dfde776a9f9ed1869540964759 Mon Sep 17 00:00:00 2001 From: "Igor Barkov (iwork)" Date: Tue, 21 Jul 2020 11:36:21 +0300 Subject: [PATCH] setters and default values --- classes/User.php | 100 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 14 deletions(-) diff --git a/classes/User.php b/classes/User.php index 78477db..171836f 100644 --- a/classes/User.php +++ b/classes/User.php @@ -3,24 +3,24 @@ class User { public $id; - public $login; - public $email; + public $login = 'Некто'; + public $email = 'неизвестно'; public $realname; public $borndate; public $info; - public $level; - public $align; - public $clan; - public $money; - public $strength; - public $dexterity; - public $intuition; - public $endurance; - public $intelligence; - public $wisdom; + public $level = 0; + public $align = 0; + public $clan = 0; + public $money = 0; + public $strength = 0; + public $dexterity = 0; + public $intuition = 0; + public $endurance = 0; + public $intelligence = 0; + public $wisdom = 0; public $ip; public $session_id; - public $admin; + public $admin = 0; public $enter_game; public $room; public $block; @@ -33,6 +33,8 @@ class User public $chestArmor = 0; public $legArmor = 0; public const STAT_MAXIMUM_AMOUNT = 40; + private const ERROR_STAT_IS_MAXIMUM = 'Ошибка: Параметр достиг своего лимита!'; + private const ERROR_STAT_UNKNOWN = 'Ошибка: Неизвестный параметр!'; // Пока несуществующие, для совместимости. public $married = 'Someone или нет.'; public $experience = 200; @@ -133,7 +135,7 @@ class User echo $variables; echo ''; echo ''; - echo '
TODO: Сделать рассчёт здоровья и модификаторов. Вывести полоску здоровья когда будет от чего отталкиваться.
'; + echo '
TODO: Сделать рассчёт модификаторов. Вывести полоску здоровья когда будет от чего отталкиваться.
'; echo ''; } @@ -188,6 +190,76 @@ class User 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() { echo '
';