<?php
namespace App\EventSubscriber\Tournament\Contest;
use App\Event\Tournament\Contest\ContestCancelledEvent;
use App\Helper\ContestHelper;
use App\Helper\PlayerHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContestCancelledEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $entityManager, private PlayerHelper $playerHelper
) {
}
public static function getSubscribedEvents(): array
{
return [
ContestCancelledEvent::class => 'onContestCancelled',
];
}
public function onContestCancelled(ContestCancelledEvent $contestCancelledEvent): void
{
$contest = $contestCancelledEvent->getContest();
// loops through all opponents of this contest and alter the loser's activity and set the only one who scored as winner
foreach ($contest->getContestPlayers() as $contestPlayer) {
$player = $contestPlayer->getPlayer();
if (!ContestHelper::hasScored($contest, $player)) {
$this->playerHelper->updateActivity($player, ContestCancelledEvent::class);
continue;
}
// the only one who scored automatically wins
$contest->setWinner($player);
$player->setXp(
$player->getXp() + ContestHelper::getXpToEarn($contest, $player)
);
$this->entityManager->flush();
}
}
}