Messages handling voor spel 1
This commit is contained in:
67
src/Game/Entity/Game.php
Normal file
67
src/Game/Entity/Game.php
Normal 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;
|
||||
}
|
||||
}
|
||||
84
src/Game/Entity/Player.php
Normal file
84
src/Game/Entity/Player.php
Normal 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;
|
||||
}
|
||||
}
|
||||
89
src/Game/Entity/Session.php
Normal file
89
src/Game/Entity/Session.php
Normal 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;
|
||||
}
|
||||
}
|
||||
10
src/Game/Enum/DecodeMessage.php
Normal file
10
src/Game/Enum/DecodeMessage.php
Normal 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.';
|
||||
}
|
||||
10
src/Game/Enum/GameStatus.php
Normal file
10
src/Game/Enum/GameStatus.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Game\Enum;
|
||||
|
||||
enum GameStatus: string
|
||||
{
|
||||
case IN_DEVELOPMENT = 'inDevelopment';
|
||||
case LOCKED = 'locked';
|
||||
case OPEN = 'open';
|
||||
}
|
||||
12
src/Game/Enum/SessionStatus.php
Normal file
12
src/Game/Enum/SessionStatus.php
Normal 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';
|
||||
}
|
||||
18
src/Game/Repository/GameRepository.php
Normal file
18
src/Game/Repository/GameRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
18
src/Game/Repository/PlayerRepository.php
Normal file
18
src/Game/Repository/PlayerRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
18
src/Game/Repository/SessionRepository.php
Normal file
18
src/Game/Repository/SessionRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,19 @@
|
||||
|
||||
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
|
||||
{
|
||||
public function __construct(
|
||||
private Security $security,
|
||||
private PlayerService $playerService,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getGameResponse(string $raw)
|
||||
{
|
||||
$info = json_decode($raw, true);
|
||||
@@ -11,10 +22,199 @@ class GameResponseService
|
||||
$message = $info['message'] ?? '';
|
||||
$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 = [];
|
||||
|
||||
if(str_starts_with($message, '/')) {
|
||||
$data = $this->checkGameCommando($message, $player);
|
||||
} else {
|
||||
$data = $this->checkConsoleCommando($message, $player);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
35
src/Game/Service/PlayerService.php
Normal file
35
src/Game/Service/PlayerService.php
Normal 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;
|
||||
}
|
||||
}
|
||||
22
src/Tech/Message/ProcessTaskMessage.php
Normal file
22
src/Tech/Message/ProcessTaskMessage.php
Normal 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;
|
||||
}
|
||||
}
|
||||
25
src/Tech/MessageHandler/ProcessTaskMessageHandler.php
Normal file
25
src/Tech/MessageHandler/ProcessTaskMessageHandler.php
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user