1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hangman\Entity; |
4
|
|
|
|
5
|
|
|
use Broadway\EventSourcing\EventSourcedAggregateRoot; |
6
|
|
|
use Hangman\Event\HangmanGameCreatedEvent; |
7
|
|
|
use Hangman\Event\HangmanGameFailedStartingEvent; |
8
|
|
|
use Hangman\Event\HangmanGameLostEvent; |
9
|
|
|
use Hangman\Event\HangmanGameStartedEvent; |
10
|
|
|
use Hangman\Event\HangmanPlayerCreatedEvent; |
11
|
|
|
use Hangman\Event\HangmanPlayerFailedCreatingEvent; |
12
|
|
|
use Hangman\Event\HangmanPlayerLostEvent; |
13
|
|
|
use Hangman\Event\HangmanPlayerProposedInvalidAnswerEvent; |
14
|
|
|
use Hangman\Event\HangmanPlayerTriedPlayingDuringAnotherPlayerTurnEvent; |
15
|
|
|
use Hangman\Event\HangmanPlayerTriedPlayingInactiveGameEvent; |
16
|
|
|
use Hangman\Event\HangmanPlayerTurnEvent; |
17
|
|
|
use Hangman\Event\HangmanPlayerWinEvent; |
18
|
|
|
use Hangman\Exception\HangmanException; |
19
|
|
|
use Hangman\Exception\HangmanPlayerOptionsException; |
20
|
|
|
use Hangman\Move\Answer; |
21
|
|
|
use Hangman\Move\Proposition; |
22
|
|
|
use Hangman\Options\HangmanPlayerOptions; |
23
|
|
|
use Hangman\Result\HangmanBadProposition; |
24
|
|
|
use Hangman\Result\HangmanGoodProposition; |
25
|
|
|
use Hangman\Result\HangmanLost; |
26
|
|
|
use Hangman\Result\HangmanWon; |
27
|
|
|
use MiniGame\Entity\MiniGame; |
28
|
|
|
use MiniGame\Entity\MiniGameId; |
29
|
|
|
use MiniGame\Entity\Player; |
30
|
|
|
use MiniGame\Entity\PlayerId; |
31
|
|
|
use MiniGame\Entity\PlayTrait; |
32
|
|
|
use MiniGame\GameResult; |
33
|
|
|
use MiniGame\PlayerOptions; |
34
|
|
|
|
35
|
|
|
class Hangman extends EventSourcedAggregateRoot implements MiniGame |
36
|
|
|
{ |
37
|
|
|
use PlayTrait; |
38
|
|
|
|
39
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
40
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
41
|
|
|
////////////////////////////////////////////// CONSTANTS /////////////////////////////////////////////////// |
42
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
43
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
44
|
|
|
|
45
|
|
|
const STATE_UNINITIALIZED = 'uninitialized'; |
46
|
|
|
const STATE_READY = 'ready'; |
47
|
|
|
const STATE_STARTED = 'started'; |
48
|
|
|
const STATE_OVER = 'over'; |
49
|
|
|
|
50
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
51
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
52
|
|
|
///////////////////////////////////////////// PROPERTIES /////////////////////////////////////////////////// |
53
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
54
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var MiniGameId |
58
|
|
|
*/ |
59
|
|
|
private $id; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $word; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var HangmanPlayer[] |
68
|
|
|
**/ |
69
|
|
|
private $players; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
protected $gameOrder; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var HangmanPlayer |
78
|
|
|
**/ |
79
|
|
|
private $currentPlayer; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
private $state; |
85
|
|
|
|
86
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
87
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
88
|
|
|
///////////////////////////////////////// PRIVATE CONSTRUCTOR ////////////////////////////////////////////// |
89
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
90
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Constructor |
94
|
|
|
*/ |
95
|
111 |
|
private function __construct() |
96
|
|
|
{ |
97
|
111 |
|
$this->state = self::STATE_UNINITIALIZED; |
98
|
111 |
|
} |
99
|
|
|
|
100
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
101
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
102
|
|
|
////////////////////////////////////////////// ACCESSORS /////////////////////////////////////////////////// |
103
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
104
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the id of the game |
108
|
|
|
* |
109
|
|
|
* @return MiniGameId |
110
|
|
|
*/ |
111
|
93 |
|
public function getId() |
112
|
|
|
{ |
113
|
93 |
|
return $this->id; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the aggregate id |
118
|
|
|
* |
119
|
|
|
* @return MiniGameId |
120
|
|
|
*/ |
121
|
111 |
|
public function getAggregateRootId() |
122
|
|
|
{ |
123
|
111 |
|
return $this->id; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the name of the mini-game |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
3 |
|
public static function getName() |
132
|
|
|
{ |
133
|
3 |
|
return 'HANGMAN'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the player identified by PlayerId |
138
|
|
|
* |
139
|
|
|
* @param PlayerId $playerId |
140
|
|
|
* |
141
|
|
|
* @return HangmanPlayer |
142
|
|
|
*/ |
143
|
57 |
|
public function getPlayer(PlayerId $playerId = null) |
144
|
|
|
{ |
145
|
57 |
|
if ($playerId === null) { |
146
|
3 |
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
54 |
|
return isset($this->players[(string)$playerId]) ? $this->players[(string)$playerId] : null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the player who can play |
154
|
|
|
* |
155
|
|
|
* @return Player |
156
|
|
|
*/ |
157
|
3 |
|
public function getCurrentPlayer() |
158
|
|
|
{ |
159
|
3 |
|
return $this->currentPlayer; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the players |
164
|
|
|
* |
165
|
|
|
* @return Player[] |
166
|
|
|
*/ |
167
|
111 |
|
public function getPlayers() |
168
|
|
|
{ |
169
|
111 |
|
return array_values($this->players); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Is game started? |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
36 |
|
public function isGameStarted() |
178
|
|
|
{ |
179
|
36 |
|
return $this->state === self::STATE_STARTED; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Is it the player's turn? |
184
|
|
|
* |
185
|
|
|
* @param PlayerId $playerId |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
36 |
|
public function canPlayerPlay(PlayerId $playerId) |
190
|
|
|
{ |
191
|
36 |
|
return $this->currentPlayer && $this->currentPlayer->getId()->equals($playerId); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
195
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
196
|
|
|
/////////////////////////////////////////// DOMAIN METHODS ///////////////////////////////////////////////// |
197
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
198
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Starts the game |
202
|
|
|
* |
203
|
|
|
* @param PlayerId $playerId |
204
|
|
|
* |
205
|
|
|
* @return GameResult |
206
|
|
|
*/ |
207
|
51 |
|
public function startGame(PlayerId $playerId) |
208
|
|
|
{ |
209
|
51 |
|
if ($this->state !== self::STATE_READY) { |
210
|
3 |
|
$event = new HangmanGameFailedStartingEvent( |
211
|
3 |
|
$this->id, |
212
|
2 |
|
$playerId, |
213
|
1 |
|
HangmanGameFailedStartingEvent::BAD_STATE |
214
|
2 |
|
); |
215
|
3 |
|
$this->apply($event); |
216
|
3 |
|
return $event; |
217
|
|
|
} |
218
|
|
|
|
219
|
51 |
|
if (count($this->players) === 0) { |
220
|
3 |
|
$event = new HangmanGameFailedStartingEvent( |
221
|
3 |
|
$this->id, |
222
|
2 |
|
$playerId, |
223
|
1 |
|
HangmanGameFailedStartingEvent::NO_PLAYER |
224
|
2 |
|
); |
225
|
3 |
|
$this->apply($event); |
226
|
3 |
|
return $event; |
227
|
|
|
} |
228
|
|
|
|
229
|
48 |
|
$event = new HangmanGameStartedEvent($this->id, $playerId); |
230
|
48 |
|
$this->apply($event); |
231
|
|
|
|
232
|
48 |
|
$this->setNextPlayer($playerId); |
233
|
|
|
|
234
|
48 |
|
return $event; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Adds a player to the game |
239
|
|
|
* |
240
|
|
|
* @param PlayerOptions $playerOptions |
241
|
|
|
* |
242
|
|
|
* @throws HangmanPlayerOptionsException |
243
|
|
|
* @throws HangmanException |
244
|
|
|
* |
245
|
|
|
* @return GameResult |
246
|
|
|
*/ |
247
|
75 |
|
public function addPlayerToGame(PlayerOptions $playerOptions) |
248
|
|
|
{ |
249
|
75 |
|
if (! $playerOptions instanceof HangmanPlayerOptions) { |
250
|
3 |
|
throw new HangmanPlayerOptionsException( |
251
|
3 |
|
$playerOptions->getPlayerId(), |
252
|
3 |
|
$this->getId(), |
253
|
1 |
|
'Options are not recognized' |
254
|
2 |
|
); |
255
|
|
|
} |
256
|
|
|
|
257
|
75 |
|
if ($this->state !== self::STATE_READY) { |
258
|
3 |
|
$event = new HangmanPlayerFailedCreatingEvent( |
259
|
3 |
|
$this->id, |
260
|
3 |
|
$playerOptions->getPlayerId(), |
261
|
3 |
|
$playerOptions->getExternalReference() |
262
|
2 |
|
); |
263
|
3 |
|
$this->apply($event); |
264
|
3 |
|
return $event; |
265
|
|
|
} |
266
|
|
|
|
267
|
75 |
|
$event = new HangmanPlayerCreatedEvent( |
268
|
75 |
|
$this->id, |
269
|
75 |
|
$playerOptions->getPlayerId(), |
270
|
75 |
|
$playerOptions->getName(), |
271
|
75 |
|
$playerOptions->getLives(), |
272
|
75 |
|
$playerOptions->getExternalReference() |
273
|
50 |
|
); |
274
|
75 |
|
$this->apply($event); |
275
|
75 |
|
return $event; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* A player leaves the game |
280
|
|
|
* |
281
|
|
|
* @param PlayerId $playerId |
282
|
|
|
* |
283
|
|
|
* @return GameResult |
284
|
|
|
*/ |
285
|
9 |
|
public function leaveGame(PlayerId $playerId) |
286
|
|
|
{ |
287
|
9 |
|
switch ($this->state) { |
288
|
9 |
|
case self::STATE_STARTED: |
289
|
3 |
|
$player = $this->getPlayer($playerId); |
290
|
3 |
|
return $player ? $this->playerLoses($player) : null; |
291
|
6 |
|
case self::STATE_OVER: |
292
|
3 |
|
break; |
293
|
2 |
|
default: |
294
|
3 |
|
if (isset($this->players[(string) $playerId])) { |
295
|
3 |
|
unset($this->players[(string) $playerId]); |
296
|
2 |
|
} |
297
|
3 |
|
break; |
298
|
4 |
|
} |
299
|
6 |
|
return null; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
303
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
304
|
|
|
////////////////////////////////////////// PRIVATE METHODS ///////////////////////////////////////////////// |
305
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
306
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Initialize the game |
310
|
|
|
* |
311
|
|
|
* @param MiniGameId $id |
312
|
|
|
* @param string $word |
313
|
|
|
*/ |
314
|
111 |
|
private function initialize(MiniGameId $id, $word) |
315
|
|
|
{ |
316
|
111 |
|
$this->apply(new HangmanGameCreatedEvent($id, $word)); |
317
|
111 |
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Player proposes a letter |
321
|
|
|
* |
322
|
|
|
* @param PlayerId $playerId |
323
|
|
|
* @param Proposition $move |
324
|
|
|
* |
325
|
|
|
* @return GameResult |
326
|
|
|
*/ |
327
|
21 |
|
private function playProposition(PlayerId $playerId, Proposition $move) |
328
|
|
|
{ |
329
|
21 |
|
if ($errorEvent = $this->ensurePlayerCanPlay($playerId)) { |
330
|
6 |
|
$this->apply($errorEvent); |
331
|
6 |
|
return $errorEvent; |
332
|
|
|
} |
333
|
|
|
|
334
|
15 |
|
return $this->currentPlayerProposeLetter($move->getText()); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Player tries an answer |
339
|
|
|
* |
340
|
|
|
* @param PlayerId $playerId |
341
|
|
|
* @param Answer $move |
342
|
|
|
* |
343
|
|
|
* @return GameResult |
344
|
|
|
*/ |
345
|
15 |
|
private function playAnswer(PlayerId $playerId, Answer $move) |
346
|
|
|
{ |
347
|
15 |
|
if ($errorEvent = $this->ensurePlayerCanPlay($playerId)) { |
348
|
3 |
|
$this->apply($errorEvent); |
349
|
3 |
|
return $errorEvent; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
try { |
353
|
12 |
|
return $this->currentPlayerProposeAnswer($move->getText()); |
354
|
3 |
|
} catch (HangmanException $e) { |
355
|
3 |
|
$event = new HangmanPlayerProposedInvalidAnswerEvent( |
356
|
3 |
|
$this->getId(), |
357
|
2 |
|
$playerId, |
358
|
|
|
$move |
359
|
2 |
|
); |
360
|
3 |
|
$this->apply($event); |
361
|
3 |
|
return $event; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Returns an error event if player cannot play |
367
|
|
|
* |
368
|
|
|
* @param PlayerId $playerId |
369
|
|
|
* |
370
|
|
|
* @return GameResult |
371
|
|
|
*/ |
372
|
36 |
|
private function ensurePlayerCanPlay(PlayerId $playerId) |
373
|
|
|
{ |
374
|
36 |
|
if (!$this->isGameStarted()) { |
375
|
3 |
|
$event = new HangmanPlayerTriedPlayingInactiveGameEvent( |
376
|
3 |
|
$this->getId(), |
377
|
|
|
$playerId |
378
|
2 |
|
); |
379
|
3 |
|
return $event; |
380
|
|
|
} |
381
|
|
|
|
382
|
33 |
|
if (!$this->canPlayerPlay($playerId)) { |
383
|
6 |
|
$event = new HangmanPlayerTriedPlayingDuringAnotherPlayerTurnEvent( |
384
|
6 |
|
$this->getId(), |
385
|
|
|
$playerId |
386
|
4 |
|
); |
387
|
6 |
|
return $event; |
388
|
|
|
} |
389
|
|
|
|
390
|
27 |
|
return null; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Propose a letter |
395
|
|
|
* |
396
|
|
|
* @param string $letter |
397
|
|
|
* |
398
|
|
|
* @return HangmanBadProposition | HangmanGoodProposition |
399
|
|
|
*/ |
400
|
15 |
|
private function currentPlayerProposeLetter($letter) |
401
|
|
|
{ |
402
|
15 |
|
$capLetter = strtoupper($letter); |
403
|
15 |
|
$letterPresent = $this->wordContains($capLetter); |
404
|
|
|
|
405
|
15 |
|
$result = (!$letterPresent) |
406
|
13 |
|
? $this->currentPlayerBadProposition($letter) // remove a life |
407
|
15 |
|
: $this->currentPlayerGoodProposition($letter); // yay! |
408
|
|
|
|
409
|
15 |
|
return $result; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Propose an answer |
414
|
|
|
* |
415
|
|
|
* @param string $answer |
416
|
|
|
* |
417
|
|
|
* @return HangmanLost | HangmanWon |
418
|
|
|
*/ |
419
|
12 |
|
private function currentPlayerProposeAnswer($answer) |
420
|
|
|
{ |
421
|
12 |
|
$this->checkAnswerIsValid($answer); |
422
|
|
|
|
423
|
9 |
|
if ($this->isTheAnswer(strtoupper($answer))) { |
424
|
3 |
|
return $this->playerWins($this->currentPlayer); // you win |
425
|
|
|
} else { |
426
|
6 |
|
return $this->playerLoses($this->currentPlayer); // you lose |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Function to call when a bad proposition has been made |
432
|
|
|
* |
433
|
|
|
* @param string $letter |
434
|
|
|
* |
435
|
|
|
* @return HangmanBadProposition | HangmanLost |
436
|
|
|
*/ |
437
|
9 |
|
private function currentPlayerBadProposition($letter) |
438
|
|
|
{ |
439
|
9 |
|
$player = $this->currentPlayer; |
440
|
|
|
|
441
|
9 |
|
$event = $player->playBadLetter($letter, 1); |
442
|
|
|
|
443
|
9 |
|
if ($event->getRemainingLives() === 0) { |
444
|
6 |
|
return $this->playerLoses($player); |
445
|
|
|
} |
446
|
|
|
|
447
|
3 |
|
$this->setNextPlayer($this->getNextPlayerId()); |
448
|
|
|
|
449
|
3 |
|
return $event; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Function to call after a good proposition of letter has been made |
454
|
|
|
* |
455
|
|
|
* @param string $letter |
456
|
|
|
* |
457
|
|
|
* @return HangmanGoodProposition | HangmanWon |
458
|
|
|
*/ |
459
|
6 |
|
private function currentPlayerGoodProposition($letter) |
460
|
|
|
{ |
461
|
6 |
|
$player = $this->currentPlayer; |
462
|
|
|
|
463
|
6 |
|
$event = $player->playGoodLetter($letter); |
464
|
|
|
|
465
|
6 |
|
if ($this->isAllLettersFoundForPlayer($player)) { |
466
|
3 |
|
return $this->playerWins($player); |
467
|
|
|
} |
468
|
|
|
|
469
|
3 |
|
$this->setNextPlayer($this->getNextPlayerId()); |
470
|
|
|
|
471
|
3 |
|
return $event; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Function to call when game is won by a player |
476
|
|
|
* |
477
|
|
|
* @param HangmanPlayer $player |
478
|
|
|
* |
479
|
|
|
* @return HangmanWon |
480
|
|
|
*/ |
481
|
6 |
|
private function playerWins(HangmanPlayer $player) |
482
|
|
|
{ |
483
|
6 |
|
$playerId = $player->getId(); |
484
|
|
|
|
485
|
6 |
|
$playedLetters = $this->getPlayedLettersForPlayer($playerId); |
486
|
6 |
|
$remainingLives = $this->getRemainingLives($playerId); |
487
|
|
|
|
488
|
6 |
|
$event = new HangmanPlayerWinEvent( |
489
|
6 |
|
$this->id, |
490
|
4 |
|
$playerId, |
491
|
4 |
|
$playedLetters, |
492
|
4 |
|
$remainingLives, |
493
|
6 |
|
$this->word |
494
|
4 |
|
); |
495
|
6 |
|
$this->apply($event); |
496
|
|
|
|
497
|
6 |
|
foreach ($this->players as $otherPlayer) { |
498
|
6 |
|
if ($otherPlayer->equals($player) || $otherPlayer->hasLost()) { |
499
|
6 |
|
continue; |
500
|
|
|
} |
501
|
6 |
|
$otherPlayer->lose($this->word); |
502
|
4 |
|
} |
503
|
|
|
|
504
|
6 |
|
return $event; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* Function to call when game is lost by a player |
509
|
|
|
* |
510
|
|
|
* @param HangmanPlayer $player |
511
|
|
|
* |
512
|
|
|
* @return hangmanLost | HangmanGameLostEvent |
513
|
|
|
*/ |
514
|
15 |
|
private function playerLoses(HangmanPlayer $player) |
515
|
|
|
{ |
516
|
15 |
|
$event = $player->lose($this->word); |
517
|
|
|
|
518
|
15 |
|
if ($this->thereIsAnInGamePlayer() && |
519
|
15 |
|
$this->currentPlayer && |
520
|
13 |
|
$player->equals($this->currentPlayer) |
521
|
10 |
|
) { |
522
|
12 |
|
$this->setNextPlayer($this->getNextPlayerId()); |
523
|
12 |
|
return $event; |
524
|
|
|
} |
525
|
|
|
|
526
|
3 |
|
$event = new HangmanGameLostEvent( |
527
|
3 |
|
$this->id, |
528
|
3 |
|
$player->getId(), |
529
|
3 |
|
$this->word |
530
|
2 |
|
); |
531
|
3 |
|
$this->apply($event); |
532
|
|
|
|
533
|
3 |
|
return $event; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* Sets the next player |
538
|
|
|
* |
539
|
|
|
* @param PlayerId $id |
540
|
|
|
*/ |
541
|
48 |
|
private function setNextPlayer(PlayerId $id = null) |
542
|
|
|
{ |
543
|
48 |
|
if ($id === null || ($this->currentPlayer && $this->currentPlayer->getId()->equals($id))) { |
544
|
|
|
return; |
545
|
|
|
} |
546
|
|
|
|
547
|
48 |
|
$this->apply( |
548
|
48 |
|
new HangmanPlayerTurnEvent($this->getId(), $id) |
549
|
32 |
|
); |
550
|
48 |
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Returns the next player in line |
554
|
|
|
* |
555
|
|
|
* @return PlayerId |
556
|
|
|
*/ |
557
|
18 |
|
private function getNextPlayerId() |
558
|
|
|
{ |
559
|
18 |
|
$nbPlayers = count($this->gameOrder); |
560
|
18 |
|
$currentPlayerId = (string)$this->currentPlayer->getId(); |
561
|
18 |
|
$nextPlayerPosition = (array_search($currentPlayerId, $this->gameOrder) + 1) % $nbPlayers; |
562
|
|
|
|
563
|
18 |
|
$pos = $nextPlayerPosition; |
564
|
|
|
do { |
565
|
18 |
|
$id = PlayerId::create($this->gameOrder[$pos]); |
566
|
18 |
|
$player = $this->getPlayer($id); |
567
|
|
|
|
568
|
18 |
|
if ($player->getState() === HangmanPlayer::STATE_IN_GAME) { |
569
|
18 |
|
return $id; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
$pos = ($pos + 1) % $nbPlayers; |
573
|
|
|
} while ($pos !== $nextPlayerPosition); |
574
|
|
|
|
575
|
|
|
return null; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Returns the list of played letters |
580
|
|
|
* |
581
|
|
|
* @param PlayerId $playerId |
582
|
|
|
* |
583
|
|
|
* @return array |
584
|
|
|
*/ |
585
|
6 |
|
private function getPlayedLettersForPlayer(PlayerId $playerId) |
586
|
|
|
{ |
587
|
6 |
|
return $this->getPlayer($playerId)->getPlayedLetters(); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
/** |
591
|
|
|
* Gets the remaining lives for the player |
592
|
|
|
* |
593
|
|
|
* @param PlayerId $playerId |
594
|
|
|
* |
595
|
|
|
* @return int |
596
|
|
|
*/ |
597
|
6 |
|
private function getRemainingLives(PlayerId $playerId) |
598
|
|
|
{ |
599
|
6 |
|
return $this->getPlayer($playerId)->getRemainingLives(); |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/** |
603
|
|
|
* Returns the indexes of the letter in the word |
604
|
|
|
* |
605
|
|
|
* @param string $letter |
606
|
|
|
* |
607
|
|
|
* @return boolean |
608
|
|
|
*/ |
609
|
15 |
|
private function wordContains($letter) |
610
|
|
|
{ |
611
|
15 |
|
return strpos(strtoupper($this->word), strtoupper($letter)) !== false; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Get the letters of the word |
616
|
|
|
* |
617
|
|
|
* @return string[] |
618
|
|
|
*/ |
619
|
42 |
|
private function getLettersFromWord() |
620
|
|
|
{ |
621
|
42 |
|
return array_unique(str_split(strtoupper($this->word))); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* Build the word from played letters |
626
|
|
|
* |
627
|
|
|
* @param string[] $playedLetters |
628
|
|
|
* |
629
|
|
|
* @return string |
630
|
|
|
*/ |
631
|
42 |
|
public function buildWord($playedLetters) |
632
|
|
|
{ |
633
|
42 |
|
$wordLetters = $this->getLettersFromWord(); |
634
|
|
|
|
635
|
42 |
|
$goodLetters = array_intersect($wordLetters, $playedLetters); |
636
|
|
|
|
637
|
42 |
|
$splitWord = str_split(strtoupper($this->word)); |
638
|
42 |
|
$word = ''; |
639
|
42 |
|
foreach ($splitWord as $letter) { |
640
|
42 |
|
$word .= (in_array($letter, $goodLetters) ? $letter : '_') . ' '; |
641
|
28 |
|
} |
642
|
|
|
|
643
|
42 |
|
return trim($word); |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Checks if all letters for the word have been found |
648
|
|
|
* |
649
|
|
|
* @param HangmanPlayer $player |
650
|
|
|
* |
651
|
|
|
* @return bool |
652
|
|
|
*/ |
653
|
6 |
|
private function isAllLettersFoundForPlayer(HangmanPlayer $player) |
654
|
|
|
{ |
655
|
6 |
|
$wordLetters = $this->getLettersFromWord(); |
656
|
6 |
|
$playerLetters = $player->getPlayedLetters(); |
657
|
6 |
|
return count(array_intersect($wordLetters, $playerLetters)) == count($wordLetters); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Checks if the answer is valid |
662
|
|
|
* If it's not, ends player turn and throws an HangmanException |
663
|
|
|
* |
664
|
|
|
* @param string $answer |
665
|
|
|
* |
666
|
|
|
* @throws HangmanException |
667
|
|
|
*/ |
668
|
12 |
|
private function checkAnswerIsValid($answer) |
669
|
|
|
{ |
670
|
12 |
|
if (strlen($answer) !== strlen($this->word)) { |
671
|
3 |
|
throw new HangmanException(sprintf('"%s" is not a valid answer!', $answer)); |
672
|
|
|
} |
673
|
9 |
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Checks if the word is the same as the solution |
677
|
|
|
* |
678
|
|
|
* @param string $word |
679
|
|
|
* |
680
|
|
|
* @return bool |
681
|
|
|
*/ |
682
|
9 |
|
private function isTheAnswer($word) |
683
|
|
|
{ |
684
|
9 |
|
return ($this->word === strtoupper($word)); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* @return bool |
689
|
|
|
*/ |
690
|
15 |
|
private function thereIsAnInGamePlayer() |
691
|
|
|
{ |
692
|
15 |
|
foreach ($this->gameOrder as $gameOrder) { |
693
|
15 |
|
$player = $this->getPlayer(PlayerId::create($gameOrder)); |
694
|
|
|
|
695
|
15 |
|
if ($player->getState() === HangmanPlayer::STATE_IN_GAME) { |
696
|
13 |
|
return true; |
697
|
|
|
} |
698
|
10 |
|
} |
699
|
|
|
|
700
|
3 |
|
return false; |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
704
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
705
|
|
|
//////////////////////////////////////////// APPLY EVENTS ////////////////////////////////////////////////// |
706
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
707
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* Apply the game created event |
711
|
|
|
* |
712
|
|
|
* @param HangmanGameCreatedEvent $event |
713
|
|
|
* |
714
|
|
|
* @return void |
715
|
|
|
*/ |
716
|
111 |
|
protected function applyHangmanGameCreatedEvent(HangmanGameCreatedEvent $event) |
717
|
|
|
{ |
718
|
111 |
|
$this->id = $event->getGameId(); |
719
|
111 |
|
$this->word = strtoupper($event->getWord()); |
720
|
111 |
|
$this->players = []; |
721
|
111 |
|
$this->gameOrder = []; |
722
|
111 |
|
$this->state = self::STATE_READY; |
723
|
111 |
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* Apply the player created event |
727
|
|
|
* |
728
|
|
|
* @param HangmanPlayerCreatedEvent $event |
729
|
|
|
* |
730
|
|
|
* @return void |
731
|
|
|
*/ |
732
|
75 |
|
protected function applyHangmanPlayerCreatedEvent(HangmanPlayerCreatedEvent $event) |
733
|
|
|
{ |
734
|
75 |
|
$player = new HangmanPlayer( |
735
|
75 |
|
$event->getPlayerId(), |
736
|
75 |
|
$event->getPlayerName(), |
737
|
75 |
|
$event->getLives(), |
738
|
50 |
|
$this, |
739
|
75 |
|
$event->getExternalReference() |
740
|
50 |
|
); |
741
|
|
|
|
742
|
75 |
|
$this->gameOrder[] = (string)$player->getId(); |
743
|
75 |
|
$this->players[(string)$player->getId()] = $player; |
744
|
75 |
|
} |
745
|
|
|
|
746
|
|
|
/** |
747
|
|
|
* Apply the game created event |
748
|
|
|
*/ |
749
|
48 |
|
protected function applyHangmanGameStartedEvent() |
750
|
|
|
{ |
751
|
48 |
|
$this->state = self::STATE_STARTED; |
752
|
48 |
|
} |
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* Apply the player turn event |
756
|
|
|
* |
757
|
|
|
* @param HangmanPlayerTurnEvent $event |
758
|
|
|
*/ |
759
|
48 |
|
protected function applyHangmanPlayerTurnEvent(HangmanPlayerTurnEvent $event) |
760
|
|
|
{ |
761
|
48 |
|
$this->currentPlayer = $this->getPlayer($event->getPlayerId()); |
762
|
48 |
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* Apply the hangman player lost event |
766
|
|
|
* |
767
|
|
|
* @param HangmanPlayerLostEvent $event |
768
|
|
|
*/ |
769
|
21 |
|
protected function applyHangmanPlayerLostEvent(HangmanPlayerLostEvent $event) |
|
|
|
|
770
|
|
|
{ |
771
|
21 |
|
$this->state = self::STATE_OVER; |
772
|
21 |
|
} |
773
|
|
|
|
774
|
|
|
/** |
775
|
|
|
* Apply the hangman player win event |
776
|
|
|
* |
777
|
|
|
* @return void |
778
|
|
|
*/ |
779
|
6 |
|
protected function applyHangmanPlayerWinEvent() |
780
|
|
|
{ |
781
|
6 |
|
$this->currentPlayer = null; |
782
|
6 |
|
$this->state = self::STATE_OVER; |
783
|
6 |
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Apply the hangman lost by all event |
787
|
|
|
* |
788
|
|
|
* @return void |
789
|
|
|
*/ |
790
|
3 |
|
protected function applyHangmanGameLostEvent() |
791
|
|
|
{ |
792
|
3 |
|
$this->currentPlayer = null; |
793
|
3 |
|
$this->state = self::STATE_OVER; |
794
|
3 |
|
} |
795
|
|
|
|
796
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
797
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
798
|
|
|
//////////////////////////////////////////// EVENT SOURCED ///////////////////////////////////////////////// |
799
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
800
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
801
|
|
|
|
802
|
|
|
/** |
803
|
|
|
* @return Player[] |
804
|
|
|
*/ |
805
|
111 |
|
protected function getChildEntities() |
806
|
|
|
{ |
807
|
111 |
|
return $this->getPlayers(); |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
811
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
812
|
|
|
///////////////////////////////////////// STATIC CONSTRUCTOR /////////////////////////////////////////////// |
813
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
814
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
815
|
|
|
|
816
|
|
|
/** |
817
|
|
|
* Create a new instance |
818
|
|
|
* |
819
|
|
|
* @param MiniGameId $id |
820
|
|
|
* @param string $word |
821
|
|
|
* |
822
|
|
|
* @return Hangman |
823
|
|
|
*/ |
824
|
111 |
|
public static function createGame(MiniGameId $id, $word) |
825
|
|
|
{ |
826
|
111 |
|
$hangman = new self(); |
827
|
111 |
|
$hangman->initialize($id, $word); |
828
|
|
|
|
829
|
111 |
|
return $hangman; |
830
|
|
|
} |
831
|
|
|
|
832
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
833
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
834
|
|
|
/////////////////////////////////////////// RECONSTITUTION ///////////////////////////////////////////////// |
835
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
836
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
837
|
|
|
|
838
|
|
|
/** |
839
|
|
|
* Static construction method for reconstitution |
840
|
|
|
* |
841
|
|
|
* @return Hangman |
842
|
|
|
*/ |
843
|
3 |
|
public static function instantiateForReconstitution() |
844
|
|
|
{ |
845
|
3 |
|
return new self(); |
846
|
|
|
} |
847
|
|
|
|
848
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
849
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
850
|
|
|
///////////////////////////////////////// APPLY RESTRICTIONS /////////////////////////////////////////////// |
851
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
852
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
853
|
|
|
|
854
|
|
|
/** |
855
|
|
|
* @param mixed $event |
856
|
|
|
*/ |
857
|
111 |
|
public function apply($event) |
858
|
|
|
{ |
859
|
111 |
|
if (! $this->isSupportedEvent($event)) { |
860
|
|
|
return; |
861
|
|
|
} |
862
|
|
|
|
863
|
111 |
|
parent::apply($event); |
864
|
111 |
|
} |
865
|
|
|
|
866
|
|
|
/** |
867
|
|
|
* @param mixed $event |
868
|
|
|
* |
869
|
|
|
* @return bool |
870
|
|
|
*/ |
871
|
111 |
|
private function isSupportedEvent($event) |
872
|
|
|
{ |
873
|
|
|
return ( |
874
|
111 |
|
$event instanceof GameResult && |
875
|
111 |
|
($this->id === null || $this->id == $event->getGameId()) |
876
|
74 |
|
); |
877
|
|
|
} |
878
|
|
|
} |
879
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.