|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MiniGameApp\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use MiniGameApp\Command\CreateGameCommand; |
|
6
|
|
|
use MiniGameApp\Command\GameMoveCommand; |
|
7
|
|
|
use MiniGameApp\Command\JoinGameCommand; |
|
8
|
|
|
use MiniGameApp\Command\LeaveGameCommand; |
|
9
|
|
|
use MiniGameApp\Command\StartGameCommand; |
|
10
|
|
|
use MiniGameApp\Error\ErrorEventHandler; |
|
11
|
|
|
use MiniGameApp\Event\MiniGameAppErrorEvent; |
|
12
|
|
|
use MiniGameApp\Event\UnableToAddPlayerEvent; |
|
13
|
|
|
use MiniGameApp\Event\UnableToCreateGameEvent; |
|
14
|
|
|
use MiniGameApp\MiniGameFactory; |
|
15
|
|
|
use MiniGameApp\Repository\GameRepository; |
|
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
17
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
18
|
|
|
use Psr\Log\NullLogger; |
|
19
|
|
|
use RemiSan\Context\ContextContainer; |
|
20
|
|
|
|
|
21
|
|
|
class MiniGameCommandHandler implements LoggerAwareInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use LoggerAwareTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var MiniGameFactory |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $builder; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var GameRepository |
|
32
|
|
|
*/ |
|
33
|
|
|
private $gameManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ErrorEventHandler |
|
37
|
|
|
*/ |
|
38
|
|
|
private $errorHandler; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor |
|
42
|
|
|
* |
|
43
|
|
|
* @param MiniGameFactory $builder |
|
44
|
|
|
* @param GameRepository $gameManager |
|
45
|
33 |
|
* @param ErrorEventHandler $errorHandler |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
MiniGameFactory $builder, |
|
49
|
|
|
GameRepository $gameManager, |
|
50
|
33 |
|
ErrorEventHandler $errorHandler |
|
51
|
33 |
|
) { |
|
52
|
33 |
|
$this->builder = $builder; |
|
53
|
33 |
|
$this->gameManager = $gameManager; |
|
54
|
33 |
|
$this->errorHandler = $errorHandler; |
|
55
|
|
|
$this->logger = new NullLogger(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Handles a CreateGameCommand |
|
60
|
|
|
* |
|
61
|
|
|
* @param CreateGameCommand $command |
|
62
|
6 |
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
6 |
|
public function handleCreateGameCommand(CreateGameCommand $command) |
|
65
|
|
|
{ |
|
66
|
|
|
ContextContainer::setContext($command->getContext()); |
|
67
|
6 |
|
|
|
68
|
6 |
|
try { |
|
69
|
6 |
|
$miniGame = $this->builder->createMiniGame( |
|
70
|
6 |
|
$command->getGameId(), |
|
71
|
4 |
|
$command->getPlayerId(), |
|
72
|
|
|
$command->getOptions() |
|
73
|
3 |
|
); |
|
74
|
5 |
|
|
|
75
|
3 |
|
$this->gameManager->save($miniGame); |
|
76
|
3 |
|
} catch (\Exception $e) { |
|
77
|
3 |
|
$this->errorHandler->handle( |
|
78
|
3 |
|
new UnableToCreateGameEvent( |
|
79
|
3 |
|
$command->getGameId(), |
|
80
|
2 |
|
$command->getPlayerId(), |
|
81
|
3 |
|
$e->getMessage() |
|
82
|
2 |
|
), |
|
83
|
|
|
$command->getContext() |
|
84
|
|
|
); |
|
85
|
6 |
|
} |
|
86
|
6 |
|
|
|
87
|
|
|
ContextContainer::reset(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Handles a StartGameCommand |
|
92
|
|
|
* |
|
93
|
|
|
* @param StartGameCommand $command |
|
94
|
6 |
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
6 |
|
public function handleStartGameCommand(StartGameCommand $command) |
|
97
|
|
|
{ |
|
98
|
|
|
ContextContainer::setContext($command->getContext()); |
|
99
|
6 |
|
|
|
100
|
|
|
try { |
|
101
|
6 |
|
$miniGame = $this->gameManager->load($command->getGameId()); |
|
102
|
|
|
|
|
103
|
3 |
|
$miniGame->startGame($command->getPlayerId()); |
|
104
|
5 |
|
|
|
105
|
3 |
|
$this->gameManager->save($miniGame); |
|
106
|
3 |
|
} catch (\Exception $e) { |
|
107
|
3 |
|
$this->errorHandler->handle( |
|
108
|
3 |
|
new MiniGameAppErrorEvent( |
|
109
|
3 |
|
$command->getGameId(), |
|
110
|
2 |
|
$command->getPlayerId(), |
|
111
|
3 |
|
$e->getMessage() |
|
112
|
2 |
|
), |
|
113
|
|
|
$command->getContext() |
|
114
|
|
|
); |
|
115
|
6 |
|
} |
|
116
|
6 |
|
|
|
117
|
|
|
ContextContainer::reset(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Handles a JoinGameCommand |
|
122
|
|
|
* |
|
123
|
|
|
* @param JoinGameCommand $command |
|
124
|
6 |
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
6 |
|
public function handleJoinGameCommand(JoinGameCommand $command) |
|
127
|
|
|
{ |
|
128
|
|
|
ContextContainer::setContext($command->getContext()); |
|
129
|
6 |
|
|
|
130
|
|
|
try { |
|
131
|
6 |
|
$miniGame = $this->gameManager->load($command->getGameId()); |
|
132
|
|
|
|
|
133
|
3 |
|
$miniGame->addPlayerToGame($command->getPlayerOptions()); |
|
134
|
5 |
|
|
|
135
|
3 |
|
$this->gameManager->save($miniGame); |
|
136
|
3 |
|
} catch (\Exception $e) { |
|
137
|
3 |
|
$this->errorHandler->handle( |
|
138
|
3 |
|
new UnableToAddPlayerEvent( |
|
139
|
3 |
|
$command->getGameId(), |
|
140
|
2 |
|
$command->getPlayerId(), |
|
141
|
3 |
|
$e->getMessage() |
|
142
|
2 |
|
), |
|
143
|
|
|
$command->getContext() |
|
144
|
|
|
); |
|
145
|
6 |
|
} |
|
146
|
6 |
|
|
|
147
|
|
|
ContextContainer::reset(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Handles a LeaveGameCommand |
|
152
|
|
|
* |
|
153
|
|
|
* @param LeaveGameCommand $command |
|
154
|
6 |
|
* @return void |
|
155
|
|
|
*/ |
|
156
|
6 |
|
public function handleLeaveGameCommand(LeaveGameCommand $command) |
|
157
|
|
|
{ |
|
158
|
|
|
ContextContainer::setContext($command->getContext()); |
|
159
|
6 |
|
|
|
160
|
|
|
try { |
|
161
|
6 |
|
$miniGame = $this->gameManager->load($command->getGameId()); |
|
162
|
|
|
|
|
163
|
3 |
|
$miniGame->leaveGame($command->getPlayerId()); |
|
164
|
5 |
|
|
|
165
|
3 |
|
$this->gameManager->save($miniGame); |
|
166
|
3 |
|
} catch (\Exception $e) { |
|
167
|
3 |
|
$this->errorHandler->handle( |
|
168
|
3 |
|
new MiniGameAppErrorEvent( |
|
169
|
3 |
|
$command->getGameId(), |
|
170
|
2 |
|
$command->getPlayerId(), |
|
171
|
3 |
|
$e->getMessage() |
|
172
|
2 |
|
), |
|
173
|
|
|
$command->getContext() |
|
174
|
|
|
); |
|
175
|
6 |
|
} |
|
176
|
6 |
|
|
|
177
|
|
|
ContextContainer::reset(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Handles a GameMoveCommand |
|
182
|
|
|
* |
|
183
|
|
|
* @param GameMoveCommand $command |
|
184
|
9 |
|
* @return void |
|
185
|
|
|
*/ |
|
186
|
9 |
|
public function handleGameMoveCommand(GameMoveCommand $command) |
|
187
|
|
|
{ |
|
188
|
|
|
ContextContainer::setContext($command->getContext()); |
|
189
|
9 |
|
|
|
190
|
|
|
try { |
|
191
|
6 |
|
$miniGame = $this->gameManager->load($command->getGameId()); |
|
192
|
|
|
|
|
193
|
3 |
|
$miniGame->play($command->getPlayerId(), $command->getMove()); |
|
194
|
8 |
|
|
|
195
|
6 |
|
$this->gameManager->save($miniGame); |
|
196
|
6 |
|
} catch (\Exception $e) { |
|
197
|
6 |
|
$this->errorHandler->handle( |
|
198
|
6 |
|
new MiniGameAppErrorEvent( |
|
199
|
6 |
|
$command->getGameId(), |
|
200
|
4 |
|
$command->getPlayerId(), |
|
201
|
6 |
|
$e->getMessage() |
|
202
|
4 |
|
), |
|
203
|
|
|
$command->getContext() |
|
204
|
|
|
); |
|
205
|
9 |
|
} |
|
206
|
9 |
|
|
|
207
|
|
|
ContextContainer::reset(); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|