Messages handling voor spel 1

This commit is contained in:
Frank
2026-01-05 15:27:37 +01:00
parent c0aa2ad44e
commit af13be2196
16 changed files with 681 additions and 1 deletions

View File

@@ -26,4 +26,4 @@ framework:
Symfony\Component\Notifier\Message\SmsMessage: async Symfony\Component\Notifier\Message\SmsMessage: async
# Route your messages to the transports # Route your messages to the transports
# 'App\Message\YourMessage': async 'App\Tech\Message\ProcessTaskMessage': async

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20260105113139 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE game (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, number_of_players INT NOT NULL, status VARCHAR(20) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE player (id INT AUTO_INCREMENT NOT NULL, session_id INT NOT NULL, user_id INT NOT NULL, screen INT NOT NULL, INDEX IDX_98197A65613FECDF (session_id), INDEX IDX_98197A65A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE session (id INT AUTO_INCREMENT NOT NULL, game_id INT NOT NULL, status VARCHAR(20) NOT NULL, timer INT NOT NULL, created DATETIME NOT NULL, INDEX IDX_D044D5D4E48FD905 (game_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE player ADD CONSTRAINT FK_98197A65613FECDF FOREIGN KEY (session_id) REFERENCES session (id)');
$this->addSql('ALTER TABLE player ADD CONSTRAINT FK_98197A65A76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id)');
$this->addSql('ALTER TABLE session ADD CONSTRAINT FK_D044D5D4E48FD905 FOREIGN KEY (game_id) REFERENCES game (id)');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE player DROP FOREIGN KEY FK_98197A65613FECDF');
$this->addSql('ALTER TABLE player DROP FOREIGN KEY FK_98197A65A76ED395');
$this->addSql('ALTER TABLE session DROP FOREIGN KEY FK_D044D5D4E48FD905');
$this->addSql('DROP TABLE game');
$this->addSql('DROP TABLE player');
$this->addSql('DROP TABLE session');
}
}

View File

@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20260105121159 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE player ADD level JSON DEFAULT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE player DROP level');
}
}

67
src/Game/Entity/Game.php Normal file
View File

@@ -0,0 +1,67 @@
<?php
namespace App\Game\Entity;
use App\Game\Enum\GameStatus;
use App\Game\Repository\GameRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: GameRepository::class)]
#[ORM\Table(name: 'game')]
class Game
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column]
private ?int $numberOfPlayers = null;
#[ORM\Column(type: 'string', length: 20, enumType: GameStatus::class)]
private ?GameStatus $status = null;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getNumberOfPlayers(): ?int
{
return $this->numberOfPlayers;
}
public function setNumberOfPlayers(int $numberOfPlayers): static
{
$this->numberOfPlayers = $numberOfPlayers;
return $this;
}
public function getStatus(): ?GameStatus
{
return $this->status;
}
public function setStatus(GameStatus $status): static
{
$this->status = $status;
return $this;
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace App\Game\Entity;
use App\Game\Repository\PlayerRepository;
use App\Tech\Entity\User;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: PlayerRepository::class)]
#[ORM\Table(name: 'player')]
class Player
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Session::class)]
#[ORM\JoinColumn(nullable: false)]
private ?Session $session = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
#[ORM\Column]
private ?int $screen = null;
#[ORM\Column(type: 'json', nullable: true)]
private ?array $level = null;
public function getId(): ?int
{
return $this->id;
}
public function getSession(): ?Session
{
return $this->session;
}
public function setSession(?Session $session): static
{
$this->session = $session;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
public function getScreen(): ?int
{
return $this->screen;
}
public function setScreen(int $screen): static
{
$this->screen = $screen;
return $this;
}
public function getLevel(): ?array
{
return $this->level;
}
public function setLevel(?array $level): static
{
$this->level = $level;
return $this;
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace App\Game\Entity;
use App\Game\Enum\SessionStatus;
use App\Game\Repository\SessionRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SessionRepository::class)]
#[ORM\Table(name: 'session')]
class Session
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Game::class)]
#[ORM\JoinColumn(nullable: false)]
private ?Game $game = null;
#[ORM\Column(type: 'string', length: 20, enumType: SessionStatus::class)]
private ?SessionStatus $status = null;
#[ORM\Column]
private ?int $timer = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $created = null;
public function __construct()
{
$this->created = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getGame(): ?Game
{
return $this->game;
}
public function setGame(?Game $game): static
{
$this->game = $game;
return $this;
}
public function getStatus(): ?SessionStatus
{
return $this->status;
}
public function setStatus(SessionStatus $status): static
{
$this->status = $status;
return $this;
}
public function getTimer(): ?int
{
return $this->timer;
}
public function setTimer(int $timer): static
{
$this->timer = $timer;
return $this;
}
public function getCreated(): ?\DateTimeInterface
{
return $this->created;
}
public function setCreated(\DateTimeInterface $created): static
{
$this->created = $created;
return $this;
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Game\Enum;
enum DecodeMessage: string
{
case TEST = 'This is a test decoding message.';
case SECRET = 'The secret code is 42.';
case WELCOME = 'Welcome to the system, agent.';
}

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Game\Enum;
enum GameStatus: string
{
case IN_DEVELOPMENT = 'inDevelopment';
case LOCKED = 'locked';
case OPEN = 'open';
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Game\Enum;
enum SessionStatus: string
{
case CREATED = 'created';
case READY = 'ready';
case PLAYING = 'playing';
case WON = 'won';
case LOST = 'lost';
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Game\Repository;
use App\Game\Entity\Game;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Game>
*/
class GameRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Game::class);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Game\Repository;
use App\Game\Entity\Player;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Player>
*/
class PlayerRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Player::class);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Game\Repository;
use App\Game\Entity\Session;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Session>
*/
class SessionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Session::class);
}
}

