From f1c08a20b9256144b553323854d5286cbf293a78 Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 7 Jan 2024 19:20:13 +0200 Subject: [PATCH] implement player stats --- src/Controller/MainController.php | 21 +++++- src/Controller/RssController.php | 92 ++++++++++++++++++++++++++ templates/default/main/index.html.twig | 53 ++++++++++++--- templates/default/rss/players.xml.twig | 15 +++++ 4 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 templates/default/rss/players.xml.twig diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index f91a13d..0ceb50b 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use App\Entity\Online; +use App\Entity\Player; use Doctrine\ORM\EntityManagerInterface; class MainController extends AbstractController @@ -28,7 +29,12 @@ class MainController extends AbstractController ): Response { // Prepare request - if (in_array($request->get('field'), ['time','players','bots','total'])) + if ('online' == $request->get('sort') && in_array($request->get('field'), ['time','players','bots','total'])) + { + $field = $request->get('field'); + } + + else if ('players' == $request->get('sort') && in_array($request->get('field'), ['name','frags','joined','online'])) { $field = $request->get('field'); } @@ -68,6 +74,7 @@ class MainController extends AbstractController $info = []; $session = []; $online = []; + $players = []; // Generate CRC32 ID $crc32server = crc32( @@ -128,7 +135,16 @@ class MainController extends AbstractController [ 'crc32server' => $crc32server ], - 'online' == $request->get('sort') && $crc32server == $request->get('crc32server') ? [$field => $order] : ['id' => 'DESC'], + 'online' == $request->get('sort') && $crc32server == $request->get('crc32server') ? [$field => $order] : ['time' => 'DESC'], + 10 + ); + + // Get players + $players = $entityManagerInterface->getRepository(Player::class)->findBy( + [ + 'crc32server' => $crc32server + ], + 'players' == $request->get('sort') && $crc32server == $request->get('crc32server') ? [$field => $order] : ['frags' => 'DESC'], 10 ); } @@ -161,6 +177,7 @@ class MainController extends AbstractController 'info' => $info, 'session' => $session, 'online' => $online, + 'players' => $players, 'status' => $status ]; } diff --git a/src/Controller/RssController.php b/src/Controller/RssController.php index 4098025..1e1a3a3 100644 --- a/src/Controller/RssController.php +++ b/src/Controller/RssController.php @@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use App\Entity\Online; +use App\Entity\Player; use Doctrine\ORM\EntityManagerInterface; class RssController extends AbstractController @@ -106,4 +107,95 @@ class RssController extends AbstractController throw $this->createNotFoundException(); } + + #[Route( + '/rss/players/{crc32server}', + name: 'rss_players', + requirements: + [ + 'crc32server' => '\d+', + ], + methods: + [ + 'GET' + ] + )] + public function players( + ?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 players + $players = $entityManagerInterface->getRepository(Player::class)->findBy( + [ + 'crc32server' => $crc32server + ], + [ + 'id' => 'DESC' + ], + 10 + ); + + $result = []; + + foreach ($players as $value) + { + $result[] = + [ + 'id' => $value->getId(), + 'name' => $value->getName(), + 'joined' => $value->getJoined() + ]; + } + + // Response + $response = new Response(); + + $response->headers->set( + 'Content-Type', + 'text/xml' + ); + + return $this->render( + 'default/rss/players.xml.twig', + [ + 'server' => + [ + 'crc32' => $crc32server, + 'host' => $hlserver->host, + 'port' => $hlserver->port, + ], + 'players' => $result + ], + $response + ); + } + + throw $this->createNotFoundException(); + } } \ No newline at end of file diff --git a/templates/default/main/index.html.twig b/templates/default/main/index.html.twig index 68500b3..bd67174 100644 --- a/templates/default/main/index.html.twig +++ b/templates/default/main/index.html.twig @@ -2,7 +2,7 @@ {% if sort == request.get('sort') and crc32server == request.get('crc32server') %} {% if field == request.get('field') %} {% if 'asc' == request.get('order') %} - + @@ -12,29 +12,33 @@ - + {% endif %} {% else %} - + - + {% endif %} {% else %} - {% if 'time' == field %} + {% if 'online' == sort and 'time' == field %} + + + + {% elseif 'players' == sort and 'frags' == field %} {% else %} - + {% endif %} - + {% endif %} @@ -125,7 +129,7 @@ {% endif %} {% if server.online %} - +

{{ 'Online' | trans }} @@ -157,6 +161,39 @@ {% endfor %} {% endif %} + {% if server.players %} + +

+ {{ 'Players' | trans }} + + + +

+ + + + + + + + {% for player in server.players %} + + + + + + + {% endfor %} +
+ {{ 'Name' | trans }} {{ sort(request, server.crc32server, 'players', 'name') }} + + {{ 'Frags' | trans }} {{ sort(request, server.crc32server, 'players', 'frags') }} + + {{ 'Joined' | trans }} {{ sort(request, server.crc32server, 'players', 'joined') }} + + {{ 'Online' | trans }} {{ sort(request, server.crc32server, 'players', 'online') }} +
{{ player.name }}{{ player.frags }}{{ player.joined | date('r') }}{{ player.online | date('r') }}
+ {% endif %} {% endfor %} {% endblock %} \ No newline at end of file diff --git a/templates/default/rss/players.xml.twig b/templates/default/rss/players.xml.twig new file mode 100644 index 0000000..7e1e04d --- /dev/null +++ b/templates/default/rss/players.xml.twig @@ -0,0 +1,15 @@ + + + + + {{ server.host }}:{{ server.port }} - {{ 'Players' | trans }} - {{ app.name }} + {{ url('main_index') }} + {% for player in players %} + + {{ player.name }} {{ 'joined game!' | trans }} + {{ player.id }} + {{ player.joined | date("D, d M Y h:i:s O") }} + + {% endfor %} + + \ No newline at end of file