Esempio n. 1
0
<?php

require 'class.roundrobin.php';
// Provide opponents in an array format
$teams = array('John Barnett', 'Phillip Carmichael', 'Daniel Carroll', 'Robert Craig', 'Thomas Griffin', 'John Hickey');
$roundrobin = new roundrobin($teams);
// Generating matches with matchdays
$roundrobin->create_matches();
// The result is a 3-dimension array: matchday->match->opponents
if ($roundrobin->finished) {
    print_r($roundrobin->matches);
}
echo "<br /><br />";
// Generating matches without matchdays
$roundrobin->create_raw_matches();
// The result is a 2-dimension array: match->opponents
if ($roundrobin->finished) {
    print_r($roundrobin->matches);
}
// See 'example_full.php' for the other features
Esempio n. 2
0
<?php

require 'class.roundrobin.php';
// Let's see how 7 of the best british football teams fight against each other
$teams = array('Banbury United', 'Bashley', 'Bedford Town', 'Brackley Town', 'Cambridge City', 'Chippenham Town', 'Clevedon Town');
$roundrobin = new roundrobin($teams);
// Generated matches with matchdays and free tickets - because we have an uneven number of teams
echo "<h3>Generated matches with matchdays and free tickets</h3><br />";
$roundrobin->free_ticket_identifer = "FREE TICKET";
//default is "free ticket"
$roundrobin->create_matches();
// Did everything went right?
if ($roundrobin->finished) {
    $i = 1;
    //Ok, iterating over the matchdays...
    while ($roundrobin->next_matchday()) {
        echo "-------Matchday " . $i . "-------<br />";
        //...and the matches of one match day
        while ($match = $roundrobin->next_match()) {
            echo $match[0] . "  <b>vs</b>  " . $match[1] . "<br />";
        }
        $i++;
        echo "<br />";
    }
}
echo "<br /><h3>Generated matches with matchdays but without free tickets</h3><br />";
$roundrobin->free_ticket = false;
// free tickets off
$roundrobin->create_matches();
if ($roundrobin->finished) {
    $i = 1;
 /**
  * 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;
 }
Esempio n. 4
0
function complete_add_season()
{
    if (valid_request(array(isset($_GET['league_id']), isset($_POST['name']), isset($_POST['start']), isset($_POST['schedule']), isset($_POST['max_teams_div'])))) {
        global $smarty;
        global $db;
        if (!preg_match(DATE_REGEX, $_POST['start'])) {
            display_errors(554);
            return true;
        }
        if (strlen($_POST['name']) > 100 || strlen($_POST['name']) < 1) {
            display_errors(552);
            return true;
        }
        if ($_POST['schedule'] > 255 || $_POST['schedule'] < 1) {
            display_errors(553);
            return true;
        }
        $new_div = array();
        foreach ($_POST as $team => $div) {
            //did we hit a team id?
            if (strpos($team, 'team') === 0) {
                $new_div[$div][] = substr($team, 4);
            }
        }
        // test for valid conditions
        foreach ($new_div as $div => $teams) {
            if ($div < 1) {
                display_errors(551);
                return true;
            }
            if (count($teams) > $_POST['max_teams_div']) {
                display_errors(550);
                return true;
            }
            if (count($teams) < 2) {
                display_errors(555);
                return true;
            }
            $div_nums[] = $div;
        }
        //creating the season and divisions
        $sql = "add_season_with_divisions(" . $_GET['league_id'] . ", \n                                              '" . $_POST['name'] . "',\n                                              " . $_POST['schedule'] . ", \n                                              '" . implode(',', $div_nums) . "')";
        $db->run($sql);
        // creating the matches
        require_once 'classes/class.roundrobin.php';
        $rr = new roundrobin();
        $rr->free_ticket = false;
        //iterating over the divisions
        foreach ($new_div as $div => $teams) {
            // getting the division_id of the related division that was just inserted
            $sql = "get_div_id(" . $_GET['league_id'] . ",\n                                   " . $div . ")";
            $db->run($sql);
            $division_id = $db->get_result_row();
            // putting the teams in the new divisions
            $sql = "add_teams_div(" . $division_id['division_id'] . ", \n                                      '" . implode(',', $teams) . "')";
            $db->run($sql);
            //deleting all signed up teams for this league
            $sql = "delete_signed_up_teams(" . $_GET['league_id'] . ")";
            $db->run($sql);
            // inserting the matches of this division one by one...
            shuffle($teams);
            $rr->pass_teams($teams);
            $rr->create_matches();
            if ($rr->finished) {
                $matchday = 1;
                while ($rr->next_matchday()) {
                    while ($match = $rr->next_match()) {
                        $sql = "add_match (" . $division_id['division_id'] . ", \n                                               " . $match[0] . ", \n                                               " . $match[1] . ",\n                                               " . $matchday . ", \n                                               '" . $_POST['start'] . "', \n                                               " . $_POST['schedule'] . ")";
                        $db->run($sql);
                    }
                    $matchday++;
                }
            }
        }
        unset($rr);
        display_success("add_season");
        $smarty->assign('content', $smarty->fetch("succes.tpl"));
    }
}
require 'class.roundrobin.php';
function shuffle_assoc(&$array)
{
    $keys = array_keys($array);
    shuffle($keys);
    foreach ($keys as $key) {
        $new[$key] = $array[$key];
    }
    $array = $new;
    echo 'shuffle_assoc<pre>', print_r($array, true), '</pre><br>';
    return true;
}
// Let's see how 7 of the best british football teams fight against each other
$teams = array('Banbury United', 'Bashley', 'Bedford Town', 'Brackley Town', 'Cambridge City', 'Chippenham Town', 'Clevedon Town');
shuffle_assoc($teams);
$roundrobin = new roundrobin($teams);
// Generated matches with matchdays and free tickets - because we have an uneven number of teams
echo "<h3>Generated matches with matchdays and free tickets</h3><br />";
$roundrobin->free_ticket_identifer = "FREE TICKET";
//default is "free ticket"
$roundrobin->create_matches();
// Did everything went right?
if ($roundrobin->finished) {
    $i = 1;
    //Ok, iterating over the matchdays...
    while ($roundrobin->next_matchday()) {
        echo "-------Matchday " . $i . "-------<br />";
        //...and the matches of one match day
        while ($match = $roundrobin->next_match()) {
            echo $match[0] . "  <b>vs</b>  " . $match[1] . "<br />";
        }