Passed
Pull Request — master (#2053)
by Janko
22:12 queued 10:55
created

PirateReaction   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 3.23%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
c 1
b 0
f 0
dl 0
loc 132
ccs 2
cts 62
cp 0.0323
rs 10
wmc 16

6 Methods

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