1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Orm\Repository; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
use Stu\Component\Database\DatabaseCategoryTypeEnum; |
9
|
|
|
use Stu\Orm\Entity\DatabaseEntry; |
10
|
|
|
use Stu\Orm\Entity\LayerInterface; |
11
|
|
|
use Stu\Orm\Entity\Map; |
12
|
|
|
use Stu\Orm\Entity\StarSystem; |
13
|
|
|
use Stu\Orm\Entity\StarSystemInterface; |
14
|
|
|
use Stu\Orm\Entity\StarSystemName; |
15
|
|
|
use Stu\Orm\Entity\StarSystemNameInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @extends EntityRepository<StarSystem> |
19
|
|
|
*/ |
20
|
|
|
final class StarSystemRepository extends EntityRepository implements StarSystemRepositoryInterface |
21
|
|
|
{ |
22
|
|
|
public function prototype(): StarSystemInterface |
23
|
|
|
{ |
24
|
|
|
return new StarSystem(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function save(StarSystemInterface $storage): void |
28
|
|
|
{ |
29
|
|
|
$em = $this->getEntityManager(); |
30
|
|
|
|
31
|
|
|
$em->persist($storage); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getByLayer(int $layerId): array |
35
|
|
|
{ |
36
|
|
|
return $this->getEntityManager() |
|
|
|
|
37
|
|
|
->createQuery( |
38
|
|
|
sprintf( |
39
|
|
|
'SELECT s FROM %s s |
40
|
|
|
JOIN %s m |
41
|
|
|
WITH m.systems_id = s.id |
42
|
|
|
WHERE m.layer_id = :layerId |
43
|
|
|
ORDER BY s.name ASC', |
44
|
|
|
StarSystem::class, |
45
|
|
|
Map::class |
46
|
|
|
) |
47
|
|
|
) |
48
|
|
|
->setParameters([ |
49
|
|
|
'layerId' => $layerId |
50
|
|
|
]) |
51
|
|
|
->getResult(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getWithoutDatabaseEntry(): array |
55
|
|
|
{ |
56
|
|
|
return $this->getEntityManager() |
|
|
|
|
57
|
|
|
->createQuery( |
58
|
|
|
sprintf( |
59
|
|
|
'SELECT t FROM %s t WHERE t.database_id NOT IN (SELECT d.id FROM %s d WHERE d.category_id = :categoryId)', |
60
|
|
|
StarSystem::class, |
61
|
|
|
DatabaseEntry::class |
62
|
|
|
) |
63
|
|
|
) |
64
|
|
|
->setParameters([ |
65
|
|
|
'categoryId' => DatabaseCategoryTypeEnum::DATABASE_CATEGORY_STARSYSTEM, |
66
|
|
|
]) |
67
|
|
|
->getResult(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getNumberOfSystemsToGenerate(LayerInterface $layer): int |
71
|
|
|
{ |
72
|
|
|
return (int) $this->getEntityManager() |
73
|
|
|
->createQuery( |
74
|
|
|
sprintf( |
75
|
|
|
'SELECT count(m.id) from %s m |
76
|
|
|
WHERE m.system_type_id IS NOT NULL |
77
|
|
|
AND m.systems_id IS NULL |
78
|
|
|
AND m.layer = :layer', |
79
|
|
|
Map::class |
80
|
|
|
) |
81
|
|
|
) |
82
|
|
|
->setParameters([ |
83
|
|
|
'layer' => $layer |
84
|
|
|
]) |
85
|
|
|
->getSingleScalarResult(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getRandomFreeSystemNames(int $amount): array |
89
|
|
|
{ |
90
|
|
|
$freeNames = $this->getEntityManager() |
91
|
|
|
->createQuery( |
92
|
|
|
sprintf( |
93
|
|
|
'SELECT ssm FROM %s ssm |
94
|
|
|
WHERE NOT EXISTS (SELECT ss.id |
95
|
|
|
FROM %s ss |
96
|
|
|
WHERE ss.name = ssm.name)', |
97
|
|
|
StarSystemName::class, |
98
|
|
|
StarSystem::class |
99
|
|
|
) |
100
|
|
|
) |
101
|
|
|
->getResult(); |
102
|
|
|
|
103
|
|
|
shuffle($freeNames); |
|
|
|
|
104
|
|
|
|
105
|
|
|
return array_slice($freeNames, 0, $amount); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getPreviousStarSystem(StarSystemInterface $current): ?StarSystemInterface |
109
|
|
|
{ |
110
|
|
|
return $this->getEntityManager() |
|
|
|
|
111
|
|
|
->createQuery( |
112
|
|
|
sprintf( |
113
|
|
|
'SELECT ss FROM %s ss |
114
|
|
|
JOIN %s m |
115
|
|
|
WITH m.systems_id = ss.id |
116
|
|
|
WHERE ss.id < :currentId |
117
|
|
|
AND m.layer = :layer |
118
|
|
|
ORDER BY ss.id DESC', |
119
|
|
|
StarSystem::class, |
120
|
|
|
Map::class |
121
|
|
|
) |
122
|
|
|
) |
123
|
|
|
->setParameters(['layer' => $current->getLayer(), 'currentId' => $current->getId()]) |
124
|
|
|
->setMaxResults(1) |
125
|
|
|
->getOneOrNullResult(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getNextStarSystem(StarSystemInterface $current): ?StarSystemInterface |
129
|
|
|
{ |
130
|
|
|
return $this->getEntityManager() |
|
|
|
|
131
|
|
|
->createQuery( |
132
|
|
|
sprintf( |
133
|
|
|
'SELECT ss FROM %s ss |
134
|
|
|
JOIN %s m |
135
|
|
|
WITH m.systems_id = ss.id |
136
|
|
|
WHERE ss.id > :currentId |
137
|
|
|
AND m.layer = :layer |
138
|
|
|
ORDER BY ss.id ASC', |
139
|
|
|
StarSystem::class, |
140
|
|
|
Map::class |
141
|
|
|
) |
142
|
|
|
) |
143
|
|
|
->setParameters(['layer' => $current->getLayer(), 'currentId' => $current->getId()]) |
144
|
|
|
->setMaxResults(1) |
145
|
|
|
->getOneOrNullResult(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|