src/Services/Api/GameManager.php line 144

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