/**
  * @param Group $group
  * @param PlanningOptions $options
  * @return array
  */
 private function populateGroup(Group $group, PlanningOptions $options)
 {
     $matches = array();
     $teams = $group->getTeams();
     $check = array();
     /* @var $teamA Team */
     foreach ($teams as $teamA) {
         $idx = 0;
         /* @var $teamB Team */
         foreach ($teams as $teamB) {
             if ($teamA->getId() != $teamB->getId() && !isset($check[$teamB->getId()])) {
                 $switch = $idx % 2 == 0 || $options->isDoublematch();
                 $match = new MatchPlan();
                 $match->setTeamA($switch ? $teamA : $teamB);
                 $match->setTeamB($switch ? $teamB : $teamA);
                 $match->setFixed(false);
                 $matches[] = $match;
                 $idx++;
             }
         }
         if (!$options->isDoublematch()) {
             $check[$teamA->getId()] = $teamA;
         }
     }
     return $matches;
 }
 /**
  * @param Tournament $tournament
  * @param $matchList
  * @param PlanningOptions $options
  * @return PlanningResults
  */
 public function setupCriteria(Tournament $tournament, $matchList, PlanningOptions $options)
 {
     $result = new PlanningResults();
     /* @var $timeslot Timeslot */
     foreach ($tournament->getTimeslots()->toArray() as $timeslot) {
         /* @var $pattr PlaygroundAttribute */
         foreach ($timeslot->getPlaygroundattributes()->toArray() as $pattr) {
             if ($pattr->getFinals() == $options->isFinals()) {
                 $pa = new PA();
                 $pa->setPA($pattr);
                 $pa->setId($pattr->getId());
                 $pa->setPlayground($pattr->getPlayground());
                 $pa->setTimeslot($pattr->getTimeslot());
                 $pa->setSchedule($pattr->getStartSchedule());
                 $categories = array();
                 foreach ($pattr->getCategories() as $category) {
                     $categories[$category->getId()] = $category;
                 }
                 $pa->setCategories($categories);
                 $pa->setMatchlist(array());
                 $result->addTimeslot($pa);
             }
         }
     }
     if ($options->isFinals()) {
         // Order the match plan by classification ascending
         usort($matchList, function (QMatchPlan $m1, QMatchPlan $m2) {
             return min(1, max(-1, $m1->getClassification() - $m2->getClassification()));
         });
     } else {
         // Order the match plan by group ascending
         usort($matchList, function (MatchPlan $m1, MatchPlan $m2) {
             return min(1, max(-1, $m1->getGroup()->getId() - $m2->getGroup()->getId()));
         });
     }
     foreach ($matchList as $match) {
         $result->appendUnresolved($match);
     }
     return $result;
 }
 /**
  * Plan matches according to assigned groups and match configuration
  * @Route("/edit/m/plan/plan/{tournamentid}", name="_edit_match_planning_plan")
  * @Template("ICupPublicSiteBundle:Edit:planmatch.html.twig")
  */
 public function planMatchesAction($tournamentid)
 {
     $tournament = $this->checkArgs($tournamentid);
     $options = new PlanningOptions();
     $options->setDoublematch($tournament->getOption()->isDrr());
     $options->setPreferpg(false);
     $this->get('planning')->planTournament($tournament, $options);
     return $this->redirect($this->generateUrl("_edit_match_planning_result", array('tournamentid' => $tournament->getId())));
 }
 /**
  * Plan tournament schedule - finals
  * Schedule is saved in match planning tables
  * @param Tournament $tournament
  * @param PlanningOptions $options
  * @param boolean $save
  * @return PlanningResults
  */
 public function planTournamentFinals(Tournament $tournament, PlanningOptions $options, $save = true)
 {
     $options->setFinals(true);
     $matchListF = (new QMatchBuilder())->populate($tournament);
     $resultF = (new MatchUtil($this->em))->setupCriteria($tournament, $matchListF, $options);
     (new QMatchPlanner($this->logger))->plan($resultF);
     if ($save) {
         $this->logic->removeMatchSchedules($tournament);
         (new MatchUtil($this->em))->savePlan($tournament, $resultF);
     }
     $timeslots = $resultF->getTimeslots();
     usort($timeslots, function (PA $ats1, PA $ats2) {
         $p1 = $ats1->getPA()->getStartSchedule()->getTimestamp() - $ats2->getPA()->getStartSchedule()->getTimestamp();
         $p2 = $ats1->getPlayground()->getNo() - $ats2->getPlayground()->getNo();
         $test = min(1, max(-1, $p1)) * 2 + min(1, max(-1, $p2));
         return min(1, max(-1, $test));
     });
     return $timeslots;
 }
 public function tstMatchPlanningOutput()
 {
     $options = new PlanningOptions();
     $options->setDoublematch(false);
     $options->setPreferpg(false);
     $this->container->get("planning")->planTournamentPre($this->tournament, $options);
     $match_schedule = $this->container->get("planning")->getSchedule($this->tournament);
     /* @var $match MatchPlan */
     foreach ($match_schedule["matches"] as $match) {
         echo $match->getDate();
         echo "  ";
         echo $match->getTime();
         echo "  ";
         echo $match->getCategory()->getName() . "|" . $match->getGroup()->getName() . ":" . $match->getPlayground()->getName();
         echo "  ";
         echo $match->getTeamA();
         echo " - ";
         echo $match->getTeamB();
         echo "\n";
     }
     $teams = array();
     /* @var $match MatchPlan */
     foreach ($match_schedule["matches"] as $match) {
         $teams[$match->getTeamA()->getId()][] = $match;
         $teams[$match->getTeamB()->getId()][] = $match;
     }
     /*
             foreach ($match_schedule["unassigned"] as $match) {
                 $teams[$match->getTeamA()->getId()][] = $match;
                 $teams[$match->getTeamB()->getId()][] = $match;
             }
     */
     foreach ($teams as $team_matches) {
         foreach ($team_matches as $match) {
             echo $match->getDate();
             echo "  ";
             echo $match->getTime();
             echo "  ";
             echo $match->getCategory()->getName() . "|" . $match->getGroup()->getName() . ":" . $match->getPlayground()->getName();
             echo "  ";
             echo $match->getTeamA();
             echo " - ";
             echo $match->getTeamB();
             echo "\n";
         }
         echo "\n";
     }
 }