Passed
Push — master ( 288b46...98b0e3 )
by Nico
31:30 queued 08:00
created

EnoughEpsCondition::getEpsNeededForFlight()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

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