Passed
Push — master ( be429c...fef648 )
by Nico
28:06
created

getPositiveEffectSecondary()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Colony;
6
7
use Stu\Lib\Colony\PlanetFieldHostInterface;
8
use Stu\Lib\ColonyProduction\ColonyProduction;
9
use Stu\Module\Commodity\CommodityTypeEnum;
10
use Stu\Orm\Entity\ColonyInterface;
11
12
final class ColonyPopulationCalculator implements ColonyPopulationCalculatorInterface
13
{
14
    private PlanetFieldHostInterface $host;
15
16
    private ?int $positive_effect_secondary = null;
17
18
    private ?int $positive_effect_primary = null;
19
20
    /** @var array<int, ColonyProduction> */
21
    private array $production;
22
23
    /**
24
     * @param array<int, ColonyProduction> $production
25
     */
26
    public function __construct(
27
        PlanetFieldHostInterface $host,
28
        array $production
29
    ) {
30
        $this->host = $host;
31
        $this->production = $production;
32
    }
33
34
    public function getFreeAssignmentCount(): int
35
    {
36
        if (!$this->host instanceof ColonyInterface) {
37
            return 0;
38
        }
39
40
        return max(0, $this->getCrewLimit() - $this->host->getCrewAssignmentAmount());
0 ignored issues
show
Bug introduced by
The method getCrewAssignmentAmount() does not exist on Stu\Lib\Colony\PlanetFieldHostInterface. It seems like you code against a sub-type of Stu\Lib\Colony\PlanetFieldHostInterface such as Stu\Orm\Entity\ColonyInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        return max(0, $this->getCrewLimit() - $this->host->/** @scrutinizer ignore-call */ getCrewAssignmentAmount());
Loading history...
41
    }
42
43
    public function getCrewLimit(): int
44
    {
45
        return (int) floor(
46
            10 +
47
                min(
48
                    max(
49
                        ($this->getPositiveEffectPrimary() - (4 * max(
50
                            0,
51
                            $this->getNegativeEffect() - $this->getPositiveEffectSecondary()
52
                        ))),
53
                        0
54
                    ),
55
                    $this->host->getWorkers()
56
                ) / 5 * $this->getLifeStandardPercentage() / 100
57
        );
58
    }
59
60
    public function getLifeStandardPercentage(): int
61
    {
62
        $colonyProduction = $this->production[CommodityTypeEnum::COMMODITY_EFFECT_LIFE_STANDARD] ?? null;
63
        $production = $colonyProduction !== null ? $colonyProduction->getProduction() : 0;
64
65
        if ($production == 0) {
66
            return 0;
67
        }
68
69
        if ($production > $this->host->getPopulation()) {
70
            return 100;
71
        }
72
73
        return (int)floor($production * 100 / $this->host->getPopulation());
74
    }
75
76
    public function getNegativeEffect(): int
77
    {
78
        return (int) ceil($this->host->getPopulation() / 70);
79
    }
80
81
    public function getPositiveEffectPrimary(): int
82
    {
83
        if ($this->positive_effect_primary === null) {
84
            $this->positive_effect_primary = 0;
85
86
            $commodity = $this->host->getUser()->getFaction()->getPrimaryEffectCommodity();
87
88
            if ($commodity !== null && array_key_exists($commodity->getId(), $this->production)) {
89
                $this->positive_effect_primary += $this->production[$commodity->getId()]->getProduction();
90
            }
91
        }
92
93
        return $this->positive_effect_primary;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->positive_effect_primary 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...
94
    }
95
96
    public function getPositiveEffectSecondary(): int
97
    {
98
        if ($this->positive_effect_secondary === null) {
99
            $this->positive_effect_secondary = 0;
100
101
            $commodity = $this->host->getUser()->getFaction()->getSecondaryEffectCommodity();
102
103
            if ($commodity !== null && array_key_exists($commodity->getId(), $this->production)) {
104
                $this->positive_effect_secondary += $this->production[$commodity->getId()]->getProduction();
105
            }
106
        }
107
        return $this->positive_effect_secondary;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->positive_effect_secondary 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...
108
    }
109
110
    public function getGrowth(): int
111
    {
112
        $host = $this->host;
113
        if (!$host instanceof ColonyInterface) {
114
            return 0;
115
        }
116
117
        if ($host->getImmigrationState() === false) {
118
            return 0;
119
        }
120
121
        // TBD: depends on social things. return dummy for now
122
        $im = ceil((($host->getMaxBev() - $host->getPopulation()) / 3) / 100 * $host->getColonyClass()->getBevGrowthRate() *  $this->getLifeStandardPercentage() / 50);
123
        if ($host->getPopulation() + $im > $host->getMaxBev()) {
124
            $im = $host->getMaxBev() - $host->getPopulation();
125
        }
126
        if ($host->getPopulationLimit() > 0 && $host->getPopulation() + $im > $host->getPopulationLimit()) {
127
            $im = $host->getPopulationLimit() - $host->getPopulation();
128
        }
129
        if ($im < 0) {
130
            return 0;
131
        }
132
        return (int) $im;
133
    }
134
}
135