1
|
|
|
<?php |
2
|
|
|
namespace VideoGamesRecords\DwhBundle\DataProvider\Strategy\Top; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
6
|
|
|
use Doctrine\ORM\NoResultException; |
7
|
|
|
use VideoGamesRecords\CoreBundle\Repository\PlayerRepository as CorePlayerRepository; |
8
|
|
|
use VideoGamesRecords\CoreBundle\Tools\Ranking as ToolsRanking; |
9
|
|
|
use VideoGamesRecords\DwhBundle\Contracts\Strategy\TopStrategyInterface; |
10
|
|
|
use VideoGamesRecords\DwhBundle\Repository\PlayerRepository as DwhPlayerRepository; |
11
|
|
|
|
12
|
|
|
class TopPlayerStrategy extends AbstractTopProvider implements TopStrategyInterface |
13
|
|
|
{ |
14
|
|
|
public function supports(string $name): bool |
15
|
|
|
{ |
16
|
|
|
return $name == self::TYPE_PLAYER; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param DateTime $date1Begin |
21
|
|
|
* @param DateTime $date1End |
22
|
|
|
* @param DateTime $date2Begin |
23
|
|
|
* @param DateTime $date2End |
24
|
|
|
* @param integer $limit |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getTop(DateTime $date1Begin, DateTime $date1End, DateTime $date2Begin, DateTime $date2End, int $limit = 20): array |
28
|
|
|
{ |
29
|
|
|
/** @var DwhPlayerRepository $dwhPlayerRepository */ |
30
|
|
|
$dwhPlayerRepository = $this->dwhEntityManager->getRepository('VideoGamesRecords\DwhBundle\Entity\Player'); |
31
|
|
|
|
32
|
|
|
/** @var CorePlayerRepository $dwhPlayerRepository */ |
33
|
|
|
$corePlayerRepository = $this->defaultEntityManager->getRepository('VideoGamesRecords\CoreBundle\Entity\Player'); |
34
|
|
|
|
35
|
|
|
$playerList1 = $dwhPlayerRepository->getTop( |
36
|
|
|
$date1Begin, |
37
|
|
|
$date1End, |
38
|
|
|
$limit |
39
|
|
|
); |
40
|
|
|
$playerList2 = $dwhPlayerRepository->getTop( |
41
|
|
|
$date2Begin, |
42
|
|
|
$date2End, |
43
|
|
|
$limit |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
// Get old rank |
47
|
|
|
$oldRank = array(); |
48
|
|
|
foreach ($playerList2 as $key => $row) { |
49
|
|
|
$oldRank[$row['id']] = $key + 1; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$nbPostFromList = 0; |
53
|
|
|
for ($i = 0, $nb = count($playerList1) - 1; $i <= $nb; ++$i) { |
|
|
|
|
54
|
|
|
$idPlayer = $playerList1[$i]['id']; |
55
|
|
|
if (isset($oldRank[$idPlayer])) { |
56
|
|
|
$playerList1[$i]['oldRank'] = $oldRank[$idPlayer]; |
57
|
|
|
} else { |
58
|
|
|
$playerList1[$i]['oldRank'] = null; |
59
|
|
|
} |
60
|
|
|
$player = $corePlayerRepository->find($idPlayer); |
61
|
|
|
$playerList1[$i]['player'] = $player; |
62
|
|
|
$nbPostFromList += $playerList1[$i]['nb']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$nbPlayer = $dwhPlayerRepository->getTotalNbPlayer($date1Begin, $date1End); |
67
|
|
|
} catch (NoResultException | NonUniqueResultException $e) { |
68
|
|
|
// OK |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$nbTotalPost = $dwhPlayerRepository->getTotalNbPostDay($date1Begin, $date1End); |
73
|
|
|
} catch (NoResultException | NonUniqueResultException $e) { |
74
|
|
|
// OK |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$playerList = ToolsRanking::addRank( |
78
|
|
|
$playerList1, |
79
|
|
|
'rank', |
80
|
|
|
['nb'], |
81
|
|
|
true |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return array( |
85
|
|
|
'list' => $playerList, |
86
|
|
|
'nbPostFromList' => $nbPostFromList, |
87
|
|
|
'nb' => $nbPlayer, |
88
|
|
|
'nbTotalPost' => $nbTotalPost, |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|