Test Failed
Push — dev ( 6952f7...3202be )
by Janko
25:15
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
            $reactorOutput = $this->getOutputCappedByLoad();
59
            $warpDriveProduction = $this->getWarpdriveProduction();
60
            $flightCost = $this->wrapper->get()->getRump()->getFlightEcost();
61
62
            $this->epsProduction = $reactorOutput - ($warpDriveProduction * $flightCost);
63
        }
64
65
        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...
66
    }
67
68
    public function getEffectiveEpsProduction(): int
69
    {
70
        if ($this->effectiveEpsProduction === null) {
71
            $epsSystem = $this->wrapper->getEpsSystemData();
72
            $missingEps = $epsSystem === null ? 0 : $epsSystem->getMaxEps() - $epsSystem->getEps();
73
            $epsGrowthCap = $this->getEpsProduction() - $this->wrapper->getEpsUsage();
74
            $this->effectiveEpsProduction = min($missingEps, $epsGrowthCap);
75
        }
76
        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...
77
    }
78
79
    public function getEffectiveWarpDriveProduction(): int
80
    {
81
        if ($this->effectiveWarpDriveProduction === null) {
82
            $warpdrive = $this->wrapper->getWarpDriveSystemData();
83
            $missingWarpdrive = $warpdrive === null ? 0 : $warpdrive->getMaxWarpDrive() - $warpdrive->getWarpDrive();
84
85
            $this->effectiveWarpDriveProduction = min($missingWarpdrive, $this->getWarpdriveProduction());
86
        }
87
88
        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...
89
    }
90
91
    public function getUsage(): int
92
    {
93
        return $this->getEffectiveEpsProduction()
94
            + $this->getEffectiveWarpDriveProduction() * $this->wrapper->get()->getRump()->getFlightEcost();
95
    }
96
97
    public function getCapacity(): int
98
    {
99
        return $this->get()->getCapacity();
100
    }
101
102
    public function getOutput(): int
103
    {
104
        return $this->get()->getOutput();
105
    }
106
107
    public function setOutput(int $output): ReactorWrapperInterface
108
    {
109
        $this->get()->setOutput($output)->update();
110
111
        return $this;
112
    }
113
114
    public function getOutputCappedByLoad(): int
115
    {
116
        if ($this->getOutput() > $this->getLoad()) {
117
            return $this->getLoad();
118
        }
119
120
        return $this->getOutput();
121
    }
122
123
    public function getLoad(): int
124
    {
125
        return $this->get()->getLoad();
126
    }
127
128
    public function setLoad(int $load): ReactorWrapperInterface
129
    {
130
        $this->get()->setLoad($load)->update();
131
132
        return $this;
133
    }
134
135
    public function changeLoad(int $amount): ReactorWrapperInterface
136
    {
137
        $this->get()->setLoad($this->get()->getLoad() + $amount)->update();
138
139
        return $this;
140
    }
141
142
    public function isHealthy(): bool
143
    {
144
        return $this->wrapper->get()->isSystemHealthy($this->get()->getSystemType());
145
    }
146
147
    public function getReactorLoadStyle(): string
148
    {
149
        $load = $this->getLoad();
150
        $output = $this->getOutput();
151
152
        if ($load < $output) {
153
            return "color: red;";
154
        }
155
156
        if ($this->getCapacity() === 0) {
157
            return "";
158
        }
159
160
        $percentage = $load / $this->getCapacity();
161
162
        return $percentage > 0.3 ? "" :  "color: yellow;";
163
    }
164
}
165