Update return types

This commit is contained in:
Valithor Obsidion 2025-08-14 17:08:36 -04:00
parent f0fc0f257b
commit 6028f50cd1
12 changed files with 133 additions and 92 deletions

View File

@ -175,7 +175,7 @@ class Admin
/** /**
* Show the full list of drops that can be edited. * 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;'); $drops = db()->query('SELECT * FROM drops ORDER BY id;');
$page = "<h2>Edit Drops</h2>Click an item's name to edit it.<br><br>\n"; $page = "<h2>Edit Drops</h2>Click an item's name to edit it.<br><br>\n";
@ -252,7 +252,7 @@ class Admin
/** /**
* List the monsters available to edit. * 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']; $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;'); $monsters = db()->query('SELECT * FROM monsters ORDER BY id;');
@ -362,7 +362,7 @@ class Admin
/** /**
* Handle the editing of a level. * Handle the editing of a level.
*/ */
public static function edit_level() public static function edit_level(): string|false
{ {
if (! isset($_POST['level'])) { if (! isset($_POST['level'])) {
return 'No level to edit.'; return 'No level to edit.';
@ -406,7 +406,7 @@ class Admin
return $page; return $page;
} }
public static function users() public static function users(): string
{ {
$users = db()->query('SELECT * FROM users ORDER BY id;'); $users = db()->query('SELECT * FROM users ORDER BY id;');
$page = '<h2>Edit Users</h2>Click a username or ID to edit the account.<br><br><div class="table-wrapper">'; $page = '<h2>Edit Users</h2>Click a username or ID to edit the account.<br><br><div class="table-wrapper">';
@ -496,7 +496,7 @@ class Admin
/** /**
* Handling adding news posts. * Handling adding news posts.
*/ */
public static function add_news() public static function add_news(): string
{ {
if (is_post()) { if (is_post()) {
$c = trim($_POST['content'] ?? ''); $c = trim($_POST['content'] ?? '');

View File

@ -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 * 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. * they've just finished fighting.
*/ */
public static function explore() public static function explore(): string
{ {
page_title('Exploring'); page_title('Exploring');
@ -38,7 +38,7 @@ class Explore
HTML; HTML;
} }
public static function move() public static function move(): string|false
{ {
// Early exit if fighting // Early exit if fighting
if (user()->currentaction == 'Fighting') { if (user()->currentaction == 'Fighting') {

View File

@ -15,6 +15,15 @@ namespace DragonKnight\Actions;
use DragonKnight\Router; 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 class Fight
{ {
public static function register_routes(Router $r): Router 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. * 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') { if (user()->currentaction !== 'Fighting') {
exit('Cheat attempt detected.<br><br>Get a life, loser.'); exit('Cheat attempt detected.<br><br>Get a life, loser.');
@ -199,7 +208,7 @@ class Fight
return $page; return $page;
} }
public static function victory() public static function victory(): string
{ {
if (user()->currentmonsterhp != 0) { if (user()->currentmonsterhp != 0) {
redirect('/fight'); redirect('/fight');
@ -251,6 +260,7 @@ class Fight
$levelrow = db()->query('SELECT * FROM levels WHERE id=? LIMIT 1;', [user()->level + 1])->fetchArray(SQLITE3_ASSOC); $levelrow = db()->query('SELECT * FROM levels WHERE id=? LIMIT 1;', [user()->level + 1])->fetchArray(SQLITE3_ASSOC);
$page = '';
if (user()->level < 100) { if (user()->level < 100) {
if ($newexp >= $levelrow[user()->charclass.'_exp']) { if ($newexp >= $levelrow[user()->charclass.'_exp']) {
user()->maxhp += $levelrow[user()->charclass.'_hp']; user()->maxhp += $levelrow[user()->charclass.'_hp'];
@ -303,7 +313,7 @@ class Fight
return $page; return $page;
} }
public static function drop() public static function drop(): string
{ {
if (user()->dropcode == 0) { if (user()->dropcode == 0) {
redirect('/'); redirect('/');
@ -455,7 +465,7 @@ class Fight
return $page; return $page;
} }
public static function dead() public static function dead(): string
{ {
return <<<HTML return <<<HTML
<b>You have died.</b><br><br> <b>You have died.</b><br><br>
@ -465,7 +475,7 @@ class Fight
HTML; HTML;
} }
public static function handleMonsterTurn(&$userrow, $monsterrow) public static function handleMonsterTurn(&$userrow, $monsterrow): string
{ {
$pagearray = ''; $pagearray = '';
if (user()->currentmonstersleep != 0) { if (user()->currentmonstersleep != 0) {
@ -511,7 +521,7 @@ class Fight
return $pagearray; return $pagearray;
} }
public static function handleSpellCast(&$userrow, $newspellrow) public static function handleSpellCast(&$userrow, $newspellrow): string
{ {
$pagearray = ''; $pagearray = '';
switch ($newspellrow['type']) { switch ($newspellrow['type']) {

View File

@ -15,6 +15,14 @@ namespace DragonKnight\Actions;
use DragonKnight\Router; 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 class Forum
{ {
public static function register_routes(Router $r): Router public static function register_routes(Router $r): Router
@ -28,7 +36,7 @@ class Forum
return $r; 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]); $query = db()->query('SELECT * FROM forum WHERE parent=0 ORDER BY newpostdate DESC LIMIT 20 OFFSET ?;', [20 * $start]);
$page = <<<HTML $page = <<<HTML
@ -72,7 +80,7 @@ class Forum
return $page; return $page;
} }
public static function showthread($id, $start) public static function showthread($id, $start): string
{ {
$posts = db()->query('SELECT * FROM forum WHERE id=? OR parent=? ORDER BY id LIMIT 15 OFFSET ?;', [$id, $id, $start * 15]); $posts = db()->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); $title = db()->query('SELECT title FROM forum WHERE id=? LIMIT 1;', [$id])->fetchArray(SQLITE3_ASSOC);
@ -89,7 +97,7 @@ class Forum
return $page; return $page;
} }
public static function reply() public static function reply(): string
{ {
$form = validate($_POST, [ $form = validate($_POST, [
'title' => [], 'title' => [],

View File

@ -13,6 +13,10 @@ declare(strict_types=1);
namespace DragonKnight\Actions; namespace DragonKnight\Actions;
use function DragonKnight\get_spell;
use function DragonKnight\page_title;
use function DragonKnight\user;
class Heal class Heal
{ {
public static function healspells(int $id): string public static function healspells(int $id): string

View File

@ -33,7 +33,7 @@ class Help
return $r; return $r;
} }
public static function main() public static function main(): string|false
{ {
$page = <<<HTML $page = <<<HTML
<h3>Table of Contents</h3> <h3>Table of Contents</h3>
@ -266,7 +266,7 @@ class Help
return self::display_help($page); return self::display_help($page);
} }
public static function items() public static function items(): string|false
{ {
$page = <<<HTML $page = <<<HTML
<table width="60%" style="border: solid 1px black" cellspacing="0" cellpadding="0"> <table width="60%" style="border: solid 1px black" cellspacing="0" cellpadding="0">
@ -331,7 +331,7 @@ class Help
return self::display_help($page); return self::display_help($page);
} }
public static function spells() public static function spells(): string|false
{ {
$page = <<<HTML $page = <<<HTML
<table width="50%" style="border: solid 1px black" cellspacing="0" cellpadding="0"> <table width="50%" style="border: solid 1px black" cellspacing="0" cellpadding="0">
@ -364,7 +364,7 @@ class Help
return self::display_help($page); return self::display_help($page);
} }
public static function monsters() public static function monsters(): string|false
{ {
$page = <<<HTML $page = <<<HTML
<table width="75%" style="border: solid 1px black" cellspacing="0" cellpadding="0"> <table width="75%" style="border: solid 1px black" cellspacing="0" cellpadding="0">
@ -383,7 +383,7 @@ class Help
return self::display_help($page.'</table>'); return self::display_help($page.'</table>');
} }
public static function levels() public static function levels(): string|false
{ {
$rows = []; $rows = [];
@ -510,7 +510,7 @@ class Help
return self::display_help($page); 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', [ return render('layouts/help', [
'content' => $content, 'content' => $content,

View File

@ -17,7 +17,6 @@ use DragonKnight\Router;
use function DragonKnight\db; use function DragonKnight\db;
use function DragonKnight\ul_from_validate_errors; use function DragonKnight\ul_from_validate_errors;
use function DragonKnight\user;
use function DragonKnight\validate; use function DragonKnight\validate;
class Install class Install
@ -38,7 +37,7 @@ class Install
/** /**
* First page - show warnings and gather info. * First page - show warnings and gather info.
*/ */
public static function first() public static function first(): string
{ {
return <<<HTML return <<<HTML
<html> <html>
@ -58,7 +57,7 @@ class Install
/** /**
* Set up database tables. * Set up database tables.
*/ */
public static function second() public static function second(): string
{ {
if (! is_dir($path = getcwd().'/db')) { if (! is_dir($path = getcwd().'/db')) {
if (mkdir($path, 0777, true) === false) { if (mkdir($path, 0777, true) === false) {
@ -686,7 +685,7 @@ class Install
/** /**
* Gather user info for admin account. * Gather user info for admin account.
*/ */
public static function third() public static function third(): string
{ {
return <<<HTML return <<<HTML
<html> <html>
@ -715,7 +714,7 @@ class Install
/** /**
* Final page: insert new user row, congratulate the person on a job well done. * 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; $post ??= $_POST;
$form = validate($post, [ $form = validate($post, [
@ -764,11 +763,11 @@ class Install
/** /**
* Call Home public static function. * 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 <a href=\"/\">the game</a>.'); exit('Dragon Knight was unable to send your URL. Please go back and try again, or just continue on to <a href=\"/\">the game</a>.');
}*/ }
return <<<HTML return <<<HTML
<html> <html>

View File

@ -22,6 +22,7 @@ use function DragonKnight\get_item;
use function DragonKnight\get_town_by_id; use function DragonKnight\get_town_by_id;
use function DragonKnight\is_post; use function DragonKnight\is_post;
use function DragonKnight\page_title; use function DragonKnight\page_title;
use function DragonKnight\pretty_date;
use function DragonKnight\redirect; use function DragonKnight\redirect;
use function DragonKnight\render; use function DragonKnight\render;
use function DragonKnight\user; use function DragonKnight\user;
@ -44,7 +45,7 @@ class Towns
/** /**
* Spit out the main town page. * 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); $town = get_town_by_xy(user()->longitude, user()->latitude);
if ($town === false) { if ($town === false) {
@ -98,13 +99,14 @@ class Towns
* Staying at the inn resets all expendable stats to their max values. * Staying at the inn resets all expendable stats to their max values.
* GET/POST /inn. * GET/POST /inn.
*/ */
public static function inn() public static function inn(): string
{ {
$town = get_town_by_xy(user()->longitude, user()->latitude); $town = get_town_by_xy(user()->longitude, user()->latitude);
if ($town === false) { if ($town === false) {
exit('Cheat attempt detected.<br><br>Get a life, loser.'); exit('Cheat attempt detected.<br><br>Get a life, loser.');
} }
$page = '';
if (user()->gold < $town['innprice']) { if (user()->gold < $town['innprice']) {
$page = <<<HTML $page = <<<HTML
You do not have enough gold to stay at this Inn tonight. <br><br> You do not have enough gold to stay at this Inn tonight. <br><br>
@ -140,7 +142,7 @@ class Towns
* redirects to home. * redirects to home.
* GET /shop. * GET /shop.
*/ */
public static function shop() public static function shop(): string
{ {
$town = get_town_by_xy(user()->longitude, user()->latitude); $town = get_town_by_xy(user()->longitude, user()->latitude);
if ($town === false) { if ($town === false) {
@ -193,7 +195,7 @@ class Towns
/** /**
* Confirm user's intent to purchase item. * 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); $town = get_town_by_xy(user()->longitude, user()->latitude);
if ($town === false) { if ($town === false) {
@ -205,6 +207,7 @@ class Towns
$item = get_item($id); $item = get_item($id);
$can_afford = user()->gold >= $item['buycost']; $can_afford = user()->gold >= $item['buycost'];
$page = '';
if (! $can_afford) { if (! $can_afford) {
$page = <<<HTML $page = <<<HTML
You do not have enough gold to buy <b>{$item['name']}</b>.<br><br> You do not have enough gold to buy <b>{$item['name']}</b>.<br><br>
@ -312,7 +315,7 @@ class Towns
/** /**
* List maps the user can buy. * List maps the user can buy.
*/ */
public static function maps() public static function maps(): string
{ {
$page = <<<HTML $page = <<<HTML
Buying maps will put the town in your Travel To box, and it won't cost you as many TP to get there.<br><br> Buying maps will put the town in your Travel To box, and it won't cost you as many TP to get there.<br><br>
@ -363,6 +366,7 @@ class Towns
redirect('/maps'); redirect('/maps');
} }
$page = '';
if (user()->gold < $town['mapprice']) { if (user()->gold < $town['mapprice']) {
$page = <<<HTML $page = <<<HTML
You do not have enough gold to buy this map.<br><br> You do not have enough gold to buy this map.<br><br>
@ -408,6 +412,7 @@ class Towns
$mapped = explode(',', user()->towns); $mapped = explode(',', user()->towns);
$travelled = false; $travelled = false;
$page = '';
if ($use_points && ! in_array($id, $mapped)) { if ($use_points && ! in_array($id, $mapped)) {
// trying to teleport to this town when it is not mapped // trying to teleport to this town when it is not mapped
redirect('/'); redirect('/');

View File

@ -13,7 +13,7 @@ declare(strict_types=1);
namespace DragonKnight; namespace DragonKnight;
Lib::env_load(getcwd().'/.env'); env_load(getcwd().'/.env');
$uri = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $uri = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$GLOBALS['cache'] = []; $GLOBALS['cache'] = [];
@ -27,50 +27,60 @@ define('ADMIN_BULK_DATA_STRUCTS', [
]); ]);
if (! file_exists((getcwd().'/.installed')) && $uri[0] !== 'install') { // need to install the game if (! file_exists((getcwd().'/.installed')) && $uri[0] !== 'install') { // need to install the game
Lib::redirect('/install'); redirect('/install');
} elseif (file_exists((getcwd().'/.installed')) && $uri[0] === 'install') { // game is installed, go play! return;
Lib::redirect('/'); }
} elseif (file_exists((getcwd().'/.installed')) && $uri[0] !== 'install') { // boostrap the game if (file_exists((getcwd().'/.installed')) && $uri[0] === 'install') { // game is installed, go play!
if (! Lib::env('game_open')) { redirect('/');
echo Lib::render('The game is currently closed for maintanence. Please check back later.'); return;
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');
}
} }
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');

View File

@ -13,6 +13,8 @@ declare(strict_types=1);
namespace DragonKnight\Models; namespace DragonKnight\Models;
use function DragonKnight\db;
class Model class Model
{ {
protected string $table_name = ''; protected string $table_name = '';

View File

@ -13,6 +13,9 @@ declare(strict_types=1);
namespace DragonKnight\Models; namespace DragonKnight\Models;
use function DragonKnight\db;
use function DragonKnight\get_spells_from_list;
class User extends Model class User extends Model
{ {
protected string $table_name = 'users'; protected string $table_name = 'users';

View File

@ -41,7 +41,7 @@ class Router
* Example: * Example:
* `$r->add($routes, 'GET', '/posts/:id', function($id) { echo "Viewing post $id"; });` * `$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->validateMethod($method);
$this->validateRoute($route); $this->validateRoute($route);
@ -106,7 +106,7 @@ class Router
/** /**
* Add a middleware function to the last inserted node's stack. * 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; $this->last_inserted_node['middleware'][] = $middleware;
@ -116,7 +116,7 @@ class Router
/** /**
* Shorthand to register a GET route. * 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); return $this->add('GET', $route, $handler);
} }
@ -124,7 +124,7 @@ class Router
/** /**
* Shorthand to register a POST route. * 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); return $this->add('POST', $route, $handler);
} }
@ -132,7 +132,7 @@ class Router
/** /**
* Shorthand to register a PUT route. * 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); return $this->add('PUT', $route, $handler);
} }
@ -140,7 +140,7 @@ class Router
/** /**
* Shorthand to register a DELETE route. * 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); return $this->add('DELETE', $route, $handler);
} }
@ -148,7 +148,7 @@ class Router
/** /**
* Shorthand to register a PATCH route. * 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); return $this->add('PATCH', $route, $handler);
} }
@ -156,7 +156,7 @@ class Router
/** /**
* Register multiple verbs to the same route. * 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) { foreach ($methods as $method) {
$this->add($method, $route, $handler); $this->add($method, $route, $handler);
@ -168,7 +168,7 @@ class Router
/** /**
* Register all verbs to the same route. * 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) { foreach (self::VALID_METHODS as $method) {
$this->add($method, $route, $handler); $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. * 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); return $this->many(['GET', 'POST'], $route, $handler);
} }