1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VideoGamesRecords\CoreBundle\Ranking\Command\Team; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
7
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Team; |
8
|
|
|
use VideoGamesRecords\CoreBundle\Event\TeamEvent; |
9
|
|
|
use VideoGamesRecords\CoreBundle\Handler\Ranking\AbstractRankingHandler; |
10
|
|
|
use VideoGamesRecords\CoreBundle\Tools\Ranking; |
11
|
|
|
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents; |
12
|
|
|
|
13
|
|
|
class TeamRankingHandler extends AbstractRankingHandler |
14
|
|
|
{ |
15
|
|
|
/*public function majAll() |
16
|
|
|
{ |
17
|
|
|
$query = $this->em->createQuery(" |
18
|
|
|
SELECT p |
19
|
|
|
FROM VideoGamesRecords\CoreBundle\Entity\Team p |
20
|
|
|
WHERE p.nbGame > 0" |
21
|
|
|
); |
22
|
|
|
$teams = $query->getResult(); |
23
|
|
|
foreach ($teams as $team) { |
24
|
|
|
$this->handle($team->getId()); |
25
|
|
|
} |
26
|
|
|
}*/ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws NonUniqueResultException |
30
|
|
|
*/ |
31
|
|
|
public function handle($mixed): void |
32
|
|
|
{ |
33
|
|
|
/** @var Team $team */ |
34
|
|
|
$team = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Team')->find($mixed); |
35
|
|
|
if (null === $team) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$query = $this->em->createQuery(" |
40
|
|
|
SELECT |
41
|
|
|
t.id, |
42
|
|
|
round(AVG(tg.rankPointChart),2) as averageGameRank, |
43
|
|
|
SUM(tg.chartRank0) as chartRank0, |
44
|
|
|
SUM(tg.chartRank1) as chartRank1, |
45
|
|
|
SUM(tg.chartRank2) as chartRank2, |
46
|
|
|
SUM(tg.chartRank3) as chartRank3, |
47
|
|
|
SUM(tg.pointChart) as pointChart, |
48
|
|
|
SUM(tg.pointGame) as pointGame, |
49
|
|
|
COUNT(DISTINCT tg.game) as nbGame |
50
|
|
|
FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg |
51
|
|
|
JOIN tg.team t |
52
|
|
|
WHERE tg.team = :team |
53
|
|
|
GROUP BY t.id"); |
54
|
|
|
|
55
|
|
|
$query->setParameter('team', $team); |
56
|
|
|
$result = $query->getResult(); |
57
|
|
|
if ($result) { |
58
|
|
|
$row = $result[0]; |
59
|
|
|
|
60
|
|
|
$team->setAverageGameRank($row['averageGameRank']); |
61
|
|
|
$team->setChartRank0($row['chartRank0']); |
62
|
|
|
$team->setChartRank1($row['chartRank1']); |
63
|
|
|
$team->setChartRank2($row['chartRank2']); |
64
|
|
|
$team->setChartRank3($row['chartRank3']); |
65
|
|
|
$team->setPointChart($row['pointChart']); |
66
|
|
|
$team->setPointGame($row['pointGame']); |
67
|
|
|
$team->setNbGame($row['nbGame']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// 2 game Ranking |
71
|
|
|
$data = [ |
72
|
|
|
'gameRank0' => 0, |
73
|
|
|
'gameRank1' => 0, |
74
|
|
|
'gameRank2' => 0, |
75
|
|
|
'gameRank3' => 0, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
//----- data rank0 |
79
|
|
|
$query = $this->em->createQuery(" |
80
|
|
|
SELECT |
81
|
|
|
t.id, |
82
|
|
|
COUNT(tg.game) as nb |
83
|
|
|
FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg |
84
|
|
|
JOIN tg.game g |
85
|
|
|
JOIN tg.team t |
86
|
|
|
WHERE g.nbTeam > 1 |
87
|
|
|
AND tg.rankPointChart = 1 |
88
|
|
|
AND tg.nbEqual = 1 |
89
|
|
|
AND tg.team = :team |
90
|
|
|
GROUP BY t.id"); |
91
|
|
|
|
92
|
|
|
$query->setParameter('team', $team); |
93
|
|
|
$row = $query->getOneOrNullResult(); |
94
|
|
|
if ($row) { |
95
|
|
|
$data['gameRank0'] = $row['nb']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
//----- data rank1 to rank3 |
99
|
|
|
$query = $this->em->createQuery(" |
100
|
|
|
SELECT |
101
|
|
|
t.id, |
102
|
|
|
COUNT(tg.game) as nb |
103
|
|
|
FROM VideoGamesRecords\CoreBundle\Entity\TeamGame tg |
104
|
|
|
JOIN tg.team t |
105
|
|
|
WHERE tg.rankPointChart = :rank |
106
|
|
|
AND tg.team = :team |
107
|
|
|
GROUP BY t.id"); |
108
|
|
|
|
109
|
|
|
$query->setParameter('team', $team); |
110
|
|
|
|
111
|
|
|
for ($i = 1; $i <= 3; $i++) { |
112
|
|
|
$query->setParameter('rank', $i); |
113
|
|
|
$row = $query->getOneOrNullResult(); |
114
|
|
|
if ($row) { |
115
|
|
|
$data['gameRank' . $i] = $row['nb']; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$team->setGameRank0($data['gameRank0']); |
120
|
|
|
$team->setGameRank1($data['gameRank1']); |
121
|
|
|
$team->setGameRank2($data['gameRank2']); |
122
|
|
|
$team->setGameRank3($data['gameRank3']); |
123
|
|
|
|
124
|
|
|
// 3 Badge Ranking |
125
|
|
|
$query = $this->em->createQuery(" |
126
|
|
|
SELECT |
127
|
|
|
t.id, |
128
|
|
|
COUNT(tb.badge) as nbMasterBadge, |
129
|
|
|
SUM(b.value) as pointBadge |
130
|
|
|
FROM VideoGamesRecords\CoreBundle\Entity\TeamBadge tb |
131
|
|
|
JOIN tb.badge b |
132
|
|
|
JOIN tb.team t |
133
|
|
|
WHERE b.type = :type |
134
|
|
|
AND tb.team = :team |
135
|
|
|
AND tb.ended_at IS NULL |
136
|
|
|
GROUP BY t.id"); |
137
|
|
|
$query->setParameter('type', 'Master'); |
138
|
|
|
$query->setParameter('team', $team); |
139
|
|
|
|
140
|
|
|
$row = $query->getOneOrNullResult(); |
141
|
|
|
if ($row) { |
142
|
|
|
$team->setNbMasterBadge($row['nbMasterBadge']); |
143
|
|
|
$team->setPointBadge($row['pointBadge']); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->em->persist($team); |
147
|
|
|
$this->em->flush(); |
148
|
|
|
|
149
|
|
|
$event = new TeamEvent($team); |
150
|
|
|
$this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::TEAM_MAJ_COMPLETED); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function majRank(): void |
157
|
|
|
{ |
158
|
|
|
$this->majRankPointChart(); |
159
|
|
|
$this->majRankPointGame(); |
160
|
|
|
$this->majRankMedal(); |
161
|
|
|
$this->majRankCup(); |
162
|
|
|
$this->majRankBadge(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
private function majRankPointChart(): void |
169
|
|
|
{ |
170
|
|
|
$teams = $this->getTeamRepository()->findBy(array(), array('pointChart' => 'DESC')); |
171
|
|
|
Ranking::addObjectRank($teams); |
172
|
|
|
$this->em->flush(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
private function majRankPointGame(): void |
179
|
|
|
{ |
180
|
|
|
$teams = $this->getTeamRepository()->findBy(array(), array('pointGame' => 'DESC')); |
181
|
|
|
Ranking::addObjectRank($teams, 'rankPointGame', array('pointGame')); |
182
|
|
|
$this->em->flush(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
private function majRankMedal(): void |
189
|
|
|
{ |
190
|
|
|
$teams = $this->getTeamRepository()->findBy(array(), array('chartRank0' => 'DESC', 'chartRank1' => 'DESC', 'chartRank2' => 'DESC', 'chartRank3' => 'DESC')); |
191
|
|
|
Ranking::addObjectRank($teams, 'rankMedal', array('chartRank0', 'chartRank1', 'chartRank2', 'chartRank3')); |
192
|
|
|
$this->em->flush(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
|
|
private function majRankCup(): void |
199
|
|
|
{ |
200
|
|
|
$teams = $this->getTeamRepository()->findBy(array(), array('gameRank0' => 'DESC', 'gameRank1' => 'DESC', 'gameRank2' => 'DESC', 'gameRank3' => 'DESC')); |
201
|
|
|
Ranking::addObjectRank($teams, 'rankCup', array('gameRank0', 'gameRank1', 'gameRank2', 'gameRank3')); |
202
|
|
|
$this->em->flush(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return void |
207
|
|
|
*/ |
208
|
|
|
private function majRankBadge(): void |
209
|
|
|
{ |
210
|
|
|
$teams = $this->getTeamRepository()->findBy(array(), array('pointBadge' => 'DESC', 'nbMasterBadge' => 'DESC')); |
211
|
|
|
Ranking::addObjectRank($teams, 'rankBadge', array('pointBadge', 'nbMasterBadge')); |
212
|
|
|
$this->em->flush(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
private function getTeamRepository(): EntityRepository |
216
|
|
|
{ |
217
|
|
|
return $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Team'); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|