Dragon-Knight/public/index.php
2025-08-15 09:34:27 -04:00

95 lines
2.9 KiB
PHP

<?php
/*
* This file is a part of the Dragon-Knight project.
*
* Copyright (c) 2024-present Sharkk
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/
namespace DragonKnight;
use DragonKnight\Actions\Fight;
use DragonKnight\Actions\Forum;
use DragonKnight\Actions\Help;
use DragonKnight\Actions\Install;
use DragonKnight\Actions\Towns;
use DragonKnight\Actions\Users;
use DragonKnight\Actions\Admin;
ini_set('display_errors', 1);
error_reporting(E_ALL);
set_time_limit(0);
ignore_user_abort(true);
ini_set('max_execution_time', 0);
ini_set('memory_limit', '-1'); // Unlimited memory usage
if (! $autoloader = require file_exists(__DIR__.'/../vendor/autoload.php') ? __DIR__.'/../vendor/autoload.php' : __DIR__.'/../../autoload.php') {
throw new \Exception('Composer autoloader not found. Run `composer install` and try again.');
}
env_load(__DIR__.'/../.env');
// Check required extensions
$requiredExtensions = [
'sqlite3',
];
if ($missing = array_filter($requiredExtensions, fn($ext) => !extension_loaded($ext))) {
throw new \Exception(
'Required PHP extensions missing: ' . implode(', ', $missing) .
'. Please enable them in your PHP configuration.'
);
}
$uri = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
// Do an early return with babblebox data if that's what's being requested
if ($uri[0] === 'babblebox' && (isset($uri[1]) && $uri[1] === 'messages')) {
echo babblebox_messages();
exit;
}
$r = new Router;
$r->get(['/', '/index.php'], fn() => \DragonKnight\index());
$r->get('/register', fn() => \DragonKnight\Actions\Users::register());
$r->post('/move', fn() => \DragonKnight\Actions\Explore::move());
$r->get('/spell/:id', fn($id) => \DragonKnight\Actions\Heal::healspells($id));
$r->get('/character', fn() => \DragonKnight\show_character_info());
$r->get('/character/:id', fn($id) => \DragonKnight\show_character_info($id));
$r->get('/showmap', fn() => \DragonKnight\show_map());
$r->form('/babblebox', fn() => \DragonKnight\babblebox());
$r->get('/babblebox/messages', fn() => \DragonKnight\babblebox_messages());
Admin::register_routes($r);
Fight::register_routes($r);
Forum::register_routes($r);
Help::register_routes($r);
Install::register_routes($r);
Towns::register_routes($r);
Users::register_routes($r);
/*
NINJA! 🥷
*/
$r->get('/ninja', function () {
exit('NINJA! 🥷');
});
// [code, handler, params, middleware]
$l = $r->lookup($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
if (is_int($l)) {
error_log($path = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI']);
exit("Error: $l at `$path`");
}
if (! isset($l['handler'])) {
error_log($path = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI']);
exit("Error: Missing handler: $path");
}
echo render_response($uri, $l['handler'](...$l['params'] ?? []));