Build out new table generator

This commit is contained in:
Sky Johnson 2024-12-19 23:16:59 -06:00
parent aca8bcc3fe
commit 2074a09663
5 changed files with 112 additions and 59 deletions

View File

@ -1,5 +1,6 @@
:root {
--font-size: 16px;
--font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
* {
@ -10,7 +11,7 @@
html {
font-size: var(--font-size);
font-family: sans-serif;
font-family: var(--font-family);
}
body {
@ -49,6 +50,37 @@ main > section {
table {
width: 100%;
border-collapse: collapse;
outline-width: none;
font-family: var(--font-family);
border: 1px solid rgba(0, 0, 0, 0.1);
& > caption {
margin: 1rem;
}
& :is(td,th) {
border: 1px solid rgba(0, 0, 0, 0.4);
padding: 0.5rem 1rem;
}
& thead tr {
background: rgba(0, 0, 0, 0.1);
}
& tbody tr:nth-of-type(even) {
background: rgba(0, 0, 0, 0.1);
&:hover { background: rgba(0, 0, 0, 0.2); }
}
td:hover {
color: white;
background: rgba(0, 0, 0, 0.1);
}
tr:hover {
background: rgba(0, 0, 0, 0.2);
}
}
a {

View File

@ -5,10 +5,11 @@
namespace Admin;
use Router;
use SQLite3Result;
function register_routes(Router $r): Router
{
if (user()->authlevel === 1) {
if (user() !== false && user()->authlevel === 1) {
$r->get('/admin', 'Admin\donothing');
$r->form('/admin/main', 'Admin\primary');
@ -111,21 +112,9 @@ function primary(): string
*/
function items(): string
{
$items = db()->query('SELECT id, name FROM items ORDER BY id;');
$page = "<h2>Edit Items</h2>Click an item's name to edit it.<br><br><table>\n";
$hasItems = false;
while ($row = $items->fetchArray(SQLITE3_BOTH)) {
$hasItems = true;
$page .= <<<HTML
<tr>
<td width="8%" style="background-color: #eeeeee;">{$row["id"]}</td>
<td style="background-color: #eeeeee;"><a href="/admin/items/{$row['id']}" hx-get="/admin/items/{$row['id']}" hx-target="#main">{$row["name"]}</a></td>
</tr>
HTML;
}
if (!$hasItems) $page .= "<tr><td width=\"8%\" style=\"background-color: #eeeeee;\">No items found.</td></tr>\n";
$items = db()->query('SELECT * FROM items ORDER BY id;');
$page = "<h2>Edit Items</h2>Click an item's name or ID to edit it.<br><br><table>\n";
$page .= build_bulk_table($items, 'name', '/admin/items');
page_title('Admin: Items');
return \Render\content($page . '</table>', 'layouts/admin');
@ -174,21 +163,9 @@ function edit_item(int $id): string
*/
function drops()
{
$drops = db()->query('SELECT * FROM drops ORDER BY id;');
$page = "<h2>Edit Drops</h2>Click an item's name to edit it.<br><br><table>\n";
$drops = db()->query('SELECT id, name FROM drops ORDER BY id;');
$has_drops = false;
while ($row = $drops->fetchArray(SQLITE3_ASSOC)) {
$has_drops = true;
$page .= <<<HTML
<tr>
<td width="8%" style="background-color: #eeeeee;">{$row["id"]}</td>
<td style="background-color: #eeeeee;"><a href="/admin/drops/{$row['id']}" hx-get="/admin/drops/{$row['id']}" hx-target="#main">{$row["name"]}</a></td>
</tr>
HTML;
}
if (!$has_drops) { $page .= "<tr><td width=\"8%\" style=\"background-color: #eeeeee;\">No drops found.</td></tr>\n"; }
$page .= build_bulk_table($drops, 'name', '/admin/drops');
page_title('Admin: Drops');
return \Render\content($page . '</table>', 'layouts/admin');
@ -870,3 +847,41 @@ function addnews()
display_admin($page, "Add News");
}
/**
* Build an HTML table containing all columns and rows of a given data structure. Takes a SQLiteResult3 of a SELECT *
* query.
*/
function build_bulk_table(SQLite3Result $query_data, string $edit_column, string $edit_link): string
{
$data = [];
while ($row = $query_data->fetchArray(SQLITE3_ASSOC)) $data[] = $row;
if (empty($data)) return 'No data.';
$columns = array_diff(array_keys($data[0]), ['password']); // Filter columns inline
$html = '<table><colgroup>';
foreach ($columns as $_) $html .= '<col>';
$html .= '</colgroup><thead><tr>';
foreach ($columns as $column) {
if ($column === 'id') $column = 'ID';
$html .= '<th>' . make_safe(ucfirst($column)) . '</th>';
}
$html .= '</tr></thead><tbody>';
foreach ($data as $row) {
$html .= '<tr>';
foreach ($columns as $column) {
$name = make_safe($row[$column]);
if (in_array($column, ['id', $edit_column])) {
$html .= <<<HTML
<td><a href="{$edit_link}/{$row['id']}" hx-get="{$edit_link}/{$row['id']}" hx-target="#main">{$name}</a></td>
HTML;
} else {
$html .= "<td>$name</td>";
}
}
$html .= '</tr>';
}
return $html . '</tbody></table>';
}

View File

@ -21,7 +21,7 @@ function register_routes(Router $r): Router
*/
function first()
{
echo <<<HTML
return <<<HTML
<html>
<head>
<title>Dragon Knight Installation</title>
@ -43,7 +43,7 @@ function second()
{
if (file_exists('../database.db')) unlink('../database.db');
echo "<html><head><title>Dragon Knight Installation</title></head><body><b>Dragon Knight Installation: Page Two</b><br><br>";
$page = "<html><head><title>Dragon Knight Installation</title></head><body><b>Dragon Knight Installation: Page Two</b><br><br>";
$query = db()->exec(<<<SQL
CREATE TABLE babble (
@ -54,7 +54,7 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Babble', 'create');
$page .= table_status_msg($query === true, 'Babble', 'create');
$query = db()->exec(<<<SQL
CREATE TABLE drops (
@ -67,7 +67,7 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Drops', 'create');
$page .= table_status_msg($query === true, 'Drops', 'create');
$query = db()->exec(<<<SQL
INSERT INTO drops VALUES
@ -105,7 +105,7 @@ function second()
(32, 'Fortune Drop', 5, 1, 'goldbonus,10', 'X');
SQL);
echo table_status_msg($query === true, 'Drops', 'populate');
$page .= table_status_msg($query === true, 'Drops', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE forum (
@ -120,7 +120,7 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Forum', 'create');
$page .= table_status_msg($query === true, 'Forum', 'create');
$query = db()->exec(<<<SQL
CREATE TABLE items (
@ -133,7 +133,7 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Items', 'create');
$page .= table_status_msg($query === true, 'Items', 'create');
$query = db()->exec(<<<SQL
INSERT INTO items VALUES
@ -172,7 +172,7 @@ function second()
(33, 3, 'Destiny Aegis', 25000, 100, 'maxhp,50');
SQL);
echo table_status_msg($query === true, 'Drops', 'populate');
$page .= table_status_msg($query === true, 'Drops', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE levels (
@ -201,7 +201,7 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Levels', 'create');
$page .= table_status_msg($query === true, 'Levels', 'create');
$query = db()->exec(<<<SQL
INSERT INTO levels VALUES
@ -307,7 +307,7 @@ function second()
(100, 16777215, 0, 0, 0, 0, 0, 0, 16777215, 0, 0, 0, 0, 0, 0, 16777215, 0, 0, 0, 0, 0, 0);
SQL);
echo table_status_msg($query === true, 'Levels', 'populate');
$page .= table_status_msg($query === true, 'Levels', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE monsters (
@ -323,7 +323,7 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Monsters', 'create');
$page .= table_status_msg($query === true, 'Monsters', 'create');
$query = db()->exec(<<<SQL
INSERT INTO monsters VALUES
@ -480,7 +480,7 @@ function second()
(151, 'Lucifuge', 600, 600, 400, 50, 10000, 10000, 2);
SQL);
echo table_status_msg($query === true, 'Monsters', 'populate');
$page .= table_status_msg($query === true, 'Monsters', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE news (
@ -491,11 +491,11 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'News', 'create');
$page .= table_status_msg($query === true, 'News', 'create');
$query = db()->exec("INSERT INTO news (content) VALUES ('This is the first news post. Please use the admin control panel to add another one and make this one go away.');");
echo table_status_msg($query === true, 'News', 'populate');
$page .= table_status_msg($query === true, 'News', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE spells (
@ -507,7 +507,7 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Spells', 'create');
$page .= table_status_msg($query === true, 'Spells', 'create');
$query = db()->exec(<<<SQL
INSERT INTO spells VALUES
@ -532,7 +532,7 @@ function second()
(19, 'Barrier', 30, 50, 5);
SQL);
echo table_status_msg($query === true, 'Spells', 'populate');
$page .= table_status_msg($query === true, 'Spells', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE towns (
@ -547,7 +547,7 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Towns', 'create');
$page .= table_status_msg($query === true, 'Towns', 'create');
$query = db()->exec(<<<SQL
INSERT INTO towns VALUES
@ -561,7 +561,7 @@ function second()
(8, 'Endworld', -250, -250, 125, 9000, 160, '16,27,33');
SQL);
echo table_status_msg($query === true, 'Towns', 'populate');
$page .= table_status_msg($query === true, 'Towns', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE users (
@ -618,10 +618,10 @@ function second()
);
SQL);
echo table_status_msg($query === true, 'Users', 'create');
$page .= table_status_msg($query === true, 'Users', 'create');
$time = round((microtime(true) - START), 4);
echo "<br>Database setup complete in $time seconds.<br><br><a href=\"/install/third\">Click here to continue with installation.</a></body></html>";
return $page . "<br>Database setup complete in $time seconds.<br><br><a href=\"/install/third\">Click here to continue with installation.</a></body></html>";
}
/**
@ -629,7 +629,7 @@ function second()
*/
function third()
{
echo <<<HTML
return <<<HTML
<html>
<head>
<title>Dragon Knight Installation</title>
@ -678,7 +678,7 @@ function fourth()
file_put_contents('../.installed', date('Y-m-d H:i:s'));
echo <<<HTML
return <<<HTML
<html>
<head>
<title>Dragon Knight Installation</title>
@ -708,7 +708,7 @@ function fifth()
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>.');
}
echo <<<HTML
return <<<HTML
<html>
<head>
<title>Dragon Knight Installation</title>

View File

@ -26,8 +26,7 @@ function login()
if (is_post()) {
$form = validate($_POST, [
'username' => ['length:3-18', 'alpha-spaces'],
'password' => ['length:6-255'],
'remember' => ['bool']
'password' => ['length:6-255']
]);
if (!$form['valid']) exit(ul_from_validate_errors($form['errors']));

View File

@ -23,11 +23,18 @@ $uri = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
$GLOBALS['cache'] = [];
$GLOBALS['state'] = [];
if (!file_exists('../.installed') && $uri[0] !== 'install') {
/**
* These are table names whose data we want to be able to edit in the admin panel in bulk.
*/
define('ADMIN_BULK_DATA_STRUCTS', [
'users', 'items', 'drops', 'towns', 'monsters', 'levels', 'spells'
]);
if (!file_exists('../.installed') && $uri[0] !== 'install') { // need to install the game
redirect('/install');
} elseif (file_exists(('../.installed')) && $uri[0] === 'install') {
} elseif (file_exists(('../.installed')) && $uri[0] === 'install') { // game is installed, go play!
redirect('/');
} elseif (file_exists(('../.installed')) && $uri[0] !== 'install') {
} elseif (file_exists(('../.installed')) && $uri[0] !== 'install') { // boostrap the game
if (!env('game_open')) {
echo Render\content('The game is currently closed for maintanence. Please check back later.');
exit;