Update healing spell logic, and add HTMX

This commit is contained in:
Sky Johnson 2024-12-19 09:12:54 -06:00
parent 1026e93ce3
commit 74a0e32cb7
3 changed files with 39 additions and 20 deletions

View File

@ -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");
$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 <a href=\"/\">exploring</a>.", "Healing Spell");
$page = <<<HTML
You have cast the {$spell['name']} spell, and gained {$restored} HP. You can now continue <a href="/" hx-get="/" hx-target="#middle">exploring</a>.
HTML;
}
page_title('Casting '.$spell['name']);
return is_htmx() ? $page : display($page, '');
}

View File

@ -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,

View File

@ -28,7 +28,9 @@
if ($user_spells !== false) {
foreach ($user_spells as $spell) {
// list only healing spells for now
if ($spell['type'] === 1) echo "<a href=\"/spell/{$spell['id']}\">".$spell['name']."</a><br>";
if ($spell['type'] === 1) echo <<<HTML
<a href="/spell/{$spell['id']}" hx-get="/spell/{$spell['id']}" hx-target="#middle">{$spell['name']}</a><br>
HTML;
}
} else {
echo 'None';