PlanetField::getActive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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