Passed
Push — master ( 0a90eb...366c47 )
by Nico
23:43
created

ReactorWrapper   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 66.27%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 78
c 2
b 1
f 0
dl 0
loc 184
ccs 57
cts 86
cp 0.6627
rs 9.84
wmc 32

17 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A __construct() 0 6 1
A getEpsProduction() 0 18 4
A getWarpdriveProduction() 0 18 3
A setOutput() 0 5 1
A getUsage() 0 4 1
A getOutput() 0 3 1
A setLoad() 0 5 1
A changeLoad() 0 5 1
A getCapacity() 0 3 1
A isHealthy() 0 3 1
A getEffectiveWarpDriveProduction() 0 7 2
A getLoad() 0 3 1
A getEffectiveEpsProduction() 0 6 2
A getOutputCappedByLoad() 0 7 2
A calculateEffectiveProduction() 0 27 5
A getReactorLoadStyle() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib;
6
7
use Stu\Component\Ship\System\Data\AbstractReactorSystemData;
8
9
final class ReactorWrapper implements ReactorWrapperInterface
10
{
11
    private ShipWrapperInterface $wrapper;
12
    private AbstractReactorSystemData $reactorSystemData;
13
14
    //absolute values
15
    private ?int $epsProduction = null;
16
    private ?int $warpdriveProduction = null;
17
18
    //effective values    
19
    private ?int $effectiveEpsProduction = null;
20
    private ?int $effectiveWarpDriveProduction = null;
21
22 8
    public function __construct(
23
        ShipWrapperInterface $wrapper,
24
        AbstractReactorSystemData $reactorSystemData
25
    ) {
26 8
        $this->wrapper = $wrapper;
27 8
        $this->reactorSystemData = $reactorSystemData;
28
    }
29
30 8
    public function get(): AbstractReactorSystemData
31
    {
32 8
        return $this->reactorSystemData;
33
    }
34
35 7
    public function getEpsProduction(): int
36
    {
37 7
        if ($this->epsProduction === null) {
38 7
            $warpdrive = $this->wrapper->getWarpDriveSystemData();
39 7
            $warpdriveSplit = $warpdrive === null ? 100 : $warpdrive->getWarpDriveSplit();
40 7
            $reactorOutput = $this->getOutputCappedByLoad();
41
42 7
            if ($warpdriveSplit === 0) {
43 2
                $this->epsProduction = min($reactorOutput, $this->wrapper->getEpsUsage());
44
            } else {
45 5
                $warpDriveProduction = $this->getWarpdriveProduction();
46 5
                $flightCost = $this->wrapper->get()->getRump()->getFlightEcost();
47
48 5
                $this->epsProduction = $reactorOutput - ($warpDriveProduction * $flightCost);
49
            }
50
        }
51
52 7
        return $this->epsProduction;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->epsProduction could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54
55 5
    private function getWarpdriveProduction(): int
56
    {
57 5
        if ($this->warpdriveProduction === null) {
58 5
            $warpdrive = $this->wrapper->getWarpDriveSystemData();
59
60 5
            if ($warpdrive === null) {
61 1
                $this->warpdriveProduction = 0;
62
            } else {
63 4
                $warpdriveSplit = $warpdrive->getWarpDriveSplit();
64 4
                $reactorOutput = $this->getOutputCappedByLoad();
65 4
                $flightCost = $this->wrapper->get()->getRump()->getFlightEcost();
66 4
                $maxWarpdriveGain = max(0, (int)floor(($reactorOutput - $this->wrapper->getEpsUsage()) / $flightCost));
67
68 4
                $this->warpdriveProduction = (int)round((1 - ($warpdriveSplit / 100)) * $maxWarpdriveGain);
69
            }
70
        }
71
72 5
        return $this->warpdriveProduction;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->warpdriveProduction could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75 2
    public function getEffectiveEpsProduction(): int
76
    {
77 2
        if ($this->effectiveEpsProduction === null) {
78 2
            $this->calculateEffectiveProduction();
79
        }
80 2
        return $this->effectiveEpsProduction;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->effectiveEpsProduction could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
81
    }
82
83 1
    public function getEffectiveWarpDriveProduction(): int
84
    {
85 1
        if ($this->effectiveWarpDriveProduction === null) {
86 1
            $this->calculateEffectiveProduction();
87
        }
88
89 1
        return $this->effectiveWarpDriveProduction;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->effectiveWarpDriveProduction could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
90
    }
91
92 3
    private function calculateEffectiveProduction(): void
93
    {
94 3
        $epsSystem = $this->wrapper->getEpsSystemData();
95 3
        $warpdrive = $this->wrapper->getWarpDriveSystemData();
96
97 3
        $missingEps = $epsSystem === null ? 0 : $epsSystem->getMaxEps() - $epsSystem->getEps();
98 3
        $missingWarpdrive = $warpdrive === null ? 0 : $warpdrive->getMaxWarpDrive() - $warpdrive->getWarpDrive();
99
100 3
        $potential = $this->getOutputCappedByLoad();
101 3
        $potential -= $this->wrapper->getEpsUsage();
102
103 3
        $flightCost = $this->wrapper->get()->getRump()->getFlightEcost();
104
105 3
        $epsChange = $this->getEpsProduction() - $this->wrapper->getEpsUsage();
106 3
        $effEpsProd = min($missingEps, $epsChange);
107 3
        $effWdProd = min($missingWarpdrive, $this->getWarpdriveProduction());
108
109 3
        if ($warpdrive !== null && $warpdrive->getAutoCarryOver()) {
110
            $excess = max(0, $potential - $effEpsProd - $effWdProd * $flightCost);
111
            $epsChange = $this->getEpsProduction() + $excess - $this->wrapper->getEpsUsage();
112
113
            $effEpsProd = min($missingEps, $epsChange);
114
            $effWdProd = min($missingWarpdrive, $this->getWarpdriveProduction() + (int)floor($excess / $flightCost));
115
        }
116
117 3
        $this->effectiveEpsProduction = $effEpsProd;
118 3
        $this->effectiveWarpDriveProduction = $effWdProd;
119
    }
120
121
    public function getUsage(): int
122
    {
123
        return $this->getEffectiveEpsProduction()
124
            + $this->getEffectiveWarpDriveProduction() * $this->wrapper->get()->getRump()->getFlightEcost();
125
    }
126
127
    public function getCapacity(): int
128
    {
129
        return $this->get()->getCapacity();
130
    }
131
132 7
    public function getOutput(): int
133
    {
134 7
        return $this->get()->getOutput();
135
    }
136
137
    public function setOutput(int $output): ReactorWrapperInterface
138
    {
139
        $this->get()->setOutput($output)->update();
140
141
        return $this;
142
    }
143
144 7
    public function getOutputCappedByLoad(): int
145
    {
146 7
        if ($this->getOutput() > $this->getLoad()) {
147 4
            return $this->getLoad();
148
        }
149
150 3
        return $this->getOutput();
151
    }
152
153 7
    public function getLoad(): int
154
    {
155 7
        return $this->get()->getLoad();
156
    }
157
158
    public function setLoad(int $load): ReactorWrapperInterface
159
    {
160
        $this->get()->setLoad($load)->update();
161
162
        return $this;
163
    }
164
165
    public function changeLoad(int $amount): ReactorWrapperInterface
166
    {
167
        $this->get()->setLoad($this->get()->getLoad() + $amount)->update();
168
169
        return $this;
170
    }
171
172
    public function isHealthy(): bool
173
    {
174
        return $this->wrapper->get()->isSystemHealthy($this->get()->getSystemType());
175
    }
176
177
    public function getReactorLoadStyle(): string
178
    {
179
        $load = $this->getLoad();
180
        $output = $this->getOutput();
181
182
        if ($load < $output) {
183
            return "color: red;";
184
        }
185
186
        if ($this->getCapacity() === 0) {
187
            return "";
188
        }
189
190
        $percentage = $load / $this->getCapacity();
191
192
        return $percentage > 0.3 ? "" :  "color: yellow;";
193
    }
194
}
195