diff --git a/src/actions/heal.php b/src/actions/heal.php
index acb8bd1..90ecf43 100644
--- a/src/actions/heal.php
+++ b/src/actions/heal.php
@@ -2,27 +2,33 @@
// heal.php :: Handles stuff from the Quick Spells menu. (Healing spells only... other spells are handled in fight.php.)
-function healspells($id)
+function healspells(int $id): string
{
- global $userrow;
+ $user_spells = user()->spells();
+ $spell = get_spell($id);
+ $has_spell = false;
+ foreach ($user_spells as $us) if ($us['id'] === $id) $has_spell = true;
- $userspells = explode(",", $userrow["spells"]);
- $spellrow = get_spell($id);
+ if ($has_spell !== true) {
+ $page = 'You have not yet learned this spell. Please go back and try again.';
+ } elseif ($spell['type'] !== 1) {
+ $page = 'This is not a healing spell. Please go back and try again.';
+ } elseif (user()->currentmp < $spell['mp']) {
+ $page = 'You do not have enough Magic Points to cast this spell. Please go back and try again.';
+ } elseif (user()->currentaction === 'Fighting') {
+ $page = 'You cannot use the Quick Spells list during a fight. Please go back and select the Healing Spell you wish to use from the Spells box on the main fighting screen to continue.';
+ } elseif (user()->currenthp == user()->maxhp) {
+ $page = 'Your HP is already full. You don\'t need to use a Healing spell now.';
+ } else {
+ $restored = user()->restore_hp($spell['attribute']);
+ user()->currentmp -= $spell['mp'];
+ user()->save();
- // All the various ways to error out.
- $spell = false;
- foreach ($userspells as $b) if ($b == $id) $spell = true;
- if ($spell !== true) return display("You have not yet learned this spell. Please go back and try again.", "Error");
- if ($spellrow["type"] != 1) return display("This is not a healing spell. Please go back and try again.", "Error");
- if ($userrow["currentmp"] < $spellrow["mp"]) return display("You do not have enough Magic Points to cast this spell. Please go back and try again.", "Error");
- if ($userrow["currentaction"] == "Fighting") return display("You cannot use the Quick Spells list during a fight. Please go back and select the Healing Spell you wish to use from the Spells box on the main fighting screen to continue.", "Error");
- if ($userrow["currenthp"] == $userrow["maxhp"]) return display("Your Hit Points are already full. You don't need to use a Healing spell now.", "Error");
+ $page = <<exploring.
+ HTML;
+ }
- $newhp = $userrow["currenthp"] + $spellrow["attribute"];
- if ($userrow["maxhp"] < $newhp) { $spellrow["attribute"] = $userrow["maxhp"] - $userrow["currenthp"]; $newhp = $userrow["currenthp"] + $spellrow["attribute"]; }
- $newmp = $userrow["currentmp"] - $spellrow["mp"];
-
- db()->query('UPDATE users SET currenthp=?, currentmp=? WHERE id=?;', [$newhp, $newmp, $userrow['id']]);
-
- return display("You have cast the ".$spellrow["name"]." spell, and gained ".$spellrow["attribute"]." Hit Points. You can now continue exploring.", "Healing Spell");
+ page_title('Casting '.$spell['name']);
+ return is_htmx() ? $page : display($page, '');
}
diff --git a/src/models/user.php b/src/models/user.php
index 241d169..f448a2c 100644
--- a/src/models/user.php
+++ b/src/models/user.php
@@ -46,6 +46,17 @@ class User extends Model
db()->query('UPDATE users SET onlinetime=CURRENT_TIMESTAMP WHERE id=?;', [$this->id]);
}
+ /**
+ * Heal HP by a given amount. Caps to max HP. Returns number of points restored.
+ */
+ function restore_hp(int $amount): int
+ {
+ $initial_hp = $this->currenthp;
+ $this->currenthp += $amount;
+ if ($this->currenthp > $this->maxhp) $this->currenthp = $this->maxhp;
+ return $this->currenthp - $initial_hp;
+ }
+
/**
* Save works just as it does on the Model class. In our case, though, user state changing may necessitate
* OOB swaps for parts of the UI that have user data displayed. Left and right nav, for example. In these cases,
diff --git a/templates/right_nav.php b/templates/right_nav.php
index 05e54a0..4454fb7 100644
--- a/templates/right_nav.php
+++ b/templates/right_nav.php
@@ -28,7 +28,9 @@
if ($user_spells !== false) {
foreach ($user_spells as $spell) {
// list only healing spells for now
- if ($spell['type'] === 1) echo "".$spell['name']."
";
+ if ($spell['type'] === 1) echo <<{$spell['name']}
+ HTML;
}
} else {
echo 'None';