1 | <?php |
||
17 | class HangmanPlayer extends SimpleEventSourcedEntity implements Player |
||
18 | { |
||
19 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
20 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
21 | ////////////////////////////////////////////// CONSTANTS /////////////////////////////////////////////////// |
||
22 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
23 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
24 | |||
25 | const STATE_IN_GAME = 'in-game'; |
||
26 | const STATE_LOST = 'lost'; |
||
27 | const STATE_WON = 'won'; |
||
28 | |||
29 | const DEFAULT_LIVES = 6; |
||
30 | |||
31 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
32 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
33 | ///////////////////////////////////////////// PROPERTIES /////////////////////////////////////////////////// |
||
34 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
35 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
36 | |||
37 | /** |
||
38 | * @var PlayerId |
||
39 | */ |
||
40 | private $id; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $name; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | private $lives; |
||
51 | |||
52 | /** |
||
53 | * @var string[] |
||
54 | */ |
||
55 | private $playedLetters; |
||
56 | |||
57 | /** |
||
58 | * @var Hangman |
||
59 | */ |
||
60 | private $game; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $state; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $externalReference; |
||
71 | |||
72 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
73 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
74 | ////////////////////////////////////////// PUBLIC CONSTRUCTOR ////////////////////////////////////////////// |
||
75 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
76 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
77 | |||
78 | /** |
||
79 | * Constructor |
||
80 | * |
||
81 | * @param PlayerId $id |
||
82 | * @param string $name |
||
83 | * @param int $lives |
||
84 | * @param Hangman $game |
||
85 | * @param string $externalReference |
||
86 | */ |
||
87 | 99 | public function __construct( |
|
106 | |||
107 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
108 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
109 | ////////////////////////////////////////////// ACCESSORS /////////////////////////////////////////////////// |
||
110 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
111 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
112 | |||
113 | /** |
||
114 | * Returns the id of the player |
||
115 | * |
||
116 | * @return PlayerId |
||
117 | */ |
||
118 | 75 | public function getId() |
|
122 | |||
123 | /** |
||
124 | * Returns the name of the player |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 6 | public function getName() |
|
132 | |||
133 | /** |
||
134 | * Gets the game |
||
135 | * |
||
136 | * @return MiniGame |
||
137 | */ |
||
138 | 6 | public function getGame() |
|
142 | |||
143 | /** |
||
144 | * Gets the number of lives remaining |
||
145 | * |
||
146 | * @return int |
||
147 | */ |
||
148 | 63 | public function getRemainingLives() |
|
152 | |||
153 | /** |
||
154 | * Gets the played letters |
||
155 | * |
||
156 | * @return string[] |
||
157 | */ |
||
158 | 57 | public function getPlayedLetters() |
|
162 | |||
163 | /** |
||
164 | * Gets the external reference |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 3 | public function getExternalReference() |
|
172 | |||
173 | /** |
||
174 | * Gets the state |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | 21 | public function getState() |
|
182 | |||
183 | /** |
||
184 | * @return bool |
||
185 | */ |
||
186 | 27 | public function hasLost() |
|
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | 21 | public function hasWon() |
|
198 | |||
199 | /** |
||
200 | * @param Player $player |
||
201 | * @return bool |
||
202 | */ |
||
203 | 6 | public function equals(Player $player = null) |
|
207 | |||
208 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
209 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
210 | /////////////////////////////////////////// DOMAIN METHODS ///////////////////////////////////////////////// |
||
211 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
212 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
213 | |||
214 | /** |
||
215 | * @param string $letter |
||
216 | * @param int $livesLost |
||
217 | * |
||
218 | * @return HangmanBadLetterProposedEvent |
||
219 | */ |
||
220 | 15 | public function playBadLetter($letter, $livesLost) |
|
239 | |||
240 | /** |
||
241 | * @param string $letter |
||
242 | * |
||
243 | * @return HangmanGoodLetterProposedEvent |
||
244 | */ |
||
245 | 15 | public function playGoodLetter($letter) |
|
263 | |||
264 | /** |
||
265 | * @param Word $word |
||
266 | * |
||
267 | * @return HangmanPlayerWinEvent |
||
268 | */ |
||
269 | 9 | public function win(Word $word) |
|
282 | |||
283 | /** |
||
284 | * @param Word $word |
||
285 | * |
||
286 | * @return HangmanPlayerLostEvent |
||
287 | */ |
||
288 | 24 | public function lose(Word $word) |
|
304 | |||
305 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
306 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
307 | //////////////////////////////////////////// UTIL METHODS ////////////////////////////////////////////////// |
||
308 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
309 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
310 | |||
311 | /** |
||
312 | * Players played a letter |
||
313 | * |
||
314 | * @param string $letter |
||
315 | */ |
||
316 | 27 | private function playedLetter($letter) |
|
320 | |||
321 | /** |
||
322 | * Player loses a life |
||
323 | * |
||
324 | * @param int $nbLives |
||
325 | */ |
||
326 | 15 | private function lifeLost($nbLives = 1) |
|
330 | |||
331 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
332 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
333 | //////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
||
334 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
335 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
336 | |||
337 | /** |
||
338 | * Apply the bad letter played event |
||
339 | * |
||
340 | * @param HangmanBadLetterProposedEvent $event |
||
341 | * @return void |
||
342 | */ |
||
343 | 15 | protected function applyHangmanBadLetterProposedEvent(HangmanBadLetterProposedEvent $event) |
|
348 | |||
349 | /** |
||
350 | * Apply the bad letter played event |
||
351 | * |
||
352 | * @param HangmanGoodLetterProposedEvent $event |
||
353 | * @return void |
||
354 | */ |
||
355 | 15 | protected function applyHangmanGoodLetterProposedEvent(HangmanGoodLetterProposedEvent $event) |
|
359 | |||
360 | /** |
||
361 | * Apply the hangman player lost event |
||
362 | * |
||
363 | * @param HangmanPlayerLostEvent $event |
||
364 | */ |
||
365 | 24 | protected function applyHangmanPlayerLostEvent(HangmanPlayerLostEvent $event) |
|
369 | |||
370 | /** |
||
371 | * Apply the hangman player win event |
||
372 | * |
||
373 | * @param HangmanPlayerWinEvent $event |
||
374 | */ |
||
375 | 9 | protected function applyHangmanPlayerWinEvent(HangmanPlayerWinEvent $event) |
|
379 | |||
380 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
381 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
382 | ///////////////////////////////////////// APPLY RESTRICTIONS /////////////////////////////////////////////// |
||
383 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
384 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
385 | |||
386 | |||
387 | /** |
||
388 | * @param mixed $event |
||
389 | */ |
||
390 | 93 | public function handleRecursively($event) |
|
398 | |||
399 | /** |
||
400 | * @param mixed $event |
||
401 | * |
||
402 | * @return bool |
||
403 | */ |
||
404 | 93 | private function isSupportedEvent($event) |
|
412 | } |
||
413 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.