Did a lot of updating and moving around to make fights and exploring work with HTMX

This commit is contained in:
Sky Johnson 2024-12-19 10:04:03 -06:00
parent 74a0e32cb7
commit 35b0e07e4e
9 changed files with 104 additions and 100 deletions

View File

@ -12,24 +12,14 @@ if ($uri[0] === 'babblebox' && (isset($uri[1]) && $uri[1] === 'messages')) {
$r = new Router;
$r->get('/', function() {
if (user()->currentaction === "In Town") {
$page = Towns\town();
} elseif (user()->currentaction === "Exploring") {
$page = Explore\explore();
} elseif (user()->currentaction === "Fighting") {
redirect('/fight');
}
return is_htmx() ? $page : display($page, '');
});
/*
NINJA! 🥷
*/
$r->get('/ninja', function() {
exit('NINJA! 🥷');
});
$r->get('/', 'index');
$r->post('/move', 'Explore\move');
$r->get('/spell/:id', 'healspells');
$r->get('/character', 'show_character_info');
$r->get('/character/:id', 'show_character_info');
$r->get('/showmap', 'showmap');
$r->form('/babblebox', 'babblebox');
$r->get('/babblebox/messages', 'babblebox_messages');
Towns\register_routes($r);
Fights\register_routes($r);
@ -39,14 +29,12 @@ Forum\register_routes($r);
Install\register_routes($r);
Admin\register_routes($r);
$r->post('/move', 'Explore\move');
$r->get('/spell/:id', 'healspells');
$r->get('/character', 'show_character_info');
$r->get('/character/:id', 'show_character_info');
$r->get('/showmap', 'showmap');
$r->form('/babblebox', 'babblebox');
$r->get('/babblebox/messages', 'babblebox_messages');
/*
NINJA! 🥷
*/
$r->get('/ninja', function() {
exit('NINJA! 🥷');
});
// [code, handler, params, middleware]
$l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
@ -67,6 +55,22 @@ if (is_htmx() && $uri[0] !== 'babblebox') {
echo $content;
exit;
/**
* Return a page for a couple generic actions.
*/
function index(): string
{
if (user()->currentaction === "In Town") {
$page = Towns\town();
} elseif (user()->currentaction === "Exploring") {
$page = Explore\explore();
} elseif (user()->currentaction === "Fighting") {
redirect('/fight');
}
return is_htmx() ? $page : display($page, '');
}
/**
* Show a character's info. Defaults to the currently logged in user.
*/

View File

@ -25,7 +25,10 @@ function move() {
// Validate direction
$form = validate($_POST, ['direction' => ['in:north,west,east,south']]);
if (!$form['valid']) return display(ul_from_validate_errors($form['errors']), 'Move Error');
if (!$form['valid']) {
$errors = ul_from_validate_errors($form['errors']);
return is_htmx() ? $errors : display($errors, 'Move Error');
}
// Current game state
$game_size = $controlrow['gamesize'];
@ -67,5 +70,5 @@ function move() {
user()->longitude = $longitude;
user()->save();
redirect('/');
return index();
}

View File

@ -173,13 +173,14 @@ function fight()
user()->currentfight += 1;
} else {
$pagearray["command"] = "<b>You have died.</b><br><br>As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.<br><br>You may now continue back to <a href=\"/\">town</a>, and we hope you fair better next time.";
$page["command"] = "<b>You have died.</b><br><br>As a consequence, you've lost half of your gold. However, you have been given back a portion of your hit points to continue your journey.<br><br>You may now continue back to <a href=\"/\">town</a>, and we hope you fair better next time.";
}
user()->save();
// Finalize page and display it
return display(render('fight', ['page' => $page]), "Fighting");
$page = render('fight', ['page' => $page]);
return is_htmx() ? $page : display($page, "Fighting");
}
function victory()
@ -252,7 +253,8 @@ function victory()
user()->currentmonsterimmune = 0;
user()->save();
return display($page, $title);
page_title($title);
return is_htmx() ? $page : display($page, $title);
}
function drop()
@ -264,7 +266,10 @@ function drop()
if (isset($_POST["submit"])) {
$slot = $_POST["slot"];
if ($slot == 0) { return display("Please go back and select an inventory slot to continue.","Error"); }
if ($slot == 0) {
$page = 'Please go back and select an inventory slot to continue.';
return is_htmx() ? $page : display($page, '');
}
$slotstr = 'slot'.$slot.'id';
if (user()->$slotstr != 0) {
@ -319,7 +324,8 @@ function drop()
}
user()->save();
return display("The item has been equipped. You can now continue <a href=\"/\">exploring</a>.", "Item Drop");
$page = 'The item has been equipped. You can now continue <a href="/" hx-get="/" hx-target="#middle">exploring</a>.';
return is_htmx() ? $page : display($page, "Item Drop");
}
$attributearray = array("maxhp"=>"Max HP",
@ -349,7 +355,7 @@ function drop()
$page .= "<form action=\"/drop\" method=\"post\"><select name=\"slot\"><option value=\"0\">Choose One</option><option value=\"1\">Slot 1: ".user()->slot1name."</option><option value=\"2\">Slot 2: ".user()->slot2name."</option><option value=\"3\">Slot 3: ".user()->slot3name."</option></select> <input type=\"submit\" name=\"submit\" value=\"Submit\" /></form>";
$page .= "You may also choose to just continue <a href=\"/\">exploring</a> and give up this item.";
return display($page, "Item Drop");
return is_htmx() ? $page : display($page, "Item Drop");
}
@ -361,7 +367,7 @@ function dead()
to continue your journey.<br><br>
You may now continue back to <a href="/">town</a>, and we hope you fair better next time.
HTML;
return display($page, 'You Died');
return is_htmx() ? $page : display($page, 'You Died');
}
function handleMonsterTurn(&$userrow, $monsterrow)

