Look into session logfiles

This commit is contained in:
Frank
2026-01-08 18:26:32 +01:00
parent 50d7ce745c
commit 732148a533
3 changed files with 83 additions and 1 deletions

View File

@@ -3,17 +3,25 @@ declare(strict_types=1);
namespace App\Game\Controller; namespace App\Game\Controller;
use App\Game\Entity\Session;
use App\Game\Repository\SessionRepository; use App\Game\Repository\SessionRepository;
use App\Tech\Repository\UserRepository; use App\Tech\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted; use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
#[Route('/admin')] #[Route('/admin')]
#[IsGranted('ROLE_ADMIN')] #[IsGranted('ROLE_ADMIN')]
final class GameAdminController extends AbstractController final class GameAdminController extends AbstractController
{ {
public function __construct(
#[Autowire('%kernel.project_dir%')]
private string $projectDir
) {
}
#[Route('', name: 'game_admin_dashboard', methods: ['GET'])] #[Route('', name: 'game_admin_dashboard', methods: ['GET'])]
public function index( public function index(
UserRepository $userRepository, UserRepository $userRepository,
@@ -27,4 +35,29 @@ final class GameAdminController extends AbstractController
'sessions' => $sessions, 'sessions' => $sessions,
]); ]);
} }
#[Route('/session/{session}', name: 'game_admin_view_session', methods: ['GET'])]
public function viewSession(Session $session): Response
{
$playersLogs = [];
foreach ($session->getPlayers() as $player) {
$username = $player->getUser()->getUsername();
$logFile = $this->projectDir . '/var/log/sessions/' . $session->getId() . '/' . $username . '.txt';
$logs = '';
if (file_exists($logFile)) {
$logs = file_get_contents($logFile);
}
$playersLogs[] = [
'username' => $username,
'logs' => $logs,
];
}
return $this->render('game/admin/session.html.twig', [
'session' => $session,
'playersLogs' => $playersLogs,
]);
}
} }

View File

@@ -67,7 +67,7 @@
</td> </td>
<td>{{ session.created|date('Y-m-d H:i') }}</td> <td>{{ session.created|date('Y-m-d H:i') }}</td>
<td> <td>
<a href="{{ path('game', {session: session.id}) }}">View Game</a> <a href="{{ path('game_admin_view_session', {session: session.id}) }}">View Game Logs</a>
</td> </td>
</tr> </tr>
{% else %} {% else %}

View File

@@ -0,0 +1,49 @@
{% extends 'base.html.twig' %}
{% block title %}View Session Logs - {{ session.id }}{% endblock %}
{% block body %}
<h1>Session: {{ session.game.name }} (#{{ session.id }})</h1>
<p><a href="{{ path('game_admin_dashboard') }}">Back to Dashboard</a></p>
<div class="tabs">
<ul style="display: flex; list-style: none; padding: 0; border-bottom: 1px solid #ccc;">
{% for playerLog in playersLogs %}
<li style="margin-right: 10px;">
<button
onclick="openTab(event, 'player-{{ loop.index }}')"
class="tablinks {{ loop.first ? 'active' : '' }}"
style="padding: 10px; cursor: pointer; border: 1px solid #ccc; border-bottom: none; background: {{ loop.first ? '#eee' : '#fff' }};"
>
{{ playerLog.username }}
</button>
</li>
{% endfor %}
</ul>
</div>
{% for playerLog in playersLogs %}
<div id="player-{{ loop.index }}" class="tabcontent" style="display: {{ loop.first ? 'block' : 'none' }}; border: 1px solid #ccc; border-top: none; padding: 20px;">
<h3>Logs for {{ playerLog.username }}</h3>
<pre style="background: #f4f4f4; padding: 15px; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;">{{ playerLog.logs ?: 'No logs found for this player.' }}</pre>
</div>
{% endfor %}
<script>
function openTab(evt, playerName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
tablinks[i].style.background = "#fff";
}
document.getElementById(playerName).style.display = "block";
evt.currentTarget.className += " active";
evt.currentTarget.style.background = "#eee";
}
</script>
{% endblock %}