Passed
Push — master ( 0a21cd...0ad97c )
by Janko
05:01
created

getEnergyForSystemsActivation()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 16
nop 2
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Movement\Component\PreFlight\Condition;
6
7
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...
8
use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
9
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Module\Spacecraft\Lib\Movement\Component\PreFlight\ConditionCheckResult;
11
use Stu\Module\Spacecraft\Lib\Movement\Route\FlightRouteInterface;
12
use Stu\Module\Spacecraft\Lib\Movement\Route\RouteModeEnum;
13
use Stu\Orm\Entity\Map;
14
use Stu\Orm\Entity\Ship;
15
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
16
use Stu\Orm\Entity\Spacecraft;
17
18
class EnoughEpsCondition implements PreFlightConditionInterface
19
{
20 20
    public function __construct(private SpacecraftSystemManagerInterface $spacecraftSystemManager) {}
21
22 23
    #[Override]
23
    public function check(
24
        SpacecraftWrapperInterface $wrapper,
25
        FlightRouteInterface $flightRoute,
26
        ConditionCheckResult $conditionCheckResult
27
    ): void {
28
29 23
        $ship = $wrapper->get();
30
31 23
        $neededEps = $this->getEnergyForSystemsActivation($flightRoute, $ship)
32 23
            + $this->getEpsNeededForFlight($flightRoute, $ship);
33
34 23
        if ($neededEps === 0) {
35 12
            return;
36
        }
37
38 11
        $epsSystem = $wrapper->getEpsSystemData();
39 11
        if ($epsSystem === null || $epsSystem->getEps() < $neededEps) {
40
41 2
            $conditionCheckResult->addBlockedShip(
42 2
                $ship,
43 2
                sprintf(
44 2
                    'Die %s hat nicht genug Energie für den %s (%d benötigt)',
45 2
                    $ship->getName(),
46 2
                    $ship->isTractoring() ? 'Traktor-Flug' : 'Flug',
47 2
                    $neededEps
48 2
                )
49 2
            );
50
        }
51
    }
52
53 23
    private function getEnergyForSystemsActivation(FlightRouteInterface $flightRoute, Spacecraft $spacecraft): int
54
    {
55 23
        $result = 0;
56
57 23
        if ($flightRoute->isImpulseDriveNeeded()) {
58 6
            $result += $this->getEnergyUsageForActivation($spacecraft, SpacecraftSystemTypeEnum::IMPULSEDRIVE);
59
        }
60
61 23
        if ($flightRoute->isWarpDriveNeeded()) {
62 6
            $result += $this->getEnergyUsageForActivation($spacecraft, SpacecraftSystemTypeEnum::WARPDRIVE);
63
        }
64
65 23
        if ($flightRoute->isTranswarpCoilNeeded()) {
66 2
            $result += $this->getEnergyUsageForActivation($spacecraft, SpacecraftSystemTypeEnum::TRANSWARP_COIL);
67
        }
68
69 23
        if ($spacecraft instanceof Ship && $spacecraft->getDockedTo() !== null) {
70 4
            $result += Spacecraft::SYSTEM_ECOST_DOCK;
71
        }
72
73 23
        return $result;
74
    }
75
76 14
    private function getEnergyUsageForActivation(Spacecraft $spacecraft, SpacecraftSystemTypeEnum $systemId): int
77
    {
78 14
        if (!$spacecraft->hasSpacecraftSystem($systemId)) {
79 3
            return 0;
80
        }
81
82 11
        if (!$spacecraft->getSystemState($systemId)) {
83 9
            return $this->spacecraftSystemManager->getEnergyUsageForActivation($systemId);
84
        }
85
86 2
        return 0;
87
    }
88
89 23
    private function getEpsNeededForFlight(FlightRouteInterface $flightRoute, Spacecraft $spacecraft): int
90
    {
91 23
        if ($flightRoute->getRouteMode() !== RouteModeEnum::FLIGHT) {
92 16
            return 0;
93
        }
94
95 7
        $nextWaypoint = $flightRoute->getNextWaypoint();
96 7
        if ($nextWaypoint instanceof Map) {
97 4
            return 0;
98
        }
99
100 3
        if (!$spacecraft->hasSpacecraftSystem(SpacecraftSystemTypeEnum::IMPULSEDRIVE)) {
101 1
            return 0;
102
        }
103
104 2
        $result = $spacecraft->getRump()->getFlightEcost();
105
106 2
        $tractoredShip = $spacecraft->getTractoredShip();
107 2
        if ($tractoredShip !== null) {
108 1
            $result += $tractoredShip->getRump()->getFlightEcost();
109
        }
110
111 2
        return $result;
112
    }
113
}
114