|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Module\Spacecraft\Lib\Battle\Party; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\Common\Collections\Collection; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Stu\Module\Control\StuRandom; |
|
9
|
|
|
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface; |
|
10
|
|
|
|
|
11
|
|
|
class RoundBasedBattleParty |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var Collection<int, int> */ |
|
14
|
|
|
private Collection $unUsedIds; |
|
15
|
|
|
|
|
16
|
8 |
|
public function __construct( |
|
17
|
|
|
private BattlePartyInterface $battleParty, |
|
18
|
|
|
private StuRandom $stuRandom |
|
19
|
|
|
) { |
|
20
|
8 |
|
$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
|
4 |
|
public function use(int $spacecraftId): void |
|
29
|
|
|
{ |
|
30
|
4 |
|
$this->unUsedIds->removeElement($spacecraftId); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
6 |
|
public function isDone(): bool |
|
34
|
|
|
{ |
|
35
|
6 |
|
return $this->isUsed() || $this->getAllUnusedThatCanFire()->isEmpty(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
6 |
|
private function isUsed(): bool |
|
39
|
|
|
{ |
|
40
|
6 |
|
return $this->unUsedIds->isEmpty(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** @return Collection<int, SpacecraftWrapperInterface> */ |
|
44
|
3 |
|
public function getAllUnusedThatCanFire(): Collection |
|
45
|
|
|
{ |
|
46
|
3 |
|
return $this->get() |
|
47
|
3 |
|
->getActiveMembers(true) |
|
48
|
3 |
|
->filter(fn(SpacecraftWrapperInterface $wrapper) => $this->unUsedIds->contains($wrapper->get()->getId())); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
public function getRandomUnused(): SpacecraftWrapperInterface |
|
52
|
|
|
{ |
|
53
|
2 |
|
$allUnusedThatCanFire = $this->getAllUnusedThatCanFire(); |
|
54
|
|
|
|
|
55
|
|
|
/** @var SpacecraftWrapperInterface|null */ |
|
56
|
2 |
|
$random = $allUnusedThatCanFire->get($this->stuRandom->array_rand($allUnusedThatCanFire->toArray())); |
|
57
|
2 |
|
if ($random === null) { |
|
58
|
|
|
throw new RuntimeException('isDone shoule be called first!'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
$this->use($random->get()->getId()); |
|
62
|
|
|
|
|
63
|
2 |
|
return $random; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|