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