Passed
Push — master ( aa3b71...5e2219 )
by Nico
55:07 queued 26:20
created

RageBehaviour::attackShip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Stu\Lib\Pirate\Behaviour;
4
5
use Stu\Lib\Pirate\Component\PirateAttackInterface;
6
use Stu\Lib\Pirate\PirateBehaviourEnum;
7
use Stu\Lib\Pirate\PirateReactionInterface;
8
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
9
use Stu\Module\Logging\LoggerUtilFactoryInterface;
10
use Stu\Module\Logging\PirateLoggerInterface;
11
use Stu\Module\Ship\Lib\Battle\FightLibInterface;
12
use Stu\Module\Ship\Lib\FleetWrapperInterface;
13
use Stu\Module\Ship\Lib\Interaction\InteractionCheckerInterface;
14
use Stu\Orm\Entity\ShipInterface;
15
use Stu\Orm\Repository\ShipRepositoryInterface;
16
17
class RageBehaviour implements PirateBehaviourInterface
18
{
19
    private PirateLoggerInterface $logger;
20
21 8
    public function __construct(
22
        private ShipRepositoryInterface $shipRepository,
23
        private InteractionCheckerInterface $interactionChecker,
24
        private FightLibInterface $fightLib,
25
        private PirateAttackInterface $pirateAttack,
26
        LoggerUtilFactoryInterface $loggerUtilFactory
27
    ) {
28 8
        $this->logger = $loggerUtilFactory->getPirateLogger();
29
    }
30
31 8
    public function action(
32
        FleetWrapperInterface $fleet,
33
        PirateReactionInterface $pirateReaction,
34
        ?ShipInterface $triggerShip
35
    ): ?PirateBehaviourEnum {
36 8
        $leadWrapper = $fleet->getLeadWrapper();
37 8
        $leadShip = $leadWrapper->get();
38
39 8
        $targets = $this->shipRepository->getPirateTargets($leadShip);
40
41 8
        $this->logger->log(sprintf('    %d targets in reach', count($targets)));
42
43 8
        $filteredTargets = array_filter(
44 8
            $targets,
45 8
            fn (ShipInterface $target) =>
46 7
            $this->interactionChecker->checkPosition($leadShip, $target)
47 7
                && $this->fightLib->canAttackTarget($leadShip, $target, true, false, false)
48 7
                && !$target->getUser()->isProtectedAgainstPirates()
49 7
                && ($target === $triggerShip
50 7
                    || $this->targetHasPositivePrestige($target))
51 8
        );
52
53 8
        $this->logger->log(sprintf('    %d filtered targets in reach', count($filteredTargets)));
54
55 8
        if (empty($filteredTargets)) {
56 5
            return PirateBehaviourEnum::SEARCH_FRIEND;
57
        }
58
59 3
        foreach ($filteredTargets as $ship) {
60 3
            $this->logger->log(sprintf(
61 3
                '      shipId %d with %F health',
62 3
                $ship->getId(),
63 3
                $this->fightLib->calculateHealthPercentage($ship)
64 3
            ));
65
        }
66
67 3
        usort(
68 3
            $filteredTargets,
69 3
            fn (ShipInterface $a, ShipInterface $b) =>
70 1
            $this->fightLib->calculateHealthPercentage($a) -  $this->fightLib->calculateHealthPercentage($b)
71 3
        );
72
73 3
        $weakestTarget = current($filteredTargets);
74
75 3
        $this->logger->logf('    attacking weakestTarget with shipId: %d', $weakestTarget->getId());
76
77 3
        $this->pirateAttack->attackShip($fleet, $weakestTarget);
78
79 3
        $pirateReaction->react(
80 3
            $fleet->get(),
81 3
            PirateReactionTriggerEnum::ON_RAGE,
82 3
            $leadShip
83 3
        );
84
85 3
        return null;
86
    }
87
88 2
    private function targetHasPositivePrestige(ShipInterface $target): bool
89
    {
90 2
        $fleet = $target->getFleet();
91 2
        if ($fleet !== null) {
92 1
            foreach ($fleet->getShips() as $ship) {
93 1
                if ($ship->getRump()->getPrestige() > 0) {
94 1
                    return true;
95
                }
96
            }
97
        }
98
99 2
        return $target->getRump()->getPrestige() > 0;
100
    }
101
}
102