|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Lib\Pirate\Behaviour; |
|
4
|
|
|
|
|
5
|
|
|
use Stu\Module\Logging\LoggerUtilFactoryInterface; |
|
6
|
|
|
use Stu\Module\Ship\Lib\FleetWrapperInterface; |
|
7
|
|
|
use Stu\Lib\Pirate\Component\PirateNavigationInterface; |
|
8
|
|
|
use Stu\Lib\Pirate\PirateBehaviourEnum; |
|
9
|
|
|
use Stu\Lib\Pirate\PirateReactionInterface; |
|
10
|
|
|
use Stu\Lib\Pirate\PirateReactionTriggerEnum; |
|
11
|
|
|
use Stu\Module\Logging\PirateLoggerInterface; |
|
12
|
|
|
use Stu\Module\Ship\Lib\Battle\FightLibInterface; |
|
13
|
|
|
use Stu\Orm\Entity\ShipInterface; |
|
14
|
|
|
use Stu\Orm\Repository\ShipRepositoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
class SearchFriendBehaviour implements PirateBehaviourInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private PirateLoggerInterface $logger; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
private ShipRepositoryInterface $shipRepository, |
|
22
|
|
|
private PirateNavigationInterface $pirateNavigation, |
|
23
|
|
|
private FightLibInterface $fightLib, |
|
24
|
|
|
LoggerUtilFactoryInterface $loggerUtilFactory |
|
25
|
|
|
) { |
|
26
|
|
|
$this->shipRepository = $shipRepository; |
|
27
|
|
|
$this->pirateNavigation = $pirateNavigation; |
|
28
|
|
|
|
|
29
|
|
|
$this->logger = $loggerUtilFactory->getPirateLogger(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function action(FleetWrapperInterface $fleet, PirateReactionInterface $pirateReaction): ?PirateBehaviourEnum |
|
33
|
|
|
{ |
|
34
|
|
|
$leadWrapper = $fleet->getLeadWrapper(); |
|
35
|
|
|
$leadShip = $leadWrapper->get(); |
|
36
|
|
|
|
|
37
|
|
|
$friends = $this->shipRepository->getPirateFriends($leadShip); |
|
38
|
|
|
|
|
39
|
|
|
$this->logger->logf(' number of friends in reach: %d', count($friends)); |
|
40
|
|
|
|
|
41
|
|
|
if (empty($friends)) { |
|
42
|
|
|
return PirateBehaviourEnum::HIDE; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
usort( |
|
46
|
|
|
$friends, |
|
47
|
|
|
fn (ShipInterface $a, ShipInterface $b) => |
|
48
|
|
|
$this->fightLib->calculateHealthPercentage($a) - $this->fightLib->calculateHealthPercentage($b) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$weakestFriend = current($friends); |
|
52
|
|
|
|
|
53
|
|
|
$this->logger->logf( |
|
54
|
|
|
' navigating from %s to weakest friend at %s', |
|
55
|
|
|
$leadShip->getSectorString(), |
|
56
|
|
|
$weakestFriend->getSectorString() |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
if ($this->pirateNavigation->navigateToTarget($fleet, $weakestFriend->getCurrentMapField())) { |
|
60
|
|
|
|
|
61
|
|
|
$this->logger->log(' reached weakest friend, now raging'); |
|
62
|
|
|
|
|
63
|
|
|
$pirateReaction->react( |
|
64
|
|
|
$fleet->get(), |
|
65
|
|
|
PirateReactionTriggerEnum::ON_RAGE |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|