From 6028f50cd1099e0ffa6206346aa34f8f6d57b99c Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Thu, 14 Aug 2025 17:08:36 -0400 Subject: [PATCH] Update return types --- src/DragonKnight/Actions/Admin.php | 10 +-- src/DragonKnight/Actions/Explore.php | 4 +- src/DragonKnight/Actions/Fight.php | 22 ++++-- src/DragonKnight/Actions/Forum.php | 14 +++- src/DragonKnight/Actions/Heal.php | 4 ++ src/DragonKnight/Actions/Help.php | 12 ++-- src/DragonKnight/Actions/Install.php | 15 ++-- src/DragonKnight/Actions/Towns.php | 15 ++-- src/DragonKnight/Bootstrap.php | 104 +++++++++++++++------------ src/DragonKnight/Models/Model.php | 2 + src/DragonKnight/Models/User.php | 3 + src/DragonKnight/Router.php | 20 +++--- 12 files changed, 133 insertions(+), 92 deletions(-) diff --git a/src/DragonKnight/Actions/Admin.php b/src/DragonKnight/Actions/Admin.php index de4e027..9468b34 100644 --- a/src/DragonKnight/Actions/Admin.php +++ b/src/DragonKnight/Actions/Admin.php @@ -175,7 +175,7 @@ class Admin /** * Show the full list of drops that can be edited. */ - public static function drops() + public static function drops(): string { $drops = db()->query('SELECT * FROM drops ORDER BY id;'); $page = "

Edit Drops

Click an item's name to edit it.

\n"; @@ -252,7 +252,7 @@ class Admin /** * List the monsters available to edit. */ - public static function monsters() + public static function monsters(): string { $max_level = db()->query('SELECT level FROM monsters ORDER BY level DESC LIMIT 1;')->fetchArray(SQLITE3_ASSOC)['level']; $monsters = db()->query('SELECT * FROM monsters ORDER BY id;'); @@ -362,7 +362,7 @@ class Admin /** * Handle the editing of a level. */ - public static function edit_level() + public static function edit_level(): string|false { if (! isset($_POST['level'])) { return 'No level to edit.'; @@ -406,7 +406,7 @@ class Admin return $page; } - public static function users() + public static function users(): string { $users = db()->query('SELECT * FROM users ORDER BY id;'); $page = '

Edit Users

Click a username or ID to edit the account.

'; @@ -496,7 +496,7 @@ class Admin /** * Handling adding news posts. */ - public static function add_news() + public static function add_news(): string { if (is_post()) { $c = trim($_POST['content'] ?? ''); diff --git a/src/DragonKnight/Actions/Explore.php b/src/DragonKnight/Actions/Explore.php index 211e1d1..4b128c3 100644 --- a/src/DragonKnight/Actions/Explore.php +++ b/src/DragonKnight/Actions/Explore.php @@ -28,7 +28,7 @@ class Explore * Just spit out a blank exploring page. Exploring without a GET string is normally when they first log in, or when * they've just finished fighting. */ - public static function explore() + public static function explore(): string { page_title('Exploring'); @@ -38,7 +38,7 @@ class Explore HTML; } - public static function move() + public static function move(): string|false { // Early exit if fighting if (user()->currentaction == 'Fighting') { diff --git a/src/DragonKnight/Actions/Fight.php b/src/DragonKnight/Actions/Fight.php index a3a7786..967c515 100644 --- a/src/DragonKnight/Actions/Fight.php +++ b/src/DragonKnight/Actions/Fight.php @@ -15,6 +15,15 @@ namespace DragonKnight\Actions; use DragonKnight\Router; +use function DragonKnight\db; +use function DragonKnight\get_drop; +use function DragonKnight\get_monster; +use function DragonKnight\get_spell; +use function DragonKnight\page_title; +use function DragonKnight\redirect; +use function DragonKnight\render; +use function DragonKnight\user; + class Fight { public static function register_routes(Router $r): Router @@ -30,7 +39,7 @@ class Fight /** * One big long public static function that determines the outcome of the fight. */ - public static function fight() + public static function fight(): string|false { if (user()->currentaction !== 'Fighting') { exit('Cheat attempt detected.

Get a life, loser.'); @@ -199,7 +208,7 @@ class Fight return $page; } - public static function victory() + public static function victory(): string { if (user()->currentmonsterhp != 0) { redirect('/fight'); @@ -251,6 +260,7 @@ class Fight $levelrow = db()->query('SELECT * FROM levels WHERE id=? LIMIT 1;', [user()->level + 1])->fetchArray(SQLITE3_ASSOC); + $page = ''; if (user()->level < 100) { if ($newexp >= $levelrow[user()->charclass.'_exp']) { user()->maxhp += $levelrow[user()->charclass.'_hp']; @@ -303,7 +313,7 @@ class Fight return $page; } - public static function drop() + public static function drop(): string { if (user()->dropcode == 0) { redirect('/'); @@ -455,7 +465,7 @@ class Fight return $page; } - public static function dead() + public static function dead(): string { return <<You have died.

@@ -465,7 +475,7 @@ class Fight HTML; } - public static function handleMonsterTurn(&$userrow, $monsterrow) + public static function handleMonsterTurn(&$userrow, $monsterrow): string { $pagearray = ''; if (user()->currentmonstersleep != 0) { @@ -511,7 +521,7 @@ class Fight return $pagearray; } - public static function handleSpellCast(&$userrow, $newspellrow) + public static function handleSpellCast(&$userrow, $newspellrow): string { $pagearray = ''; switch ($newspellrow['type']) { diff --git a/src/DragonKnight/Actions/Forum.php b/src/DragonKnight/Actions/Forum.php index 80711a7..564e9b0 100644 --- a/src/DragonKnight/Actions/Forum.php +++ b/src/DragonKnight/Actions/Forum.php @@ -15,6 +15,14 @@ namespace DragonKnight\Actions; use DragonKnight\Router; +use function DragonKnight\db; +use function DragonKnight\page_title; +use function DragonKnight\pretty_date; +use function DragonKnight\redirect; +use function DragonKnight\ul_from_validate_errors; +use function DragonKnight\user; +use function DragonKnight\validate; + class Forum { public static function register_routes(Router $r): Router @@ -28,7 +36,7 @@ class Forum return $r; } - public static function donothing($start = 0) + public static function donothing($start = 0): string { $query = db()->query('SELECT * FROM forum WHERE parent=0 ORDER BY newpostdate DESC LIMIT 20 OFFSET ?;', [20 * $start]); $page = <<query('SELECT * FROM forum WHERE id=? OR parent=? ORDER BY id LIMIT 15 OFFSET ?;', [$id, $id, $start * 15]); $title = db()->query('SELECT title FROM forum WHERE id=? LIMIT 1;', [$id])->fetchArray(SQLITE3_ASSOC); @@ -89,7 +97,7 @@ class Forum return $page; } - public static function reply() + public static function reply(): string { $form = validate($_POST, [ 'title' => [], diff --git a/src/DragonKnight/Actions/Heal.php b/src/DragonKnight/Actions/Heal.php index dff212a..9440d11 100644 --- a/src/DragonKnight/Actions/Heal.php +++ b/src/DragonKnight/Actions/Heal.php @@ -13,6 +13,10 @@ declare(strict_types=1); namespace DragonKnight\Actions; +use function DragonKnight\get_spell; +use function DragonKnight\page_title; +use function DragonKnight\user; + class Heal { public static function healspells(int $id): string diff --git a/src/DragonKnight/Actions/Help.php b/src/DragonKnight/Actions/Help.php index b17d1f0..d390e9e 100644 --- a/src/DragonKnight/Actions/Help.php +++ b/src/DragonKnight/Actions/Help.php @@ -33,7 +33,7 @@ class Help return $r; } - public static function main() + public static function main(): string|false { $page = <<Table of Contents @@ -266,7 +266,7 @@ class Help return self::display_help($page); } - public static function items() + public static function items(): string|false { $page = << @@ -331,7 +331,7 @@ class Help return self::display_help($page); } - public static function spells() + public static function spells(): string|false { $page = << @@ -364,7 +364,7 @@ class Help return self::display_help($page); } - public static function monsters() + public static function monsters(): string|false { $page = << @@ -383,7 +383,7 @@ class Help return self::display_help($page.''); } - public static function levels() + public static function levels(): string|false { $rows = []; @@ -510,7 +510,7 @@ class Help return self::display_help($page); } - public static function display_help(string $content) + public static function display_help(string $content): string|false { return render('layouts/help', [ 'content' => $content, diff --git a/src/DragonKnight/Actions/Install.php b/src/DragonKnight/Actions/Install.php index b2e8c09..9c16653 100644 --- a/src/DragonKnight/Actions/Install.php +++ b/src/DragonKnight/Actions/Install.php @@ -17,7 +17,6 @@ use DragonKnight\Router; use function DragonKnight\db; use function DragonKnight\ul_from_validate_errors; -use function DragonKnight\user; use function DragonKnight\validate; class Install @@ -38,7 +37,7 @@ class Install /** * First page - show warnings and gather info. */ - public static function first() + public static function first(): string { return << @@ -58,7 +57,7 @@ class Install /** * Set up database tables. */ - public static function second() + public static function second(): string { if (! is_dir($path = getcwd().'/db')) { if (mkdir($path, 0777, true) === false) { @@ -686,7 +685,7 @@ class Install /** * Gather user info for admin account. */ - public static function third() + public static function third(): string { return << @@ -715,7 +714,7 @@ class Install /** * Final page: insert new user row, congratulate the person on a job well done. */ - public static function fourth(?array $post = null) + public static function fourth(?array $post = null): string { $post ??= $_POST; $form = validate($post, [ @@ -764,11 +763,11 @@ class Install /** * Call Home public static function. */ - public static function fifth() + public static function fifth(): string { - /**if (mail('sky@sharkk.net', 'Dragon Knight Call Home', $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']) !== true) { + if (mail('sky@sharkk.net', 'Dragon Knight Call Home', $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']) !== true) { exit('Dragon Knight was unable to send your URL. Please go back and try again, or just continue on to the game.'); - }*/ + } return << diff --git a/src/DragonKnight/Actions/Towns.php b/src/DragonKnight/Actions/Towns.php index a9d4c4e..7e9c676 100644 --- a/src/DragonKnight/Actions/Towns.php +++ b/src/DragonKnight/Actions/Towns.php @@ -22,6 +22,7 @@ use function DragonKnight\get_item; use function DragonKnight\get_town_by_id; use function DragonKnight\is_post; use function DragonKnight\page_title; +use function DragonKnight\pretty_date; use function DragonKnight\redirect; use function DragonKnight\render; use function DragonKnight\user; @@ -44,7 +45,7 @@ class Towns /** * Spit out the main town page. */ - public static function town() + public static function town(): string|false { $town = get_town_by_xy(user()->longitude, user()->latitude); if ($town === false) { @@ -98,13 +99,14 @@ class Towns * Staying at the inn resets all expendable stats to their max values. * GET/POST /inn. */ - public static function inn() + public static function inn(): string { $town = get_town_by_xy(user()->longitude, user()->latitude); if ($town === false) { exit('Cheat attempt detected.

Get a life, loser.'); } + $page = ''; if (user()->gold < $town['innprice']) { $page = <<
@@ -140,7 +142,7 @@ class Towns * redirects to home. * GET /shop. */ - public static function shop() + public static function shop(): string { $town = get_town_by_xy(user()->longitude, user()->latitude); if ($town === false) { @@ -193,7 +195,7 @@ class Towns /** * Confirm user's intent to purchase item. */ - public static function buy(int $id) + public static function buy(int $id): string { $town = get_town_by_xy(user()->longitude, user()->latitude); if ($town === false) { @@ -205,6 +207,7 @@ class Towns $item = get_item($id); $can_afford = user()->gold >= $item['buycost']; + $page = ''; if (! $can_afford) { $page = <<{$item['name']}.

@@ -312,7 +315,7 @@ class Towns /** * List maps the user can buy. */ - public static function maps() + public static function maps(): string { $page = <<
@@ -363,6 +366,7 @@ class Towns redirect('/maps'); } + $page = ''; if (user()->gold < $town['mapprice']) { $page = <<
@@ -408,6 +412,7 @@ class Towns $mapped = explode(',', user()->towns); $travelled = false; + $page = ''; if ($use_points && ! in_array($id, $mapped)) { // trying to teleport to this town when it is not mapped redirect('/'); diff --git a/src/DragonKnight/Bootstrap.php b/src/DragonKnight/Bootstrap.php index 72b9f81..057f264 100644 --- a/src/DragonKnight/Bootstrap.php +++ b/src/DragonKnight/Bootstrap.php @@ -13,7 +13,7 @@ declare(strict_types=1); namespace DragonKnight; -Lib::env_load(getcwd().'/.env'); +env_load(getcwd().'/.env'); $uri = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $GLOBALS['cache'] = []; @@ -27,50 +27,60 @@ define('ADMIN_BULK_DATA_STRUCTS', [ ]); if (! file_exists((getcwd().'/.installed')) && $uri[0] !== 'install') { // need to install the game - Lib::redirect('/install'); -} elseif (file_exists((getcwd().'/.installed')) && $uri[0] === 'install') { // game is installed, go play! - Lib::redirect('/'); -} elseif (file_exists((getcwd().'/.installed')) && $uri[0] !== 'install') { // boostrap the game - if (! Lib::env('game_open')) { - echo Lib::render('The game is currently closed for maintanence. Please check back later.'); - exit; - } - - $auth = new Auth(); - - // Login (or verify) if not logged in. - if (Lib::user() === false) { - if (! in_array($uri[0], ['login', 'register', 'verify', 'lostpassword', 'help'])) { - Lib::redirect('/login'); - } - } elseif ($auth->good()) { - // Block user if he/she has been banned. - if (Lib::user()->authlevel === 2) { - exit('Your account has been banned.'); - } - - // Force verify if the user isn't verified yet. - if (Lib::env('verify_email') && Lib::user()->verify !== 'g2g' && ! in_array($uri[0], ['verify', 'logout'])) { - Lib::redirect('/verify'); - } - - // Ensure the user can't use the admin panel. - if (Lib::user()->authlevel !== 1 && $uri[0] === 'admin') { - Lib::redirect('/'); - } - - // Update default page layout based on root endpoint - Lib::page_layout('layouts/primary'); - if ($uri[0] === 'admin') { - Lib::page_layout('layouts/admin'); - } - if ($uri[0] === 'help') { - Lib::page_layout('layouts/help'); - } - - Lib::user()->update_online_time(); - } else { - $auth->logout(); - Lib::redirect('/login'); - } + redirect('/install'); + return; +} +if (file_exists((getcwd().'/.installed')) && $uri[0] === 'install') { // game is installed, go play! + redirect('/'); + return; } +if (! file_exists((getcwd().'/.installed')) || $uri[0] === 'install') { + return; +} + +// boostrap the game +if (! env('game_open')) { + echo render('The game is currently closed for maintanence. Please check back later.'); + exit; +} + +$auth = new Auth(); + +// Login (or verify) if not logged in. +if (user() === false) { + if (! in_array($uri[0], ['login', 'register', 'verify', 'lostpassword', 'help'])) { + redirect('/login'); + } + return; +} +if ($auth->good()) { + // Block user if he/she has been banned. + if (user()->authlevel === 2) { + exit('Your account has been banned.'); + } + + // Force verify if the user isn't verified yet. + if (env('verify_email') && user()->verify !== 'g2g' && ! in_array($uri[0], ['verify', 'logout'])) { + redirect('/verify'); + } + + // Ensure the user can't use the admin panel. + if (user()->authlevel !== 1 && $uri[0] === 'admin') { + redirect('/'); + } + + // Update default page layout based on root endpoint + page_layout('layouts/primary'); + if ($uri[0] === 'admin') { + page_layout('layouts/admin'); + } + if ($uri[0] === 'help') { + page_layout('layouts/help'); + } + + user()->update_online_time(); + return; +} + +$auth->logout(); +redirect('/login'); \ No newline at end of file diff --git a/src/DragonKnight/Models/Model.php b/src/DragonKnight/Models/Model.php index b52da04..c5e7f23 100644 --- a/src/DragonKnight/Models/Model.php +++ b/src/DragonKnight/Models/Model.php @@ -13,6 +13,8 @@ declare(strict_types=1); namespace DragonKnight\Models; +use function DragonKnight\db; + class Model { protected string $table_name = ''; diff --git a/src/DragonKnight/Models/User.php b/src/DragonKnight/Models/User.php index a5d4ba8..77ab4c8 100644 --- a/src/DragonKnight/Models/User.php +++ b/src/DragonKnight/Models/User.php @@ -13,6 +13,9 @@ declare(strict_types=1); namespace DragonKnight\Models; +use function DragonKnight\db; +use function DragonKnight\get_spells_from_list; + class User extends Model { protected string $table_name = 'users'; diff --git a/src/DragonKnight/Router.php b/src/DragonKnight/Router.php index 6869fc5..561526b 100644 --- a/src/DragonKnight/Router.php +++ b/src/DragonKnight/Router.php @@ -41,7 +41,7 @@ class Router * Example: * `$r->add($routes, 'GET', '/posts/:id', function($id) { echo "Viewing post $id"; });` */ - public function add(string $method, string $route, callable $handler): Router + public function add(string $method, string $route, callable $handler): self { $this->validateMethod($method); $this->validateRoute($route); @@ -106,7 +106,7 @@ class Router /** * Add a middleware function to the last inserted node's stack. */ - public function middleware(callable $middleware): Router + public function middleware(callable $middleware): self { $this->last_inserted_node['middleware'][] = $middleware; @@ -116,7 +116,7 @@ class Router /** * Shorthand to register a GET route. */ - public function get(string $route, callable $handler): Router + public function get(string $route, callable $handler): self { return $this->add('GET', $route, $handler); } @@ -124,7 +124,7 @@ class Router /** * Shorthand to register a POST route. */ - public function post(string $route, callable $handler): Router + public function post(string $route, callable $handler): self { return $this->add('POST', $route, $handler); } @@ -132,7 +132,7 @@ class Router /** * Shorthand to register a PUT route. */ - public function put(string $route, callable $handler): Router + public function put(string $route, callable $handler): self { return $this->add('PUT', $route, $handler); } @@ -140,7 +140,7 @@ class Router /** * Shorthand to register a DELETE route. */ - public function delete(string $route, callable $handler): Router + public function delete(string $route, callable $handler): self { return $this->add('DELETE', $route, $handler); } @@ -148,7 +148,7 @@ class Router /** * Shorthand to register a PATCH route. */ - public function patch(string $route, callable $handler): Router + public function patch(string $route, callable $handler): self { return $this->add('PATCH', $route, $handler); } @@ -156,7 +156,7 @@ class Router /** * Register multiple verbs to the same route. */ - public function many(array $methods, string $route, callable $handler): Router + public function many(array $methods, string $route, callable $handler): self { foreach ($methods as $method) { $this->add($method, $route, $handler); @@ -168,7 +168,7 @@ class Router /** * Register all verbs to the same route. */ - public function any(string $route, callable $handler): Router + public function any(string $route, callable $handler): self { foreach (self::VALID_METHODS as $method) { $this->add($method, $route, $handler); @@ -180,7 +180,7 @@ class Router /** * Some pages function entirely as forms; thus we can shorthand a GET/POST route. */ - public function form(string $route, callable $handler): Router + public function form(string $route, callable $handler): self { return $this->many(['GET', 'POST'], $route, $handler); }