diff --git a/public/index.php b/public/index.php index 09f6ffc..ed709ef 100644 --- a/public/index.php +++ b/public/index.php @@ -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. */ diff --git a/src/actions/explore.php b/src/actions/explore.php index 34f9098..4594e65 100644 --- a/src/actions/explore.php +++ b/src/actions/explore.php @@ -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(); } diff --git a/src/actions/fight.php b/src/actions/fight.php index 21be638..2087dea 100644 --- a/src/actions/fight.php +++ b/src/actions/fight.php @@ -173,13 +173,14 @@ function fight() user()->currentfight += 1; } else { - $pagearray["command"] = "You have died.

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.

You may now continue back to town, and we hope you fair better next time."; + $page["command"] = "You have died.

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.

You may now continue back to town, 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 exploring.", "Item Drop"); + $page = 'The item has been equipped. You can now continue exploring.'; + return is_htmx() ? $page : display($page, "Item Drop"); } $attributearray = array("maxhp"=>"Max HP", @@ -349,7 +355,7 @@ function drop() $page .= "
"; $page .= "You may also choose to just continue exploring 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.

You may now continue back to town, 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) diff --git a/src/actions/towns.php b/src/actions/towns.php index 960f9d4..ce89093 100644 --- a/src/actions/towns.php +++ b/src/actions/towns.php @@ -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"); - } - $mapped = explode(",",user()->towns); - if (!in_array($id, $mapped)) { display("Cheat attempt detected.

Get a life, loser.", "Error"); } - } - - if ((user()->latitude == $townrow["latitude"]) && (user()->longitude == $townrow["longitude"])) { - return display("You are already in this town. Click here to return to the main town screen.", "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 go back 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 = <<{$town['name']}! It has been added to your mapped towns.

+ You may now enter this town. + HTML; + } else { + $page = 'You are already in this town. Click here to return.'; + } + } else { + user()->latitude = $town['latitude']; + user()->longitude = $town['longitude']; + user()->currenttp -= $cost; + $travelled = true; + $page = 'You have travelled to '.$town['name'].'. You may now enter this town.'; } - $newtp = ($usepoints) ? user()->currenttp - $townrow["travelpoints"] : user()->currenttp; + if ($travelled) { + user()->currentaction = 'In Town'; + user()->save(); + } - $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"; - - 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 enter this town."; - return display($page, "Travel To"); + page_title('Travelling to '.$town['name']); + return is_htmx() ? $page : display($page, ''); } diff --git a/src/actions/users.php b/src/actions/users.php index 2e95c82..552ff8d 100644 --- a/src/actions/users.php +++ b/src/actions/users.php @@ -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'); } diff --git a/src/bootstrap.php b/src/bootstrap.php index eace2ba..90f7b99 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -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'); } } diff --git a/src/render.php b/src/render.php index 094a943..00cc755 100644 --- a/src/render.php +++ b/src/render.php @@ -23,7 +23,8 @@ function header_links(): string function debug_db_info(): string { $total_time = round(microtime(true) - START, 4); - return '
'. $total_time . ' Seconds, ' . db()->count . ' Queries
'; + $htmx = is_htmx() ? ' (htmx)' : ''; + return '
'. $total_time . ' Seconds, ' . db()->count . ' Queries'.$htmx.'
'; } function right_nav(): string diff --git a/templates/left_nav.php b/templates/left_nav.php index bc9ee57..a6317c1 100644 --- a/templates/left_nav.php +++ b/templates/left_nav.php @@ -10,13 +10,13 @@ Latitude:
Longitude:
View Map
-
- + +
- - + +
- +
@@ -36,7 +36,9 @@ while ($row = $towns->fetchArray(SQLITE3_ASSOC)) { $mapped = true; if (in_array($row['id'], $town_list)) { - echo "".$row["name"]."
"; + echo <<{$row['name']}
+ HTML; } } if (!$mapped) echo 'You have no towns mapped.'; @@ -45,8 +47,8 @@
Functions
- Home
- Forum
+ Home
+ Forum
Settings
Change Password
Log Out
diff --git a/templates/login.php b/templates/login.php index a157f5a..38bdfa5 100644 --- a/templates/login.php +++ b/templates/login.php @@ -8,13 +8,6 @@ Password: - - Remember me? - - - - - @@ -22,9 +15,9 @@ 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.

Want to play? You gotta - register your own character.

You may also - change your password, or - request a new one if you've lost yours. + register your own character.

You may also + change your password, or + request a new one if you've lost yours.