1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Orm\Repository; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
use Doctrine\ORM\NoResultException; |
9
|
|
|
use Stu\Orm\Entity\Anomaly; |
10
|
|
|
use Stu\Orm\Entity\AnomalyInterface; |
11
|
|
|
use Stu\Orm\Entity\Map; |
12
|
|
|
use Stu\Orm\Entity\ShipInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @extends EntityRepository<Anomaly> |
16
|
|
|
*/ |
17
|
|
|
final class AnomalyRepository extends EntityRepository implements AnomalyRepositoryInterface |
18
|
|
|
{ |
19
|
|
|
public function prototype(): AnomalyInterface |
20
|
|
|
{ |
21
|
|
|
return new Anomaly(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function save(AnomalyInterface $anomaly): void |
25
|
|
|
{ |
26
|
|
|
$em = $this->getEntityManager(); |
27
|
|
|
|
28
|
|
|
$em->persist($anomaly); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function delete(AnomalyInterface $anomaly): void |
32
|
|
|
{ |
33
|
|
|
$em = $this->getEntityManager(); |
34
|
|
|
|
35
|
|
|
$em->remove($anomaly); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array<AnomalyInterface> |
40
|
|
|
*/ |
41
|
|
|
public function findAllActive(): array |
42
|
|
|
{ |
43
|
|
|
return $this->getEntityManager() |
|
|
|
|
44
|
|
|
->createQuery( |
45
|
|
|
sprintf( |
46
|
|
|
'SELECT a |
47
|
|
|
FROM %s a |
48
|
|
|
WHERE a.remaining_ticks > 0', |
49
|
|
|
Anomaly::class |
50
|
|
|
) |
51
|
|
|
) |
52
|
|
|
->getResult(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getClosestAnomalyDistance(ShipInterface $ship): ?int |
56
|
|
|
{ |
57
|
|
|
$map = $ship->getMap(); |
58
|
|
|
if ($map === null) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$range = $ship->getSensorRange() * 2; |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
return (int)$this->getEntityManager()->createQuery( |
66
|
|
|
sprintf( |
67
|
|
|
'SELECT SQRT(ABS(m.cx - :x) * ABS(m.cx - :x) + ABS(m.cy - :y) * ABS(m.cy - :y)) as foo |
68
|
|
|
FROM %s a |
69
|
|
|
JOIN %s m |
70
|
|
|
WITH a.map_id = m.id |
71
|
|
|
WHERE (m.cx BETWEEN :startX AND :endX) |
72
|
|
|
AND (m.cy BETWEEN :startY AND :endY) |
73
|
|
|
AND m.layer = :layer |
74
|
|
|
ORDER BY foo ASC', |
75
|
|
|
Anomaly::class, |
76
|
|
|
Map::class |
77
|
|
|
) |
78
|
|
|
) |
79
|
|
|
->setMaxResults(1) |
80
|
|
|
->setParameters([ |
81
|
|
|
'layer' => $map->getLayer(), |
82
|
|
|
'x' => $map->getX(), |
83
|
|
|
'y' => $map->getY(), |
84
|
|
|
'startX' => $map->getX() - $range, |
85
|
|
|
'endX' => $map->getX() + $range, |
86
|
|
|
'startY' => $map->getY() - $range, |
87
|
|
|
'endY' => $map->getY() + $range |
88
|
|
|
]) |
89
|
|
|
->getSingleScalarResult(); |
90
|
|
|
} catch (NoResultException) { |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|