|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Lib\Pirate\Behaviour; |
|
4
|
|
|
|
|
5
|
|
|
use RuntimeException; |
|
6
|
|
|
use Stu\Lib\Map\DistanceCalculationInterface; |
|
7
|
|
|
use Stu\Lib\Pirate\Component\PirateNavigationInterface; |
|
8
|
|
|
use Stu\Lib\Pirate\Component\ReloadMinimalEpsInterface; |
|
9
|
|
|
use Stu\Lib\Pirate\PirateBehaviourEnum; |
|
10
|
|
|
use Stu\Lib\Pirate\PirateCreationInterface; |
|
11
|
|
|
use Stu\Module\Ship\Lib\FleetWrapperInterface; |
|
12
|
|
|
use Stu\Lib\Pirate\PirateReactionInterface; |
|
13
|
|
|
use Stu\Lib\Pirate\PirateReactionTriggerEnum; |
|
14
|
|
|
use Stu\Module\Logging\LoggerUtilFactoryInterface; |
|
15
|
|
|
use Stu\Module\Logging\PirateLoggerInterface; |
|
16
|
|
|
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface; |
|
17
|
|
|
use Stu\Orm\Entity\FleetInterface; |
|
18
|
|
|
use Stu\Orm\Entity\ShipInterface; |
|
19
|
|
|
use Stu\Orm\Repository\ShipRepositoryInterface; |
|
20
|
|
|
|
|
21
|
|
|
class CallForSupportBehaviour implements PirateBehaviourInterface |
|
22
|
|
|
{ |
|
23
|
|
|
private PirateLoggerInterface $logger; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
private ShipRepositoryInterface $shipRepository, |
|
27
|
|
|
private PirateCreationInterface $pirateCreation, |
|
28
|
|
|
private DistanceCalculationInterface $distanceCalculation, |
|
29
|
|
|
private ReloadMinimalEpsInterface $reloadMinimalEps, |
|
30
|
|
|
private PirateNavigationInterface $pirateNavigation, |
|
31
|
|
|
private ShipWrapperFactoryInterface $shipWrapperFactory, |
|
32
|
|
|
LoggerUtilFactoryInterface $loggerUtilFactory |
|
33
|
|
|
) { |
|
34
|
|
|
$this->logger = $loggerUtilFactory->getPirateLogger(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function action(FleetWrapperInterface $fleet, PirateReactionInterface $pirateReaction): ?PirateBehaviourEnum |
|
38
|
|
|
{ |
|
39
|
|
|
$leadWrapper = $fleet->getLeadWrapper(); |
|
40
|
|
|
$leadShip = $leadWrapper->get(); |
|
41
|
|
|
|
|
42
|
|
|
$supportFleet = $this->getSupportFleet($leadShip); |
|
43
|
|
|
|
|
44
|
|
|
$pirateReaction->react( |
|
45
|
|
|
$supportFleet, |
|
46
|
|
|
PirateReactionTriggerEnum::ON_SUPPORT_CALL |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function getSupportFleet(ShipInterface $leadShip): FleetInterface |
|
53
|
|
|
{ |
|
54
|
|
|
$friends = $this->shipRepository->getPirateFriends($leadShip); |
|
55
|
|
|
|
|
56
|
|
|
usort( |
|
57
|
|
|
$friends, |
|
58
|
|
|
fn (ShipInterface $a, ShipInterface $b) => |
|
59
|
|
|
$this->distanceCalculation->shipToShipDistance($leadShip, $a) - $this->distanceCalculation->shipToShipDistance($leadShip, $b) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$closestFriend = current($friends); |
|
63
|
|
|
if (!$closestFriend) { |
|
64
|
|
|
return $this->createSupportFleet($leadShip); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$supportFleet = $closestFriend->getFleet(); |
|
68
|
|
|
if ($supportFleet === null) { |
|
69
|
|
|
throw new RuntimeException('pirate ships should always be in fleet'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$fleetWrapper = $this->shipWrapperFactory->wrapFleet($supportFleet); |
|
73
|
|
|
|
|
74
|
|
|
$this->reloadMinimalEps->reload($fleetWrapper, 75); |
|
75
|
|
|
if (!$this->pirateNavigation->navigateToTarget($fleetWrapper, $leadShip->getCurrentMapField())) { |
|
76
|
|
|
return $this->createSupportFleet($leadShip); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->logger->logf( |
|
80
|
|
|
' already existing support fleet (%d) "%s" reached here %s', |
|
81
|
|
|
$supportFleet->getId(), |
|
82
|
|
|
$supportFleet->getName(), |
|
83
|
|
|
$supportFleet->getLeadShip()->getSectorString() |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
return $supportFleet; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function createSupportFleet(ShipInterface $leadShip): FleetInterface |
|
90
|
|
|
{ |
|
91
|
|
|
$supportFleet = $this->pirateCreation->createPirateFleet($leadShip); |
|
92
|
|
|
$this->logger->logf( |
|
93
|
|
|
' created support fleet %d "%s" here %s', |
|
94
|
|
|
$supportFleet->getId(), |
|
95
|
|
|
$supportFleet->getName(), |
|
96
|
|
|
$supportFleet->getLeadShip()->getSectorString() |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
return $supportFleet; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|