|
1
|
|
|
<?php |
|
2
|
|
|
namespace VideoGamesRecords\CoreBundle\EventSubscriber\PlayerChart; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
5
|
|
|
use Doctrine\ORM\Exception\ORMException; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
7
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Chart; |
|
8
|
|
|
use VideoGamesRecords\CoreBundle\Entity\LostPosition; |
|
9
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Player; |
|
10
|
|
|
use VideoGamesRecords\CoreBundle\Event\PlayerChartEvent; |
|
11
|
|
|
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents; |
|
12
|
|
|
|
|
13
|
|
|
final class InsertLostPositionSubscriber implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private EntityManagerInterface $em; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(EntityManagerInterface $em) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->em = $em; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public static function getSubscribedEvents(): array |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
VideoGamesRecordsCoreEvents::PLAYER_CHART_MAJ_COMPLETED => 'insertLostPosition', |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param PlayerChartEvent $event |
|
31
|
|
|
* @throws ORMException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function insertLostPosition(PlayerChartEvent $event): void |
|
34
|
|
|
{ |
|
35
|
|
|
$playerChart = $event->getPlayerChart(); |
|
36
|
|
|
$oldRank = $event->getOldRank(); |
|
37
|
|
|
$oldNbEqual = $event->getOldNbEqual(); |
|
38
|
|
|
$newRank = $playerChart->getRank(); |
|
39
|
|
|
$newNbEqual = $playerChart->getNbEqual(); |
|
40
|
|
|
|
|
41
|
|
|
if ((($oldRank >= 1) && ($oldRank <= 3) && ($newRank > $oldRank)) || |
|
42
|
|
|
(($oldRank === 1) && ($oldNbEqual === 1) && ($newRank === 1) && ($newNbEqual > 1)) |
|
43
|
|
|
) { |
|
44
|
|
|
$lostPosition = new LostPosition(); |
|
45
|
|
|
$lostPosition->setNewRank($newRank); |
|
|
|
|
|
|
46
|
|
|
$lostPosition->setOldRank(($oldNbEqual == 1 && $oldRank == 1) ? 0 : $oldRank); //----- zero for losing platinum medal |
|
|
|
|
|
|
47
|
|
|
$lostPosition->setPlayer($this->em->getReference(Player::class, $playerChart->getPlayer()->getId())); |
|
|
|
|
|
|
48
|
|
|
$lostPosition->setChart($this->em->getReference(Chart::class, $playerChart->getChart()->getId())); |
|
|
|
|
|
|
49
|
|
|
$this->em->persist($lostPosition); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|