예제 #1
0
 public function getPoolRanks(Pool $pool)
 {
     $rankings = [];
     /** @var Team $team */
     foreach ($pool->getTeams() as $team) {
         $rankings[$team->getId()] = ["team" => $team->getName(), "win" => 0, "played" => 0, "loose" => 0, "advantage" => 0, "points" => 0];
     }
     /** @var Game $game */
     foreach ($pool->getGames() as $game) {
         if ($game->getScore1() || $game->getScore2()) {
             if ($game->getScore1() >= $game->getScore2()) {
                 $rankings[$game->getTeam1()->getId()]["win"]++;
                 $rankings[$game->getTeam2()->getId()]["loose"]++;
             } else {
                 $rankings[$game->getTeam1()->getId()]["loose"]++;
                 $rankings[$game->getTeam2()->getId()]["win"]++;
             }
             $rankings[$game->getTeam1()->getId()]["played"]++;
             $rankings[$game->getTeam2()->getId()]["played"]++;
             $rankings[$game->getTeam1()->getId()]["advantage"] += $game->getScore1() - $game->getScore2();
             $rankings[$game->getTeam2()->getId()]["advantage"] += $game->getScore2() - $game->getScore1();
             $rankings[$game->getTeam1()->getId()]["points"] += $game->getScore1();
             $rankings[$game->getTeam2()->getId()]["points"] += $game->getScore2();
         }
     }
     usort($rankings, function (array $rank1, array $rank2) {
         foreach (["win", "advantage", "points", "played"] as $criteria) {
             if ($rank1[$criteria] > $rank2[$criteria]) {
                 return -1;
             }
             if ($rank1[$criteria] < $rank2[$criteria]) {
                 return 1;
             }
         }
         return -1;
     });
     return $rankings;
 }
예제 #2
0
 /**
  * Populate $newGame with the lazyiest teams in $pool
  *
  * @param Game $newGame
  * @param Pool $pool
  */
 private function setLazyiestTeams(Game $newGame, Pool $pool)
 {
     // Order the teams from the lazyiest to the busyiest
     $teams = $pool->getTeams()->toArray();
     usort($teams, function (Team $a, Team $b) use($pool) {
         return $pool->getTeamNbParticipations($a) > $pool->getTeamNbParticipations($b);
     });
     // Try to schedule the lazyiest team first
     foreach ($teams as $team1) {
         // Is the team already playing in this round ?
         if ($newGame->getRound()->hasTeam($team1)) {
             continue;
         }
         foreach ($teams as $team2) {
             if ($team1 == $team2 || $newGame->getRound()->hasTeam($team2)) {
                 continue;
             }
             if ($pool->hasGame($team1, $team2)) {
                 continue;
             }
             // We found a viable match!
             $newGame->setTeam1($team1);
             $newGame->setTeam2($team2);
             return;
         }
     }
 }