3 Commits

Author SHA1 Message Date
Frank
732148a533 Look into session logfiles 2026-01-08 18:26:32 +01:00
Frank
50d7ce745c Logfiles for sessions 2026-01-08 18:14:56 +01:00
a475c1a268 Merge pull request 'set-correct-screens-for-players' (#9) from set-correct-screens-for-players into main
Reviewed-on: #9
2026-01-08 17:02:10 +00:00
9 changed files with 359 additions and 2 deletions

View File

@@ -16,5 +16,9 @@ services:
App\: App\:
resource: '../src/' resource: '../src/'
App\Game\Service\GameResponseService:
arguments:
$projectDir: '%kernel.project_dir%'
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace App\Game\Controller;
use App\Game\Entity\Session;
use App\Game\Repository\SessionRepository;
use App\Tech\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
#[Route('/admin')]
#[IsGranted('ROLE_ADMIN')]
final class GameAdminController extends AbstractController
{
public function __construct(
#[Autowire('%kernel.project_dir%')]
private string $projectDir
) {
}
#[Route('', name: 'game_admin_dashboard', methods: ['GET'])]
public function index(
UserRepository $userRepository,
SessionRepository $sessionRepository
): Response {
$players = $userRepository->findByRole('ROLE_PLAYER');
$sessions = $sessionRepository->findAll();
return $this->render('game/admin/index.html.twig', [
'players' => $players,
'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

@@ -21,6 +21,7 @@ class GameResponseService
private SessionSettingRepository $sessionSettingRepository, private SessionSettingRepository $sessionSettingRepository,
private HubInterface $hub, private HubInterface $hub,
private EntityManagerInterface $entityManager, private EntityManagerInterface $entityManager,
private string $projectDir,
) { ) {
} }
@@ -44,7 +45,7 @@ class GameResponseService
if(!$player) if(!$player)
return ['error' => 'You are not in a game.']; return ['error' => 'You are not in a game.'];
// TODO: Here i need to add a message handler to save the message in a big log. $this->logSessionActivity($player, 'PLAYER: ' . $message);
$data = []; $data = [];
@@ -54,9 +55,43 @@ class GameResponseService
$data = $this->checkConsoleCommando($message, $player); $data = $this->checkConsoleCommando($message, $player);
} }
$responseLog = '';
if (isset($data['result']) && is_array($data['result'])) {
foreach ($data['result'] as $line) {
if (is_array($line)) {
$responseLog .= json_encode($line) . "\n";
} elseif (is_string($line) || is_numeric($line)) {
$responseLog .= (string)$line . "\n";
}
}
} elseif (isset($data['error'])) {
$responseLog = 'ERROR: ' . $data['error'];
}
if ($responseLog !== '') {
$this->logSessionActivity($player, 'SERVER: ' . trim($responseLog));
}
return $data; return $data;
} }
private function logSessionActivity(Player $player, string $content): void
{
$sessionId = $player->getSession()->getId();
$username = $player->getUser()->getUsername();
$logDir = $this->projectDir . '/var/log/sessions/' . $sessionId;
if (!is_dir($logDir)) {
mkdir($logDir, 0777, true);
}
$logFile = $logDir . '/' . $username . '.txt';
$timestamp = date('Y-m-d H:i:s');
$logMessage = sprintf("[%s] %s\n", $timestamp, $content);
file_put_contents($logFile, $logMessage, FILE_APPEND);
}
private function getRechten(Player $player): array private function getRechten(Player $player): array
{ {
$settingName = SessionSettingType::tryFrom('RightsForPlayer' . $player->getScreen()); $settingName = SessionSettingType::tryFrom('RightsForPlayer' . $player->getScreen());

View File

@@ -32,4 +32,16 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader
$this->getEntityManager()->persist($user); $this->getEntityManager()->persist($user);
$this->getEntityManager()->flush(); $this->getEntityManager()->flush();
} }
/**
* @return User[]
*/
public function findByRole(string $role): array
{
return $this->createQueryBuilder('u')
->andWhere('u.roles LIKE :role')
->setParameter('role', '%"' . $role . '"%')
->getQuery()
->getResult();
}
} }

View File

@@ -0,0 +1,82 @@
{% extends 'base.html.twig' %}
{% block title %}Game Admin Dashboard{% endblock %}
{% block body %}
<h1>Game Admin Dashboard</h1>
<div style="display: flex; gap: 2rem;">
<div style="flex: 1;">
<h2>All Players ({{ players|length }})</h2>
<table style="width: 100%; border-collapse: collapse;">
<thead>
<tr style="background-color: #f2f2f2;">
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Roles</th>
<th>Verified</th>
</tr>
</thead>
<tbody>
{% for player in players %}
<tr>
<td>{{ player.id }}</td>
<td>{{ player.username }}</td>
<td>{{ player.email }}</td>
<td>{{ player.roles|join(', ') }}</td>
<td>{{ player.isVerified ? 'Yes' : 'No' }}</td>
</tr>
{% else %}
<tr>
<td colspan="5">No players found.</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div style="flex: 2;">
<h2>All Sessions ({{ sessions|length }})</h2>
<table style="width: 100%; border-collapse: collapse;">
<thead>
<tr style="background-color: #f2f2f2;">
<th>ID</th>
<th>Game</th>
<th>Status</th>
<th>Players Joined</th>
<th>Created At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for session in sessions %}
<tr>
<td>{{ session.id }}</td>
<td>{{ session.game.name }}</td>
<td>{{ session.status.value }}</td>
<td>
<ul>
{% for p in session.players %}
<li>{{ p.user.username }} (Screen: {{ p.screen ?? 'N/A' }})</li>
{% else %}
<li>No players</li>
{% endfor %}
</ul>
({{ session.players|length }} / {{ session.game.numberOfPlayers }})
</td>
<td>{{ session.created|date('Y-m-d H:i') }}</td>
<td>
<a href="{{ path('game_admin_view_session', {session: session.id}) }}">View Game Logs</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="6">No sessions found.</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}

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 %}

