Passed
Push — master ( 6d8b29...d7e053 )
by Janko
27:40
created

RageBehaviour::targetHasPositivePrestige()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Lib\Pirate\Behaviour;
4
5
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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