Fix some links, onlinetime update

This commit is contained in:
Sky Johnson 2024-12-13 15:24:08 -06:00
parent 5235239c1c
commit d10d69fb1a
5 changed files with 24 additions and 4 deletions

View File

@ -35,7 +35,7 @@ main > section {
padding: 4px; padding: 4px;
} }
main > section > section:not(:last-child) { main > section > section {
margin-bottom: 2rem; margin-bottom: 2rem;
} }

View File

@ -9,6 +9,9 @@ class Database extends SQLite3
public array $log = []; public array $log = [];
public float $query_time = 0; public float $query_time = 0;
/**
* Make a big, chonky connection to our SQLite database.
*/
public function __construct(string $db_path) public function __construct(string $db_path)
{ {
parent::__construct($db_path); parent::__construct($db_path);
@ -18,6 +21,10 @@ class Database extends SQLite3
parent::exec('PRAGMA temp_store = MEMORY'); parent::exec('PRAGMA temp_store = MEMORY');
} }
/**
* Query the database. Uses prepared statements, and your choise of placeholder syntax. (:key or ?) Logs
* the query in the class.
*/
public function query(string $query, array $params = []): SQLite3Result|false public function query(string $query, array $params = []): SQLite3Result|false
{ {
$p = strpos($query, '?') !== false; $p = strpos($query, '?') !== false;
@ -31,6 +38,9 @@ class Database extends SQLite3
return $r; return $r;
} }
/**
* Perform a result-less query on the database.
*/
public function exec(string $query): bool public function exec(string $query): bool
{ {
$start = microtime(true); $start = microtime(true);
@ -39,6 +49,10 @@ class Database extends SQLite3
return $r; return $r;
} }
/**
* Determines whether a given value exists in a given column in a given table. Can optionally make it
* case-sensitive!
*/
public function exists(string $table, string $column, mixed $value, bool $case_insensitive = true): bool public function exists(string $table, string $column, mixed $value, bool $case_insensitive = true): bool
{ {
if ($case_insensitive) { if ($case_insensitive) {
@ -51,6 +65,9 @@ class Database extends SQLite3
return $result->fetchArray(SQLITE3_NUM) !== false; return $result->fetchArray(SQLITE3_NUM) !== false;
} }
/**
* 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): void
{ {
$this->count++; $this->count++;
@ -58,6 +75,9 @@ class Database extends SQLite3
$this->log[] = [$query, $time_taken]; $this->log[] = [$query, $time_taken];
} }
/**
* Return the correct SQLite3 type for the given value.
*/
private function getSQLiteType(mixed $value): int private function getSQLiteType(mixed $value): int
{ {
return match (true) { return match (true) {

View File

@ -25,7 +25,7 @@ function move()
$town = get_town_by_xy($longitude, $latitude); $town = get_town_by_xy($longitude, $latitude);
if ($town !== false) { if ($town !== false) {
require_once __DIR__ . '/towns.php'; require_once __DIR__ . '/towns.php';
travelto($town['id'], false); Towns\travelto($town['id'], false);
return; return;
} }

View File

@ -233,7 +233,7 @@ function checkcookies()
} }
$row = $query->fetchArray(SQLITE3_ASSOC); $row = $query->fetchArray(SQLITE3_ASSOC);
set_cookie('dkgame', implode(" ", $theuser), (int) $theuser[3] === 1 ? time() + 31536000 : 0); set_cookie('dkgame', implode(" ", $theuser), (int) $theuser[3] === 1 ? time() + 31536000 : 0);
db()->exec('UPDATE users SET onlinetime = CURRENT_TIMESTAMP WHERE id = ?;', [$theuser[0]]); db()->query('UPDATE users SET onlinetime = CURRENT_TIMESTAMP WHERE id = ?;', [$theuser[0]]);
} }
return $row; return $row;

View File

@ -28,7 +28,7 @@ $template = <<<HTML
<a href="/">Home</a><br> <a href="/">Home</a><br>
{{forumslink}} {{forumslink}}
{{adminlink}} {{adminlink}}
<a href="users.php?do=changepassword">Change Password</a><br> <a href="/changepassword">Change Password</a><br>
<a href="/logout">Log Out</a><br> <a href="/logout">Log Out</a><br>
<a href="/help">Help</a> <a href="/help">Help</a>
</section> </section>