View File

@@ -5,6 +5,10 @@
{% block body %} {% block body %}
<h1>Game Dashboard</h1> <h1>Game Dashboard</h1>
{% if is_granted('ROLE_ADMIN') %}
<p><a href="{{ path('game_admin_dashboard') }}">Go to Game Admin Dashboard</a></p>
{% endif %}
<h2>Create New Session</h2> <h2>Create New Session</h2>
{% if availableGames is not empty %} {% if availableGames is not empty %}
<form method="post"> <form method="post">

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace App\Tests\Game; namespace App\Tests\Game;
use App\Game\Entity\Game;
use App\Game\Entity\Player; use App\Game\Entity\Player;
use App\Game\Entity\Session; use App\Game\Entity\Session;
use App\Game\Entity\SessionSetting; use App\Game\Entity\SessionSetting;
@@ -39,7 +40,8 @@ class GameResponseServiceChatVerifyCodeTest extends TestCase
$this->playerService, $this->playerService,
$this->sessionSettingRepository, $this->sessionSettingRepository,
$this->hub, $this->hub,
$this->entityManager $this->entityManager,
'H:\escapepage'
); );
$_ENV['MERCURE_TOPIC_BASE'] = 'http://test'; $_ENV['MERCURE_TOPIC_BASE'] = 'http://test';
@@ -50,8 +52,12 @@ class GameResponseServiceChatVerifyCodeTest extends TestCase
$user = new User(); $user = new User();
$user->setUsername('testuser'); $user->setUsername('testuser');
$game = $this->createMock(Game::class);
$game->method('getNumberOfPlayers')->willReturn(4);
$session = $this->createMock(Session::class); $session = $this->createMock(Session::class);
$session->method('getId')->willReturn(123); $session->method('getId')->willReturn(123);
$session->method('getGame')->willReturn($game);
$player = $this->createMock(Player::class); $player = $this->createMock(Player::class);
$player->method('getUser')->willReturn($user); $player->method('getUser')->willReturn($user);

View File

@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace App\Tests\Game;
use App\Game\Entity\Player;
use App\Game\Entity\Session;
use App\Game\Entity\SessionSetting;
use App\Game\Enum\SessionSettingType;
use App\Game\Repository\SessionSettingRepository;
use App\Game\Service\GameResponseService;
use App\Game\Service\PlayerService;
use App\Tech\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Mercure\HubInterface;
class SessionLoggingTest extends TestCase
{
private string $tempDir;
private $security;
private $playerService;
private $sessionSettingRepository;
private $hub;
private $entityManager;
private $service;
protected function setUp(): void
{
$this->tempDir = sys_get_temp_dir() . '/escapepage_test_' . uniqid();
mkdir($this->tempDir, 0777, true);
$this->security = $this->createMock(Security::class);
$this->playerService = $this->createMock(PlayerService::class);
$this->sessionSettingRepository = $this->createMock(SessionSettingRepository::class);
$this->hub = $this->createMock(HubInterface::class);
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->service = new GameResponseService(
$this->security,
$this->playerService,
$this->sessionSettingRepository,
$this->hub,
$this->entityManager,
$this->tempDir
);
}
protected function tearDown(): void
{
$this->removeDir($this->tempDir);
}
private function removeDir(string $dir): void
{
if (!is_dir($dir)) return;
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->removeDir("$dir/$file") : unlink("$dir/$file");
}
rmdir($dir);
}
public function testLogging(): void
{
$user = new User();
$user->setUsername('player1');
$session = $this->createMock(Session::class);
$session->method('getId')->willReturn(456);
$player = $this->createMock(Player::class);
$player->method('getUser')->willReturn($user);
$player->method('getSession')->willReturn($session);
$player->method('getScreen')->willReturn(1);
$this->security->method('getUser')->willReturn($user);
$this->playerService->method('GetCurrentlyActiveAsPlayer')->willReturn($player);
// Mock rights
$rightsSetting = new SessionSetting();
$rightsSetting->setValue(json_encode(['chat']));
$this->sessionSettingRepository->method('getSetting')
->willReturnMap([
[$session, SessionSettingType::RIGHTS_FOR_PLAYER1, $player, $rightsSetting],
]);
// Simulate 'help' command (always returns something)
$raw = json_encode(['message' => 'help', 'ts' => '123']);
$result = $this->service->getGameResponse($raw);
$this->assertNotEmpty($result);
$logFilePath = $this->tempDir . '/var/log/sessions/456/player1.txt';
$this->assertFileExists($logFilePath);
$logContent = file_get_contents($logFilePath);
$this->assertStringContainsString('PLAYER: help', $logContent);
$this->assertStringContainsString('SERVER:', $logContent);
}
}