public function checkPhaseGames(Phase $phase) { $errors = []; // Check a team does not play on several ground at the same time foreach ($phase->getRounds() as $round) { $busyTeams = []; foreach ($round->getGames() as $game) { if (in_array($game->getTeam1(), $busyTeams)) { $errors[] = "L'équipe {$game->getTeam1()->getName()} est à deux endroits dans le round {$round->getNumber()}"; } $busyTeams[] = $game->getTeam1(); if (in_array($game->getTeam2(), $busyTeams)) { $errors[] = "L'équipe {$game->getTeam2()->getName()} est à deux endroits dans le round {$round->getNumber()}"; } $busyTeams[] = $game->getTeam2(); if (in_array($game->getReferee(), $busyTeams)) { $errors[] = "L'arbitre {$game->getReferee()->getName()} joue déjà dans le round {$round->getNumber()}"; } $busyTeams[] = $game->getReferee(); // Check all teams belong to the same pool if (!$game->getPool()->hasTeam($game->getTeam1())) { $errors[] = "L'équipe {$game->getTeam1()->getName()} est programmée sur match du groupe {$game->getPool()->getName()} dans le round {$round->getNumber()}"; } if (!$game->getPool()->hasTeam($game->getTeam2())) { $errors[] = "L'équipe {$game->getTeam2()->getName()} est programmée sur match du groupe {$game->getPool()->getName()} dans le round {$round->getNumber()}"; } if (!$game->getPool()->hasTeam($game->getReferee())) { $errors[] = "L'équipe {$game->getReferee()->getName()} arbitre un match du groupe {$game->getPool()->getName()} dans le round {$round->getNumber()}"; } } } // Check all pools are fully scheduled foreach ($phase->getPools() as $pool) { $scheduledRate = $pool->getScheduledRate(); if ($scheduledRate < 1) { $errors[] = "Il manque des matchs dans le groupe {$pool->getName()}"; } if ($scheduledRate > 1) { $errors[] = "Il y a des matchs en trop dans le groupe {$pool->getName()}"; } } return $errors; }
/** * Return a Game object pointing at the next available ground in the next available round * it may create a new Round if all rounds are complete * * @param Phase $phase * @param Collection $eventGrounds * @return Game */ private function nextGameSlot(Phase $phase, Collection $eventGrounds) { // Get the first incomplete round $newGame = new Game(); $roundNumber = 0; foreach ($phase->getRounds() as $existingRound) { $roundNumber = $existingRound->getNumber(); $takenGrounds = new ArrayCollection(); foreach ($existingRound->getGames() as $existingGame) { $takenGrounds->add($existingGame->getGround()); } // Search for a free ground foreach ($eventGrounds as $eventGround) { if (!$takenGrounds->contains($eventGround)) { // We found a free ground in an incomplete round $existingRound->addGame($newGame); $newGame->setGround($eventGround); return $newGame; } } } // If no free ground in incomplete round, create a new round $round = new Round(); $round->setNumber($roundNumber + 1); $round->addGame($newGame); $phase->addRound($round); $newGame->setGround($eventGrounds[0]); return $newGame; }