Browse Source

implement pagination

main
ghost 10 months ago
parent
commit
bb5eec002d
  1. 52
      src/Controller/ServerController.php
  2. 13
      src/Repository/OnlineRepository.php
  3. 13
      src/Repository/PlayerRepository.php
  4. 86
      templates/default/server/index.html.twig

52
src/Controller/ServerController.php

@ -45,30 +45,15 @@ class ServerController extends AbstractController @@ -45,30 +45,15 @@ class ServerController extends AbstractController
throw $this->createNotFoundException();
}
// Prepare request
if ('online' == $request->get('sort') && in_array($request->get('field'), ['time','players','bots','total']))
// Prepare page request
if ($request->get('page') && (int) $request->get('page') > 1)
{
$field = $request->get('field');
}
else if ('players' == $request->get('sort') && in_array($request->get('field'), ['name','frags','joined','online']))
{
$field = $request->get('field');
$page = (int) $request->get('page');
}
else
{
$field = 'time';
}
if (in_array($request->get('order'), ['asc','desc']))
{
$order = $request->get('order');
}
else
{
$order = 'desc';
$page = 1;
}
// Init defaults
@ -80,8 +65,12 @@ class ServerController extends AbstractController @@ -80,8 +65,12 @@ class ServerController extends AbstractController
[
'crc32server' => $server->getCrc32server()
],
'online' == $request->get('sort') ? [$field => $order] : ['time' => 'DESC'],
$this->getParameter('app.server.online.limit')
[
'online' == $request->get('sort') && in_array($request->get('field'), ['time','players','bots','total'])
? $request->get('field') : 'time' => in_array($request->get('order'), ['asc','desc']) ? $request->get('order') : 'desc',
],
$this->getParameter('app.server.online.limit'),
'online' == $request->get('sort') ? ($page - 1) * $this->getParameter('app.server.online.limit') : 0
);
// Get players
@ -89,8 +78,12 @@ class ServerController extends AbstractController @@ -89,8 +78,12 @@ class ServerController extends AbstractController
[
'crc32server' => $server->getCrc32server()
],
'players' == $request->get('sort') ? [$field => $order] : ['frags' => 'DESC'],
$this->getParameter('app.server.players.limit')
[
'players' == $request->get('sort') && in_array($request->get('field'), ['name','frags','joined','online'])
? $request->get('field') : 'frags' => in_array($request->get('order'), ['asc','desc']) ? $request->get('order') : 'desc',
],
$this->getParameter('app.server.players.limit'),
'players' == $request->get('sort') ? ($page - 1) * $this->getParameter('app.server.players.limit') : 0
);
// Format address
@ -175,6 +168,19 @@ class ServerController extends AbstractController @@ -175,6 +168,19 @@ class ServerController extends AbstractController
'online' => $online,
'players' => $players,
'status' => $status,
'pagination' =>
[
'players' => ceil(
$entityManagerInterface->getRepository(Player::class)->getTotalByCrc32server(
$server->getCrc32server()
) / $this->getParameter('app.server.players.limit')
),
'online' => ceil(
$entityManagerInterface->getRepository(Online::class)->getTotalByCrc32server(
$server->getCrc32server()
) / $this->getParameter('app.server.online.limit')
)
],
'connections' => empty($info['Players']) || $info['Players'] < 0 || empty($info['Bots']) || $info['Bots'] < 0
? 0
: (int) $info['Players'] - (int) $info['Bots']

13
src/Repository/OnlineRepository.php

@ -20,4 +20,17 @@ class OnlineRepository extends ServiceEntityRepository @@ -20,4 +20,17 @@ class OnlineRepository extends ServiceEntityRepository
{
parent::__construct($registry, Online::class);
}
public function getTotalByCrc32server(
int $crc32server
): int
{
return $this->createQueryBuilder('o')
->select('count(o.id)')
->where('o.crc32server = :crc32server')
->setParameter('crc32server', $crc32server)
->getQuery()
->getSingleScalarResult()
;
}
}

13
src/Repository/PlayerRepository.php

@ -20,4 +20,17 @@ class PlayerRepository extends ServiceEntityRepository @@ -20,4 +20,17 @@ class PlayerRepository extends ServiceEntityRepository
{
parent::__construct($registry, Player::class);
}
public function getTotalByCrc32server(
int $crc32server
): int
{
return $this->createQueryBuilder('p')
->select('count(p.id)')
->where('p.crc32server = :crc32server')
->setParameter('crc32server', $crc32server)
->getQuery()
->getSingleScalarResult()
;
}
}

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

