1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VideoGamesRecords\CoreBundle\Controller\Player\Game; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
7
|
|
|
use Symfony\Component\Intl\Locale; |
8
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Player; |
9
|
|
|
|
10
|
|
|
class GetStats extends AbstractController |
11
|
|
|
{ |
12
|
|
|
protected EntityManagerInterface $em; |
13
|
|
|
|
14
|
|
|
public function __construct(EntityManagerInterface $em) |
15
|
|
|
{ |
16
|
|
|
$this->em = $em; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Player $player |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
public function __invoke(Player $player): array |
24
|
|
|
{ |
25
|
|
|
$playerGames = $this->getPlayerGameStats($player); |
26
|
|
|
$stats = $this->getStatusPerGame($player); |
27
|
|
|
|
28
|
|
|
foreach ($playerGames as $playerGame) { |
29
|
|
|
if (isset($stats[$playerGame->getGame()->getId()])) { |
30
|
|
|
$playerGame->setStatuses($stats[$playerGame->getGame()->getId()]); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
return $playerGames; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return data from player with game and platforms |
38
|
|
|
* |
39
|
|
|
* @param $player |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
private function getPlayerGameStats($player): array |
43
|
|
|
{ |
44
|
|
|
$qb = $this->em->createQueryBuilder() |
45
|
|
|
->select('pg') |
46
|
|
|
->from('VideoGamesRecords\CoreBundle\Entity\PlayerGame', 'pg') |
47
|
|
|
->join('pg.game', 'g') |
48
|
|
|
->addSelect('g') |
49
|
|
|
->join('g.platforms', 'p') |
50
|
|
|
->addSelect('p') |
51
|
|
|
->where('pg.player = :player') |
52
|
|
|
->setParameter('player', $player) |
53
|
|
|
->orderBy('g.' . (Locale::getDefault() == 'fr' ? 'libGameFr' : 'libGameEn'), 'ASC'); |
54
|
|
|
|
55
|
|
|
return $qb->getQuery()->getResult(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $player |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
private function getStatusPerGame($player): array |
63
|
|
|
{ |
64
|
|
|
$qb = $this->em->createQueryBuilder() |
65
|
|
|
->select('gam.id') |
66
|
|
|
->from('VideoGamesRecords\CoreBundle\Entity\Game', 'gam') |
67
|
|
|
->addSelect('status.id as idStatus') |
68
|
|
|
->addSelect('COUNT(pc) as nb') |
69
|
|
|
->innerJoin('gam.groups', 'grp') |
70
|
|
|
->innerJoin('grp.charts', 'chr') |
71
|
|
|
->innerJoin('chr.playerCharts', 'pc') |
72
|
|
|
->innerJoin('pc.status', 'status') |
73
|
|
|
->where('pc.player = :player') |
74
|
|
|
->setParameter('player', $player) |
75
|
|
|
->groupBy('gam.id') |
76
|
|
|
->addGroupBy('status.id') |
77
|
|
|
->orderBy('gam.id', 'ASC') |
78
|
|
|
->addOrderBy('status.id', 'ASC'); |
79
|
|
|
|
80
|
|
|
$list = $qb->getQuery()->getResult(2); |
81
|
|
|
|
82
|
|
|
$games = []; |
83
|
|
|
foreach ($list as $row) { |
84
|
|
|
$idGame = $row['id']; |
85
|
|
|
if (!array_key_exists($idGame, $games)) { |
86
|
|
|
$games[$idGame] = []; |
87
|
|
|
} |
88
|
|
|
$games[$idGame][] = [ |
89
|
|
|
'status' => $this->em->find('VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus', $row['idStatus']), |
90
|
|
|
'nb' => $row['nb'], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
return $games; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|