View File

@ -363,43 +363,45 @@ function buy_map(int $id): string
/**
* Send a user to a town from the Travel To menu.
*/
function travelto($id, bool $usepoints = true)
function travelto(int $id, bool $use_points = true): string
{
if (user()->currentaction == "Fighting") redirect('/fight');
$townrow = get_town_by_id($id);
$town = get_town_by_id($id);
$cost = $use_points ? $town['travelpoints'] : 0;
$mapped = explode(',', user()->towns);
$travelled = false;
if ($usepoints) {
if (user()->currenttp < $townrow["travelpoints"]) {
return display("You do not have enough TP to travel here. Please go back and try again when you get more TP.", "Travel To");
if ($use_points && !in_array($id, $mapped)) {
// trying to teleport to this town when it is not mapped
redirect('/');
} elseif (user()->currenttp < $cost) {
$page = 'You do not have enough TP to travel here. Please <a href="/" hx-get="/" hx-target="#middle">go back</a> and try again when you get more TP.';
} elseif ((user()->latitude == $town['latitude']) && (user()->longitude == $town['longitude'])) {
if (!in_array($id, $mapped)) {
// add town to user's mapped if they travelled here
user()->towns .= ",$id";
$travelled = true;
$page = <<<HTML
You have discovered <b>{$town['name']}</b>! It has been added to your mapped towns.<br><br>
You may now <a href="/" hx-get="/" hx-target="#middle">enter this town</a>.
HTML;
} else {
$page = 'You are already in this town. <a href="/" hx-get="/" hx-target="#middle">Click here</a> to return.';
}
$mapped = explode(",",user()->towns);
if (!in_array($id, $mapped)) { display("Cheat attempt detected.<br><br>Get a life, loser.", "Error"); }
} else {
user()->latitude = $town['latitude'];
user()->longitude = $town['longitude'];
user()->currenttp -= $cost;
$travelled = true;
$page = 'You have travelled to <b>'.$town['name'].'</b>. You may now <a href="/" hx-get="/" hx-target="#middle">enter this town</a>.';
}
if ((user()->latitude == $townrow["latitude"]) && (user()->longitude == $townrow["longitude"])) {
return display("You are already in this town. <a href=\"/\">Click here</a> to return to the main town screen.", "Travel To");
}
$newtp = ($usepoints) ? user()->currenttp - $townrow["travelpoints"] : user()->currenttp;
$newlat = $townrow["latitude"];
$newlon = $townrow["longitude"];
// If they got here by exploring, add this town to their map.
$mapped = explode(",",user()->towns);
$town = false;
foreach($mapped as $b) if ($b == $id) $town = true;
$mapped = implode(",", $mapped);
if ($town == false) $mapped .= ",$id";
if ($travelled) {
user()->currentaction = 'In Town';
user()->towns = $mapped;
user()->currenttp = $newtp;
user()->longitude = $newlon;
user()->latitude = $newlat;
user()->save();
}
$page = "You have travelled to ".$townrow["name"].". You may now <a href=\"/\">enter this town</a>.";
return display($page, "Travel To");
page_title('Travelling to '.$town['name']);
return is_htmx() ? $page : display($page, '');
}

View File

@ -30,20 +30,11 @@ function login()
'remember' => ['bool']
]);
if (!$form['valid']) {
exit(ul_from_validate_errors($form['errors']));
}
if (!$form['valid']) exit(ul_from_validate_errors($form['errors']));
$form = $form['data'];
$row = get_user($form['username']);
if ($row === false || !$auth->login($form['username'], $form['password']))
die("Invalid username or password. Please go back and try again.");
$good = $auth->login($form['data']['username'], $form['data']['password']);
if (!$good) exit('Invalid username or password. Please go back and try again.');
$expiretime = $form['remember'] ? time() + 31536000 : 0;
$rememberme = $form['remember'] ? 1 : 0;
$cookie = implode(' ', [$row['id'], $row['username'], $row['password'], $rememberme]);
set_cookie("dkgame", $cookie, $expiretime);
redirect('/');
}
@ -57,7 +48,6 @@ function logout()
{
global $auth;
$auth->logout();
set_cookie("dkgame", "", -3600);
redirect('/login');
}

