Passed
Push — master ( bb5d68...d9c4a7 )
by Nico
56:35 queued 26:36
created

ColonySurface::getSurfaceTileStyle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\Lib;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Stu\Component\Building\BuildingEnum;
9
use Stu\Lib\Colony\PlanetFieldHostInterface;
10
use Stu\Module\Building\BuildingFunctionTypeEnum;
11
use Stu\Module\Logging\LoggerUtilInterface;
12
use Stu\Orm\Entity\ColonyInterface;
13
use Stu\Orm\Entity\PlanetFieldInterface;
14
use Stu\Orm\Repository\BuildingRepositoryInterface;
15
use Stu\Orm\Repository\ColonyRepositoryInterface;
16
use Stu\Orm\Repository\PlanetFieldRepositoryInterface;
17
use Stu\Orm\Repository\ResearchedRepositoryInterface;
18
use Stu\PlanetGenerator\Exception\PlanetGeneratorException;
19
use Stu\PlanetGenerator\PlanetGeneratorInterface;
20
21
/**
22
 * Provides access to several colony surface related methods
23
 */
24
final class ColonySurface implements ColonySurfaceInterface
25
{
26
    private PlanetFieldRepositoryInterface $planetFieldRepository;
27
28
    private BuildingRepositoryInterface $buildingRepository;
29
30
    private ColonyRepositoryInterface $colonyRepository;
31
32
    private ResearchedRepositoryInterface $researchedRepository;
33
34
    private PlanetGeneratorInterface $planetGenerator;
35
36
    private EntityManagerInterface $entityManager;
37
38
    private PlanetFieldHostInterface $host;
39
40
    private ?int $buildingId;
41
42
    private bool $showUnderground;
43
44
    private PlanetFieldTypeRetrieverInterface $planetFieldTypeRetriever;
45
46
    public function __construct(
47
        PlanetFieldRepositoryInterface $planetFieldRepository,
48
        BuildingRepositoryInterface $buildingRepository,
49
        ColonyRepositoryInterface $colonyRepository,
50
        ResearchedRepositoryInterface $researchedRepository,
51
        PlanetGeneratorInterface $planetGenerator,
52
        EntityManagerInterface $entityManager,
53
        PlanetFieldTypeRetrieverInterface $planetFieldTypeRetriever,
54
        PlanetFieldHostInterface $host,
55
        ?int $buildingId,
56
        bool $showUnderground,
57
        LoggerUtilInterface $loggerUtil
0 ignored issues
show
Unused Code introduced by
The parameter $loggerUtil is not used and could be removed. ( Ignorable by Annotation )

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

57
        /** @scrutinizer ignore-unused */ LoggerUtilInterface $loggerUtil

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    ) {
59
        $this->host = $host;
60
        $this->planetFieldRepository = $planetFieldRepository;
61
        $this->buildingRepository = $buildingRepository;
62
        $this->buildingId = $buildingId;
63
        $this->colonyRepository = $colonyRepository;
64
        $this->researchedRepository = $researchedRepository;
65
        $this->planetGenerator = $planetGenerator;
66
        $this->entityManager = $entityManager;
67
        $this->showUnderground = $showUnderground;
68
        $this->planetFieldTypeRetriever = $planetFieldTypeRetriever;
69
    }
70
71
    public function getSurface(): array
72
    {
73
        try {
74
            $this->updateSurface();
75
        } catch (PlanetGeneratorException $e) {
76
            return $this->host->getPlanetFields()->toArray();
77
        }
78
79
        $fields = $this->host->getPlanetFields()->toArray();
80
81
        if (!$this->showUnderground) {
82
            $fields = array_filter(
83
                $fields,
84
                fn (PlanetFieldInterface $field): bool => !$this->planetFieldTypeRetriever->isUndergroundField($field)
85
            );
86
        }
87
88
        if ($this->buildingId !== null) {
89
            $building = $this->buildingRepository->find($this->buildingId);
90
            $user = $this->host->getUser();
91
92
            $researchedArray = $this->researchedRepository->getFinishedListByUser($user->getId());
93
94
            array_walk(
95
                $fields,
96
                function (PlanetFieldInterface $field) use ($building, $researchedArray): void {
97
                    if (
98
                        $field->getTerraformingId() === null &&
99
                        $building->getBuildableFields()->containsKey($field->getFieldType())
100
                    ) {
101
                        //PlanetFieldTypeBuildingInterface
102
                        $fieldBuilding = $building->getBuildableFields()->get($field->getFieldType());
103
104
                        $researchId = $fieldBuilding->getResearchId();
105
                        if ($researchId == null || $this->isResearched($researchId, $researchedArray)) {
106
                            $field->setBuildMode(true);
107
                        }
108
                    }
109
                }
110
            );
111
        }
112
113
        return $fields;
114
    }
115
116
    private function isResearched(int $researchId, array $researched): bool
117
    {
118
        foreach ($researched as $research) {
119
            if ($research->getResearchId() == $researchId) {
120
                return true;
121
            }
122
        }
123
124
        return false;
125
    }
126
127
    public function getSurfaceTileStyle(): string
128
    {
129
        $width = $this->planetGenerator->loadColonyClassConfig($this->host->getColonyClass()->getId())['sizew'];
130
        $gridArray = [];
131
        for ($i = 0; $i < $width; $i++) {
132
            $gridArray[] = '43px';
133
        }
134
135
        return sprintf('display: grid; grid-template-columns: %s;', implode(' ', $gridArray));
136
    }
137
138
    public function updateSurface(): void
139
    {
140
        $host = $this->host;
141
        if (!$host instanceof ColonyInterface) {
142
            return;
143
        }
144
        if (!$host->isFree()) {
145
            return;
146
        }
147
148
        $mask = $host->getMask();
149
150
        if ($mask === null) {
151
            $planetConfig = $this->planetGenerator->generateColony(
152
                $host->getColonyClassId(),
153
                $host->getSystem()->getBonusFieldAmount()
154
            );
155
156
            $mask = base64_encode(serialize($planetConfig->getFieldArray()));
157
158
            $host->setMask($mask);
159
            $host->setSurfaceWidth($planetConfig->getSurfaceWidth());
160
161
            $this->colonyRepository->save($host);
162
        }
163
164
        $fields = $host->getPlanetFields()->toArray();
165
166
        $surface = unserialize(base64_decode($mask));
167
        foreach ($surface as $fieldId => $type) {
168
            if (!array_key_exists($fieldId, $fields)) {
169
                $newField = $this->planetFieldRepository->prototype();
170
                $fields[$fieldId] = $newField;
171
                $fields[$fieldId]->setColony($host);
172
                $fields[$fieldId]->setFieldId($fieldId);
173
                $host->getPlanetFields()->set($fieldId, $newField);
174
            }
175
176
            $fields[$fieldId]->setBuilding(null);
177
            $fields[$fieldId]->setIntegrity(0);
178
            $fields[$fieldId]->setFieldType((int) $type);
179
            $fields[$fieldId]->setActive(0);
180
181
            $this->planetFieldRepository->save($fields[$fieldId]);
182
        }
183
184
        $this->entityManager->flush();
185
    }
186
187
    public function hasShipyard(): bool
188
    {
189
        return $this->planetFieldRepository->getCountByColonyAndBuildingFunctionAndState(
190
            $this->host,
191
            BuildingFunctionTypeEnum::getShipyardOptions(),
192
            [0, 1]
193
        ) > 0;
194
    }
195
196
    public function hasAirfield(): bool
197
    {
198
        return $this->planetFieldRepository->getCountByColonyAndBuildingFunctionAndState(
199
            $this->host,
200
            [BuildingEnum::BUILDING_FUNCTION_AIRFIELD],
201
            [0, 1]
202
        ) > 0;
203
    }
204
}
205