|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Module\Ship\Lib\Battle\Party; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
use Stu\Module\Ship\Lib\ShipWrapperInterface; |
|
8
|
|
|
use Stu\Orm\Repository\ShipRepositoryInterface; |
|
9
|
|
|
|
|
10
|
|
|
class RoundBasedBattleParty |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var array<int> */ |
|
13
|
|
|
private array $unUsedIds; |
|
14
|
|
|
|
|
15
|
7 |
|
public function __construct( |
|
16
|
|
|
private BattlePartyInterface $battleParty, |
|
17
|
|
|
private ShipRepositoryInterface $shipRepository |
|
18
|
|
|
) { |
|
19
|
7 |
|
$this->unUsedIds = $battleParty->getActiveMembers()->getKeys(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
5 |
|
public function get(): BattlePartyInterface |
|
23
|
|
|
{ |
|
24
|
5 |
|
return $this->battleParty; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
2 |
|
public function use(int $spacecraftId): void |
|
28
|
|
|
{ |
|
29
|
2 |
|
unset($this->unUsedIds[$spacecraftId]); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
public function isDone(): bool |
|
33
|
|
|
{ |
|
34
|
4 |
|
return $this->isUsed() || $this->getAllUnusedThatCanFire()->isEmpty(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
4 |
|
private function isUsed(): bool |
|
38
|
|
|
{ |
|
39
|
4 |
|
return empty($this->unUsedIds); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** @return Collection<int, ShipWrapperInterface> */ |
|
43
|
3 |
|
public function getAllUnusedThatCanFire(): Collection |
|
44
|
|
|
{ |
|
45
|
3 |
|
return $this->get() |
|
46
|
3 |
|
->getActiveMembers(true) |
|
47
|
3 |
|
->filter(fn (ShipWrapperInterface $wrapper) => in_array($wrapper->get()->getId(), $this->unUsedIds)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function getRandomUnused(): ShipWrapperInterface |
|
51
|
|
|
{ |
|
52
|
1 |
|
$allUnusedThatCanFire = $this->getAllUnusedThatCanFire(); |
|
53
|
|
|
|
|
54
|
|
|
/** @var ShipWrapperInterface|null */ |
|
55
|
1 |
|
$random = $allUnusedThatCanFire->get(array_rand($allUnusedThatCanFire->toArray())); |
|
|
|
|
|
|
56
|
1 |
|
if ($random === null) { |
|
57
|
|
|
throw new RuntimeException('isDone shoule be called first!'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
unset($this->unUsedIds[$random->get()->getId()]); |
|
61
|
|
|
|
|
62
|
1 |
|
return $random; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function saveActiveMembers(): void |
|
66
|
|
|
{ |
|
67
|
1 |
|
foreach ($this->get()->getActiveMembers(false, false) as $wrapper) { |
|
68
|
|
|
|
|
69
|
1 |
|
$this->shipRepository->save($wrapper->get()); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|