/**
  * Populate project with matchdays
  * 
  * @param int $scheduling scheduling type
  * @param string $time start time for games
  * @param int $interval interval between rounds
  * @param string $start start date for new roundsrounds
  * @param string $roundname round name format (use %d for round number)
  * @return boolean true on success
  */
 function populate($project_id, $scheduling, $time, $interval, $start, $roundname, $teamsorder = null)
 {
     if (!strtotime($start)) {
         $start = strftime('%Y-%m-%d');
     }
     if (!preg_match("/^[0-9]+:[0-9]+\$/", $time)) {
         $time = '20:00';
     }
     $teams = $this->getProjectTeams();
     if ($teamsorder) {
         $ordered = array();
         foreach ($teamsorder as $ptid) {
             foreach ($teams as $t) {
                 if ($t->projectteam_id == $ptid) {
                     $ordered[] = $t;
                     break;
                 }
             }
         }
         if (count($ordered)) {
             $teams = $ordered;
         }
     }
     // diddipoeler
     $rrteams = array();
     foreach ($teams as $team) {
         $rrteams[] = $team;
     }
     $roundrobin = new roundrobin($rrteams);
     $roundrobin->free_ticket = false;
     // free tickets off
     $roundrobin->create_matches();
     echo '<pre>', print_r($roundrobin->matches, true), '</pre><br>';
     if ($roundrobin->finished) {
         $i = 1;
         while ($roundrobin->next_matchday()) {
             echo "-------Matchday " . $i . "-------<br />";
             while ($match = $roundrobin->next_match()) {
                 //echo $match[0]."  <b>vs</b>  ".$match[1]."<br />";
             }
             $i++;
             echo "<br />";
         }
     }
     if (!$teams || !count($teams)) {
         $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_NO_TEAM'));
         return false;
     }
     $rounds = $this->getData();
     $rounds = $rounds ? $rounds : array();
     if ($scheduling < 2) {
         require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers' . DS . 'RRobin.class.php';
         $helper = new RRobin();
         $helper->create($teams);
         $schedule = $helper->getSchedule($scheduling + 1);
     } else {
         $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_UNDEFINED_SCHEDULING'));
         return false;
     }
     $current_date = null;
     $current_code = 0;
     foreach ($schedule as $k => $games) {
         if (isset($rounds[$k])) {
             $round_id = $rounds[$k]->id;
             $current_date = $rounds[$k]->round_date_first;
             $current_code = $rounds[$k]->roundcode;
         } else {
             $round = JTable::getInstance('Round', 'Table');
             $round->project_id = $project_id;
             $round->round_date_first = strtotime($current_date) ? strftime('%Y-%m-%d', strtotime($current_date) + $interval * 24 * 3600) : $start;
             $round->round_date_last = $round->round_date_first;
             $round->roundcode = $current_code ? $current_code + 1 : 1;
             $round->name = sprintf($roundname, $round->roundcode);
             if (!($round->check() && $round->store())) {
                 $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_CREATING_ROUND') . ': ' . $round->getError());
                 return false;
             }
             $current_date = $round->round_date_first;
             $current_code = $round->roundcode;
             $round_id = $round->id;
         }
         // create games !
         foreach ($games as $g) {
             if (!isset($g[0]) || !isset($g[1])) {
                 // happens if number of team is odd ! one team gets a by
                 continue;
             }
             $game = JTable::getInstance('Match', 'Table');
             $game->round_id = $round_id;
             $game->division_id = 0;
             $game->projectteam1_id = $g[0]->projectteam_id;
             $game->projectteam2_id = $g[1]->projectteam_id;
             $game->published = 1;
             $game->match_date = $current_date . ' ' . $time;
             if (!($game->check() && $game->store())) {
                 $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_CREATING_GAME') . ': ' . $game->getError());
                 return false;
             }
         }
     }
     //echo '<pre>';print_r($schedule); echo '</pre>';
     //exit;
     return true;
 }
