Passed
Push — dev ( e70415...d9e229 )
by Janko
25:36
created

PirateReaction::canAnyoneFire()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 12
rs 10
1
<?php
2
3
namespace Stu\Lib\Pirate;
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\Behaviour\PirateBehaviourInterface;
7
use Stu\Lib\Pirate\Component\PirateWrathManagerInterface;
8
use Stu\Lib\Pirate\Component\ReloadMinimalEpsInterface;
9
use Stu\Module\Control\StuRandom;
10
use Stu\Module\Logging\LoggerUtilFactoryInterface;
11
use Stu\Module\Logging\PirateLoggerInterface;
12
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
13
use Stu\Module\Ship\Lib\FleetWrapperInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
15
use Stu\Orm\Entity\FleetInterface;
16
use Stu\Orm\Entity\ShipInterface;
17
use Stu\Orm\Entity\SpacecraftInterface;
18
19
class PirateReaction implements PirateReactionInterface
20
{
21
    private PirateLoggerInterface $logger;
22
23
    /** @param array<int, PirateBehaviourInterface> $behaviours */
24 1
    public function __construct(
25
        private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
26
        private ReloadMinimalEpsInterface $reloadMinimalEps,
27
        private PirateWrathManagerInterface $pirateWrathManager,
28
        private StuRandom $stuRandom,
29
        LoggerUtilFactoryInterface $loggerUtilFactory,
30
        private array $behaviours
31
    ) {
32 1
        $this->logger = $loggerUtilFactory->getPirateLogger();
33
    }
34
35
    #[Override]
36
    public function checkForPirateReaction(
37
        SpacecraftInterface $target,
38
        PirateReactionTriggerEnum $reactionTrigger,
39
        SpacecraftInterface $triggerSpacecraft
40
    ): bool {
41
42
        $targetFleet = $target instanceof ShipInterface ? $target->getFleet() : null;
43
        if (
44
            $targetFleet === null
45
            || $targetFleet->getUser()->getId() !== UserEnum::USER_NPC_KAZON
46
        ) {
47
            return false;
48
        }
49
50
        $this->react(
51
            $targetFleet,
52
            $reactionTrigger,
53
            $triggerSpacecraft,
54
            new PirateReactionMetadata()
55
        );
56
57
        return true;
58
    }
59
60
    #[Override]
61
    public function react(
62
        FleetInterface $fleet,
63
        PirateReactionTriggerEnum $reactionTrigger,
64
        SpacecraftInterface $triggerSpacecraft,
65
        PirateReactionMetadata $reactionMetadata
66
    ): void {
67
        $this->pirateWrathManager->increaseWrathViaTrigger($triggerSpacecraft->getUser(), $reactionTrigger);
68
69
        // check if fleet already defeated
70
        if ($fleet->getShips()->isEmpty()) {
71
            $this->logger->logf('pirateFleet %s has no ships left, no reaction triggered', $fleet->getName());
72
            return;
73
        }
74
75
        $fleetWrapper = $this->spacecraftWrapperFactory->wrapFleet($fleet);
76
77
        $behaviourType = $this->getRandomBehaviourType($reactionTrigger);
78
        if (
79
            $behaviourType->needsWeapons()
80
            && !$this->canAnyoneFire($fleetWrapper)
81
        ) {
82
            $this->logger->logf('pirateFleet %s cant fire, no reaction triggered', $fleet->getName());
83
            return;
84
        }
85
86
        $this->logger->log(sprintf(
87
            'pirateFleetId %d reacts on %s from "%s" (%d) with %s',
88
            $fleet->getId(),
89
            $reactionTrigger->name,
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Stu\Lib\Pirate\PirateReactionTriggerEnum.
Loading history...
90
            $triggerSpacecraft->getName(),
91
            $triggerSpacecraft->getId(),
92
            $behaviourType->name
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Stu\Lib\Pirate\PirateBehaviourEnum.
Loading history...
93
        ));
94
95
        if ($behaviourType === PirateBehaviourEnum::DO_NOTHING) {
96
            return;
97
        }
98
99
100
        $alternativeBehaviour = $this->action($behaviourType, $fleetWrapper, $reactionMetadata, $triggerSpacecraft);
101
        if (
102
            $reactionTrigger->triggerAlternativeReaction()
103
            &&  $alternativeBehaviour !== null
104
        ) {
105
            $this->logger->log(sprintf(
106
                'pirateFleetId %d does alternative behaviour %s',
107
                $fleet->getId(),
108
                $alternativeBehaviour->name
109
            ));
110
            $this->action($alternativeBehaviour, $fleetWrapper, $reactionMetadata, $triggerSpacecraft);
111
        }
112
113
        if ($reactionTrigger === PirateReactionTriggerEnum::ON_ATTACK) {
114
            $this->action(PirateBehaviourEnum::GO_ALERT_RED, $fleetWrapper, $reactionMetadata, null);
115
        }
116
117
        $this->action(PirateBehaviourEnum::DEACTIVATE_SHIELDS, $fleetWrapper, $reactionMetadata, null);
118
    }
119
120
    private function canAnyoneFire(FleetWrapperInterface $fleetWrapper): bool
121
    {
122
        foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
123
            if ($wrapper->canFire()) {
124
                return true;
125
            }
126
        }
127
128
        return false;
129
    }
130
131
    private function getRandomBehaviourType(PirateReactionTriggerEnum $reactionTrigger): PirateBehaviourEnum
132
    {
133
        $value = $this->stuRandom->randomKeyOfProbabilities($reactionTrigger->getBehaviourProbabilities());
134
135
        return PirateBehaviourEnum::from($value);
136
    }
137
138
    private function action(
139
        PirateBehaviourEnum $behaviour,
140
        FleetWrapperInterface $fleetWrapper,
141
        PirateReactionMetadata $reactionMetadata,
142
        ?SpacecraftInterface $triggerSpacecraft
143
    ): ?PirateBehaviourEnum {
144
145
        $reactionMetadata->addReaction($behaviour);
146
147
        $alternativeBehaviour = $this->behaviours[$behaviour->value]->action(
148
            $fleetWrapper,
149
            $this,
150
            $reactionMetadata,
151
            $triggerSpacecraft
152
        );
153
154
        $this->reloadMinimalEps->reload($fleetWrapper);
155
156
        return $alternativeBehaviour;
157
    }
158
}
159