View File

@ -41,7 +41,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') {
if (!in_array($uri[0], ['login', 'register', 'verify', 'lostpassword', 'help'])) {
redirect('/login');
}
} elseif($auth->good()) {
} elseif ($auth->good()) {
// Block user if he/she has been banned.
if (user()->authlevel === 2) {
exit("Your account has been banned.");
@ -58,5 +58,8 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') {
}
user()->update_online_time();
} else {
$auth->logout();
redirect('/login');
}
}

View File

@ -23,7 +23,8 @@ function header_links(): string
function debug_db_info(): string {
$total_time = round(microtime(true) - START, 4);
return '<div id="debug-db-info" hx-swap-oob="true">'. $total_time . ' Seconds, ' . db()->count . ' Queries</div>';
$htmx = is_htmx() ? ' (htmx)' : '';
return '<div id="debug-db-info" hx-swap-oob="true">'. $total_time . ' Seconds, ' . db()->count . ' Queries'.$htmx.'</div>';
}
function right_nav(): string

View File

@ -10,13 +10,13 @@
Latitude: <?= $lat ?><br>
Longitude: <?= $lon ?><br>
<a href="javascript:openmappopup()">View Map</a><br>
<form action="/move" method="post" class="move-compass">
<button type="submit" name="direction" value="north" class="north">North</button>
<form action="/move" method="post" hx-post="/move" hx-target="#middle" class="move-compass">
<button name="direction" value="north" class="north">North</button>
<div class="mid">
<button type="submit" name="direction" value="west" class="west">West</button>
<button type="submit" name="direction" value="east" class="east">East</button>
<button name="direction" value="west" class="west">West</button>
<button name="direction" value="east" class="east">East</button>
</div>
<button type="submit" name="direction" value="south" class="south">South</button>
<button name="direction" value="south" class="south">South</button>
</form>
</section>
@ -36,7 +36,9 @@
while ($row = $towns->fetchArray(SQLITE3_ASSOC)) {
$mapped = true;
if (in_array($row['id'], $town_list)) {
echo "<a href=\"/gotown/{$row["id"]}\">".$row["name"]."</a><br>";
echo <<<HTML
<a href="/gotown/{$row['id']}" hx-get="/gotown/{$row['id']}" hx-target="#middle">{$row['name']}</a><br>
HTML;
}
}
if (!$mapped) echo 'You have no towns mapped.';
@ -45,8 +47,8 @@
<section>
<div class="title"><img src="/img/button_functions.gif" alt="Functions" title="Functions"></div>
<a href="/">Home</a><br>
<a href="/forum">Forum</a><br>
<a href="/" hx-get="/" hx-target="#middle">Home</a><br>
<a href="/forum" hx-get="/forum" hx-target="#middle">Forum</a><br>
<a href="/settings">Settings</a><br>
<a href="/changepassword">Change Password</a><br>
<a href="/logout">Log Out</a><br>

View File

@ -8,13 +8,6 @@
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>Remember me?</td>
<td>
<input type="hidden" name="remember" value="0">
<input type="checkbox" name="remember" value="1">
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Log In"></td>
</tr>
@ -22,9 +15,9 @@
<td colspan="2">
Checking the "Remember Me" option will store your login information in a cookie so you don't have
to enter it next time you get online.<br><br>Want to play? You gotta
<a href="users.php?do=register">register your own character.</a><br><br>You may also
<a href="users.php?do=changepassword">change your password</a>, or
<a href="users.php?do=lostpassword">request a new one</a> if you've lost yours.
<a href="/register">register your own character.</a><br><br>You may also
<a href="/changepassword">change your password</a>, or
<a href="/lostpassword">request a new one</a> if you've lost yours.
</td>
</tr>
</table>