mirror of
https://github.com/YGGverse/HLState.git
synced 2026-03-31 17:15:39 +00:00
implement online history
This commit is contained in:
parent
2071796a07
commit
272d219066
8 changed files with 495 additions and 10 deletions
137
src/Controller/CrontabController.php
Normal file
137
src/Controller/CrontabController.php
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use App\Entity\Online;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class CrontabController extends AbstractController
|
||||
{
|
||||
#[Route(
|
||||
'/crontab/online',
|
||||
name: 'crontab_online',
|
||||
methods:
|
||||
[
|
||||
'GET'
|
||||
]
|
||||
)]
|
||||
public function online(
|
||||
?Request $request,
|
||||
EntityManagerInterface $entityManagerInterface
|
||||
): Response
|
||||
{
|
||||
// Get HLServers config
|
||||
if ($hlservers = file_get_contents($this->getParameter('app.hlservers')))
|
||||
{
|
||||
$hlservers = json_decode($hlservers);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$hlservers = [];
|
||||
}
|
||||
|
||||
// Collect servers info
|
||||
$servers = [];
|
||||
|
||||
foreach ($hlservers as $hlserver)
|
||||
{
|
||||
try
|
||||
{
|
||||
$server = new \xPaw\SourceQuery\SourceQuery();
|
||||
|
||||
$server->Connect(
|
||||
$hlserver->host,
|
||||
$hlserver->port
|
||||
);
|
||||
|
||||
if ($server->Ping())
|
||||
{
|
||||
if ($info = (array) $server->GetInfo())
|
||||
{
|
||||
// Filter response
|
||||
$bots = isset($info['Bots']) && $info['Bots'] > 0 ? (int) $info['Bots'] : 0;
|
||||
$players = isset($info['Players']) && $info['Players'] > 0 ? (int) $info['Players'] - $bots : 0;
|
||||
$total = $players + $bots;
|
||||
|
||||
// Generate CRC32 server ID
|
||||
$crc32Server = crc32(
|
||||
$hlserver->host . ':' . $hlserver->port
|
||||
);
|
||||
|
||||
// Get last online value
|
||||
$online = $entityManagerInterface->getRepository(Online::class)->findOneBy(
|
||||
[
|
||||
'crc32server' => $crc32Server
|
||||
],
|
||||
[
|
||||
'id' => 'DESC' // same as online.time but faster
|
||||
]
|
||||
);
|
||||
|
||||
// Add new record if online changed
|
||||
if
|
||||
(
|
||||
is_null($online)
|
||||
||
|
||||
$players !== $online->getPlayers()
|
||||
||
|
||||
$bots !== $online->getBots()
|
||||
||
|
||||
$total !== $online->getTotal()
|
||||
)
|
||||
{
|
||||
$online = new Online();
|
||||
|
||||
$online->setCrc32server(
|
||||
$crc32Server
|
||||
);
|
||||
|
||||
$online->setTime(
|
||||
time()
|
||||
);
|
||||
|
||||
$online->setPlayers(
|
||||
$players
|
||||
);
|
||||
|
||||
$online->setBots(
|
||||
$bots
|
||||
);
|
||||
|
||||
$online->setTotal(
|
||||
$total
|
||||
);
|
||||
|
||||
$entityManagerInterface->persist(
|
||||
$online
|
||||
);
|
||||
|
||||
$entityManagerInterface->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception $error)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
$server->Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// Render response
|
||||
return new Response(); // @TODO
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,9 @@ use Symfony\Component\Routing\Annotation\Route;
|
|||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use App\Entity\Online;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class MainController extends AbstractController
|
||||
{
|
||||
#[Route(
|
||||
|
|
@ -20,7 +23,8 @@ class MainController extends AbstractController
|
|||
]
|
||||
)]
|
||||
public function index(
|
||||
?Request $request
|
||||
?Request $request,
|
||||
EntityManagerInterface $entityManagerInterface
|
||||
): Response
|
||||
{
|
||||
// Get HLServers config
|
||||
|
|
@ -52,15 +56,49 @@ class MainController extends AbstractController
|
|||
{
|
||||
if ($info = (array) $server->GetInfo())
|
||||
{
|
||||
// Generate CRC32 ID
|
||||
$crc32server = crc32(
|
||||
$hlserver->host . ':' . $hlserver->port
|
||||
);
|
||||
|
||||
// Get session
|
||||
$session = empty($info['Players']) ? [] : (array) $server->GetPlayers();
|
||||
|
||||
// Sort by players by frags
|
||||
if ($session)
|
||||
{
|
||||
array_multisort(
|
||||
array_column(
|
||||
$session,
|
||||
'Frags'
|
||||
),
|
||||
SORT_DESC,
|
||||
$session
|
||||
);
|
||||
}
|
||||
|
||||
// Get online
|
||||
$online = $entityManagerInterface->getRepository(Online::class)->findBy(
|
||||
[
|
||||
'crc32server' => $crc32server
|
||||
],
|
||||
[
|
||||
'id' => 'DESC' // same as online.time but faster
|
||||
],
|
||||
10
|
||||
);
|
||||
|
||||
// Add server
|
||||
$servers[] = [
|
||||
'host' => $hlserver->host,
|
||||
'port' => $hlserver->port,
|
||||
'alias' => $hlserver->alias,
|
||||
'info' => $info,
|
||||
'online' => empty($info['Players']) ? [] : (array) $server->GetPlayers()
|
||||
'crc32server' => $crc32server,
|
||||
'host' => $hlserver->host,
|
||||
'port' => $hlserver->port,
|
||||
'alias' => $hlserver->alias,
|
||||
'info' => $info,
|
||||
'session' => $session,
|
||||
'online' => $online
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
109
src/Controller/RssController.php
Normal file
109
src/Controller/RssController.php
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use App\Entity\Online;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class RssController extends AbstractController
|
||||
{
|
||||
#[Route(
|
||||
'/rss/online/{crc32server}',
|
||||
name: 'rss_online',
|
||||
requirements:
|
||||
[
|
||||
'crc32server' => '\d+',
|
||||
],
|
||||
methods:
|
||||
[
|
||||
'GET'
|
||||
]
|
||||
)]
|
||||
public function online(
|
||||
?Request $request,
|
||||
EntityManagerInterface $entityManagerInterface
|
||||
): Response
|
||||
{
|
||||
// Get HLServers config
|
||||
if ($hlservers = file_get_contents($this->getParameter('app.hlservers')))
|
||||
{
|
||||
$hlservers = json_decode($hlservers);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
$hlservers = [];
|
||||
}
|
||||
|
||||
// Find server info
|
||||
foreach ($hlservers as $hlserver)
|
||||
{
|
||||
// Generate CRC32 server ID
|
||||
$crc32server = crc32(
|
||||
$hlserver->host . ':' . $hlserver->port
|
||||
);
|
||||
|
||||
// Skip servers not registered in HLServers
|
||||
if ($crc32server != $request->get('crc32server'))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get last online value
|
||||
$online = $entityManagerInterface->getRepository(Online::class)->findBy(
|
||||
[
|
||||
'crc32server' => $crc32server
|
||||
],
|
||||
[
|
||||
'id' => 'DESC' // same as online.time but faster
|
||||
],
|
||||
10
|
||||
);
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($online as $value)
|
||||
{
|
||||
$result[] =
|
||||
[
|
||||
'id' => $value->getId(),
|
||||
'bots' => $value->getBots(),
|
||||
'players' => $value->getPlayers(),
|
||||
'total' => $value->getTotal(),
|
||||
'time' => $value->getTime()
|
||||
];
|
||||
}
|
||||
|
||||
// Response
|
||||
$response = new Response();
|
||||
|
||||
$response->headers->set(
|
||||
'Content-Type',
|
||||
'text/xml'
|
||||
);
|
||||
|
||||
return $this->render(
|
||||
'default/rss/online.xml.twig',
|
||||
[
|
||||
'server' =>
|
||||
[
|
||||
'crc32' => $crc32server,
|
||||
'host' => $hlserver->host,
|
||||
'port' => $hlserver->port,
|
||||
],
|
||||
'online' => $result
|
||||
],
|
||||
$response
|
||||
);
|
||||
}
|
||||
|
||||
throw $this->createNotFoundException();
|
||||
}
|
||||
}
|
||||
96
src/Entity/Online.php
Normal file
96
src/Entity/Online.php
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\OnlineRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: OnlineRepository::class)]
|
||||
class Online
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: Types::BIGINT)]
|
||||
private ?int $crc32server = null;
|
||||
|
||||
#[ORM\Column(type: Types::BIGINT)]
|
||||
private ?int $time = null;
|
||||
|
||||
#[ORM\Column]
|
||||
private ?int $total = null;
|
||||
|
||||
#[ORM\Column]
|
||||
private ?int $players = null;
|
||||
|
||||
#[ORM\Column]
|
||||
private ?int $bots = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getCrc32server(): ?int
|
||||
{
|
||||
return $this->crc32server;
|
||||
}
|
||||
|
||||
public function setCrc32server(int $crc32server): static
|
||||
{
|
||||
$this->crc32server = $crc32server;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTime(): ?int
|
||||
{
|
||||
return $this->time;
|
||||
}
|
||||
|
||||
public function setTime(int $time): static
|
||||
{
|
||||
$this->time = $time;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTotal(): ?int
|
||||
{
|
||||
return $this->total;
|
||||
}
|
||||
|
||||
public function setTotal(int $total): static
|
||||
{
|
||||
$this->total = $total;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPlayers(): ?int
|
||||
{
|
||||
return $this->players;
|
||||
}
|
||||
|
||||
public function setPlayers(int $players): static
|
||||
{
|
||||
$this->players = $players;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBots(): ?int
|
||||
{
|
||||
return $this->bots;
|
||||
}
|
||||
|
||||
public function setBots(int $bots): static
|
||||
{
|
||||
$this->bots = $bots;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
23
src/Repository/OnlineRepository.php
Normal file
23
src/Repository/OnlineRepository.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Online;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Online>
|
||||
*
|
||||
* @method Online|null find($id, $lockMode = null, $lockVersion = null)
|
||||
* @method Online|null findOneBy(array $criteria, array $orderBy = null)
|
||||
* @method Online[] findAll()
|
||||
* @method Online[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
|
||||
*/
|
||||
class OnlineRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Online::class);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue