|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Command; |
| 4 | + |
| 5 | +use App\Repository\GameServerRepository; |
| 6 | +use App\Service\GameServerOperations; |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use App\Service\Connection; |
| 9 | +use App\Service\LogService; |
| 10 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 11 | +use Symfony\Component\Console\Command\Command; |
| 12 | +use Symfony\Component\Console\Input\InputArgument; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | + |
| 16 | +#[AsCommand( |
| 17 | + name: 'cron:server:custom', |
| 18 | + description: 'Run the custom command of the game server', |
| 19 | +)] |
| 20 | +class CronServerCustomCommand extends Command |
| 21 | +{ |
| 22 | + #@var GameServerRepository |
| 23 | + private $gameServerRepository; |
| 24 | + |
| 25 | + #@var GameServerOperations |
| 26 | + private $gameOperations; |
| 27 | + |
| 28 | + #@var EntityManagerInterface |
| 29 | + private $em; |
| 30 | + |
| 31 | + #@var Connection |
| 32 | + private $connection; |
| 33 | + |
| 34 | + #@var LogService |
| 35 | + private $logService; |
| 36 | + |
| 37 | + #@param GameServerRepository |
| 38 | + #@param GameServerOperations |
| 39 | + #@param EntityManagerInterface |
| 40 | + #@param Connection |
| 41 | + public function __construct( |
| 42 | + GameServerRepository $gameServerRepository, |
| 43 | + GameServerOperations $gameOperations, |
| 44 | + EntityManagerInterface $em, |
| 45 | + Connection $connection, |
| 46 | + LogService $logService |
| 47 | + ) |
| 48 | + { |
| 49 | + $this->gameServerRepository = $gameServerRepository; |
| 50 | + $this->gameOperations = $gameOperations; |
| 51 | + $this->em = $em; |
| 52 | + $this->connection = $connection; |
| 53 | + $this->logService = $logService; |
| 54 | + |
| 55 | + parent::__construct(); |
| 56 | + } |
| 57 | + |
| 58 | + protected function configure(): void |
| 59 | + { |
| 60 | + $this->addArgument('id', InputArgument::REQUIRED, 'id of game server'); |
| 61 | + } |
| 62 | + |
| 63 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 64 | + { |
| 65 | + $id = $input->getArgument('id'); |
| 66 | + $game = $this->gameServerRepository->findById($id); |
| 67 | + |
| 68 | + if (null === $game) { |
| 69 | + $output->writeln('Game server not found'); |
| 70 | + |
| 71 | + return Command::FAILURE; |
| 72 | + } |
| 73 | + |
| 74 | + if (null === $game->getCommandCustomInternal()) { |
| 75 | + $output->writeln('No custom command set'); |
| 76 | + |
| 77 | + return Command::FAILURE; |
| 78 | + } |
| 79 | + |
| 80 | + $connection = $this->connection->getConnection($game->getServer()); |
| 81 | + if (null === $connection) { |
| 82 | + $output->writeln('Failed to connect to server'); |
| 83 | + |
| 84 | + return Command::FAILURE; |
| 85 | + } |
| 86 | + |
| 87 | + $name = $this->gameOperations->getGameServerNameScreen($game); |
| 88 | + $cmd = $game->getCommandCustomInternal(); |
| 89 | + |
| 90 | + $command = "screen -S $name -X stuff \"$cmd\"`echo -ne '\015'`"; |
| 91 | + |
| 92 | + $output->writeln('Custom command sended!'); |
| 93 | + $response = $this->connection->sendCommand($connection, $command); |
| 94 | + sleep(10); |
| 95 | + |
| 96 | + if (false === $response) { |
| 97 | + $output->writeln('Failed to send custom command on game server'); |
| 98 | + |
| 99 | + return Command::FAILURE; |
| 100 | + } |
| 101 | + |
| 102 | + return Command::SUCCESS; |
| 103 | + } |
| 104 | +} |
0 commit comments