Lots of rendering fixes, adjust user table, add settings, skins

This commit is contained in:
Sky Johnson 2024-12-16 19:58:46 -06:00
parent f7f6e7fb97
commit 3291c1caa6
12 changed files with 104 additions and 89 deletions

View File

@ -8,7 +8,11 @@ html {
}
body {
background-image: url('/img/background.jpg');
background-image: url('/img/backgrounds/classic.jpg');
&.skin-1 {
background-image: url('/img/backgrounds/snowstorm.jpg');
}
}
div#game-container {

View File

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 386 KiB

View File

@ -41,6 +41,8 @@ function first()
*/
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>";
$query = db()->exec(<<<SQL
@ -52,7 +54,7 @@ function second()
);
SQL);
echo $query === true ? 'Babble Box table created.<br>' : 'Error creating Babble Box table.';
echo table_status_msg($query === true, 'Babble', 'create');
$query = db()->exec(<<<SQL
CREATE TABLE control (
@ -72,11 +74,14 @@ function second()
);
SQL);
echo $query === true ? 'Control table created.<br>' : 'Error creating Control table.';
echo table_status_msg($query === true, 'Control', 'create');
$query = db()->exec("INSERT INTO control VALUES (1, 'Dragon Knight', 250, 1, {$_SERVER['SERVER_NAME']}, 'noreply@'.{$_SERVER['SERVER_NAME']}, 'Mage', 'Warrior', 'Paladin', 1, 1, 1, 1);");
$query = db()->query("INSERT INTO control VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [
1, 'Dragon Knight', 250, 1, $_SERVER['SERVER_NAME'], 'noreply@'.$_SERVER['SERVER_NAME'],
'Mage', 'Warrior', 'Paladin', 1, 1, 1, 1
]);
echo $query === true ? 'Control table populated.<br>' : 'Error populating Control table.';
echo table_status_msg($query !== false, 'Control', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE drops (
@ -89,7 +94,7 @@ function second()
);
SQL);
echo $query == true ? 'Drops table created.<br>' : 'Error creating Drops table.';
echo table_status_msg($query === true, 'Drops', 'create');
$query = db()->exec(<<<SQL
INSERT INTO drops VALUES
@ -127,7 +132,7 @@ function second()
(32, 'Fortune Drop', 5, 1, 'goldbonus,10', 'X');
SQL);
echo $query === true ? 'Drops table populated.<br>' : 'Error populating Drops table.';
echo table_status_msg($query === true, 'Drops', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE forum (
@ -142,7 +147,7 @@ function second()
);
SQL);
echo $query === true ? 'Forum table created.<br>' : 'Error creating Forum table.';
echo table_status_msg($query === true, 'Forum', 'create');
$query = db()->exec(<<<SQL
CREATE TABLE items (
@ -155,7 +160,7 @@ function second()
);
SQL);
echo $query === true ? 'Items table created.<br>' : 'Error creating Items table.';
echo table_status_msg($query === true, 'Items', 'create');
$query = db()->exec(<<<SQL
INSERT INTO items VALUES
@ -194,7 +199,7 @@ function second()
(33, 3, 'Destiny Aegis', 25000, 100, 'maxhp,50');
SQL);
echo $query === true ? 'Items table populated.<br>' : 'Error populating Items table.';
echo table_status_msg($query === true, 'Drops', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE levels (
@ -223,7 +228,7 @@ function second()
);
SQL);
echo $query === true ? 'Levels table created.<br>' : 'Error creating Levels table.';
echo table_status_msg($query === true, 'Levels', 'create');
$query = db()->exec(<<<SQL
INSERT INTO levels VALUES
@ -329,7 +334,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 $query === true ? 'Levels table populated.<br>' : 'Error populating Levels table.';
echo table_status_msg($query === true, 'Levels', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE monsters (
@ -345,7 +350,7 @@ function second()
);
SQL);
echo $query === true ? 'Monsters table created.<br>' : 'Error creating Monsters table.';
echo table_status_msg($query === true, 'Monsters', 'create');
$query = db()->exec(<<<SQL
INSERT INTO monsters VALUES
@ -502,7 +507,7 @@ function second()
(151, 'Lucifuge', 600, 600, 400, 50, 10000, 10000, 2);
SQL);
echo $query === true ? 'Monsters table populated.<br>' : 'Error populating Monsters table.';
echo table_status_msg($query === true, 'Monsters', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE news (
@ -513,11 +518,11 @@ function second()
);
SQL);
echo $query === true ? 'News table created.<br>' : 'Error creating News table.';
echo 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 $query === true ? 'News table populated.<br>' : 'Error populating News table.';
echo table_status_msg($query === true, 'News', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE spells (
@ -529,7 +534,7 @@ function second()
);
SQL);
echo $query === true ? 'Spells table created.<br>' : 'Error creating Spells table.';
echo table_status_msg($query === true, 'Spells', 'create');
$query = db()->exec(<<<SQL
INSERT INTO spells VALUES
@ -554,7 +559,7 @@ function second()
(19, 'Barrier', 30, 50, 5);
SQL);
echo $query === true ? 'Spells table populated.<br>' : 'Error populating Spells table.';
echo table_status_msg($query === true, 'Spells', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE towns (
@ -569,7 +574,7 @@ function second()
);
SQL);
echo $query === true ? 'Towns table created.<br>' : 'Error creating Towns table.';
echo table_status_msg($query === true, 'Towns', 'create');
$query = db()->exec(<<<SQL
INSERT INTO towns VALUES
@ -583,7 +588,7 @@ function second()
(8, 'Endworld', -250, -250, 125, 9000, 160, '16,27,33');
SQL);
echo $query === true ? 'Towns table populated.<br>' : 'Error populating Towns table.';
echo table_status_msg($query === true, 'Towns', 'populate');
$query = db()->exec(<<<SQL
CREATE TABLE users (
@ -635,11 +640,12 @@ function second()
`slot3name` TEXT NOT NULL default 'None',
`dropcode` INTEGER NOT NULL default 0,
`spells` TEXT NOT NULL default '0',
`towns` TEXT NOT NULL default '0'
`towns` TEXT NOT NULL default '0',
`game_skin` INTEGER NOT NULL DEFAULT 0
);
SQL);
echo $query === true ? 'Users table created.<br>' : 'Error creating Users table.';
echo 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>";
@ -691,7 +697,7 @@ function fourth()
$form = $form['data'];
if (db()->query(
"INSERT INTO users (username, password, email, verify, charclass, authlevel) VALUES (?, ?, ?, 1, ?, 1)",
"INSERT INTO users (username, password, email, verify, charclass, authlevel) VALUES (?, ?, ?, 'g2g', ?, 1)",
[$form['username'], password_hash($form['password'], PASSWORD_ARGON2ID), $form['email'], $form['charclass']]
) === false) {
exit("Failed to create user.");
@ -742,3 +748,17 @@ function fifth()
</html>
HTML;
}
function table_status_msg(bool $condition, string $table_name, string $verb): string
{
$verb = match($verb) {
'create' => ['created', 'creating'],
'populate' => ['populated', 'populating']
};
if ($condition === false) {
return "Error {$verb[1]} $table_name table. (".db()->lastErrorMsg().")<br>";
}
return "$table_name table {$verb[0]}.<br>";
}

View File

@ -12,6 +12,7 @@ function register_routes(Router $r): Router
$r->form('/lostpassword', 'Users\lostpassword');
$r->form('/changepassword', 'Users\changepassword');
$r->form('/verify', 'Users\verify');
$r->form('/settings', 'Users\settings');
return $r;
}
@ -183,6 +184,26 @@ function changepassword()
display(render('changepassword'), "Change Password", true, false, false);
}
function settings()
{
global $userrow;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$form = validate($_POST, [
'game_skin' => ['in:0,1']
]);
if (!$form['valid']) exit(ul_from_validate_errors($form['errors']));
$form = $form['data'];
db()->query('UPDATE users SET game_skin=? WHERE id=?;', [$form['game_skin'], $userrow['id']]);
$alert = '<div class="alert">Settings updated</div>';
display($alert . render('settings'), "Account Settings");
}
display(render('settings'), "Account Settings");
}
function sendpassemail($emailaddress, $password)
{
global $controlrow;

View File

@ -41,7 +41,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') {
}
// Force verify if the user isn't verified yet.
if ($controlrow['verifyemail'] && $userrow['verify'] !== 'g2g' && $uri[0] !== 'verify') {
if ($controlrow['verifyemail'] && $userrow['verify'] !== 'g2g' && !in_array($uri[0], ['verify', 'logout'])) {
redirect('/verify');
}

View File

@ -28,12 +28,18 @@ class Database extends SQLite3
public function query(string $query, array $params = []): SQLite3Result|false
{
$p = strpos($query, '?') !== false;
$stmt = $this->prepare($query);
foreach ($params ?? [] as $k => $v) $stmt->bindValue($p ? $k + 1 : $k, $v, $this->getSQLiteType($v));
try {
$stmt = $this->prepare($query);
foreach ($params ?? [] as $k => $v) $stmt->bindValue($p ? $k + 1 : $k, $v, $this->getSQLiteType($v));
} catch (Exception $e) {
exit("Failed to prepare query ($query): ".$this->lastErrorMsg());
}
$start = microtime(true);
$r = $stmt->execute();
$this->log($query, microtime(true) - $start);
$error = '';
$r = $stmt->execute();
$this->log($query, microtime(true) - $start, $error);
return $r;
}
@ -68,11 +74,11 @@ class Database extends SQLite3
/**
* Log the query, including the time it took. Increment the query counter.
*/
private function log(string $query, float $time_taken): void
private function log(string $query, float $time_taken, string $error = ''): void
{
$this->count++;
$this->query_time += $time_taken;
$this->log[] = [$query, $time_taken];
$this->log[] = [$query, $time_taken, $error];
}
/**

View File

@ -184,6 +184,7 @@ function display($content, $title, bool $topnav = true, bool $leftnav = true, bo
"dkgamename" => $controlrow["gamename"],
"title" => $title,
"content" => $content,
"game_skin" => $userrow['game_skin'] ??= '0',
'rightnav' => $rightnav ? render('rightnav', ['user' => $userrow]) : '',
"leftnav" => $leftnav ? render('leftnav', ['user' => $userrow]) : '',
"topnav" => $topnav,

View File

@ -26,6 +26,7 @@
<a href="/">Home</a><br>
<?= $user['forumslink'] ?>
<?= $user['adminlink'] ?>
<a href="/settings">Settings</a><br>
<a href="/changepassword">Change Password</a><br>
<a href="/logout">Log Out</a><br>
<a href="/help">Help</a>

View File

@ -1,63 +1,9 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $title ?></title>
<style type="text/css">
body {
background-image: url('/img/background.jpg');
color: black;
font: 11px verdana;
}
table {
border-style: none;
padding: 0px;
font: 11px verdana;
}
td {
border-style: none;
padding: 3px;
vertical-align: top;
}
td.top {
border-bottom: solid 2px black;
}
td.left {
width: 150px;
border-right: solid 2px black;
}
td.right {
width: 150px;
border-left: solid 2px black;
}
a {
color: #663300;
text-decoration: none;
font-weight: bold;
}
a:hover {
color: #330000;
}
.small {
font: 10px verdana;
}
.highlight {
color: red;
}
.light {
color: #999999;
}
.title {
border: solid 1px black;
background-color: #eeeeee;
font-weight: bold;
padding: 5px;
margin: 3px;
}
.copyright {
border: solid 1px black;
background-color: #eeeeee;
font: 10px verdana;
}
</style>
<link rel="stylesheet" href="/css/dk.css">
</head>
<body>
<?= $content ?>

View File

@ -19,7 +19,7 @@
}
</script>
</head>
<body>
<body class="skin-<?= $game_skin ?>">
<div id="game-container">
<header>
<img id="logo" src="/img/logo.gif" alt="<?= $dkgamename ?>" title="<?= $dkgamename ?>">
@ -42,7 +42,11 @@
<?php
if (!empty($querylog)) {
echo '<pre>';
foreach ($querylog as $record) echo '<div>['.round($record[1], 2)."s] {$record[0]}</div>";
foreach ($querylog as $record) {
$query_string = str_replace(["\r\n", "\n", "\r"], ' ', $record[0]);
$error_string = !empty($record[2]) ? '// '.$record[2] : '';
echo '<div>['.round($record[1], 2)."s] {$query_string}{$error_string}</div>";
}
echo '</pre>';
}
?>

12
templates/settings.php Normal file
View File

@ -0,0 +1,12 @@
<h1>Account Settings</h1>
<p>Here you can change some basic settings for your account.</p>
<form action="/settings" method="post">
<label for="game_skin">Game Skin</label>
<select id="game_skin" name="game_skin">
<option value="0">Default</option>
<option value="1">Snowstorm</option>
</select>
<button type="submit">Save</button>
</form>