Passed
Push — dev ( 220852...a22d66 )
by Janko
21:21
created

RageBehaviour::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
nc 1
nop 6
dl 0
loc 15
ccs 7
cts 7
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\Information\InformationWrapper;
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\Battle\ShipAttackCoreInterface;
13
use Stu\Module\Ship\Lib\FleetWrapperInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
15
use Stu\Module\Ship\Lib\Interaction\InteractionCheckerInterface;
16
use Stu\Orm\Entity\ShipInterface;
17
use Stu\Orm\Repository\ShipRepositoryInterface;
18
19
class RageBehaviour implements PirateBehaviourInterface
20
{
21
    private ShipRepositoryInterface $shipRepository;
22
23
    private InteractionCheckerInterface $interactionChecker;
24
25
    private FightLibInterface $fightLib;
26
27
    private ShipAttackCoreInterface $shipAttackCore;
28
29
    private ShipWrapperFactoryInterface $shipWrapperFactory;
30
31
    private PirateLoggerInterface $logger;
32
33 5
    public function __construct(
34
        ShipRepositoryInterface $shipRepository,
35
        InteractionCheckerInterface $interactionChecker,
36
        FightLibInterface $fightLib,
37
        ShipAttackCoreInterface $shipAttackCore,
38
        ShipWrapperFactoryInterface $shipWrapperFactory,
39
        LoggerUtilFactoryInterface $loggerUtilFactory
40
    ) {
41 5
        $this->shipRepository = $shipRepository;
42 5
        $this->interactionChecker = $interactionChecker;
43 5
        $this->fightLib = $fightLib;
44 5
        $this->shipAttackCore = $shipAttackCore;
45 5
        $this->shipWrapperFactory = $shipWrapperFactory;
46
47 5
        $this->logger = $loggerUtilFactory->getPirateLogger();
48
    }
49
50 5
    public function action(FleetWrapperInterface $fleet, PirateReactionInterface $pirateReaction): ?PirateBehaviourEnum
51
    {
52 5
        $leadWrapper = $fleet->getLeadWrapper();
53 5
        $leadShip = $leadWrapper->get();
54
55 5
        $targets = $this->shipRepository->getPirateTargets($leadShip);
56
57 5
        $this->logger->log(sprintf('    %d targets in reach', count($targets)));
58
59 5
        $filteredTargets = array_filter(
60 5
            $targets,
61 5
            fn (ShipInterface $target) =>
62 4
            $this->interactionChecker->checkPosition($leadShip, $target)
63 4
                && $this->fightLib->canAttackTarget($leadShip, $target, false)
64 5
        );
65
66 5
        $this->logger->log(sprintf('    %d filtered targets in reach', count($filteredTargets)));
67
68 5
        if (empty($filteredTargets)) {
69 3
            return PirateBehaviourEnum::SEARCH_FRIEND;
70
        }
71
72 2
        foreach ($filteredTargets as $ship) {
73 2
            $this->logger->log(sprintf('      shipId with %F health', $this->fightLib->calculateHealthPercentage($ship)));
74
        }
75
76 2
        usort(
77 2
            $filteredTargets,
78 2
            fn (ShipInterface $a, ShipInterface $b) =>
79 1
            $this->fightLib->calculateHealthPercentage($a) -  $this->fightLib->calculateHealthPercentage($b)
80 2
        );
81
82 2
        $weakestTarget = current($filteredTargets);
83
84 2
        $this->logger->logf('    attacking weakestTarget with shipId: %d', $weakestTarget->getId());
85
86 2
        $this->attackShip($fleet, $weakestTarget);
87
88 2
        $pirateReaction->react(
89 2
            $fleet->get(),
90 2
            PirateReactionTriggerEnum::ON_RAGE
91 2
        );
92
93 2
        return null;
94
    }
95
96 2
    private function attackShip(FleetWrapperInterface $fleetWrapper, ShipInterface $target): void
97
    {
98 2
        $leadWrapper = $fleetWrapper->getLeadWrapper();
99 2
        $isFleetFight = false;
100 2
        $informations = new InformationWrapper();
101
102 2
        $this->shipAttackCore->attack(
103 2
            $leadWrapper,
104 2
            $this->shipWrapperFactory->wrapShip($target),
105 2
            $isFleetFight,
106 2
            $informations
107 2
        );
108
    }
109
}
110