|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rottenwood\KingdomBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Rottenwood\KingdomBundle\Command\Infrastructure\CommandResponse; |
|
6
|
|
|
use Rottenwood\KingdomBundle\Command\Infrastructure\GameCommandInterface; |
|
7
|
|
|
use Rottenwood\KingdomBundle\Entity\Infrastructure\User; |
|
8
|
|
|
use Rottenwood\KingdomBundle\Exception\CommandNotFound; |
|
9
|
|
|
use Rottenwood\KingdomBundle\Exception\InvalidCommandResponse; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Game entry point. Executes another game commands |
|
17
|
|
|
*/ |
|
18
|
|
|
class ExecuteCommand extends ContainerAwareCommand |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** {@inheritDoc} */ |
|
22
|
|
|
protected function configure() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->setName('kingdom:execute'); |
|
25
|
|
|
$this->setDescription('Входная точка игры'); |
|
26
|
|
|
|
|
27
|
|
|
$this->addArgument( |
|
28
|
|
|
'userId', |
|
29
|
|
|
InputArgument::REQUIRED, |
|
30
|
|
|
'id игрока' |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
$this->addArgument( |
|
34
|
|
|
'externalCommand', |
|
35
|
|
|
InputArgument::REQUIRED, |
|
36
|
|
|
'команда для выполнения' |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
$this->addArgument( |
|
40
|
|
|
'parameters', |
|
41
|
|
|
InputArgument::OPTIONAL, |
|
42
|
|
|
'аргументы команды' |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** {@inheritDoc} */ |
|
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
48
|
|
|
{ |
|
49
|
|
|
$userId = $input->getArgument('userId'); |
|
50
|
|
|
$command = $input->getArgument('externalCommand'); |
|
51
|
|
|
$parameters = $input->getArgument('parameters'); |
|
52
|
|
|
|
|
53
|
|
|
$output->writeln($this->executeExternal($userId, $command, $parameters)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Запуск внешней команды |
|
58
|
|
|
* @param int $userId |
|
59
|
|
|
* @param string $commandName |
|
60
|
|
|
* @param string|null $parameters |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
private function executeExternal(int $userId, string $commandName, $parameters): string |
|
64
|
|
|
{ |
|
65
|
|
|
$commandClass = __NAMESPACE__ . '\\Game\\' . ucfirst($commandName); |
|
66
|
|
|
$container = $this->getContainer(); |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
if (class_exists($commandClass)) { |
|
70
|
|
|
$userRepository = $container->get('doctrine')->getRepository(User::class); |
|
71
|
|
|
$user = $userRepository->find($userId); |
|
72
|
|
|
|
|
73
|
|
|
/** @var GameCommandInterface $command */ |
|
74
|
|
|
$command = new $commandClass($user, $commandName, $parameters, $container); |
|
75
|
|
|
} else { |
|
76
|
|
|
throw new CommandNotFound(sprintf('Команда %s не найдена', $commandName)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$result = $command->execute(); |
|
80
|
|
|
|
|
81
|
|
|
if (!$result instanceof CommandResponse) { |
|
82
|
|
|
throw new InvalidCommandResponse; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$resultData = $result->getContents(); |
|
86
|
|
|
} catch (\Exception $exception) { |
|
87
|
|
|
$logger = $container->get('kingdom.logger.commands_errors'); |
|
88
|
|
|
$logger->info($exception->getMessage(), ['exception' => $exception]); |
|
89
|
|
|
|
|
90
|
|
|
$resultData = []; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return json_encode($resultData); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|