View File

@@ -2,8 +2,19 @@
namespace App\Game\Service; namespace App\Game\Service;
use App\Game\Enum\DecodeMessage;
use App\Game\Entity\Player;
use App\Tech\Entity\User;
use Symfony\Bundle\SecurityBundle\Security;
class GameResponseService class GameResponseService
{ {
public function __construct(
private Security $security,
private PlayerService $playerService,
) {
}
public function getGameResponse(string $raw) public function getGameResponse(string $raw)
{ {
$info = json_decode($raw, true); $info = json_decode($raw, true);
@@ -11,10 +22,199 @@ class GameResponseService
$message = $info['message'] ?? ''; $message = $info['message'] ?? '';
$ts = $info['ts'] ?? ''; $ts = $info['ts'] ?? '';
if(!is_string($message))
return ['error' => 'Invalid message.'];
$user = $this->security->getUser();
if(!$user instanceof User)
return ['error' => 'You are not logged in.'];
$player = $this->playerService->GetCurrentlyActiveAsPlayer($user);
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.
$data = []; $data = [];
if(str_starts_with($message, '/')) {
$data = $this->checkGameCommando($message, $player);
} else {
$data = $this->checkConsoleCommando($message, $player);
}
return $data; return $data;
} }
private function checkGameCommando(string $message, Player $player) : array
{
$messagePart = explode(' ', $message);
$rechten = json_decode($player->getLevel());
switch($messagePart[0]) {
case '/chat':
if(!in_array('chat', $rechten))
return ['result' => ['Unknown command.']];
$this->handleChatMessage($message);
break;
case '/help':
return ['result' => $this->getHelpCommand($rechten)];
case '/decode':
if(!in_array('decode', $rechten))
return ['result' => ['Unknown command.']];
return ['result' => [$this->handleDecodeMessage($messagePart[1], $player)]];
case '/verify':
if(!in_array('verify', $rechten))
return ['result' => ['Unknown command.']];
$this->handleVerifyMessage($message);
break;
default:
return ['result' => ['Unknown command.']];
}
return [];
}
private function checkConsoleCommando(string $message, Player $player) : array
{
$messagePart = explode(' ', $message);
$rechten = json_decode($player->getLevel());
switch($messagePart[0]) {
case 'help':
return ['result' => $this->getHelpCommand($rechten)];
case 'ls':
break;
case 'cd':
break;
case 'rm':
break;
case 'sudo':
break;
default:
return ['result' => ['Unknown command.']];
}
return [];
}
private function getHelpCommand(mixed $rechten) : array
{
$messages = [];
foreach($rechten as $recht) {
switch($recht) {
case 'chat':
$messages[] = '/chat';
$messages[] = ' Use /chat {message} to send the message to the other agents.';
$messages[] = ' If you want to send a message specifically to one other agent, use the id of the agent after /chat, like /chat 6 {message}';
$messages[] = ' This will send the message only to agent with id 6.';
$messages[] = ' USAGE: /chat {message}';
$messages[] = '';
break;
case 'help':
$messages[] = '/help';
$messages[] = ' This shows this help.';
$messages[] = ' USAGE: /help';
$messages[] = '';
break;
case 'decode':
$messages[] = '/decode';
$messages[] = ' This message will decode the message followed by it.';
$messages[] = ' Every agent has a different way to decode messages. This is a security measure. The AI Virus has no access to all decoders.';
$messages[] = ' USAGE: /decode {message}';
$messages[] = '';
break;
case 'cat':
$messages[] = 'cat';
$messages[] = ' To read a file, use cat {filename}.';
$messages[] = ' This will print the full content of the file on the screen.';
$messages[] = ' USAGE: cat {filename}';
$messages[] = '';
break;
case 'ls':
$messages[] = 'ls';
$messages[] = ' To show all the files in the current directory, use ls.';
$messages[] = ' This will print the full list of directories and files of the current location on your screen.';
$messages[] = ' USAGE: ls';
$messages[] = '';
break;
case 'rm':
$messages[] = 'rm';
$messages[] = ' Use rm to delete a file.';
$messages[] = ' Be careful with this command. It can not be undone and we do not want to lose any valuable data.';
$messages[] = ' USAGE: rm {filename}';
$messages[] = '';
break;
case 'cd':
$messages[] = 'cd';
$messages[] = ' Use cd to move to a different directory.';
$messages[] = ' You can go into a folder by using cd {foldername}, or a folder up by using "cd ..".';
$messages[] = ' Using cd / moves you to the root directory.';
$messages[] = ' USAGE: cd {directory}';
$messages[] = '';
break;
case 'sudo':
$messages[] = 'sudo';
$messages[] = ' If you do not have enough rights to execute a command, you can use sudo to execute it as root.';
$messages[] = ' This is only possible for verified users. To verify yourself, use the /verify command.';
$messages[] = ' USAGE: sudo {command}';
$messages[] = '';
break;
case 'verify':
$messages[] = '/verify';
$messages[] = ' You can verify yourself by using this command.';
$messages[] = ' Use this command and follow instructions to verify yourself.';
$messages[] = ' USAGE: /verify';
$messages[] = '';
break;
}
}
return $messages;
}
private function handleChatMessage(string $message)
{
}
private function handleDecodeMessage(string $message, Player $player)
{
$userNumber = $player->getScreen();
preg_match('/\d+/', $message, $matches);
$num = $matches[0] ?? null;
$randomString = $this->generateRandomString(250, 500);
if(is_null($num) || $num != $userNumber)
return $randomString;
foreach (DecodeMessage::cases() as $decodeMessage) {
if ($decodeMessage->name === $message) {
return $decodeMessage->value;
}
}
return $randomString;
}
private function generateRandomString(int $min, int $max): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
$charactersLength = strlen($characters);
$randomString = '';
$length = random_int($min, $max);
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
private function handleVerifyMessage(string $message)
{
}
} }

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Game\Service;
use App\Game\Entity\Game;
use App\Game\Entity\Player;
use App\Game\Enum\SessionStatus;
use App\Game\Repository\PlayerRepository;
use App\Tech\Entity\User;
class PlayerService
{
public function __construct(
private PlayerRepository $playerRepository,
) {
}
public function GetCurrentlyActiveAsPlayer(User $user): ?Player
{
$player = $this->playerRepository->createQueryBuilder('p')
->join('p.session', 's')
->where('p.user = :user')
->andWhere('s.status IN (:statuses)')
->setParameter('user', $user)
->setParameter('statuses', [
SessionStatus::READY,
SessionStatus::PLAYING,
])
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
return $player;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Tech\Message;
class ProcessTaskMessage
{
public function __construct(
private string $taskName,
private array $payload = [],
) {
}
public function getTaskName(): string
{
return $this->taskName;
}
public function getPayload(): array
{
return $this->payload;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Tech\MessageHandler;
use App\Tech\Message\ProcessTaskMessage;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
class ProcessTaskMessageHandler
{
public function __construct(
private LoggerInterface $logger,
) {
}
public function __invoke(ProcessTaskMessage $message): void
{
$this->logger->info('Processing task: ' . $message->getTaskName(), [
'payload' => $message->getPayload(),
]);
// Implement logic based on taskName here
}
}