Browse Source

implement player stats

main
ghost 11 months ago
parent
commit
f1c08a20b9
  1. 21
      src/Controller/MainController.php
  2. 92
      src/Controller/RssController.php
  3. 53
      templates/default/main/index.html.twig
  4. 15
      templates/default/rss/players.xml.twig

21
src/Controller/MainController.php

@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\Response; @@ -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 @@ -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 @@ -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 @@ -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 @@ -161,6 +177,7 @@ class MainController extends AbstractController
'info' => $info,
'session' => $session,
'online' => $online,
'players' => $players,
'status' => $status
];
}

92
src/Controller/RssController.php

@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\Response; @@ -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 @@ -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();
}
}

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

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
{% if sort == request.get('sort') and crc32server == request.get('crc32server') %}
{% if field == request.get('field') %}
{% if 'asc' == request.get('order') %}
<a href="{{ path('main_index', { sort : sort, field : field, order : 'desc', crc32server : crc32server, '_fragment' : 'online-%d' | format(crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<a href="{{ path('main_index', { sort : sort, field : field, order : 'desc', crc32server : crc32server, '_fragment' : '%s-%d' | format(sort, crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 4a.5.5 0 0 0-.374.832l4 4.5a.5.5 0 0 0 .748 0l4-4.5A.5.5 0 0 0 12 6z"/>
</svg></a>
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
@ -12,29 +12,33 @@ @@ -12,29 +12,33 @@
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 4a.5.5 0 0 0-.374.832l4 4.5a.5.5 0 0 0 .748 0l4-4.5A.5.5 0 0 0 12 6z"/>
</svg>
<a href="{{ path('main_index', { sort : sort, field : field, order : 'asc', crc32server : crc32server, '_fragment' : 'online-%d' | format(crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<a href="{{ path('main_index', { sort : sort, field : field, order : 'asc', crc32server : crc32server, '_fragment' : '%s-%d' | format(sort, crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 9h8a.5.5 0 0 0 .374-.832l-4-4.5a.5.5 0 0 0-.748 0l-4 4.5A.5.5 0 0 0 4 11"/>
</svg></a>
{% endif %}
{% else %}
<a href="{{ path('main_index', { sort : sort, field : field, order : 'desc', crc32server : crc32server, '_fragment' : 'online-%d' | format(crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<a href="{{ path('main_index', { sort : sort, field : field, order : 'desc', crc32server : crc32server, '_fragment' : '%s-%d' | format(sort, crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 4a.5.5 0 0 0-.374.832l4 4.5a.5.5 0 0 0 .748 0l4-4.5A.5.5 0 0 0 12 6z"/>
</svg></a>
<a href="{{ path('main_index', { sort : sort, field : field, order : 'asc', crc32server : crc32server, '_fragment' : 'online-%d' | format(crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<a href="{{ path('main_index', { sort : sort, field : field, order : 'asc', crc32server : crc32server, '_fragment' : '%s-%d' | format(sort, crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 9h8a.5.5 0 0 0 .374-.832l-4-4.5a.5.5 0 0 0-.748 0l-4 4.5A.5.5 0 0 0 4 11"/>
</svg></a>
{% endif %}
{% else %}
{% if 'time' == field %}
{% if 'online' == sort and 'time' == field %}
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 4a.5.5 0 0 0-.374.832l4 4.5a.5.5 0 0 0 .748 0l4-4.5A.5.5 0 0 0 12 6z"/>
</svg>
{% elseif 'players' == sort and 'frags' == field %}
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 4a.5.5 0 0 0-.374.832l4 4.5a.5.5 0 0 0 .748 0l4-4.5A.5.5 0 0 0 12 6z"/>
</svg>
{% else %}
<a href="{{ path('main_index', { sort : sort, field : field, order : 'desc', crc32server : crc32server, '_fragment' : 'online-%d' | format(crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<a href="{{ path('main_index', { sort : sort, field : field, order : 'desc', crc32server : crc32server, '_fragment' : '%s-%d' | format(sort, crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 4a.5.5 0 0 0-.374.832l4 4.5a.5.5 0 0 0 .748 0l4-4.5A.5.5 0 0 0 12 6z"/>
</svg></a>
{% endif %}
<a href="{{ path('main_index', { sort : sort, field : field, order : 'asc', crc32server : crc32server, '_fragment' : 'online-%d' | format(crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<a href="{{ path('main_index', { sort : sort, field : field, order : 'asc', crc32server : crc32server, '_fragment' : '%s-%d' | format(sort, crc32server) }) }}" rel="nofollow"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm4 9h8a.5.5 0 0 0 .374-.832l-4-4.5a.5.5 0 0 0-.748 0l-4 4.5A.5.5 0 0 0 4 11"/>
</svg></a>
{% endif %}
@ -125,7 +129,7 @@ @@ -125,7 +129,7 @@
</table>
{% endif %}
{% if server.online %}
<a name="{{ 'online-%d' | format(server.crc32server) }}"></a>
<a name="{{ '%s-%d' | format('online', server.crc32server) }}"></a>
<h3 class="padding-y-8-px text-align-right">
{{ 'Online' | trans }}
<a href="{{ path('rss_online', { crc32server : server.crc32server }) }}" title="{{ 'RSS' | trans }}"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
@ -157,6 +161,39 @@ @@ -157,6 +161,39 @@
{% endfor %}
</table>
{% endif %}
{% if server.players %}
<a name="{{ '%s-%d' | format('players', server.crc32server) }}"></a>
<h3 class="padding-y-8-px text-align-right">
{{ 'Players' | trans }}
<a href="{{ path('rss_players', { crc32server : server.crc32server }) }}" title="{{ 'RSS' | trans }}"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" viewBox="0 0 16 16">
<path d="M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zm1.5 2.5c5.523 0 10 4.477 10 10a1 1 0 1 1-2 0 8 8 0 0 0-8-8 1 1 0 0 1 0-2m0 4a6 6 0 0 1 6 6 1 1 0 1 1-2 0 4 4 0 0 0-4-4 1 1 0 0 1 0-2m.5 7a1.5 1.5 0 1 1 0-3 1.5 1.5 0 0 1 0 3"/>
</svg></a>
</h3>
<table>
<tr>
<th class="text-align-left">
{{ 'Name' | trans }} {{ sort(request, server.crc32server, 'players', 'name') }}
</th>
<th class="text-align-center">
{{ 'Frags' | trans }} {{ sort(request, server.crc32server, 'players', 'frags') }}
</th>
<th class="text-align-left">
{{ 'Joined' | trans }} {{ sort(request, server.crc32server, 'players', 'joined') }}
</th>
<th class="text-align-left">
{{ 'Online' | trans }} {{ sort(request, server.crc32server, 'players', 'online') }}
</th>
</tr>
{% for player in server.players %}
<tr>
<td class="text-align-left">{{ player.name }}</td>
<td class="text-align-center">{{ player.frags }}</td>
<td class="text-align-left">{{ player.joined | date('r') }}</td>
<td class="text-align-left">{{ player.online | date('r') }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
</div>
{% endfor %}
{% endblock %}

15
templates/default/rss/players.xml.twig

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<atom:link href="{{ url('main_index') }}" rel="self" type="application/rss+xml"></atom:link>
<title>{{ server.host }}:{{ server.port }} - {{ 'Players' | trans }} - {{ app.name }}</title>
<link>{{ url('main_index') }}</link>
{% for player in players %}
<item>
<title>{{ player.name }} {{ 'joined game!' | trans }}</title>
<guid>{{ player.id }}</guid>
<pubDate>{{ player.joined | date("D, d M Y h:i:s O") }}</pubDate>
</item>
{% endfor %}
</channel>
</rss>
Loading…
Cancel
Save