Store server list in the database.

This commit is contained in:
flash 2023-08-17 00:23:55 +00:00
parent a708067ba7
commit 1a4d2c0e39
8 changed files with 189 additions and 9 deletions

View file

@ -0,0 +1,26 @@
<?php
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigration;
final class CreateServersTable_20230817_001507 implements IDbMigration {
public function migrate(IDbConnection $conn): void {
$conn->execute('
CREATE TABLE servers (
server_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
server_name VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci",
server_details TEXT NOT NULL DEFAULT "" COLLATE "utf8mb4_unicode_520_ci",
server_order SMALLINT(6) NOT NULL DEFAULT "0",
server_java_address VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci",
server_java_version VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci",
server_bedrock_address VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci",
server_bedrock_version VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci",
server_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
server_deleted TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (server_id),
KEY servers_created_index (server_created),
KEY servers_deleted_index (server_deleted),
KEY servers_order_index (server_order)
) ENGINE=InnoDB COLLATE=utf8mb4_bin
');
}
}

View file

@ -42,7 +42,7 @@ $router->setDefaultErrorHandler(function($response, $request, $code, $text) use
])); ]));
}); });
(new HomeRoutes($templating, $userInfo))->register($router); (new HomeRoutes(new Servers($db), $templating, $userInfo))->register($router);
(new WhitelistRoutes(new Whitelist($db), $csrfp, $userInfo))->register($router); (new WhitelistRoutes(new Whitelist($db), $csrfp, $userInfo))->register($router);
$router->dispatch(); $router->dispatch();

View file

