|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Lib\Map; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Collection; |
|
8
|
|
|
use Doctrine\Common\Collections\ReadableCollection; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use Stu\Component\Anomaly\Type\AnomalyTypeEnum; |
|
11
|
|
|
use Stu\Orm\Entity\AnomalyInterface; |
|
12
|
|
|
use Stu\Orm\Entity\LayerInterface; |
|
13
|
|
|
use Stu\Orm\Entity\MapInterface; |
|
14
|
|
|
use Stu\Orm\Entity\ShipInterface; |
|
15
|
|
|
use Stu\Orm\Entity\StarSystemMapInterface; |
|
16
|
|
|
|
|
17
|
|
|
class Location |
|
18
|
|
|
{ |
|
19
|
|
|
private MapInterface|StarSystemMapInterface $location; |
|
20
|
|
|
|
|
21
|
10 |
|
public function __construct(?MapInterface $map, ?StarSystemMapInterface $sysMap) |
|
22
|
|
|
{ |
|
23
|
|
|
if ( |
|
24
|
10 |
|
$map === null && $sysMap === null |
|
|
|
|
|
|
25
|
10 |
|
|| $map !== null && $sysMap !== null |
|
26
|
|
|
) { |
|
27
|
2 |
|
throw new InvalidArgumentException('Either map or systemMap has to be filled'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
8 |
|
$this->location = $map ?? $sysMap; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function get(): MapInterface|StarSystemMapInterface |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->location; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return Collection<int, ShipInterface> |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function getShips(): Collection |
|
42
|
|
|
{ |
|
43
|
2 |
|
return $this->location->getShips(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function getSectorString(): string |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->location->getSectorString(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return ReadableCollection<int, AnomalyInterface> |
|
53
|
|
|
*/ |
|
54
|
5 |
|
public function getAnomalies(): ReadableCollection |
|
55
|
|
|
{ |
|
56
|
5 |
|
return $this->location->getAnomalies()->filter(fn (AnomalyInterface $anomaly): bool => $anomaly->isActive()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
4 |
|
public function hasAnomaly(AnomalyTypeEnum $type): bool |
|
60
|
|
|
{ |
|
61
|
4 |
|
foreach ($this->getAnomalies() as $anomaly) { |
|
62
|
2 |
|
if ($anomaly->getAnomalyType()->getId() === $type->value) { |
|
63
|
1 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function isMap(): bool |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->location instanceof MapInterface; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getId(): int |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->location->getId(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getLayer(): ?LayerInterface |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->location instanceof MapInterface) { |
|
83
|
|
|
return $this->location->getLayer(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$parentMap = $this->location->getSystem()->getMapField(); |
|
87
|
|
|
if ($parentMap === null) { |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $parentMap->getLayer(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|