src/Services/Api/GameManager.php line 458

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