@ -5,6 +5,7 @@ use Index\Routing\IRouter;
class HomeRoutes { class HomeRoutes {
public function __construct( public function __construct(
private Servers $servers,
private Templating $templating, private Templating $templating,
private object $userInfo private object $userInfo
) {} ) {}
@ -54,6 +55,7 @@ class HomeRoutes {
$this->templating->setVar('whitelist_pending', floor($this->userInfo->mc_whitelisted / 300) === floor(time() / 300)); $this->templating->setVar('whitelist_pending', floor($this->userInfo->mc_whitelisted / 300) === floor(time() / 300));
return $this->templating->render('index', [ return $this->templating->render('index', [
'servers' => $this->servers->getServers(deleted: false),
'wladdform_username' => $name, 'wladdform_username' => $name,
]); ]);
} }

View file

@ -2,7 +2,7 @@
namespace Mince; namespace Mince;
use RuntimeException; use RuntimeException;
use Index\Serialisation\Serialiser; use Index\Serialisation\UriBase64;
class RemoteV2 { class RemoteV2 {
public function __construct( public function __construct(
@ -45,7 +45,7 @@ class RemoteV2 {
$input = "{$time}%{$method} {$path}%" . implode('#', $compare); $input = "{$time}%{$method} {$path}%" . implode('#', $compare);
return Serialiser::uriBase64()->serialise( return UriBase64::encode(
hash_hmac('sha256', $input, $this->secretKey, true) hash_hmac('sha256', $input, $this->secretKey, true)
); );
} }

97
src/ServerInfo.php Normal file
View file

@ -0,0 +1,97 @@
<?php
namespace Mince;
use Index\DateTime;
use Index\Data\IDbResult;
class ServerInfo {
private string $id;
private string $name;
private string $details;
private ?string $javaAddress;
private ?string $javaVersion;
private ?string $bedrockAddress;
private ?string $bedrockVersion;
private int $created;
private ?int $deleted;
public function __construct(IDbResult $result) {
$this->id = $result->getString(0);
$this->name = $result->getString(1);
$this->details = $result->getString(2);
$this->javaAddress = $result->isNull(3) ? null : $result->getString(3);
$this->javaVersion = $result->isNull(4) ? null : $result->getString(4);
$this->bedrockAddress = $result->isNull(5) ? null : $result->getString(5);
$this->bedrockVersion = $result->isNull(6) ? null : $result->getString(6);
$this->created = $result->getInteger(7);
$this->deleted = $result->isNull(8) ? null : $result->getInteger(8);
}
public function getId(): string {
return $this->id;
}
public function getName(): string {
return $this->name;
}
public function hasDetails(): bool {
return $this->details !== '';
}
public function getDetails(): string {
return $this->details;
}
public function hasJavaAddress(): bool {
return $this->javaAddress !== null;
}
public function getJavaAddress(): ?string {
return $this->javaAddress;
}
public function hasJavaVersion(): bool {
return $this->javaVersion !== null;
}
public function getJavaVersion(): ?string {
return $this->javaVersion;
}
public function hasBedrockAddress(): bool {
return $this->bedrockAddress !== null;
}
public function getBedrockAddress(): ?string {
return $this->bedrockAddress;
}
public function hasBedrockVersion(): bool {
return $this->bedrockVersion !== null;
}
public function getBedrockVersion(): ?string {
return $this->bedrockVersion;
}
public function getCreatedTime(): int {
return $this->created;
}
public function getCreatedAt(): DateTime {
return DateTime::fromUnixTimeSeconds($this->created);
}
public function isDeleted(): bool {
return $this->deleted !== null;
}
public function getDeletedTime(): ?int {
return $this->deleted;
}
public function getDeletedAt(): ?DateTime {
return $this->deleted === null ? null : DateTime::fromUnixTimeSeconds($this->deleted);
}
}

37
src/Servers.php Normal file
View file

@ -0,0 +1,37 @@
<?php
namespace Mince;
use Index\Data\DbStatementCache;
use Index\Data\IDbConnection;
class Servers {
//private IDbConnection $dbConn;
private DbStatementCache $cache;
public function __construct(IDbConnection $dbConn) {
//$this->dbConn = $dbConn;
$this->cache = new DbStatementCache($dbConn);
}
public function getServers(
?bool $deleted = null
): array {
$hasDeleted = $deleted !== null;
$query = 'SELECT server_id, server_name, server_details, server_java_address, server_java_version, server_bedrock_address, server_bedrock_version, UNIX_TIMESTAMP(server_created), UNIX_TIMESTAMP(server_deleted) FROM servers';
if($hasDeleted)
$query .= sprintf(' WHERE server_deleted %s NULL', $deleted ? 'IS NOT ' : 'IS');
$query .= ' ORDER BY server_order';
$stmt = $this->cache->get($query);
$stmt->execute();
$result = $stmt->getResult();
$servers = [];
while($result->next())
$servers[] = new ServerInfo($result);
return $servers;
}
}

View file

@ -34,14 +34,30 @@
<h2>Servers</h2> <h2>Servers</h2>
<table class="servers"> <table class="servers">
<thead> <thead>
<tr><th class="col-name">Name</th> <th class="col-address">Address</th> <th class="col-java">Java version</th> <th class="col-bedrock">Bedrock version</th> <th class="col-details">Details</th></tr> <tr>
<th class="col-name">Name</th>
<th class="col-address">Address</th>
<th class="col-java">Version</th>
{# <th class="col-address">Bedrock address</th> #}
{# <th class="col-bedrock">Bedrock version</th> #}
<th class="col-details">Details</th>
</tr>
</thead> </thead>
<tbody> <tbody>
{# <tr><td class="col-name">Vanilla Survival</td> <td class="col-address"><code>mc-survival.flashii.net</code></td> <td class="col-java">1.19.2</td> <td class="col-bedrock">N/A</td> <td class="col-details">Regular Minecraft Survival with some server-side extensions.</td></tr> #} {% if servers is empty %}
{# <tr><td class="col-name">Beta Survival</td> <td class="col-address"><code>mc-beta.flashii.net</code></td> <td class="col-java">Beta 1.7.3</td> <td class="col-bedrock">N/A</td> <td class="col-details">Classic Minecraft Survival!</td></tr> #} <tr><td class="col-java" colspan="4">There are currently no active servers, check back later!</td></tr>
{# <tr><td class="col-name">Tekkit Classic</td> <td class="col-address"><code>mc-tekkit.flashii.net</code></td> <td class="col-java">1.2.5</td> <td class="col-bedrock">N/A</td> <td class="col-details"><a href="https://www.technicpack.net/modpack/tekkit.552560" target="_blank" rel="noopener">Page for this modpack on the Technic Platform</a></td></tr> #} {% else %}
{# <tr><td class="col-name">All of Fabric 6</td> <td class="col-address"><code>mc-aof6.flashii.net</code></td> <td class="col-java">1.19.2</td> <td class="col-bedrock">N/A</td> <td class="col-details"><a href="https://www.curseforge.com/minecraft/modpacks/all-of-fabric-6" target="_blank" rel="noopener">Page for this modpack on CurseForge</a></td></tr> #} {% for server in servers %}
<tr><td class="col-java" colspan="5">There are currently no active servers, check back later!</td></tr> <tr id="s{{ server.id }}">
<td class="col-name">{{ server.name }}</td>
<td class="col-address">{% if server.hasJavaAddress %}<code>{{ server.javaAddress }}</code>{% else %}N/A{% endif %}</td>
<td class="col-java">{% if server.hasJavaVersion %}<code>{{ server.javaVersion }}</code>{% else %}N/A{% endif %}</td>
{# <td class="col-address">{% if server.hasBedrockAddress %}<code>{{ server.bedrockAddress }}</code>{% else %}N/A{% endif %}</td> #}
{# <td class="col-bedrock">{% if server.hasBedrockVersion %}<code>{{ server.bedrockVersion }}</code>{% else %}N/A{% endif %}</td> #}
<td class="col-details">{{ server.details|raw }}</td>
</tr>
{% endfor %}
{% endif %}
</tbody> </tbody>
</table> </table>
</div> </div>

View file

@ -4,6 +4,8 @@ use Mince\Whitelist;
require_once __DIR__ . '/../mince.php'; require_once __DIR__ . '/../mince.php';
// rewrite this to use the database shit
echo 'Syncing server whitelists...' . PHP_EOL; echo 'Syncing server whitelists...' . PHP_EOL;
$rInfo = $remote->getInfo(); $rInfo = $remote->getInfo();