Passed
Push — master ( 643140...6d8b29 )
by Janko
26:27
created

PlanetField::getConstructionStatusBar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Entity;
6
7
use Doctrine\ORM\Mapping\Column;
8
use Doctrine\ORM\Mapping\Entity;
9
use Doctrine\ORM\Mapping\GeneratedValue;
10
use Doctrine\ORM\Mapping\Id;
11
use Doctrine\ORM\Mapping\Index;
12
use Doctrine\ORM\Mapping\JoinColumn;
13
use Doctrine\ORM\Mapping\ManyToOne;
14
use Doctrine\ORM\Mapping\Table;
15
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use RuntimeException;
17
use Stu\Orm\Repository\PlanetFieldRepository;
18
19
#[Table(name: 'stu_colonies_fielddata')]
20
#[Index(name: 'colony_field_idx', columns: ['colonies_id', 'field_id'])]
21
#[Index(name: 'sandbox_field_idx', columns: ['colony_sandbox_id', 'field_id'])]
22
#[Index(name: 'colony_building_active_idx', columns: ['colonies_id', 'buildings_id', 'aktiv'])]
23
#[Index(name: 'sandbox_building_active_idx', columns: ['colony_sandbox_id', 'buildings_id', 'aktiv'])]
24
#[Index(name: 'active_idx', columns: ['aktiv'])]
25
#[Entity(repositoryClass: PlanetFieldRepository::class)]
26
class PlanetField implements PlanetFieldInterface
27
{
28
    #[Id]
29
    #[Column(type: 'integer')]
30
    #[GeneratedValue(strategy: 'IDENTITY')]
31
    private int $id;
32
33
    #[Column(type: 'integer', nullable: true)]
34
    private ?int $colonies_id = null;
0 ignored issues
show
introduced by
The private property $colonies_id is not used, and could be removed.
Loading history...
35
36
    #[Column(type: 'integer', nullable: true)]
37
    private ?int $colony_sandbox_id = null;
0 ignored issues
show
introduced by
The private property $colony_sandbox_id is not used, and could be removed.
Loading history...
38
39
    #[Column(type: 'smallint')]
40
    private int $field_id = 0;
41
42
    #[Column(type: 'integer')]
43
    private int $type_id = 0;
44
45
    #[Column(type: 'integer', nullable: true)]
46
    private ?int $buildings_id = null;
47
48
    #[Column(type: 'integer', nullable: true)]
49
    private ?int $terraforming_id = null;
50
51
    #[Column(type: 'smallint')]
52
    private int $integrity = 0;
53
54
    #[Column(type: 'integer')]
55
    private int $aktiv = 0;
56
57
    #[Column(type: 'boolean')]
58
    private bool $activate_after_build = true;
59
60
    #[ManyToOne(targetEntity: 'Building')]
61
    #[JoinColumn(name: 'buildings_id', referencedColumnName: 'id')]
62
    private ?BuildingInterface $building = null;
63
64
    #[ManyToOne(targetEntity: 'Terraforming')]
65
    #[JoinColumn(name: 'terraforming_id', referencedColumnName: 'id')]
66
    private ?TerraformingInterface $terraforming = null;
67
68
    #[ManyToOne(targetEntity: 'Colony')]
69
    #[JoinColumn(name: 'colonies_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
70
    private ?ColonyInterface $colony = null;
71
72
    #[ManyToOne(targetEntity: 'ColonySandbox')]
73
    #[JoinColumn(name: 'colony_sandbox_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
74
    private ?ColonySandboxInterface $sandbox = null;
75
76
    private bool $buildmode = false;
77
78
    #[Override]
79
    public function getId(): int
80
    {
81
        return $this->id;
82
    }
83
84
    #[Override]
85
    public function getFieldId(): int
86
    {
87
        return $this->field_id;
88
    }
89
90
    #[Override]
91
    public function setFieldId(int $fieldId): PlanetFieldInterface
92
    {
93
        $this->field_id = $fieldId;
94
        return $this;
95
    }
96
97
    #[Override]
98
    public function getFieldType(): int
99
    {
100
        return $this->type_id;
101
    }
102
103
    #[Override]
104
    public function setFieldType(int $planetFieldTypeId): PlanetFieldInterface
105
    {
106
        $this->type_id = $planetFieldTypeId;
107
        return $this;
108
    }
109
110
    #[Override]
111
    public function getBuildingId(): ?int
112
    {
113
        return $this->buildings_id;
114
    }
115
116
    #[Override]
117
    public function getTerraformingId(): ?int
118
    {
119
        return $this->terraforming_id;
120
    }
121
122
    #[Override]
123
    public function getIntegrity(): int
124
    {
125
        return $this->integrity;
126
    }
127
128
    #[Override]
129
    public function setIntegrity(int $integrity): PlanetFieldInterface
130
    {
131
        $this->integrity = $integrity;
132
        return $this;
133
    }
134
135
    #[Override]
136
    public function getActive(): int
137
    {
138
        return $this->aktiv;
139
    }
140
141
    #[Override]
142
    public function setActive(int $aktiv): PlanetFieldInterface
143
    {
144
        $this->aktiv = $aktiv;
145
        return $this;
146
    }
147
148
    #[Override]
149
    public function getActivateAfterBuild(): bool
150
    {
151
        return $this->activate_after_build;
152
    }
153
154
    #[Override]
155
    public function setActivateAfterBuild(bool $activateAfterBuild): PlanetFieldInterface
156
    {
157
        $this->activate_after_build = $activateAfterBuild;
158
        return $this;
159
    }
160
161
    #[Override]
162
    public function setBuildMode(bool $value): void
163
    {
164
        $this->buildmode = $value;
165
    }
166
167
    #[Override]
168
    public function getBuildtime(): int
169
    {
170
        return $this->getActive();
171
    }
172
173
    #[Override]
174
    public function isActive(): bool
175
    {
176
        return $this->getActive() === 1;
177
    }
178
179
    #[Override]
180
    public function isActivateable(): bool
181
    {
182
        if ($this->hasBuilding() === false) {
183
            return false;
184
        }
185
        if ($this->isUnderConstruction()) {
186
            return false;
187
        }
188
        return $this->getBuilding()->isActivateable();
189
    }
190
191
    #[Override]
192
    public function hasHighDamage(): bool
193
    {
194
        if (!$this->isDamaged()) {
195
            return false;
196
        }
197
        return round((100 / $this->getBuilding()->getIntegrity()) * $this->getIntegrity()) < 50;
198
    }
199
200
    #[Override]
201
    public function isUnderConstruction(): bool
202
    {
203
        return $this->getActive() > 1;
204
    }
205
206
    #[Override]
207
    public function hasBuilding(): bool
208
    {
209
        return $this->getBuilding() !== null;
210
    }
211
212
    #[Override]
213
    public function getCssClass(): string
214
    {
215
        if ($this->buildmode === true) {
216
            return 'cfb';
217
        }
218
        if ($this->isActive()) {
219
            if ($this->isDamaged()) {
220
                return 'cfld';
221
            }
222
            return 'cfa';
223
        }
224
        if ($this->hasHighDamage()) {
225
            return 'cfhd';
226
        }
227
        if ($this->hasBuilding()) {
228
            if ($this->isUnderConstruction()) {
229
                return 'cfc';
230
            }
231
232
            if ($this->isActivateable()) {
233
                return 'cfd';
234
            }
235
        }
236
237
        return 'cfu';
238
    }
239
240
    #[Override]
241
    public function getBuildingState(): string
242
    {
243
        if ($this->isUnderConstruction()) {
244
            return 'b';
245
        }
246
        return 'a';
247
    }
248
249
    #[Override]
250
    public function getBuilding(): ?BuildingInterface
251
    {
252
        return $this->building;
253
    }
254
255
    #[Override]
256
    public function setBuilding(?BuildingInterface $building): PlanetFieldInterface
257
    {
258
        $this->building = $building;
259
260
        return $this;
261
    }
262
263
    #[Override]
264
    public function isDamaged(): bool
265
    {
266
        if (!$this->hasBuilding()) {
267
            return false;
268
        }
269
        if ($this->isUnderConstruction()) {
270
            return false;
271
        }
272
        return $this->getIntegrity() !== $this->getBuilding()->getIntegrity();
273
    }
274
275
    #[Override]
276
    public function clearBuilding(): void
277
    {
278
        $this->setBuilding(null);
279
        $this->setIntegrity(0);
280
        $this->setActive(0);
281
    }
282
283
    #[Override]
284
    public function getHost(): ColonyInterface|ColonySandboxInterface
285
    {
286
        $colony = $this->colony;
287
        $sandbox = $this->sandbox;
288
289
        if ($colony === null && $sandbox === null) {
290
            throw new RuntimeException('this should not happen');
291
        }
292
293
        return $colony ?? $sandbox;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $colony ?? $sandbox could return the type null which is incompatible with the type-hinted return Stu\Orm\Entity\ColonyInt...\ColonySandboxInterface. Consider adding an additional type-check to rule them out.
Loading history...
294
    }
295
296
    #[Override]
297
    public function setColony(ColonyInterface $colony): PlanetFieldInterface
298
    {
299
        $this->colony = $colony;
300
        return $this;
301
    }
302
303
    #[Override]
304
    public function setColonySandbox(ColonySandboxInterface $sandbox): PlanetFieldInterface
305
    {
306
        $this->sandbox = $sandbox;
307
        return $this;
308
    }
309
310
    #[Override]
311
    public function getTerraforming(): ?TerraformingInterface
312
    {
313
        return $this->terraforming;
314
    }
315
316
    #[Override]
317
    public function setTerraforming(?TerraformingInterface $terraforming): PlanetFieldInterface
318
    {
319
        $this->terraforming = $terraforming;
320
321
        return $this;
322
    }
323
324
    #[Override]
325
    public function getDayNightPrefix(): string
326
    {
327
        $twilightZone = $this->getHost()->getTwilightZone();
328
329
        if ($twilightZone >= 0) {
330
            return $this->getFieldId() % $this->getHost()->getSurfaceWidth() >= $twilightZone ? 'n' : 't';
331
        }
332
333
        return $this->getFieldId() % $this->getHost()->getSurfaceWidth() < -$twilightZone ? 'n' : 't';
334
    }
335
336
    #[Override]
337
    public function getBuildProgress(): int
338
    {
339
        $building = $this->getBuilding();
340
        if ($building === null) {
341
            return 0;
342
        }
343
344
        $start = $this->getBuildtime() - $building->getBuildTime();
345
        return time() - $start;
346
    }
347
348
    #[Override]
349
    public function getOverlayWidth(): int
350
    {
351
        $building = $this->getBuilding();
352
        if ($building === null) {
353
            throw new RuntimeException('building is null');
354
        }
355
356
        $buildtime = $building->getBuildtime();
357
        $perc = max(0, @round((100 / $buildtime) * min($this->getBuildProgress(), $buildtime)));
358
        return (int) round((40 / 100) * $perc);
359
    }
360
361
    #[Override]
362
    public function getPictureType(): string
363
    {
364
        return $this->getBuildingId() . "/" . $this->getBuilding()->getBuildingType() . $this->getBuildingState();
365
    }
366
367
    #[Override]
368
    public function isColonizeAble(): bool
369
    {
370
        return in_array($this->getFieldType(), $this->getHost()->getColonyClass()->getColonizeableFields());
371
    }
372
}
373