Passed
Push — master ( 37217b...efdda9 )
by Nico
19:16 queued 09:28
created

StarSystemMap::setSystemId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
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\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\Entity;
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\JoinColumn;
14
use Doctrine\ORM\Mapping\ManyToOne;
15
use Doctrine\ORM\Mapping\OneToMany;
16
use Doctrine\ORM\Mapping\OneToOne;
17
use Doctrine\ORM\Mapping\OrderBy;
18
use Doctrine\ORM\Mapping\Table;
19
use Doctrine\ORM\Mapping\UniqueConstraint;
20
use Stu\Component\Map\MapEnum;
21
use Stu\Lib\SectorString;
22
23
/**
24
 * @Entity(repositoryClass="Stu\Orm\Repository\StarSystemMapRepository")
25
 * @Table(
26
 *     name="stu_sys_map",
27
 *     uniqueConstraints={@UniqueConstraint(name="system_coordinates_idx", columns={"sx", "sy", "systems_id"})}
28
 * )
29
 **/
30
class StarSystemMap implements StarSystemMapInterface
31
{
32
    /**
33
     * @Id
34
     * @Column(type="integer")
35
     * @GeneratedValue(strategy="IDENTITY")
36
     */
37
    private int $id;
38
39
    /**
40
     * @Column(type="smallint")
41
     *
42
     */
43
    private int $sx = 0;
44
45
    /**
46
     * @Column(type="smallint")
47
     *
48
     */
49
    private int $sy = 0;
50
51
    /**
52
     * @Column(type="integer")
53
     *
54
     */
55
    private int $systems_id = 0;
56
57
    /**
58
     * @Column(type="integer")
59
     *
60
     */
61
    private int $field_id = 0;
62
63
    /**
64
     *
65
     * @ManyToOne(targetEntity="StarSystem", inversedBy="fields")
66
     * @JoinColumn(name="systems_id", referencedColumnName="id")
67
     */
68
    private StarSystemInterface $starSystem;
69
70
    /**
71
     * @OneToOne(targetEntity="Colony", mappedBy="starsystem_map")
72
     */
73
    private ?ColonyInterface $colony = null;
74
75
    /**
76
     *
77
     * @ManyToOne(targetEntity="MapFieldType")
78
     * @JoinColumn(name="field_id", referencedColumnName="id")
79
     */
80
    private MapFieldTypeInterface $mapFieldType;
81
82
    /**
83
     * @var ArrayCollection<int, ShipInterface>
84
     *
85
     * @OneToMany(targetEntity="Ship", mappedBy="starsystem_map", fetch="EXTRA_LAZY")
86
     */
87
    private Collection $ships;
88
89
    /**
90
     * @var ArrayCollection<int, FlightSignatureInterface>
91
     *
92
     * @OneToMany(targetEntity="FlightSignature", mappedBy="starsystem_map")
93
     * @OrderBy({"time": "DESC"})
94
     */
95
    private Collection $signatures;
96
97
    /**
98
     * @var ArrayCollection<int, AnomalyInterface>
99
     *
100
     * @OneToMany(targetEntity="Anomaly", mappedBy="starsystem_map", fetch="EXTRA_LAZY")
101
     */
102
    private Collection $anomalies;
103
104
    /**
105
     * @var ArrayCollection<int, WormholeEntryInterface>
106
     *
107
     * @OneToMany(targetEntity="WormholeEntry", mappedBy="systemMap")
108
     */
109
    private Collection $wormholeEntries;
110
111
    public function __construct()
112
    {
113
        $this->ships = new ArrayCollection();
114
        $this->signatures = new ArrayCollection();
115
        $this->anomalies = new ArrayCollection();
116
        $this->wormholeEntries = new ArrayCollection();
117
    }
118
119
    public function getId(): int
120
    {
121
        return $this->id;
122
    }
123
124
    public function getSx(): int
125
    {
126
        return $this->sx;
127
    }
128
129
    public function setSx(int $sx): StarSystemMapInterface
130
    {
131
        $this->sx = $sx;
132
133
        return $this;
134
    }
135
136
    public function getX(): int
137
    {
138
        return $this->getSx();
139
    }
140
141
    public function getSy(): int
142
    {
143
        return $this->sy;
144
    }
145
146
    public function setSy(int $sy): StarSystemMapInterface
147
    {
148
        $this->sy = $sy;
149
150
        return $this;
151
    }
152
153
    public function getY(): int
154
    {
155
        return $this->getSy();
156
    }
157
158
    public function getSystemId(): int
159
    {
160
        return $this->systems_id;
161
    }
162
163
    public function getSystem(): StarSystemInterface
164
    {
165
        return $this->starSystem;
166
    }
167
168
    public function setSystem(StarSystemInterface $starSystem): StarSystemMapInterface
169
    {
170
        $this->starSystem = $starSystem;
171
172
        return $this;
173
    }
174
175
    public function getFieldId(): int
176
    {
177
        return $this->field_id;
178
    }
179
180
    public function getFieldType(): MapFieldTypeInterface
181
    {
182
        return $this->mapFieldType;
183
    }
184
185
    public function setFieldType(MapFieldTypeInterface $mapFieldType): StarSystemMapInterface
186
    {
187
        $this->mapFieldType = $mapFieldType;
188
189
        return $this;
190
    }
191
192
    public function getBackgroundId(): string
193
    {
194
195
        $x = (string)$this->getSx();
196
        $y = (string)$this->getSy();
197
198
199
        $x = str_pad($x, 2, '0', STR_PAD_LEFT);
200
        $y = str_pad($y, 2, '0', STR_PAD_LEFT);
201
202
203
        $backgroundId = $y . $x;
204
205
        return $backgroundId;
206
    }
207
208
209
210
    public function getColony(): ?ColonyInterface
211
    {
212
        return $this->colony;
213
    }
214
215
    public function getMapRegion(): ?MapRegionInterface
216
    {
217
        return null;
218
    }
219
220
    public function getAdministratedRegion(): ?MapRegionInterface
221
    {
222
        return null;
223
    }
224
225
    public function getInfluenceArea(): ?StarSystemInterface
226
    {
227
        return null;
228
    }
229
230
    public function getFieldStyle(): string
231
    {
232
        return "background-image: url('/assets/map/" . $this->getFieldId() . ".png'); opacity:1;";
233
    }
234
235
    public function getFieldGraphicID(): int
236
    {
237
        $fieldId = $this->getFieldId();
238
239
        if ($fieldId === 1) {
240
            return 0;
241
        } else {
242
            return $fieldId;
243
        }
244
    }
245
246
247
    public function getShips(): Collection
248
    {
249
        return $this->ships;
250
    }
251
252
    public function getSignatures(): Collection
253
    {
254
        return $this->signatures;
255
    }
256
257
    public function getAnomalies(): Collection
258
    {
259
        return $this->anomalies;
260
    }
261
262
    public function getRandomWormholeEntry(): ?WormholeEntryInterface
263
    {
264
        if ($this->wormholeEntries->isEmpty()) {
265
            return null;
266
        }
267
268
        $usableEntries =  array_filter(
269
            $this->wormholeEntries->toArray(),
270
            function (WormholeEntryInterface $entry): bool {
271
                $type = $entry->getType();
272
273
                return $entry->isUsable() && ($type === MapEnum::WORMHOLE_ENTRY_TYPE_BOTH ||
274
                    $type === MapEnum::WORMHOLE_ENTRY_TYPE_OUT);
275
            }
276
        );
277
278
        return $usableEntries === [] ? null : $usableEntries[array_rand($usableEntries)];
279
    }
280
281
    public function getSectorString(): string
282
    {
283
        return SectorString::getForStarSystemMap($this);
284
    }
285
}
286