2024-07-11 17:21:33 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
The app container exists to be a dependency container for the game.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class App
|
|
|
|
{
|
|
|
|
public static Database $db;
|
2024-07-13 23:15:25 -05:00
|
|
|
private static string $dbPath;
|
2024-07-11 17:21:33 -05:00
|
|
|
public static Request $req;
|
2024-07-15 19:54:50 -05:00
|
|
|
public static Auth $auth;
|
2024-07-15 14:24:13 -05:00
|
|
|
public static array $s = []; // game settings
|
2024-07-17 12:51:50 -05:00
|
|
|
public static array $flashes = []; // flash messages
|
2024-07-11 17:21:33 -05:00
|
|
|
|
|
|
|
public function __construct(string $dbPath)
|
|
|
|
{
|
|
|
|
self::$req = new Request(); // the current request
|
|
|
|
self::$db = new Database($dbPath); // the database
|
2024-07-13 23:15:25 -05:00
|
|
|
self::$dbPath = $dbPath; // the database path
|
2024-07-15 14:24:13 -05:00
|
|
|
|
2024-07-17 12:51:50 -05:00
|
|
|
// stuff that can only be loaded if the database is installed
|
|
|
|
if (INSTALLED) {
|
|
|
|
// load game settings
|
|
|
|
$s = self::$db->q('SELECT * FROM settings WHERE id = 1;');
|
|
|
|
self::$s = $s ? $s->fetch() : [];
|
2024-07-15 19:54:50 -05:00
|
|
|
|
2024-07-17 12:51:50 -05:00
|
|
|
// init authentication
|
|
|
|
self::$auth = new Auth();
|
|
|
|
}
|
|
|
|
|
|
|
|
// load flash messages
|
|
|
|
self::$flashes = $_SESSION['flash'] ?? [];
|
2024-07-13 23:15:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function performDatabaseReset(): void
|
|
|
|
{
|
|
|
|
if (file_exists(self::$dbPath)) {
|
|
|
|
unlink(self::$dbPath);
|
|
|
|
self::$db = new Database(self::$dbPath);
|
|
|
|
}
|
2024-07-11 17:21:33 -05:00
|
|
|
}
|
2024-07-15 19:54:50 -05:00
|
|
|
|
|
|
|
public static function auth(): bool
|
|
|
|
{
|
|
|
|
return self::$auth->good();
|
|
|
|
}
|
2024-07-17 12:51:50 -05:00
|
|
|
|
|
|
|
public static function flash(string $key, mixed $value = null): mixed
|
|
|
|
{
|
|
|
|
// get a flash message
|
|
|
|
if ($value === null) return self::$flashes[$key] ?? null;
|
|
|
|
|
|
|
|
// set a flash message
|
|
|
|
$_SESSION['flash'][$key] = $value;
|
|
|
|
self::$flashes[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
// clean up flash messages
|
|
|
|
$_SESSION['flash'] = [];
|
|
|
|
}
|
2024-07-11 17:21:33 -05:00
|
|
|
}
|