|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Lib\Pirate\Component; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
|
6
|
|
|
use Stu\Module\Control\StuRandom; |
|
7
|
|
|
use Stu\Module\Prestige\Lib\PrestigeCalculationInterface; |
|
8
|
|
|
use Stu\Module\Ship\Lib\Battle\AlertDetection\AlertedShipsDetectionInterface; |
|
9
|
|
|
use Stu\Module\Ship\Lib\ShipWrapperInterface; |
|
10
|
|
|
use Stu\Orm\Entity\LocationInterface; |
|
11
|
|
|
use Stu\Orm\Entity\ShipInterface; |
|
12
|
|
|
|
|
13
|
|
|
class TrapDetection implements TrapDetectionInterface |
|
14
|
|
|
{ |
|
15
|
4 |
|
public function __construct( |
|
16
|
|
|
private AlertedShipsDetectionInterface $alertedShipsDetection, |
|
17
|
|
|
private PrestigeCalculationInterface $prestigeCalculation, |
|
18
|
|
|
private StuRandom $stuRandom |
|
19
|
4 |
|
) {} |
|
20
|
|
|
|
|
21
|
4 |
|
public function isAlertTrap(LocationInterface $location, ShipInterface $leadShip): bool |
|
22
|
|
|
{ |
|
23
|
4 |
|
$alertedWrappers = $this->alertedShipsDetection->getAlertedShipsOnLocation( |
|
24
|
4 |
|
$location, |
|
25
|
4 |
|
$leadShip->getUser() |
|
26
|
4 |
|
); |
|
27
|
4 |
|
if ($alertedWrappers->isEmpty()) { |
|
28
|
1 |
|
return false; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
$piratePrestige = $this->prestigeCalculation->getPrestigeOfSpacecraftOrFleet($leadShip); |
|
32
|
3 |
|
$alertedPrestige = $this->getPrestigeOfAlertedSpacecrafts($alertedWrappers); |
|
33
|
|
|
|
|
34
|
3 |
|
if ($alertedPrestige <= 3 * $piratePrestige) { |
|
35
|
1 |
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
return $this->stuRandom->rand(0, $alertedPrestige) > $piratePrestige; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** @param Collection<int, ShipWrapperInterface> $alertedWrappers */ |
|
42
|
3 |
|
private function getPrestigeOfAlertedSpacecrafts(Collection $alertedWrappers): int |
|
43
|
|
|
{ |
|
44
|
3 |
|
return $alertedWrappers |
|
45
|
3 |
|
->map(fn(ShipWrapperInterface $wrapper) => $this->prestigeCalculation->getPrestigeOfSpacecraftOrFleet($wrapper)) |
|
46
|
3 |
|
->reduce( |
|
47
|
3 |
|
fn(int $sum, int $prestige) => $sum + $prestige, |
|
48
|
3 |
|
0 |
|
49
|
3 |
|
); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|