Passed
Push — dev ( 996fbb...25004f )
by Janko
16:40
created

FlyBehaviour::leaveStarSystem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 6
rs 10
c 0
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\Coordinate;
7
use Stu\Lib\Pirate\Component\PirateFlightInterface;
8
use Stu\Lib\Pirate\Component\SafeFlightRouteInterface;
9
use Stu\Lib\Pirate\PirateBehaviourEnum;
10
use Stu\Lib\Pirate\PirateReactionInterface;
11
use Stu\Lib\Pirate\PirateReactionMetadata;
12
use Stu\Module\Control\StuRandom;
13
use Stu\Module\Logging\LoggerUtilFactoryInterface;
14
use Stu\Module\Logging\PirateLoggerInterface;
15
use Stu\Module\Ship\Lib\FleetWrapperInterface;
16
use Stu\Module\Spacecraft\Lib\Movement\Route\FlightRouteFactoryInterface;
17
use Stu\Module\Ship\Lib\ShipWrapperInterface;
18
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
19
use Stu\Orm\Entity\LocationInterface;
20
use Stu\Orm\Entity\SpacecraftInterface;
21
use Stu\Orm\Entity\StarSystemMapInterface;
22
23
class FlyBehaviour implements PirateBehaviourInterface
24
{
25
    private PirateLoggerInterface $logger;
26
27 1
    public function __construct(
28
        private FlightRouteFactoryInterface $flightRouteFactory,
29
        private SafeFlightRouteInterface $safeFlightRoute,
30
        private PirateFlightInterface $pirateFlight,
31
        private StuRandom $stuRandom,
32
        LoggerUtilFactoryInterface $loggerUtilFactory
33
    ) {
34 1
        $this->logger = $loggerUtilFactory->getPirateLogger();
35
    }
36
37
    #[Override]
38
    public function action(
39
        FleetWrapperInterface $fleet,
40
        PirateReactionInterface $pirateReaction,
41
        PirateReactionMetadata $reactionMetadata,
42
        ?SpacecraftInterface $triggerSpacecraft
43
    ): ?PirateBehaviourEnum {
44
        $leadWrapper = $fleet->getLeadWrapper();
45
        $leadShip = $leadWrapper->get();
46
47
        $currentLocation = $leadShip->getLocation();
48
49
        if (
50
            $currentLocation instanceof StarSystemMapInterface
51
            && $this->stuRandom->rand(1, 4) === 1
52
        ) {
53
            $this->leaveStarSystem($leadWrapper, $currentLocation);
54
55
            $mapField = $currentLocation->getSystem()->getMap();
56
            $this->logger->logf('    left star system: %s', $mapField !== null ? $mapField->getSectorString() : $currentLocation->getSectorString());
57
        }
58
59
        $currentLocation = $leadShip->getLocation();
60
        $this->logger->logf('    currentPosition: %s', $currentLocation->getSectorString());
61
62
        $flightRoute = $this->safeFlightRoute->getSafeFlightRoute(
63
            $leadShip,
64
            fn(): Coordinate => $this->getCoordinate($leadWrapper, $currentLocation)
65
        );
66
        if ($flightRoute === null) {
67
            $this->logger->log('    no safe flight route found');
68
            return null;
69
        }
70
71
        $this->pirateFlight->movePirate($leadWrapper, $flightRoute);
72
73
        $newLocation = $leadShip->getLocation();
74
        $this->logger->logf('    newLocation: %s', $newLocation->getSectorString());
75
76
        return null;
77
    }
78
79
    private function getCoordinate(
80
        SpacecraftWrapperInterface $leadWrapper,
81
        LocationInterface $currentLocation
82
    ): Coordinate {
83
84
        $isInXDirection = $this->stuRandom->rand(0, 1) === 0;
85
        $sensorRange = $leadWrapper->getLssSystemData()?->getSensorRange() ?? 0;
86
        $maxFields = $sensorRange * 2;
87
88
        return new Coordinate(
89
            $isInXDirection ? $currentLocation->getX() + $this->stuRandom->rand(-$maxFields, $maxFields) : $currentLocation->getX(),
90
            $isInXDirection ? $currentLocation->getY() : $currentLocation->getY() + $this->stuRandom->rand(-$maxFields, $maxFields)
91
        );
92
    }
93
94
    private function leaveStarSystem(ShipWrapperInterface $wrapper, StarSystemMapInterface $currentLocation): void
95
    {
96
        $mapField = $currentLocation->getSystem()->getMap();
97
        if ($mapField === null) {
98
            return;
99
        }
100
101
        $flightRoute = $this->flightRouteFactory->getRouteForMapDestination(
102
            $mapField
103
        );
104
105
        $this->pirateFlight->movePirate($wrapper, $flightRoute);
106
    }
107
}
108