Passed
Pull Request — master (#1686)
by Nico
31:24
created

ReactorWrapper::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
    public function __construct(
23
        ShipWrapperInterface $wrapper,
24
        AbstractReactorSystemData $reactorSystemData
25
    ) {
26
        $this->wrapper = $wrapper;
27
        $this->reactorSystemData = $reactorSystemData;
28
    }
29
30
    public function get(): AbstractReactorSystemData
31
    {
32
        return $this->reactorSystemData;
33
    }
34
35
    private function getWarpdriveProduction(): int
36
    {
37
        if ($this->warpdriveProduction === null) {
38
            $warpdrive = $this->wrapper->getWarpDriveSystemData();
39
40
            if ($warpdrive === null) {
41
                $this->warpdriveProduction = 0;
42
            } else {
43
                $warpdriveSplit = $warpdrive->getWarpdriveSplit();
44
                $reactorOutput = $this->getOutputCappedByLoad();
45
                $flightCost = $this->wrapper->get()->getRump()->getFlightEcost();
46
                $maxWarpdriveGain = (int)floor(($reactorOutput - $this->wrapper->getEpsUsage()) / $flightCost);
47
48
                $this->warpdriveProduction = (int)round((1 - ($warpdriveSplit / 100)) * $maxWarpdriveGain);
49
            }
50
        }
51
52
        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...
53
    }
54
55
    public function getEpsProduction(): int
56
    {
57
        if ($this->epsProduction === null) {
58
            $warpdrive = $this->wrapper->getWarpDriveSystemData();
59
            $warpdriveSplit = $warpdrive === null ? 100 : $warpdrive->getWarpdriveSplit();
60
61
            if ($warpdriveSplit === 0) {
62
                $this->epsProduction = $this->wrapper->getEpsUsage();
63
            } else {
64
                $reactorOutput = $this->getOutputCappedByLoad();
65
                $warpDriveProduction = $this->getWarpdriveProduction();
66
                $flightCost = $this->wrapper->get()->getRump()->getFlightEcost();
67
68
                $this->epsProduction = $reactorOutput - ($warpDriveProduction * $flightCost);
69
            }
70
        }
71
72
        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...
73
    }
74
75
    public function getEffectiveEpsProduction(): int
76
    {
77
        if ($this->effectiveEpsProduction === null) {
78
            $epsSystem = $this->wrapper->getEpsSystemData();
79
            $missingEps = $epsSystem === null ? 0 : $epsSystem->getMaxEps() - $epsSystem->getEps();
80
            $epsGrowthCap = $this->getEpsProduction() - $this->wrapper->getEpsUsage();
81
            $this->effectiveEpsProduction = min($missingEps, $epsGrowthCap);
82
        }
83
        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...
84
    }
85
86
    public function getEffectiveWarpDriveProduction(): int
87
    {
88
        if ($this->effectiveWarpDriveProduction === null) {
89
            $warpdrive = $this->wrapper->getWarpDriveSystemData();
90
            $missingWarpdrive = $warpdrive === null ? 0 : $warpdrive->getMaxWarpDrive() - $warpdrive->getWarpDrive();
91
92
            $this->effectiveWarpDriveProduction = min($missingWarpdrive, $this->getWarpdriveProduction());
93
        }
94
95
        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...
96
    }
97
98
    public function getUsage(): int
99
    {
100
        return $this->getEffectiveEpsProduction()
101
            + $this->getEffectiveWarpDriveProduction() * $this->wrapper->get()->getRump()->getFlightEcost();
102
    }
103
104
    public function getCapacity(): int
105
    {
106
        return $this->get()->getCapacity();
107
    }
108
109
    public function getOutput(): int
110
    {
111
        return $this->get()->getOutput();
112
    }
113
114
    public function setOutput(int $output): ReactorWrapperInterface
115
    {
116
        $this->get()->setOutput($output)->update();
117
118
        return $this;
119
    }
120
121
    public function getOutputCappedByLoad(): int
122
    {
123
        if ($this->getOutput() > $this->getLoad()) {
124
            return $this->getLoad();
125
        }
126
127
        return $this->getOutput();
128
    }
129
130
    public function getLoad(): int
131
    {
132
        return $this->get()->getLoad();
133
    }
134
135
    public function setLoad(int $load): ReactorWrapperInterface
136
    {
137
        $this->get()->setLoad($load)->update();
138
139
        return $this;
140
    }
141
142
    public function changeLoad(int $amount): ReactorWrapperInterface
143
    {
144
        $this->get()->setLoad($this->get()->getLoad() + $amount)->update();
145
146
        return $this;
147
    }
148
149
    public function isHealthy(): bool
150
    {
151
        return $this->wrapper->get()->isSystemHealthy($this->get()->getSystemType());
152
    }
153
154
    public function getReactorLoadStyle(): string
155
    {
156
        $load = $this->getLoad();
157
        $output = $this->getOutput();
158
159
        if ($load < $output) {
160
            return "color: red;";
161
        }
162
163
        if ($this->getCapacity() === 0) {
164
            return "";
165
        }
166
167
        $percentage = $load / $this->getCapacity();
168
169
        return $percentage > 0.3 ? "" :  "color: yellow;";
170
    }
171
}
172