Logfiles for sessions

This commit is contained in:
Frank
2026-01-08 18:14:56 +01:00
parent a475c1a268
commit 50d7ce745c
8 changed files with 277 additions and 2 deletions

View File

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

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace App\Game\Controller;
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;
#[Route('/admin')]
#[IsGranted('ROLE_ADMIN')]
final class GameAdminController extends AbstractController
{
#[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,
]);
}
}

View File

@@ -21,6 +21,7 @@ class GameResponseService
private SessionSettingRepository $sessionSettingRepository,
private HubInterface $hub,
private EntityManagerInterface $entityManager,
private string $projectDir,
) {
}
@@ -44,7 +45,7 @@ class GameResponseService
if(!$player)
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 = [];
@@ -54,9 +55,43 @@ class GameResponseService
$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;
}
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
{
$settingName = SessionSettingType::tryFrom('RightsForPlayer' . $player->getScreen());

View File

@@ -32,4 +32,16 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader
$this->getEntityManager()->persist($user);
$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', {session: session.id}) }}">View Game</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="6">No sessions found.</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}

View File

@@ -5,6 +5,10 @@
{% block body %}
<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>
{% if availableGames is not empty %}
<form method="post">

View File

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