/** * Replace match schedule with other matches with same team * @param PlanningResults $result * @param MatchPlan $match * @return MatchPlan|null */ private function findAngel(PlanningResults $result, MatchPlan $match) { $result->mark(); while ($pa = $result->cycleTimeslot()) { /* Find a candidate for replacement */ $matchlist = $pa->getMatchlist(); /* @var $replan_match MatchPlan */ foreach ($matchlist as $idx => $replan_match) { // search for matches that are not yet tried to swap and share the same team if (!$replan_match->isFixed() && $this->teamsMatch($match, $replan_match)) { // another match with same team was found - try release the match schedule and see if that makes some space $result->getTeamCheck()->freeCapacity($replan_match, $pa->getTimeslot()); // does this make room for assignment? if ($result->getTeamCheck()->isCapacity($match, $replan_match->getSchedule(), $replan_match->getPlayground(), $pa->getTimeslot())) { // yes - try this schedule and see if puzzle is solved... $match->setDate($replan_match->getDate()); $match->setTime($replan_match->getTime()); $match->setPlayground($replan_match->getPlayground()); $match->setFixed(true); $matchlist[$idx] = $match; $pa->setMatchlist($matchlist); $result->getTeamCheck()->reserveCapacity($match, $pa->getTimeslot()); $result->rewind(); return $replan_match; } else { // nope - redo the release and try another match schedule $result->getTeamCheck()->reserveCapacity($replan_match, $pa->getTimeslot()); } } } } return null; }
/** * @param Tournament $tournament * @param PlanningResults $results */ public function savePlan(Tournament $tournament, PlanningResults $results) { /* @var $pa PA */ foreach ($results->getTimeslots() as $pa) { /* @var $match MatchPlan */ foreach ($pa->getMatchlist() as $match) { $ms = $this->makeMatchSchedule($tournament, $match); $mp = new MatchSchedulePlan(); $mp->setPlaygroundAttribute($this->em->merge($pa->getPA())); $mp->setMatchstart($match->getTime()); $mp->setFixed($match->isFixed()); $ms->setPlan($mp); $this->em->persist($ms); } } /* @var $match MatchPlan */ foreach ($results->getUnresolved() as $match) { $ms = $this->makeMatchSchedule($tournament, $match); $this->em->persist($ms); if (!$match instanceof QMatchPlan) { /* @var $pa PA */ foreach ($results->getTimeslots() as $pa) { /* Both teams must be allowed to play now */ if ($results->getTeamCheck()->isCapacity($match, $pa->getSchedule(), $pa->getPlayground(), $pa->getTimeslot())) { $matchAlternative = new MatchAlternative(); $matchAlternative->setMatchSchedule($ms); $matchAlternative->setPlaygroundAttribute($this->em->merge($pa->getPA())); $this->em->persist($matchAlternative); } } } } $this->em->flush(); }