Browse Source

cache latest server HostName in database

main
ghost 11 months ago
parent
commit
af4ad5f65b
  1. 35
      migrations/Version20240113013727.php
  2. 43
      src/Controller/CrontabController.php
  3. 1
      src/Controller/MainController.php
  4. 1
      src/Controller/ServerController.php
  5. 15
      src/Entity/Server.php
  6. 10
      templates/default/main/index.html.twig
  7. 2
      templates/default/server/index.html.twig

35
migrations/Version20240113013727.php

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240113013727 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE server ADD COLUMN name VARCHAR(255) DEFAULT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TEMPORARY TABLE __temp__server AS SELECT id, crc32server, added, updated, online, host, port FROM server');
$this->addSql('DROP TABLE server');
$this->addSql('CREATE TABLE server (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, crc32server BIGINT NOT NULL, added BIGINT NOT NULL, updated BIGINT NOT NULL, online BIGINT NOT NULL, host VARCHAR(255) NOT NULL, port INTEGER NOT NULL)');
$this->addSql('INSERT INTO server (id, crc32server, added, updated, online, host, port) SELECT id, crc32server, added, updated, online, host, port FROM __temp__server');
$this->addSql('DROP TABLE __temp__server');
}
}

43
src/Controller/CrontabController.php

@ -133,27 +133,20 @@ class CrontabController extends AbstractController @@ -133,27 +133,20 @@ class CrontabController extends AbstractController
// Collect servers info
$servers = [];
foreach ((array) $entityManagerInterface->getRepository(Server::class)->findBy(
[
'crc32server' => (int) $request->get('crc32server')
],
[
'id' => 'ASC'
],
) as $server)
foreach ((array) $entityManagerInterface->getRepository(Server::class)->findAll() as $server)
{
try
{
$query = new \xPaw\SourceQuery\SourceQuery();
$node = new \xPaw\SourceQuery\SourceQuery();
$query->Connect(
$node->Connect(
false === filter_var($server->getHost(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? $server->getHost() : "[{$server->getHost()}]",
$server->port
);
if ($query->Ping())
if ($node->Ping())
{
if ($info = (array) $query->GetInfo())
if ($info = (array) $node->GetInfo())
{
// Filter response
$bots = isset($info['Bots']) && $info['Bots'] > 0 ? (int) $info['Bots'] : 0;
@ -165,6 +158,28 @@ class CrontabController extends AbstractController @@ -165,6 +158,28 @@ class CrontabController extends AbstractController
$server->host . ':' . $server->port
);
// Update server name
if (!empty($info['HostName']) && mb_strlen($info['HostName']) < 256)
{
$server->setName(
(string) $info['HostName']
);
}
$server->setUpdated(
time()
);
$server->setOnline(
time()
);
$entityManagerInterface->persist(
$server
);
$entityManagerInterface->flush();
// Get last online value
$online = $entityManagerInterface->getRepository(Online::class)->findOneBy(
[
@ -219,7 +234,7 @@ class CrontabController extends AbstractController @@ -219,7 +234,7 @@ class CrontabController extends AbstractController
// Update player stats
if ($players)
{
foreach ((array) $query->GetPlayers() as $session)
foreach ((array) $node->GetPlayers() as $session)
{
// Validate fields
if
@ -329,7 +344,7 @@ class CrontabController extends AbstractController @@ -329,7 +344,7 @@ class CrontabController extends AbstractController
finally
{
$query->Disconnect();
$node->Disconnect();
}
}

1
src/Controller/MainController.php

@ -95,6 +95,7 @@ class MainController extends AbstractController @@ -95,6 +95,7 @@ class MainController extends AbstractController
// Add server
$servers[] = [
'crc32server' => $server->getCrc32server(),
'name' => $server->getName(),
'host' => $server->getHost(),
'port' => $server->getPort(),
'added' => $server->getAdded(),

1
src/Controller/ServerController.php

@ -169,6 +169,7 @@ class ServerController extends AbstractController @@ -169,6 +169,7 @@ class ServerController extends AbstractController
[
'address' => $address,
'crc32server' => $server->getCrc32Server(),
'name' => $server->getName(),
'host' => $server->getHost(),
'port' => $server->getPort(),
'info' => $info,

15
src/Entity/Server.php

@ -32,6 +32,9 @@ class Server @@ -32,6 +32,9 @@ class Server
#[ORM\Column(type: Types::INTEGER)]
private ?int $port = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $name = null;
public function getId(): ?int
{
return $this->id;
@ -108,4 +111,16 @@ class Server @@ -108,4 +111,16 @@ class Server
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): static
{
$this->name = $name;
return $this;
}
}

10
templates/default/main/index.html.twig

@ -31,7 +31,15 @@ @@ -31,7 +31,15 @@
</span>
{% endif %}
</td>
<td class="text-align-left">{{ server.info.HostName }}</td>
<td class="text-align-left">
{% if server.name %}
{{ server.name }}
{% elseif server.info.HostName %}
{{ server.info.HostName }}
{% else %}
#{{ server.crc32server }}
{% endif %}
</td>
<td class="text-align-left">{{ server.info.Map }}</td>
<td class="text-align-left">{{ server.added | format_date }}</td>
<td class="text-align-left">{{ server.online | format_date }}</td>

2
templates/default/server/index.html.twig

@ -45,7 +45,7 @@ @@ -45,7 +45,7 @@
{% endmacro %}
{% from _self import sort %}
{% extends 'default/layout.html.twig' %}
{% block head_title_content %}{% if server.info.HostName is defined %}{{ server.info.HostName }}{% else %}#{{ server.crc32server }}{% endif %} - {{ app.name }}{% endblock %}
{% block head_title_content %}{% if server.name %}{{ server.name }}{% else %}#{{ server.crc32server }}{% endif %} - {{ app.name }}{% endblock %}
{% block main_content %}
<div class="border-default padding-8-px margin-y-8-px">
<h2>

Loading…
Cancel
Save