|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VideoGamesRecords\CoreBundle\DataProvider; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\Exception\ORMException; |
|
9
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Player; |
|
10
|
|
|
use VideoGamesRecords\CoreBundle\Security\UserProvider; |
|
11
|
|
|
|
|
12
|
|
|
class TopScoreProvider |
|
13
|
|
|
{ |
|
14
|
|
|
protected EntityManagerInterface $em; |
|
15
|
|
|
protected UserProvider $userProvider; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct( |
|
18
|
|
|
EntityManagerInterface $em, |
|
19
|
|
|
UserProvider $userProvider |
|
20
|
|
|
) { |
|
21
|
|
|
$this->em = $em; |
|
22
|
|
|
$this->userProvider = $userProvider; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @throws ORMException |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function getPlayer(): ?Player |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->userProvider->getUser()) { |
|
31
|
|
|
return $this->userProvider->getPlayer(); |
|
32
|
|
|
} |
|
33
|
|
|
return null; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @throws ORMException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function load($group, string $locale = 'en'): mixed |
|
40
|
|
|
{ |
|
41
|
|
|
$player = $this->getPlayer(); |
|
42
|
|
|
$query = $this->em->createQueryBuilder() |
|
43
|
|
|
->select('ch') |
|
44
|
|
|
->from('VideoGamesRecords\CoreBundle\Entity\Chart', 'ch') |
|
45
|
|
|
->join('ch.group', 'gr') |
|
46
|
|
|
->addSelect('gr') |
|
47
|
|
|
->addSelect('pc') |
|
48
|
|
|
->andWhere('ch.group = :group') |
|
49
|
|
|
->setParameter('group', $group); |
|
50
|
|
|
|
|
51
|
|
|
$column = ($locale == 'fr') ? 'libChartFr' : 'libChartEn'; |
|
52
|
|
|
$query->orderBy("ch.$column", 'ASC'); |
|
53
|
|
|
|
|
54
|
|
|
if ($player !== null) { |
|
55
|
|
|
$query->leftJoin('ch.playerCharts', 'pc', 'WITH', 'pc.rank = 1 OR pc.player = :player') |
|
56
|
|
|
->setParameter('player', $player); |
|
57
|
|
|
} else { |
|
58
|
|
|
$query->leftJoin('ch.playerCharts', 'pc', 'WITH', 'pc.rank = 1'); |
|
59
|
|
|
} |
|
60
|
|
|
$charts = $query->getQuery()->getResult(); |
|
61
|
|
|
|
|
62
|
|
|
// Set top1 and player score |
|
63
|
|
|
foreach ($charts as $chart) { |
|
64
|
|
|
foreach ($chart->getPlayerCharts() as $playerChart) { |
|
65
|
|
|
if ($playerChart->getRank() == 1) { |
|
66
|
|
|
$chart->setPlayerChart1($playerChart); |
|
67
|
|
|
} |
|
68
|
|
|
if (($player !== null) && ($playerChart->getPlayer()->getId() == $player->getId())) { |
|
69
|
|
|
$chart->setPlayerChartP($playerChart); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return $charts; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|