|
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\Message\Lib\PrivateMessageFolderSpecialEnum; |
|
9
|
|
|
use Stu\Module\Ship\Lib\ShipWrapperInterface; |
|
10
|
|
|
use Stu\Orm\Entity\UserInterface; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractBattleParty implements BattlePartyInterface |
|
13
|
|
|
{ |
|
14
|
|
|
private bool $isBase; |
|
15
|
|
|
private UserInterface $user; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Collection<int, ShipWrapperInterface> $members */ |
|
18
|
|
|
private ?Collection $members = null; |
|
19
|
|
|
|
|
20
|
21 |
|
public function __construct( |
|
21
|
|
|
protected ShipWrapperInterface $leader |
|
22
|
|
|
) { |
|
23
|
21 |
|
$this->isBase = $leader->get()->isBase(); |
|
24
|
21 |
|
$this->user = $leader->get()->getUser(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** @return Collection<int, ShipWrapperInterface> */ |
|
28
|
|
|
protected abstract function initMembers(): Collection; |
|
29
|
|
|
|
|
30
|
|
|
public function getUser(): UserInterface |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->user; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getLeader(): ShipWrapperInterface |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->leader; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
19 |
|
public function getActiveMembers(bool $canFire = false, bool $filterDisabled = true): Collection |
|
41
|
|
|
{ |
|
42
|
19 |
|
if ($this->members === null) { |
|
43
|
19 |
|
$this->members = $this->initMembers(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
19 |
|
return $this->members->filter( |
|
|
|
|
|
|
47
|
19 |
|
fn (ShipWrapperInterface $wrapper) => !$wrapper->get()->isDestroyed() |
|
48
|
17 |
|
&& (!$filterDisabled || !$wrapper->get()->isDisabled()) |
|
49
|
17 |
|
&& (!$canFire || $wrapper->canFire()) |
|
50
|
19 |
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getRandomActiveMember(): ShipWrapperInterface |
|
54
|
|
|
{ |
|
55
|
|
|
$activeMembers = $this->getActiveMembers(); |
|
56
|
|
|
$randomActiveMember = $activeMembers->get(array_rand($activeMembers->toArray())); |
|
|
|
|
|
|
57
|
|
|
if ($randomActiveMember === null) { |
|
58
|
|
|
throw new RuntimeException('isDefeated should be called first'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $randomActiveMember; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function isDefeated(): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getActiveMembers()->isEmpty(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
2 |
|
public function isBase(): bool |
|
71
|
|
|
{ |
|
72
|
2 |
|
return $this->isBase; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return Collection<int, ShipWrapperInterface> |
|
77
|
|
|
*/ |
|
78
|
7 |
|
protected function createSingleton(ShipWrapperInterface $wrapper): Collection |
|
79
|
|
|
{ |
|
80
|
7 |
|
return new ArrayCollection([$wrapper->get()->getId() => $wrapper]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function count(): int |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getActiveMembers()->count(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getPrivateMessageType(): int |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->isBase() |
|
91
|
|
|
? PrivateMessageFolderSpecialEnum::PM_SPECIAL_STATION |
|
92
|
|
|
: PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.