示例#2
0
 /**
  * Populate project with matchdays
  * 
  * @param int $project_id project id
  * @param int $scheduling scheduling type
  * @param string $time start time for games
  * @param int $interval interval between rounds
  * @param string $start start date for new roundsrounds
  * @param string $roundname round name format (use %d for round number)
  * @param array $teamsorder list of teams
  * @param int $matchnummer starting by number, increasing by 1
  * @return boolean true on success
  */
 function populate($project_id, $scheduling, $time, $interval, $start, $roundname, $teamsorder = null, $iMatchnumber = 0)
 {
     if (!strtotime($start)) {
         $start = strftime('%Y-%m-%d');
     }
     if (!preg_match("/^[0-9]+:[0-9]+\$/", $time)) {
         $time = '20:00';
     }
     $teams = $this->getProjectTeams();
     if ($teamsorder) {
         $ordered = array();
         foreach ($teamsorder as $ptid) {
             foreach ($teams as $t) {
                 if ($t->projectteam_id == $ptid) {
                     $ordered[] = $t;
                     break;
                 }
             }
         }
         if (count($ordered)) {
             $teams = $ordered;
         }
     }
     if (!$teams || !count($teams)) {
         $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_NO_TEAM'));
         return false;
     }
     $rounds = $this->getData();
     $rounds = $rounds ? $rounds : array();
     if ($scheduling < 2) {
         require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/rrobin.php';
         $helper = new RRobin();
         $helper->create($teams);
         $schedule = $helper->getSchedule($scheduling + 1);
     } else {
         $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_UNDEFINED_SCHEDULING'));
         return false;
     }
     $current_date = null;
     $current_code = 0;
     foreach ($schedule as $k => $games) {
         if (isset($rounds[$k])) {
             $round_id = $rounds[$k]->id;
             $current_date = $rounds[$k]->round_date_first;
             $current_code = $rounds[$k]->roundcode;
         } else {
             $tblRound = JTable::getInstance('Round', 'Table');
             $tblRound->project_id = $project_id;
             $tblRound->round_date_first = strtotime($current_date) ? strftime('%Y-%m-%d', strtotime($current_date) + $interval * 24 * 3600) : $start;
             $tblRound->round_date_last = $round->round_date_first;
             $tblRound->roundcode = $current_code ? $current_code + 1 : 1;
             $tblRound->name = sprintf($roundname, $tblRound->roundcode);
             if (!($tblRound->check() && $tblRound->store())) {
                 $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_CREATING_ROUND') . ': ' . $tblRound->getError());
                 return false;
             }
             $current_date = $tblRound->round_date_first;
             $current_code = $tblRound->roundcode;
             $round_id = $tblRound->id;
         }
         // create games !
         // we need to convert game date+time to utc
         $project_tz = $this->getProject()->timezone;
         $utc_tz = new DateTimeZone('UTC');
         $date = JFactory::getDate($current_date . ' ' . $time, $project_tz)->setTimezone($utc_tz);
         $utc_sql_date = $date->toSql();
         foreach ($games as $g) {
             if (!isset($g[0]) || !isset($g[1])) {
                 // happens if number of team is odd ! one team gets a by
                 continue;
             }
             $tblMatch = JTable::getInstance('Match', 'Table');
             $tblMatch->round_id = $round_id;
             $tblMatch->projectteam1_id = $g[0]->projectteam_id;
             $tblMatch->projectteam2_id = $g[1]->projectteam_id;
             $tblMatch->match_date = $utc_sql_date;
             $tblMatch->published = 1;
             if ($iMatchnumber > 0) {
                 $tblMatch->match_number = $iMatchnumber++;
             }
             if (!($tblMatch->check() && $tblMatch->store())) {
                 $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_ROUNDS_POPULATE_ERROR_CREATING_GAME') . ': ' . $tblMatch->getError());
                 return false;
             }
         }
     }
     return true;
 }
示例#3
0
 /**
  * Get the match schedule using Round Robin scheme
  * @param array ordered list of project teams
  * @param int $scheduling use 0 for a single and 1 for a double Round Robin scheme
  * @return the match schedule (array) if everything went ok, false otherwise
  */
 private function getRoundRobinSchedule($teams, $scheduling)
 {
     require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/rrobin.php';
     $helper = new RRobin();
     $result = $helper->create($teams);
     if (!$result) {
         return false;
     }
     $schedule = $helper->getSchedule($scheduling + 1);
     return $schedule;
 }
示例#4
0
 public static function create(array $input)
 {
     /**
      * Creates a new tournament.
      *
      * Arguments:
      * ----------
      *  name, type, rs, teams => array(team_ids, ...), 'rounds'
      **/
     /* Check input */
     // Done in in scheduler section code.
     /* Create tournament */
     // Quit if can't make tournament entry.
     $query = "INSERT INTO tours (name, f_did, type, rs, date_created, allow_sched) VALUES ('" . mysql_real_escape_string($input['name']) . "', {$input['did']}, {$input['type']}, {$input['rs']}, NOW(), {$input['allow_sched']})";
     if (!mysql_query($query)) {
         return false;
     }
     $tour_id = mysql_insert_id();
     /* Generate matches depending on type */
     // FFA match(es)?
     if ($input['type'] == TT_FFA) {
         $status = true;
         for ($i = 0; $i < count($input['teams']) / 2; $i++) {
             list($exitStatus, $mid) = Match::create(array('team1_id' => $input['teams'][$i * 2], 'team2_id' => $input['teams'][$i * 2 + 1], 'round' => $input['rounds'] ? $input['rounds'] : 1, 'f_tour_id' => $tour_id));
             $status &= !$exitStatus;
         }
         return $status;
     } elseif ($input['type'] == TT_RROBIN) {
         if (sizeof($input['teams']) == 0) {
             return true;
         }
         // Quit if can't make tournament schedule.
         $robin = new RRobin();
         if (!$robin->create($input['teams'])) {
             # If can't create Round-Robin tour -> quit.
             return false;
         }
         // Okey, so $input['rounds'] is incorrect in the sense that this is the multiplier of times to schedule the same round-set comprising the RR tour.
         // Instead we denote $real_rounds to be the actual number of rounds in the scheduled RR tour.
         $real_rounds = count($robin->tour);
         // Create inverse depiction round.
         foreach ($robin->tour as $ridx => $r) {
             foreach ($r as $idx => $m) {
                 $robin->tour_inv[$ridx][$idx] = array($m[1], $m[0]);
             }
         }
         $status = true;
         for ($i = 1; $i <= $input['rounds']; $i++) {
             $rounds = $robin->{$i % 2 ? 'tour' : 'tour_inv'};
             # Invert pair-up?
             // Shuffle the order of rounds in the bracket seeding, $i.
             $rounds_k = array_keys($rounds);
             $rounds_v = array_values($rounds);
             shuffle($rounds_k);
             shuffle($rounds_v);
             $rounds = array_combine($rounds_k, $rounds_v);
             ksort($rounds);
             // Create new bracket.
             foreach ($rounds as $ridx => $r) {
                 foreach ($r as $match) {
                     // Depict round's match compets inversely for every other round.
                     list($exitStatus, $mid) = Match::create(array('team1_id' => $match[0], 'team2_id' => $match[1], 'round' => $ridx + ($i - 1) * $real_rounds, 'f_tour_id' => $tour_id));
                     $status &= !$exitStatus;
                 }
             }
         }
         return $status;
     }
     return false;
     # Return false if tournament type was not recognized.
 }