diff --git a/src/Controller/ServerController.php b/src/Controller/ServerController.php index 916563f..efa9f25 100644 --- a/src/Controller/ServerController.php +++ b/src/Controller/ServerController.php @@ -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'); - } - - else - { - $field = 'time'; - } - - if (in_array($request->get('order'), ['asc','desc'])) - { - $order = $request->get('order'); + $page = (int) $request->get('page'); } else { - $order = 'desc'; + $page = 1; } // Init defaults @@ -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 [ '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 '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'] diff --git a/src/Repository/OnlineRepository.php b/src/Repository/OnlineRepository.php index 7e37dfe..24e8207 100644 --- a/src/Repository/OnlineRepository.php +++ b/src/Repository/OnlineRepository.php @@ -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() + ; + } } diff --git a/src/Repository/PlayerRepository.php b/src/Repository/PlayerRepository.php index eaf39d3..09abcc0 100644 --- a/src/Repository/PlayerRepository.php +++ b/src/Repository/PlayerRepository.php @@ -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() + ; + } } diff --git a/templates/default/server/index.html.twig b/templates/default/server/index.html.twig index 258ca99..9ac8a8e 100644 --- a/templates/default/server/index.html.twig +++ b/templates/default/server/index.html.twig @@ -146,6 +146,49 @@ {% endfor %} +
+ {% if request.get('sort') == 'online' and request.get('page') > 1 %} + + + + {% else %} + + + + {% endif %} + {% if request.get('sort') != 'online' or request.get('page') < server.pagination.online %} + + + + {% else %} + + + + {% endif %} + {{ 'page' | trans }} {{ request.get('sort') == 'online' and request.get('page') ? request.get('page') : 1 }}/{{ server.pagination.online }} +
{% endif %} {% if server.players %} @@ -187,6 +230,49 @@ {% endfor %} +
+ {% if request.get('sort') == 'players' and request.get('page') > 1 %} + + + + {% else %} + + + + {% endif %} + {% if request.get('sort') != 'players' or request.get('page') < server.pagination.players %} + + + + {% else %} + + + + {% endif %} + {{ 'page' | trans }} {{ request.get('sort') == 'players' and request.get('page') ? request.get('page') : 1 }}/{{ server.pagination.online }} +
{% endif %} {% endblock %} \ No newline at end of file