/** * Dispatch games according to the round robin rule * * @param Phase $phase */ protected function dispatchRoundRobinGames(Phase $phase) { $eventGrounds = $phase->getEvent()->getGrounds(); // Loop while we have games to schedule while (!$phase->isFullyScheduled()) { // Create a new game $newGame = $this->nextGameSlot($phase, $eventGrounds); // Order the pools from the lazyiest to the busyiest /** @var ArrayCollection $pools */ $pools = $phase->getPools(); $poolsArray = $pools->toArray(); usort($poolsArray, function (Pool $a, Pool $b) { return $a->getScheduledRate() > $b->getScheduledRate(); }); // Try to schedule games for the lazyiest pool first, then try busyier pools /** @var Pool $pool */ foreach ($poolsArray as $pool) { if ($pool->getScheduledRate() >= 1) { // This pool is already fully scheduled continue; } // Populate the game with lazyiest teams $this->setLazyiestTeams($newGame, $pool); if ($newGame->getTeam1() && $newGame->getTeam2()) { $pool->addGame($newGame); break; } } } }