Passed
Push — master ( 15d483...fbd3ae )
by Nico
31:54 queued 02:53
created

EnoughEpsCondition::check()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 3
nop 3
dl 0
loc 26
ccs 15
cts 15
cp 1
crap 5
rs 9.4555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\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\Ship\System\ShipSystemManagerInterface;
9
use Stu\Component\Ship\System\ShipSystemTypeEnum;
10
use Stu\Module\Ship\Lib\Movement\Component\PreFlight\ConditionCheckResult;
11
use Stu\Module\Ship\Lib\Movement\Route\FlightRouteInterface;
12
use Stu\Module\Ship\Lib\Movement\Route\RouteModeEnum;
13
use Stu\Module\Ship\Lib\ShipWrapperInterface;
14
use Stu\Orm\Entity\MapInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
use Stu\Component\Ship\ShipEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\ShipEnum 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...
17
18
class EnoughEpsCondition implements PreFlightConditionInterface
19
{
20 20
    public function __construct(private ShipSystemManagerInterface $shipSystemManager) {}
21
22 19
    #[Override]
23
    public function check(
24
        ShipWrapperInterface $wrapper,
25
        FlightRouteInterface $flightRoute,
26
        ConditionCheckResult $conditionCheckResult
27
    ): void {
28
29 19
        $ship = $wrapper->get();
30
31 19
        $neededEps = $this->getEnergyForSystemsActivation($flightRoute, $ship)
32 19
            + $this->getEpsNeededForFlight($flightRoute, $ship);
33
34 19
        if ($neededEps === 0) {
35 12
            return;
36
        }
37
38 7
        $epsSystem = $wrapper->getEpsSystemData();
39 7
        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 19
    private function getEnergyForSystemsActivation(FlightRouteInterface $flightRoute, ShipInterface $ship): int
54
    {
55 19
        $result = 0;
56
57 19
        if ($flightRoute->isImpulseDriveNeeded()) {
58 5
            $result += $this->getEnergyUsageForActivation($ship, ShipSystemTypeEnum::SYSTEM_IMPULSEDRIVE);
59
        }
60
61 19
        if ($flightRoute->isWarpDriveNeeded()) {
62 3
            $result += $this->getEnergyUsageForActivation($ship, ShipSystemTypeEnum::SYSTEM_WARPDRIVE);
63
        }
64
65 19
        if ($flightRoute->isTranswarpCoilNeeded()) {
66 2
            $result += $this->getEnergyUsageForActivation($ship, ShipSystemTypeEnum::SYSTEM_TRANSWARP_COIL);
67
        }
68
69 19
        if ($ship->getDockedTo() !== null) {
70
            $result += ShipEnum::SYSTEM_ECOST_DOCK;
71
        }
72
73 19
        return $result;
74
    }
75
76 10
    private function getEnergyUsageForActivation(ShipInterface $ship, ShipSystemTypeEnum $systemId): int
77
    {
78 10
        if (!$ship->hasShipSystem($systemId)) {
79 3
            return 0;
80
        }
81
82 7
        if (!$ship->getSystemState($systemId)) {
83 5
            return $this->shipSystemManager->getEnergyUsageForActivation($systemId);
84
        }
85
86 2
        return 0;
87
    }
88
89 19
    private function getEpsNeededForFlight(FlightRouteInterface $flightRoute, ShipInterface $ship): int
90
    {
91 19
        if ($flightRoute->getRouteMode() !== RouteModeEnum::ROUTE_MODE_FLIGHT) {
92 15
            return 0;
93
        }
94
95 4
        $nextWaypoint = $flightRoute->getNextWaypoint();
96 4
        if ($nextWaypoint instanceof MapInterface) {
97 1
            return 0;
98
        }
99
100 3
        if (!$ship->hasShipSystem(ShipSystemTypeEnum::SYSTEM_IMPULSEDRIVE)) {
101 1
            return 0;
102
        }
103
104 2
        $result = $ship->getRump()->getFlightEcost();
105
106 2
        $tractoredShip = $ship->getTractoredShip();
107 2
        if ($tractoredShip !== null) {
108 1
            $result += $tractoredShip->getRump()->getFlightEcost();
109
        }
110
111 2
        return $result;
112
    }
113
}
114