Passed
Push — develop ( c67e1d...954e7b )
by BENARD
04:26
created

setPlayerChartRankingQuert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Ranking\Command\Player;
4
5
6
use VideoGamesRecords\CoreBundle\Entity\Chart;
7
use VideoGamesRecords\CoreBundle\Entity\PlayerChart;
8
use VideoGamesRecords\CoreBundle\Event\PlayerChartEvent;
9
use VideoGamesRecords\CoreBundle\Ranking\Provider\Player\PlayerChartRankingProvider;
10
use VideoGamesRecords\CoreBundle\Ranking\Command\AbstractRankingHandler;
11
use VideoGamesRecords\CoreBundle\Tools\Ranking;
12
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
13
14
class PlayerChartRankingHandler extends AbstractRankingHandler
15
{
16
    private PlayerChartRankingProvider $playerChartRankingProvider;
17
    private array $players = [];
18
    private array $games = [];
19
    private array $groups = [];
20
21
22
    /**
23
     * @param PlayerChartRankingProvider $playerChartRankingProvider
24
     * @return void
25
     */
26
    public function setPlayerChartRankingProvider(PlayerChartRankingProvider $playerChartRankingProvider): void
27
    {
28
        $this->playerChartRankingProvider = $playerChartRankingProvider;
29
    }
30
31
    public function handle($mixed): void
32
    {
33
        /** @var Chart $chart */
34
        $chart = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Chart')->find($mixed);
35
        if (null === $chart) {
36
            return;
37
        }
38
39
        $this->groups[$chart->getGroup()->getId()] = $chart->getGroup();
40
        $this->games[$chart->getGroup()->getGame()->getId()] = $chart->getGroup()->getGame();
41
42
        $ranking     = $this->playerChartRankingProvider->getRanking($chart);
43
        $pointsChart = Ranking::chartPointProvider(count($ranking));
44
45
        $topScoreLibValue = '';
46
        $previousLibValue = '';
47
        $rank             = 1;
48
        $nbEqual          = 1;
49
        $playerChartEqual = [];
50
51
        $result = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\PlayerChart')->getPlatforms($chart);
52
        $platforms = [];
53
        foreach ($result as $row) {
54
            $platforms[$row['id']] = [
55
                'count' => $row['nb'],
56
                'points' => Ranking::platformPointProvider($row['nb']),
57
                'previousLibValue' => '',
58
                'rank' => 0,
59
                'nbEqual' => 1,
60
                'playerChartEqual' => [],
61
            ];
62
        }
63
64
        foreach ($ranking as $k => $item) {
65
            $libValue = '';
66
            /** @var PlayerChart $playerChart */
67
            $playerChart = $item[0];
68
69
            $this->players[$playerChart->getPlayer()->getId()] = $playerChart->getPlayer();
70
71
            // Lost position ?
72
            $oldRank = $playerChart->getRank();
73
            $oldNbEqual = $playerChart->getNbEqual();
74
            $playerChart->setTopScore(false);
75
76
            foreach ($chart->getLibs() as $lib) {
77
                $libValue .= $item['value_' . $lib->getIdLibChart()] . '/';
78
            }
79
            if ($k === 0) {
80
                // Premier élément => topScore
81
                $playerChart->setTopScore(true);
82
                $topScoreLibValue = $libValue;
83
            } else {
84
                if ($libValue === $topScoreLibValue) {
85
                    $playerChart->setTopScore(true);
86
                }
87
                if ($previousLibValue === $libValue) {
88
                    ++$nbEqual;
89
                } else {
90
                    $rank += $nbEqual;
91
                    $nbEqual = 1;
92
                    $playerChartEqual = [];
93
                }
94
95
            }
96
            // Platform point
97
            if ($playerChart->getPlatform() != null) {
98
                $idPlatForm = $playerChart->getPlatform()->getId();
99
                if ($platforms[$idPlatForm]['previousLibValue'] === $libValue) {
100
                    ++$platforms[$idPlatForm]['nbEqual'];
101
                } else {
102
                    $platforms[$idPlatForm]['rank'] += $platforms[$idPlatForm]['nbEqual'];
103
                    $platforms[$idPlatForm]['nbEqual'] = 1;
104
                    $platforms[$idPlatForm]['playerChartEqual'] = [];
105
                }
106
                $platforms[$idPlatForm]['playerChartEqual'][] = $playerChart;
107
            }
108
109
            $playerChartEqual[] = $playerChart;
110
111
            $playerChart
112
                ->setNbEqual($nbEqual)
113
                ->setRank($rank)
114
                ->setPointChart((int) (
115
                    array_sum(
116
                        array_slice(array_values($pointsChart), $playerChart->getRank() - 1, $playerChart->getNbEqual())
117
                    ) / $playerChart->getNbEqual()
118
                ));
119
120
            if ($nbEqual > 1) {
121
                // Pour les égalités déjà passées on met à jour le nbEqual et l'attribution des points
122
                foreach ($playerChartEqual as $playerChartToModify) {
123
                    $playerChartToModify
124
                        ->setNbEqual($nbEqual)
125
                        ->setPointChart($playerChart->getPointChart());
126
                }
127
            }
128
129
            if ($playerChart->getPlatform() != null) {
130
                $idPlatForm = $playerChart->getPlatform()->getId();
131
                $playerChart->setPointPlatform((int) (
132
                    array_sum(
133
                        array_slice(array_values($platforms[$idPlatForm]['points']), $platforms[$idPlatForm]['rank'] - 1, $platforms[$idPlatForm]['nbEqual'])
134
                    ) / $platforms[$idPlatForm]['nbEqual']
135
                ));
136
                if ($platforms[$idPlatForm]['nbEqual'] > 1) {
137
                    // Pour les égalités déjà passées on met à jour le nbEqual et l'attribution des points
138
                    foreach ($platforms[$idPlatForm]['playerChartEqual'] as $playerChartToModify) {
139
                        $playerChartToModify
140
                            ->setPointPlatform($playerChart->getPointPlatform());
141
                    }
142
                }
143
            } else {
144
                $playerChart->setPointPlatform(0);
145
            }
146
147
148
            $event = new PlayerChartEvent($playerChart, $oldRank, $oldNbEqual);
149
            $this->eventDispatcher->dispatch($event, VideoGamesRecordsCoreEvents::PLAYER_CHART_MAJ_COMPLETED);
150
151
            $previousLibValue = $libValue;
152
153
            // Platform point
154
            if ($playerChart->getPlatform() != null) {
155
                $platforms[$playerChart->getPlatform()->getId()]['previousLibValue'] = $libValue;
156
            }
157
        }
158
        $this->em->flush();
159
    }
160
161
    public function getPlayers(): array
162
    {
163
        return $this->players;
164
    }
165
166
    public function getGames(): array
167
    {
168
        return $this->games;
169
    }
170
171
    public function getGroups(): array
172
    {
173
        return $this->groups;
174
    }
175
}
176