@ -146,6 +146,49 @@ @@ -146,6 +146,49 @@
</tr>
{% endfor %}
</table>
<div class="padding-y-8-px">
{% if request.get('sort') == 'online' and request.get('page') > 1 %}
<a rel="nofollow" href="{{
path(
'server_index',
{
sort : 'online',
field : request.get('field') ? request.get('field') : 'frags',
order : request.get('order') ? request.get('order') : 'desc',
page : request.get('page') ? request.get('page') - 1 : 1,
crc32server : server.crc32server,
'_fragment' : 'online'
}
) }}"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" class="bi bi-caret-left-square-fill" 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-2zm10.5 10V4a.5.5 0 0 0-.832-.374l-4.5 4a.5.5 0 0 0 0 .748l4.5 4A.5.5 0 0 0 10.5 12"/>
</svg></a>
{% else %}
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" class="bi bi-caret-left-square-fill" 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-2zm10.5 10V4a.5.5 0 0 0-.832-.374l-4.5 4a.5.5 0 0 0 0 .748l4.5 4A.5.5 0 0 0 10.5 12"/>
</svg>
{% endif %}
{% if request.get('sort') != 'online' or request.get('page') < server.pagination.online %}
<a rel="nofollow" href="{{
path(
'server_index',
{
sort : 'online',
field : request.get('sort') == 'online' and request.get('field') ? request.get('field') : 'time',
order : request.get('sort') == 'online' and request.get('order') ? request.get('order') : 'desc',
page : (request.get('sort') == 'online' and request.get('page') ? request.get('page') : 1) + 1,
crc32server : server.crc32server,
'_fragment' : 'online'
}
) }}"><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-2zm5.5 10a.5.5 0 0 0 .832.374l4.5-4a.5.5 0 0 0 0-.748l-4.5-4A.5.5 0 0 0 5.5 4z"/>
</svg></a>
{% else %}
<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-2zm5.5 10a.5.5 0 0 0 .832.374l4.5-4a.5.5 0 0 0 0-.748l-4.5-4A.5.5 0 0 0 5.5 4z"/>
</svg>
{% endif %}
{{ 'page' | trans }} {{ request.get('sort') == 'online' and request.get('page') ? request.get('page') : 1 }}/{{ server.pagination.online }}
</div>
{% endif %}
{% if server.players %}
<a name="players"></a>
@ -187,6 +230,49 @@ @@ -187,6 +230,49 @@
</tr>
{% endfor %}
</table>
<div class="padding-y-8-px">
{% if request.get('sort') == 'players' and request.get('page') > 1 %}
<a rel="nofollow" href="{{
path(
'server_index',
{
sort : 'players',
field : request.get('field') ? request.get('field') : 'frags',
order : request.get('order') ? request.get('order') : 'desc',
page : request.get('page') ? request.get('page') - 1 : 1,
crc32server : server.crc32server,
'_fragment' : 'players'
}
) }}"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" class="bi bi-caret-left-square-fill" 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-2zm10.5 10V4a.5.5 0 0 0-.832-.374l-4.5 4a.5.5 0 0 0 0 .748l4.5 4A.5.5 0 0 0 10.5 12"/>
</svg></a>
{% else %}
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="currentColor" class="bi bi-caret-left-square-fill" 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-2zm10.5 10V4a.5.5 0 0 0-.832-.374l-4.5 4a.5.5 0 0 0 0 .748l4.5 4A.5.5 0 0 0 10.5 12"/>
</svg>
{% endif %}
{% if request.get('sort') != 'players' or request.get('page') < server.pagination.players %}
<a rel="nofollow" href="{{
path(
'server_index',
{
sort : 'players',
field : request.get('sort') == 'players' and request.get('field') ? request.get('field') : 'time',
order : request.get('sort') == 'players' and request.get('order') ? request.get('order') : 'desc',
page : (request.get('sort') == 'players' and request.get('page') ? request.get('page') : 1) + 1,
crc32server : server.crc32server,
'_fragment' : 'players'
}
) }}"><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-2zm5.5 10a.5.5 0 0 0 .832.374l4.5-4a.5.5 0 0 0 0-.748l-4.5-4A.5.5 0 0 0 5.5 4z"/>
</svg></a>
{% else %}
<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-2zm5.5 10a.5.5 0 0 0 .832.374l4.5-4a.5.5 0 0 0 0-.748l-4.5-4A.5.5 0 0 0 5.5 4z"/>
</svg>
{% endif %}
{{ 'page' | trans }} {{ request.get('sort') == 'players' and request.get('page') ? request.get('page') : 1 }}/{{ server.pagination.online }}
</div>
{% endif %}
</div>
{% endblock %}
Loading…
Cancel
Save