src/Controller/BattleController.php line 75

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Vote;
  4. use App\Repository\BattleRepository;
  5. use App\Repository\ClientRepository;
  6. use App\Repository\VoteRepository;
  7. use App\Service\BattleWinnerService;
  8. use DateTime;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Security;
  14. class BattleController extends AbstractController
  15. {
  16.     private BattleRepository $battleRepository;
  17.     private ClientRepository $clientRepository;
  18.     private VoteRepository $voteRepository;
  19.     private UserDataController $userDataController;
  20.     private BattleWinnerService $battleWinnerService;
  21.     public function __construct(
  22.         BattleRepository $battleRepository,
  23.         ClientRepository $clientRepository,
  24.         VoteRepository $voteRepository,
  25.         UserDataController $userDataController,
  26.         BattleWinnerService $battleWinnerService
  27.     )
  28.     {
  29.         $this->battleRepository $battleRepository;
  30.         $this->clientRepository $clientRepository;
  31.         $this->voteRepository $voteRepository;
  32.         $this->userDataController $userDataController;
  33.         $this->battleWinnerService $battleWinnerService;
  34.     }
  35.     #[Route(path:  [
  36.         'cs' => '/battly',
  37.         'en' => '/battles'
  38.     ], name'battles')]
  39.     public function frontendBattlesAction(Security $security): Response
  40.     {
  41.         $user $this->getUser();
  42.         $allBattles $this->battleRepository->findAll();
  43.         $this->checkForWinners($allBattles);
  44.         if ($user==null){
  45.             return $this->render('pages/battles.html.twig',[
  46.                 'allBattles' => $allBattles
  47.             ]);
  48.         }else{
  49.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  50.             if($client!=null && $client->isIsActive()){
  51.                 return $this->render('pages/battles.html.twig',[
  52.                     'userData'=>$this->userDataController->getUserData(),
  53.                     'allBattles' => $allBattles
  54.                 ]);
  55.             }else{
  56.                 return $this->render('pages/non-active-content.html.twig',[
  57.                     'email'=>$client->getEmail(),
  58.                     'user'=>$client
  59.                 ]);
  60.             }
  61.         }
  62.     }
  63.     #[Route(path:  [
  64.         'en' => '/battle/{id}',
  65.         'cs' => '/bitva/{id}'
  66.     ], name'battleDetail')]
  67.     public function frontendBattleDetailAction($id): Response
  68.     {
  69.         $user $this->getUser();
  70.         $battle $this->battleRepository->findOneBy(['id'=>$id]);
  71.         if ($user==null){
  72.             return $this->render('pages/battle-detail.html.twig',[
  73.                 'battle'=>$battle
  74.             ]);
  75.         }else{
  76.             $client $this->clientRepository->findOneByEmail($user->getUserIdentifier());
  77.             if($client!=null && $client->isIsActive()){
  78.                 return $this->render('pages/battle-detail.html.twig',[
  79.                     'userData'=>$this->userDataController->getUserData(),
  80.                     'battle'=>$battle
  81.                 ]);
  82.             }else{
  83.                 return $this->render('pages/non-active-content.html.twig',[
  84.                     'email'=>$client->getEmail(),
  85.                     'userData'=>$this->userDataController->getUserData(),
  86.                     'user'=>$client
  87.                 ]);
  88.             }
  89.         }
  90.     }
  91.     #[Route(path:  '/vote-challenger/{id}'name'voteChallenger')]
  92.     public function frontendVoteChallengerAction($id): JsonResponse
  93.     {
  94.         $battle $this->battleRepository->findOneBy(['id'=>$id]);
  95.         $votingClient $this->clientRepository->findOneBy(["email"=>$this->getUser()->getUserIdentifier()]);
  96.         $voterVote $this->voteRepository->findOneBy(["voter"=>$votingClient]);
  97.         if($voterVote!=null){
  98.             if($voterVote->getVoteValue()===1){
  99.                 $this->voteRepository->remove($voterVote,true);
  100.                 return new JsonResponse("Removed",200);
  101.             }else{
  102.                 $voterVote->setVoteValue(1);
  103.                 $savedVote $this->voteRepository->save($voterVote,true);
  104.                 if($savedVote->getId()!=0){
  105.                     return new JsonResponse($savedVote->getId(),201);
  106.                 }
  107.             }
  108.         }else{
  109.             $vote = new Vote();
  110.             $vote
  111.                 ->setBattle($battle)
  112.                 ->setVoter($votingClient)
  113.                 ->setVoteValue(1)
  114.             ;
  115.             $savedVote $this->voteRepository->save($vote,true);
  116.             if($savedVote->getId()!=0){
  117.                 return new JsonResponse($savedVote->getId(),201);
  118.             }
  119.         }
  120.         return new JsonResponse("Err",400);
  121.     }
  122.     #[Route(path:  '/vote-oponnent/{id}'name'voteOponnent')]
  123.     public function frontendBattleVoteOponnentAction($id): JsonResponse
  124.     {
  125.         $battle $this->battleRepository->findOneBy(['id'=>$id]);
  126.         $votingClient $this->clientRepository->findOneBy(["email"=>$this->getUser()->getUserIdentifier()]);
  127.         $voterVote $this->voteRepository->findOneBy(["voter"=>$votingClient]);
  128.         if($voterVote!=null){
  129.             if($voterVote->getVoteValue()===2){
  130.                 $this->voteRepository->remove($voterVote,true);
  131.                 return new JsonResponse("Removed",200);
  132.             }else{
  133.                 $voterVote->setVoteValue(2);
  134.                 $savedVote $this->voteRepository->save($voterVote,true);
  135.                 if($savedVote->getId()!=0){
  136.                     return new JsonResponse($savedVote->getId(),201);
  137.                 }
  138.             }
  139.         }else{
  140.             $vote = new Vote();
  141.             $vote
  142.                 ->setBattle($battle)
  143.                 ->setVoter($votingClient)
  144.                 ->setVoteValue(2)
  145.             ;
  146.             $savedVote $this->voteRepository->save($vote,true);
  147.             if($savedVote->getId()!=0){
  148.                 return new JsonResponse($savedVote->getId(),201);
  149.             }
  150.         }
  151.         return new JsonResponse("Err",400);
  152.     }
  153.     #[Route("/get-battles/"name"getBattlesApi")]
  154.     public  function getBattlesUrlAction(): JsonResponse
  155.     {
  156.         $battles=$this->battleRepository->findAll();
  157.         $this->checkForWinners($battles);
  158.         $data = [];
  159.         foreach ($battles as $battle){
  160.             $data[] = [
  161.                 'id' => $battle->getId(),
  162.                 'battleFileName' => $battle->getBattleFileName(),
  163.                 'challenger' => $battle->getChallenger()->__toArray(),
  164.                 'oponnent' => $battle->getOponnent()->__toArray(),
  165.                 'allVotes'=>$battle->getAllVotes(),
  166.             ];
  167.         }
  168.         $jsonResponse = [
  169.             "battles" => $data
  170.         ];
  171.         return new JsonResponse($jsonResponse,200);
  172.     }
  173.     public function checkForWinners(array $battles):void
  174.     {
  175.         foreach ($battles as $battle)
  176.         {
  177.             if($battle->isFinished() && $battle->getWinner()==null){
  178.                 $potentialWinner $this->battleWinnerService->getWinner($battle);
  179.                 if($potentialWinner!=null){
  180.                     $battle
  181.                         ->setWinner($potentialWinner)
  182.                         ->setUpdatedAt(new DateTime('now'))
  183.                     ;
  184.                     $savedBattle $this->battleRepository->save($battle,true);
  185.                     dump("potentialWinnerNotNull: ".$savedBattle->getId());
  186.                 }else{
  187.                     dump("potentialWinnerNull");
  188.                 }
  189.             }
  190.         }
  191.     }
  192. }