src/Services/Api/GameManager.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Services\Api;
  3. use App\Entity\Competition;
  4. use App\Entity\Convocatoria;
  5. use App\Entity\ConvocatoriaPlayer;
  6. use App\Entity\Customer;
  7. use App\Entity\Difficulty;
  8. use App\Entity\Game;
  9. use App\Entity\Player;
  10. use App\Entity\Minutes;
  11. use App\Entity\YellowCards;
  12. use App\Entity\RedCards;
  13. use App\Entity\Goals;
  14. use App\Entity\GoalAssistances;
  15. use App\Entity\TrainingAsist;
  16. use App\Entity\Titulars;
  17. use App\Entity\PlayerJustification;
  18. use App\Entity\GameAlignment;
  19. use App\Entity\GamePlayerStatistics;
  20. use App\Entity\JustificationType;
  21. use App\Entity\KilometersTraveled;
  22. use App\Entity\MediaGame;
  23. use App\Entity\SeasonPlayer;
  24. use App\Entity\ShotsOnGoal;
  25. use App\Entity\Locality;
  26. use App\Entity\TrainingAsistPlayer;
  27. use Symfony\Component\Filesystem\Filesystem;
  28. class GameManager extends BaseApiManager
  29. {
  30.   public function add($data$request)
  31.   {
  32.     $data $this->runGoalVerifier($data);
  33.     $r $this->validGame($data);
  34.     if ($r['code'] != 200)
  35.       return $r;
  36.     /** @var Customer $customer */
  37.     $customer $this->security->getUser();
  38.     $season $customer->getSeasonActive();
  39.     if (empty($season))
  40.       return [
  41.         "messageType" => "danger",
  42.         "message" => $this->translator->trans('msg.season_dont_active'),
  43.         "code" => 403
  44.       ];
  45.     $game = new Game();
  46.     $game->setSeason($season);
  47.     $game $this->setValues($game$data);
  48.     if ($data['rivalTactic'] && $data['rivalPlayersFormation']) {
  49.       $rivalAlignment = new GameAlignment();
  50.       $rivalAlignment->setTactic($data['rivalTactic']);
  51.       $rivalAlignment->setAlignment($data['rivalPlayersFormation']);
  52.       $game->setRivalAlignment($rivalAlignment);
  53.     }
  54.     // locality 
  55.     $locality $this->em->getRepository(Locality::class)->find((int) $data['localityId']);
  56.     if ($locality) {
  57.       $game->setLocality($locality);
  58.     }
  59.     if (!$locality) {
  60.       $defaultLocality $this->em->getRepository(Locality::class)->findOneBy([]);
  61.       if ($defaultLocality) {
  62.         $game->setLocality($defaultLocality);
  63.       }
  64.     }
  65.     // competition
  66.     $competition $this->em->getRepository(Competition::class)->find((int) $data['competitionId']);
  67.     if ($competition) {
  68.       $game->setCompetition($competition);
  69.     }
  70.     if (!$competition) {
  71.       $defaultCompetition $this->em->getRepository(Competition::class)->findOneBy([]);
  72.       if ($defaultCompetition) {
  73.         $game->setCompetition($defaultCompetition);
  74.       }
  75.     }
  76.     $this->em->persist($game);
  77.     $this->em->flush();
  78.     return [
  79.       "messageType" => "success",
  80.       "message" => $this->translator->trans('msg.game_add_success'),
  81.       "newGame" => $data,
  82.       'game' => $game,
  83.       "gameId" => $game->getId(),
  84.       "code" => 200
  85.     ];
  86.   }
  87.   public function runGoalVerifier($data)
  88.   {
  89.     if (!isset($data["goals"]) || is_null($data["goals"])) {
  90.       $data["goals"] = "0";
  91.     }
  92.     if (!isset($data["category"]) || is_null($data["category"])) {
  93.       $data["category"] = 11;
  94.     }
  95.     if (!isset($data["timeMatch"]) || is_null($data["timeMatch"])) {
  96.       $data["timeMatch"] = 90;
  97.     }
  98.     if (!isset($data["rivalGoals"]) || is_null($data["rivalGoals"])) {
  99.       $data["rivalGoals"] = "0";
  100.     }
  101.     if (!isset($data["shotsGoal"]) || is_null($data["shotsGoal"])) {
  102.       $data["shotsGoal"] = "0";
  103.     }
  104.     if (!isset($data["shotsOpponent"]) || is_null($data["shotsOpponent"])) {
  105.       $data["shotsOpponent"] = "0";
  106.     }
  107.     if (!isset($data["cornersFavor"]) || is_null($data["cornersFavor"])) {
  108.       $data["cornersFavor"] = "0";
  109.     }
  110.     if (!isset($data["cornersAgainst"]) || is_null($data["cornersAgainst"])) {
  111.       $data["cornersAgainst"] = "0";
  112.     }
  113.     if (!isset($data["penaltiesFavor"]) || is_null($data["penaltiesFavor"])) {
  114.       $data["penaltiesFavor"] = "0";
  115.     }
  116.     if (!isset($data["penaltiesAgainst"]) || is_null($data["penaltiesAgainst"])) {
  117.       $data["penaltiesAgainst"] = "0";
  118.     }
  119.     if (!isset($data["foulsFavor"]) || is_null($data["foulsFavor"])) {
  120.       $data["foulsFavor"] = "0";
  121.     }
  122.     if (!isset($data["foulsAgainst"]) || is_null($data["foulsAgainst"])) {
  123.       $data["foulsAgainst"] = "0";
  124.     }
  125.     if (!isset($data["day"]) || is_null($data["day"]) || empty($data["day"])) {
  126.       $data["day"] = "1";
  127.     }
  128.     if (!isset($data["offsidesFavor"]) || is_null($data["offsidesFavor"]) || empty($data["offsidesFavor"])) {
  129.       $data["offsidesFavor"] = 0;
  130.     }
  131.     if (!isset($data["offsidesAgainst"]) || is_null($data["offsidesAgainst"]) || empty($data["offsidesAgainst"])) {
  132.       $data["offsidesAgainst"] = 0;
  133.     }
  134.     if (!isset($data["goalKicksFavor"]) || is_null($data["goalKicksFavor"]) || empty($data["goalKicksFavor"])) {
  135.       $data["goalKicksFavor"] = 0;
  136.     }
  137.     if (!isset($data["goalKicksAgainst"]) || is_null($data["goalKicksAgainst"]) || empty($data["goalKicksAgainst"])) {
  138.       $data["goalKicksAgainst"] = 0;
  139.     }
  140.     if (!isset($data["entriesIntoTheBoxFavor"]) || is_null($data["entriesIntoTheBoxFavor"]) || empty($data["entriesIntoTheBoxFavor"])) {
  141.       $data["entriesIntoTheBoxFavor"] = 0;
  142.     }
  143.     if (!isset($data["entriesIntoTheBoxAgainst"]) || is_null($data["entriesIntoTheBoxAgainst"]) || empty($data["entriesIntoTheBoxAgainst"])) {
  144.       $data["entriesIntoTheBoxAgainst"] = 0;
  145.     }
  146.     return $data;
  147.   }
  148.   public function edit($game$data$request)
  149.   {
  150.     $data $this->runGoalVerifier($data);
  151.     $r $this->validGame($data);
  152.     if ($r['code'] != 200)
  153.       return $r;
  154.     /** @var Customer $customer */
  155.     $customer $this->security->getUser();
  156.     $season $customer->getSeasonActive();
  157.     if (empty($season))
  158.       return [
  159.         "messageType" => "danger",
  160.         "message" => $this->translator->trans('msg.season_dont_active'),
  161.         "code" => 403
  162.       ];
  163.     $filter = [
  164.       "season" => $season->getId(),
  165.       "id" => $game
  166.     ];
  167.     /** @var Game $game */
  168.     $game $this->em->getRepository(Game::class)->findOneBy($filter);
  169.     if (!empty($game)) {
  170.       $minutes_game_before = (int) $game->getTimeMatch();
  171.       $game $this->setValues($game$data);
  172.       if ($game->getRivalAlignment()) {
  173.         $rivalAlignment $game->getRivalAlignment();
  174.         if ($data['rivalTactic'] && $data['rivalPlayersFormation']) {
  175.           $rivalAlignment->setTactic($data['rivalTactic']);
  176.           $rivalAlignment->setAlignment($data['rivalPlayersFormation']);
  177.           $game->setRivalAlignment($rivalAlignment);
  178.         } else {
  179.           $game->setRivalAlignment(null);
  180.         }
  181.       } else {
  182.         if ($data['rivalTactic'] && $data['rivalPlayersFormation']) {
  183.           $rivalAlignment = new GameAlignment();
  184.           $rivalAlignment->setTactic($data['rivalTactic']);
  185.           $rivalAlignment->setAlignment($data['rivalPlayersFormation']);
  186.           $game->setRivalAlignment($rivalAlignment);
  187.         }
  188.       }
  189.       /**
  190.        * Verifica minutos jugados por los jugadores segun el partido
  191.        */
  192.       $minutes_game_after = (int) $game->getTimeMatch();
  193.       if ($minutes_game_before != $minutes_game_after) {
  194.         /**
  195.          * Valida y actualiza minutes players
  196.          */
  197.         $criteria = [
  198.           "game" => $game
  199.         ];
  200.         /** @var Minutes[] $obj_minutes */
  201.         /** @var Minutes $minutes */
  202.         $obj_minutes $this->em->getRepository(Minutes::class)->findBy($criteria);
  203.         foreach ($obj_minutes as $_minutes) {
  204.           if ($_minutes) {
  205.             $minutes $_minutes;
  206.             $edit_minutes false;
  207.             $minutes_player = (int) $minutes->getQuantity();
  208.             if ($minutes_game_after $minutes_player) {
  209.               $minutes->setQuantity($minutes_game_after);
  210.               $edit_minutes true;
  211.             } elseif ($minutes_game_after $minutes_player && $minutes_game_before === $minutes_player) {
  212.               $minutes->setQuantity($minutes_game_after);
  213.               $edit_minutes true;
  214.             }
  215.             if ($edit_minutes) {
  216.               $this->em->persist($minutes);
  217.             }
  218.           }
  219.         }
  220.       }
  221.       $difficulty $this->em->getRepository(Difficulty::class)->find((int) $data['pressureRivalId']);
  222.       if ($difficulty) {
  223.         $game->setPressureRival($difficulty);
  224.       }
  225.       $locality $this->em->getRepository(Locality::class)->find((int) $data['localityId']);
  226.       if ($locality) {
  227.         $game->setLocality($locality);
  228.       }
  229.       $competition $this->em->getRepository(Competition::class)->find((int) $data['competitionId']);
  230.       if ($competition) {
  231.         $game->setCompetition($competition);
  232.       }
  233.       $game->setOpponentCurrentPositionLeague((int) $data['opponentCurrentPositionLeague']);
  234.       $this->em->persist($game);
  235.       $this->em->flush();
  236.     }
  237.     return [
  238.       "messageType" => "success",
  239.       "message" => $this->translator->trans('msg.game_edit_success'),
  240.       "code" => 200
  241.     ];
  242.   }
  243.   public function deleteGame(Game $game)
  244.   {
  245.     /** @var Customer $customer */
  246.     $customer $this->security->getUser();
  247.     $season $customer->getSeasonActive();
  248.     if (empty($season))
  249.       return [
  250.         "messageType" => "danger",
  251.         "message" => $this->translator->trans('msg.season_dont_active'),
  252.         "code" => 403
  253.       ];
  254.     $game->setDeletedAt(new \DateTime('now'));
  255.     $this->em->persist($game);
  256.     $this->em->flush();
  257.     return [
  258.       "messageType" => "success",
  259.       "message" => $this->translator->trans('msg.game_delete_success'),
  260.       "code" => 200
  261.     ];
  262.   }
  263.   public function restoreGame(Game $game){
  264.         /** @var Customer $customer */
  265.         $customer $this->security->getUser();
  266.         $season $customer->getSeasonActive();
  267.         if (empty($season))
  268.           return [
  269.             "messageType" => "danger",
  270.             "message" => $this->translator->trans('msg.season_dont_active'),
  271.             "code" => 403
  272.           ];
  273.     
  274.         $game->setDeletedAt(null);
  275.         $this->em->persist($game);
  276.         $this->em->flush();
  277.     
  278.         return [
  279.           "messageType" => "success",
  280.           "message" => $this->translator->trans('msg.game_delete_success'),
  281.           "code" => 200
  282.         ];
  283.   }
  284.   public function deleteGamePermanent(Game $gamestring $urlFiles)
  285.   {
  286.     /** @var Customer $customer */
  287.     $customer $this->security->getUser();
  288.     $season $customer->getSeasonActive();
  289.     if (empty($season))
  290.       return [
  291.         "messageType" => "danger",
  292.         "message" => $this->translator->trans('msg.season_dont_active'),
  293.         "code" => 403
  294.       ];
  295.     $counter 0;
  296.     $criteria = [
  297.       "game" => $game
  298.     ];
  299.     /** @var YellowCards $yellowCards */
  300.     foreach ($this->em->getRepository(YellowCards::class)->findBy($criteria) as $yellowCard) {
  301.       if ($yellowCard) {
  302.         $counter++;
  303.         $this->em->remove($yellowCard);
  304.         $this->em->flush();
  305.       }
  306.     }
  307.     /** @var Minutes $minutes */
  308.     foreach ($this->em->getRepository(Minutes::class)->findBy($criteria) as $minute) {
  309.       if ($minute) {
  310.         $counter++;
  311.         $this->em->remove($minute);
  312.         $this->em->flush();
  313.       }
  314.     }
  315.     /** @var RedCards $redCards */
  316.     foreach ($this->em->getRepository(RedCards::class)->findBy($criteria) as $redCard) {
  317.       if ($redCard) {
  318.         $counter++;
  319.         $this->em->remove($redCard);
  320.         $this->em->flush();
  321.       }
  322.     }
  323.     /** @var Goals $goals */
  324.     foreach ($this->em->getRepository(Goals::class)->findBy($criteria) as $goal) {
  325.       if ($goal) {
  326.         $counter++;
  327.         $this->em->remove($goal);
  328.         $this->em->flush();
  329.       }
  330.     }
  331.     /** @var GoalAssistances $goalAssistances */
  332.     foreach ($this->em->getRepository(GoalAssistances::class)->findBy($criteria) as $goalAssistance) {
  333.       if ($goalAssistance) {
  334.         $counter++;
  335.         $this->em->remove($goalAssistance);
  336.         $this->em->flush();
  337.       }
  338.     }
  339.     /** @var Titulars $titulars */
  340.     foreach ($this->em->getRepository(Titulars::class)->findBy($criteria) as $titulars) {
  341.       if ($titulars) {
  342.         $counter++;
  343.         $this->em->remove($titulars);
  344.         $this->em->flush();
  345.       }
  346.     }
  347.     /** @var KilometersTraveled $titulars */
  348.     foreach ($this->em->getRepository(KilometersTraveled::class)->findBy($criteria) as $kilometers) {
  349.       if ($kilometers) {
  350.         $counter++;
  351.         $this->em->remove($kilometers);
  352.         $this->em->flush();
  353.       }
  354.     }
  355.     /** @var ShotsOnGoal $titulars */
  356.     foreach ($this->em->getRepository(ShotsOnGoal::class)->findBy($criteria) as $shotsOnGoal) {
  357.       if ($shotsOnGoal) {
  358.         $counter++;
  359.         $this->em->remove($shotsOnGoal);
  360.         $this->em->flush();
  361.       }
  362.     }
  363.     /** @var GamePlayerStatistics $titulars */
  364.     foreach ($this->em->getRepository(GamePlayerStatistics::class)->findBy($criteria) as $gamePlayerStatistc) {
  365.       if ($gamePlayerStatistc) {
  366.         $this->em->remove($gamePlayerStatistc);
  367.         $this->em->flush();
  368.       }
  369.     }
  370.     /** @var MediaGame $titulars */
  371.     foreach ($this->em->getRepository(MediaGame::class)->findBy($criteria) as $mediaGame) {
  372.       if ($mediaGame) {
  373.         $media $mediaGame->__toArray($urlFiles);
  374.         $fileSystem = new Filesystem();
  375.         $fileSystem->remove($media);
  376.         $this->em->remove($mediaGame);
  377.         $this->em->flush();
  378.       }
  379.     }
  380.     $this->em->remove($game);
  381.     $this->em->flush();
  382.     return [
  383.       "deleted" => $counter,
  384.       "messageType" => "success",
  385.       "message" => $this->translator->trans('msg.remove_game_permanent_success'),
  386.       "code" => 200
  387.     ];
  388.   }
  389.   public function editLineUp($id$lineup)
  390.   {
  391.     /** @var Customer $customer */
  392.     $customer $this->security->getUser();
  393.     $season $customer->getSeasonActive();
  394.     if (empty($season))
  395.       return [
  396.         "messageType" => "danger",
  397.         "message" => $this->translator->trans('msg.season_dont_active'),
  398.         "code" => 403
  399.       ];
  400.     // foreach ($alignment as $playerId => $info) {
  401.     //   /** @var SeasonPlayer $seasonPlayer */
  402.     //   $seasonPlayer = $this->em->getRepository(SeasonPlayer::class)->find($playerId);
  403.     //   $minutes = $seasonPlayer->getMinutes();
  404.     //   $minutes = json_decode($minutes, true);
  405.     //   $minutes[$id] = (int) $info->minutes;
  406.     //   $minutes = json_encode($minutes);
  407.     //   $seasonPlayer->setMinutes($minutes);
  408.     //   $this->em->persist($seasonPlayer);
  409.     //   $this->em->flush();
  410.     // }
  411.     /** @var Game $game */
  412.     $game $this->em->getRepository(Game::class)->find($id);
  413.     $game->setLineup($lineup);
  414.     $this->em->persist($game);
  415.     $this->em->flush();
  416.     return [
  417.       "messageType" => "success",
  418.       "message" => $this->translator->trans('msg.alignment_success'),
  419.       "code" => 200
  420.     ];
  421.   }
  422.   public function editAlignment($id$alignment)
  423.   {
  424.     /** @var Customer $customer */
  425.     $customer $this->security->getUser();
  426.     $season $customer->getSeasonActive();
  427.     if (empty($season))
  428.       return [
  429.         "messageType" => "danger",
  430.         "message" => $this->translator->trans('msg.season_dont_active'),
  431.         "code" => 403
  432.       ];
  433.     /** @var Game $game */
  434.     $game $this->em->getRepository(Game::class)->find($id);
  435.     /** @var GameAlignment $gameAlignment */
  436.     $gameAlignment $game->getAlignment();
  437.     if (isset($gameAlignment)) {
  438.       $gameAlignment->setTactic($alignment['tactic']);
  439.       $gameAlignment->setAlignment($alignment['alignment']);
  440.     } else {
  441.       $gameAlignment = new GameAlignment();
  442.       $gameAlignment->setTactic($alignment['tactic']);
  443.       $gameAlignment->setAlignment($alignment['alignment']);
  444.       $gameAlignment->setGame($game);
  445.     }
  446.     if(isset($alignment['captain'])){
  447.       $playerId $alignment['captain'];
  448.       /** @var SeasonPlayer $seasonPlayer */
  449.       $seasonPlayer $this->em->getRepository(SeasonPlayer::class)->findOneBy([
  450.         "season" => $game->getSeason(),
  451.         "player" => $playerId,
  452.       ]);
  453.       $gameAlignment->setCaptain($seasonPlayer);
  454.     } else {
  455.       $gameAlignment->setCaptain(null);
  456.     }
  457.     $this->em->persist($gameAlignment);
  458.     $this->em->flush();
  459.     return [
  460.       "messageType" => "success",
  461.       "message" => $this->translator->trans('msg.alignment_success'),
  462.       "code" => 200
  463.     ];
  464.   }
  465.   public function editConvocatoria($game$players)
  466.   {
  467.     /** @var Customer $customer */
  468.     $customer $this->security->getUser();
  469.     $season $customer->getSeasonActive();
  470.     if (empty($season))
  471.       return [
  472.         "messageType" => "danger",
  473.         "message" => $this->translator->trans('msg.season_dont_active'),
  474.         "code" => 403
  475.       ];
  476.     $filter = [
  477.       "season" => $season->getId(),
  478.       "id" => $game
  479.     ];
  480.     /** @var Game $game */
  481.     $convocatoriaPlayerRepository $this->em->getRepository(ConvocatoriaPlayer::class);
  482.     $game $this->em->getRepository(Game::class)->findOneBy($filter);
  483.     if (!empty($game) && !empty($players)) {
  484.       /** @var Convocatoria $convocatoria */
  485.       $convocatoria $game->getConvocatoria();
  486.       if (empty($convocatoria)) {
  487.         $convocatoria = new Convocatoria();
  488.         $convocatoria->setGame($game);
  489.         $this->em->persist($convocatoria);
  490.         $this->em->flush();
  491.         $this->em->clear();
  492.         $convocatoriaId $convocatoria->getId();
  493.       } else {
  494.         $convocatoriaId $convocatoria->getId();
  495.         $convocatoriaPlayers $convocatoriaPlayerRepository->findBy([
  496.           'convocatoria_id' => $convocatoriaId,
  497.         ]);
  498.         $convocatoria $game->getConvocatoria();
  499.         /**
  500.          * SI NO ESTA EL JUGADOR EN LA CONVOCATORIA ENTRATE, SE ELIMINA
  501.          */
  502.         foreach ($convocatoriaPlayers as $player) {
  503.           if (!in_array($player->getPlayerId(), array_column($players'id'))) {
  504.             $this->em->remove($player);
  505.           }
  506.         }
  507.       }
  508.       $playerRepository $this->em->getRepository(Player::class);
  509.       foreach ($players as $p) {
  510.         /** @var Player $player */
  511.         $player $playerRepository->find($p["id"]);
  512.         if ($player) {
  513.           /** @var ConvocatoriaPlayer $convocatoriaPlayer */
  514.           $convocatoriaPlayer $convocatoriaPlayerRepository->findOneBy([
  515.             'player_id' => $p['id'],
  516.             'convocatoria_id' => $convocatoriaId,
  517.           ]);
  518.           if (empty($convocatoriaPlayer)) {
  519.             $convocatoriaPlayer = new ConvocatoriaPlayer;
  520.             $convocatoriaPlayer->setPlayerId($p['id']);
  521.             $convocatoriaPlayer->setConvocatoriaId($convocatoriaId);
  522.           }
  523.           $isOn true;
  524.           /** @var JustificationType $justificationType */
  525.           $justificationType $this->em->getRepository(JustificationType::class)->find($p['justification_type']);
  526.           if (isset($p['is_active'])) {
  527.             //INPUT DE FORMDATA
  528.             $isOn $p['is_active'];
  529.             // $justificationType = null;
  530.             if (intval($isOn) == && intval($p['justification_type']) > 0) {
  531.               // $justificationType = $p['justification_type'];
  532.             }
  533.           }
  534.         }
  535.         $convocatoriaPlayer->setIsActive($isOn);
  536.         $convocatoriaPlayer->setJustificationType($justificationType);
  537.         $this->em->persist($convocatoriaPlayer);
  538.       }
  539.     }
  540.     $this->em->flush();
  541.     return [
  542.       "messageType" => "success",
  543.       "message" => $this->translator->trans('msg.convocatoria_success'),
  544.       "code" => 200
  545.     ];
  546.   }
  547.   public function editTraining($date$players)
  548.   {
  549.     /** @var Customer $customer */
  550.     $customer $this->security->getUser();
  551.     $season $customer->getSeasonActive();
  552.     if (empty($season))
  553.       return [
  554.         "messageType" => "danger",
  555.         "message" => $this->translator->trans('msg.season_dont_active'),
  556.         "code" => 403
  557.       ];
  558.     $date = \DateTime::createFromFormat("d-m-Y"$date);
  559.     $filter = [
  560.       "season" => $season->getId(),
  561.       "date" => $date
  562.     ];
  563.     
  564.     /** Players with Delay mark */
  565.     $delayedPlayers = [];
  566.     /** @var Game $game */
  567.     $trainingAsist $this->em->getRepository(TrainingAsist::class)->findOneBy($filter);
  568.     if (empty($trainingAsist)) {
  569.       $trainingAsist = new TrainingAsist();
  570.       $trainingAsist->setSeason($season)
  571.         ->setDate($date);
  572.     }
  573.     if (!empty($players)) {
  574.       foreach ($players as $p) {
  575.         /** @var Player $player */
  576.         $player $this->em->getRepository(Player::class)->find($p["id"]);
  577.         if (!empty($player)) {
  578.           if ($p["in"]) {
  579.             if (isset($p['delay'])) {
  580.               $delayedPlayers[] = [
  581.                 "player" => $player,
  582.                 "delay" => $p['delay']
  583.               ];
  584.             }
  585.             
  586.             if (!$trainingAsist->getPlayers()->contains($player))
  587.               $trainingAsist->addPlayer($player);
  588.           } else {
  589.             if ($trainingAsist->getPlayers()->contains($player))
  590.               $trainingAsist->removePlayer($player);
  591.           }
  592.           if (isset($p['justification'])) {
  593.             $subfilter = [
  594.               "date" => $date,
  595.               "player" => $player->getId()
  596.             ];
  597.             /** @var PlayerJustification $playerJustification */
  598.             $playerJustification $this->em->getRepository(PlayerJustification::class)->findOneBy($subfilter);
  599.             /** @var JustificationType $justificationType */
  600.             $justificationType $this->em->getRepository(JustificationType::class)->find($p['justification']['id']);
  601.             if (empty($playerJustification)) {
  602.               $playerJustification = new PlayerJustification();
  603.               $playerJustification->setJustificationType($justificationType);
  604.               $playerJustification->setPlayer($player);
  605.               $playerJustification->setIsActive(true);
  606.               $playerJustification->setDate($date);
  607.               $playerJustification->setSeason($season);
  608.             } else {
  609.               $playerJustification->setJustificationType($justificationType);
  610.             }
  611.             $this->em->persist($playerJustification);
  612.             $this->em->flush();
  613.           }
  614.         }
  615.       }
  616.       $this->em->persist($trainingAsist);
  617.       $this->em->flush();
  618.       if (!empty($delayedPlayers)) {
  619.         foreach ($delayedPlayers as $delayedPlayer) {
  620.           /** @var Player $player */
  621.           $player $delayedPlayer['player'];
  622.           $delay $delayedPlayer['delay'];
  623.   
  624.           // Check if the player is already linked to the training session
  625.           /** @var TrainingAsistPlayer $trainingAsistPlayer */
  626.           $trainingAsistPlayer $this->em->getRepository(TrainingAsistPlayer::class)->findOneBy([
  627.               'trainingAsist' => $trainingAsist,
  628.               'player' => $player,
  629.           ]);
  630.   
  631.           // If the player isn't found, create a new association entry
  632.           if (empty($trainingAsistPlayer)) {
  633.               continue;
  634.           }
  635.         
  636.           // Set the delay status
  637.           $trainingAsistPlayer->setDelay($delay);
  638.   
  639.           // Persist the updated or new entity
  640.           $this->em->persist($trainingAsistPlayer);
  641.         }
  642.         // Finally, flush all changes
  643.         $this->em->flush();
  644.       }
  645.     }
  646.     return [
  647.       "messageType" => "success",
  648.       "message" => $this->translator->trans('msg.training_edit_success'),
  649.       "code" => 200
  650.     ];
  651.   }
  652.   protected function validGame($data)
  653.   {
  654.     $code 200;
  655.     $validates = [];
  656.     if (empty($data["rival"])) {
  657.       $code 404;
  658.       $validates["rival"] = [
  659.         "type" => "error",
  660.         "msg" => "El nombre de rival es requerido",
  661.         "icon" => "glyphicon-warning-sign",
  662.       ];
  663.     }
  664.     if (empty($data["localityId"])) {
  665.       $code 404;
  666.       $validates["localityId"] = [
  667.         "type" => "error",
  668.         "msg" => "La localia es requerida",
  669.         "icon" => "glyphicon-warning-sign",
  670.       ];
  671.     }
  672.     if (empty($data["day"])) {
  673.       $code 404;
  674.       $validates["day"] = [
  675.         "type" => "error",
  676.         "msg" => "La jornada es requerida",
  677.         "icon" => "glyphicon-warning-sign",
  678.       ];
  679.     }
  680.     if (empty($data["date"])) {
  681.       $code 404;
  682.       $validates["date"] = [
  683.         "type" => "error",
  684.         "msg" => "La fecha es requerida",
  685.         "icon" => "glyphicon-warning-sign",
  686.       ];
  687.     }
  688.     if (!isset($data["goals"]) || is_null($data["goals"])) {
  689.       $code 404;
  690.       $validates["goals"] = [
  691.         "type" => "error",
  692.         "msg" => "Los goles son requeridos",
  693.         "icon" => "glyphicon-warning-sign",
  694.       ];
  695.     }
  696.     if (!isset($data["rivalGoals"]) || is_null($data["rivalGoals"])) {
  697.       $code 404;
  698.       $validates["rivalGoals"] = [
  699.         "type" => "error",
  700.         "msg" => "Los goles son requeridos",
  701.         "icon" => "glyphicon-warning-sign",
  702.       ];
  703.     }
  704.     return [
  705.       "code" => $code,
  706.       "messageType" => "warning",
  707.       "message" => $this->translator->trans('msg.filed_requiered'),
  708.       "validates" => $validates
  709.     ];
  710.   }
  711.   public function reconcileTitulars(Game $game)
  712.   {
  713.     /** @var GameAlignment $gameAlignment */
  714.     $gameAlignment $game->getAlignment();
  715.     if ($gameAlignment) {
  716.       $alignments json_decode((string) $gameAlignment->getAlignment());
  717.       if (JSON_ERROR_NONE !== json_last_error()) {
  718.         return false;
  719.       }
  720.       /** [[{id:0},{id:0}],[{id:0}]] */
  721.       foreach ($alignments as $alignment) {
  722.         /** [{id:0},{id:0}] */
  723.         foreach ($alignment as $player) {
  724.           if (isset($player['id']) && $player['id'] > 0) {
  725.             /** @var SeasonPlayer $seasonPlayer */
  726.             $seasonPlayer $this->em->getRepository(SeasonPlayer::class)->find($player['id']);
  727.             $titular $this->em->getRepository(Titulars::class)->findOneBy([
  728.               "seasonplayer" => $seasonPlayer,
  729.               "game" => $game
  730.             ]);
  731.             if ($titular) {
  732.               $titular->setIsTitular(true);
  733.               $this->em->persist($titular);
  734.             }
  735.           }
  736.         }
  737.       }
  738.       $this->em->flush();
  739.     }
  740.     return true;
  741.   }
  742.   public function addGameCaptain($id$playerId)
  743.   {
  744.     /** @var Customer $customer */
  745.     $customer $this->security->getUser();
  746.     /** @var Game $game */
  747.     $game $this->em->getRepository(Game::class)->find($id);
  748.     /** @var GameAlignment $gameAlignment */
  749.     $gameAlignment $game->getAlignment();
  750.     
  751.     /** @var SeasonPlayer $seasonPlayer */
  752.     $seasonPlayer $this->em->getRepository(SeasonPlayer::class)->findOneBy([
  753.       "season" => $game->getSeason(),
  754.       "player" => $playerId,
  755.     ]);
  756.     if (empty($seasonPlayer))
  757.       return [
  758.         "messageType" => "warning",
  759.         "message" => $this->translator->trans('msg.player_dont_exist'),
  760.         "code" => 404
  761.       ];
  762.     if (isset($gameAlignment)) {
  763.       $gameAlignment->setCaptain($seasonPlayer);
  764.     } else {
  765.       $gameAlignment = new GameAlignment();
  766.       $gameAlignment->setTactic("");
  767.       $gameAlignment->setAlignment("");
  768.       $gameAlignment->setGame($game);
  769.       $gameAlignment->setCaptain($seasonPlayer);
  770.     }
  771.     $this->em->persist($gameAlignment);
  772.     $this->em->flush();
  773.     return [
  774.       "messageType" => "success",
  775.       "message" => $this->translator->trans('msg.alignment_success'),
  776.       "code" => 200
  777.     ];
  778.   }
  779.   public function getGameCaptain($id)
  780.   {
  781.     /** @var Customer $customer */
  782.     $customer $this->security->getUser();
  783.     $season $customer->getSeasonActive();
  784.     if (empty($season))
  785.       return [
  786.         "messageType" => "danger",
  787.         "message" => $this->translator->trans('msg.season_dont_active'),
  788.         "code" => 403
  789.       ];
  790.     /** @var Game $game */
  791.     $game $this->em->getRepository(Game::class)->find($id);
  792.     /** @var GameAlignment $gameAlignment */
  793.     $gameAlignment $game->getAlignment();
  794.     return [
  795.       "messageType" => "success",
  796.       "data" => [
  797.         'captainPlayerId' => $gameAlignment->getCaptain()->getPlayer()->getId(),
  798.       ],
  799.       "message" => $this->translator->trans('msg.alignment_success'),
  800.       "code" => 200
  801.     ];
  802.   }
  803. }