Esempio n. 1
0
function cs_cups_generate($seedings, $gridsize, $gridtype)
{
    /* init match array */
    $matches = array();
    /* first generate the complete winner bracket grid positions */
    $seed = array();
    $grid = array();
    $nextmatch = array();
    $curmatch = array();
    $match = array();
    for ($i = 1; $i <= $gridsize; $i++) {
        $grid[$i] = 0;
        $seed[$i] = 0;
        $nextmatch[$i] = 0;
        $curmatch[$i] = 0;
        $match[$i] = 0;
    }
    /* calculate the seeding positions
     *
     * we do this by reverse calculation
     * starting * from the (expected) winner (seed #1)
     * 
     * for each previous round we can say that
     * the player X who got this far should
     * have played Y a player with seed position
     * seed(Y) = gridsize + 1 - seed(X)
     * in the previous round.
     *
     * for each pair of players in the previous
     * calculation we put the first one on top
     * and the second on the bottom.
     * this ensures that seed #1 ends on top
     * of the final grid, and seed #2 at the
     * bottom and orders the grid in a nice
     * fashion.
     * we do this for each round untill we
     * reach our final gridsize.
     *
     * for example, the calculation goes like this:
     *
     * start with gridsize = 1 and seed #1
     *
     * previous round had gridsize = 2
     * we had only seed #1 before, so he played
     * seed(Y) = 2 + 1 - 1 = 2
     * so #1 vs #2
     *
     * previous round gridsize = 4
     * we already had seed #1, so #1 played
     * seed(Y) = 4 + 1 - 1 = 4
     * so #1 vs #4
     * we already had seed #2, so #2 played
     * seed(Y) = 4 + 1 - 2 = 3
     * so #3 vs #2
     *
     * previous round gridsize = 8
     * we already had seed #1, so #1 played
     * seed(Y) = 8 + 1 - 1 = 8
     * so #1 vs #8
     * we already had seed #4, so #4 played
     * seed(Y) = 8 + 1 - 4 = 5
     * so #5 vs #4
     * we already had seed #3, so #3 played
     * seed(Y) = 8 + 1 - 3 = 6
     * so #3 vs #6
     * we already had seed #2, so #2 played
     * seed(Y) = 8 + 1 - 2 = 7
     * so #7 vs #2
     *
     * etc...
     */
    $n = 1;
    // current gridsize
    $seed[1] = 1;
    // seed #1 is on this position
    $maxrounds = cs_cups_log($gridsize);
    // maximum number of wb rounds
    $curround = $maxrounds;
    if ($gridtype == CS_CUPS_SYSTEM_LB) {
        $curmatch[1] = 0;
    } else {
        $curmatch[1] = CS_CUPS_NO_NEXTMATCH;
    }
    // no match
    while ($n < $gridsize) {
        $n *= 2;
        /* make a grid for 1 to $n */
        $top = true;
        // switch top and lower
        $i = 1;
        // i goes from 1 - n/2
        $j = 1;
        // j goes from 1 - n
        while ($j < $n) {
            if ($top) {
                $nextmatch[$j] = $curmatch[$i];
                // next match #
                $match[$i] = $n / 2 + $i;
                // current match #
                $grid[$j++] = $seed[$i];
                // top seed plays
                $grid[$j++] = $n + 1 - $seed[$i];
                // vs lower seed
                $top = false;
            } else {
                $nextmatch[$j] = $curmatch[$i];
                // next match
                $match[$i] = $n / 2 + $i;
                // current match #
                $grid[$j++] = $n + 1 - $seed[$i];
                // lower seed plays
                $grid[$j++] = $seed[$i];
                // vs top seed
                $top = true;
            }
            $i++;
        }
        /* copy current grid array to seed */
        for ($i = 1; $i <= $n; $i++) {
            $seed[$i] = $grid[$i];
            $grid[$i] = 0;
            if ($i % 2 == 0) {
                $curmatch[$i] = $match[$i / 2];
            } else {
                $curmatch[$i] = $match[($i + 1) / 2];
            }
        }
        //		echo '======= '.$n.' ======<br />';
        /* grid size reached */
        for ($i = 0; $i < $n / 2; $i++) {
            //			echo '---<br />';
            //			echo 'round #'.$curround.'<br />';
            //			echo 'match #'.$match[$i+1].'<br />';
            //			echo '#['.$seed[$i*2+1].']<br />';
            //			echo '|<br />';
            //			echo '#['.$seed[$i*2+2].']<br />';
            //			echo 'winner to match #'.$nextmatch[$i*2+1].'<br />';
            //			echo '---<br />';
            $nextmatchlb = CS_CUPS_NO_NEXTMATCH;
            if ($gridtype == CS_CUPS_SYSTEM_KO3RD && $n == 4) {
                /* if semi-final in WB and we have a 3rd place match */
                $nextmatchlb = 0;
            }
            if ($n < $gridsize) {
                $matches[$match[$i + 1]] = array('nextmatch' => $nextmatch[$i * 2 + 1], 'nextmatchlb' => $nextmatchlb, 'loserbracket' => 0, 'round' => $curround, 'squad_id1' => CS_CUPS_TEAM_UNKNOWN, 'squad_id2' => CS_CUPS_TEAM_UNKNOWN, 'tree_order' => $match[$i + 1], 'seed1' => $seed[$i * 2 + 1], 'seed2' => $seed[$i * 2 + 2]);
            } else {
                $matches[$match[$i + 1]] = array('nextmatch' => $nextmatch[$i * 2 + 1], 'nextmatchlb' => $nextmatchlb, 'loserbracket' => 0, 'round' => $curround, 'squad_id1' => $seedings[$seed[$i * 2 + 1]], 'squad_id2' => $seedings[$seed[$i * 2 + 2]], 'tree_order' => $match[$i + 1], 'seed1' => $seed[$i * 2 + 1], 'seed2' => $seed[$i * 2 + 2]);
            }
        }
        $curround--;
    }
    /* we now have all grid positions and matches for the winner bracket and all the teams */
    /* add grand final if we have lb */
    if ($gridtype == CS_CUPS_SYSTEM_LB) {
        $matches[0] = array('nextmatch' => CS_CUPS_NO_NEXTMATCH, 'nextmatchlb' => CS_CUPS_NO_NEXTMATCH, 'loserbracket' => 0, 'round' => 0, 'squad_id1' => CS_CUPS_TEAM_UNKNOWN, 'squad_id2' => CS_CUPS_TEAM_UNKNOWN, 'tree_order' => 0, 'seed1' => 1, 'seed2' => 2);
    } else {
        if ($gridtype == CS_CUPS_SYSTEM_KO3RD) {
            $matches[0] = array('nextmatch' => CS_CUPS_NO_NEXTMATCH, 'nextmatchlb' => CS_CUPS_NO_NEXTMATCH, 'loserbracket' => 0, 'round' => 0, 'squad_id1' => CS_CUPS_TEAM_UNKNOWN, 'squad_id2' => CS_CUPS_TEAM_UNKNOWN, 'tree_order' => 0, 'seed1' => 3, 'seed2' => 4);
        }
    }
    /* get LB */
    if ($gridtype == CS_CUPS_SYSTEM_LB) {
        $curround = (double) $maxrounds - 0.5;
        for ($i = 1; $i <= $gridsize; $i++) {
            $grid[$i] = 0;
            $seed[$i] = 0;
            $nextmatch[$i] = 0;
            $match[$i] = 0;
        }
        $wbmatches1 = array();
        foreach ($matches as $key => $match) {
            if ($match['loserbracket'] == 0 && $match['round'] == 1) {
                $wbmatches1[$match['seed1']] = $key;
                $wbmatches1[$match['seed2']] = $key;
            }
        }
        while ($curround >= 1.0) {
            $even = true;
            /* uneven round, so losers from WB join the LB */
            if ($curround - floor($curround) == 0.5) {
                $even = false;
            }
            /* calculate number of matches in this LB round */
            if ($even) {
                $K = $gridsize / pow(2, $curround);
                $Z = $K + 1;
                $nummatches = $gridsize / pow(2, (int) $curround + 1);
            } else {
                $nummatches = $gridsize / pow(2, (int) ($curround + 0.5));
                // K = N / (2 ^ (A+0.5))
                /* in round X+0.5 the losers from the WB X+1 join the LB */
                /* get all matches from WB round X+1 */
                $wbmatches = array();
                $wbround = (int) ($curround + 0.5);
                foreach ($matches as $key => $match) {
                    if ($wbround == $match['round'] && $match['loserbracket'] == 0) {
                        /* get probable loser */
                        $seed = $match['seed1'] > $match['seed2'] ? $match['seed1'] : $match['seed2'];
                        $wbmatches[$seed] = $key;
                    }
                }
            }
            $top = true;
            $i = 1;
            $j = 1;
            while ($i <= $nummatches) {
                if ($even) {
                    $matchnr = $gridsize + $nummatches * 3 + $i;
                    /* in round X the lower seeds from the WB round X should be here */
                    $seedX = $K + $i;
                    // X-K + Y-K = Z => Y = Z - X + 2*K
                    $seedY = $Z + 2 * $K - $seedX;
                    /* in the first round we need to point all matches from the wb to this round */
                    $matches[$wbmatches1[$seedX]]['nextmatchlb'] = $matchnr;
                    $matches[$wbmatches1[$seedY]]['nextmatchlb'] = $matchnr;
                    $nextmatch = CS_CUPS_NO_NEXTMATCH;
                    foreach ($matches as $key => $match) {
                        if ($match['loserbracket'] == 1 && $match['round'] == (int) ($curround * 2.0 + 1) && ($match['seed1'] == $seedX || $match['seed2'] == $seedX)) {
                            $nextmatch = $key;
                            break;
                        }
                    }
                    $matches[$matchnr] = array('nextmatch' => $nextmatch, 'nextmatchlb' => CS_CUPS_NO_NEXTMATCH, 'loserbracket' => 1, 'round' => (int) ($curround * 2.0), 'squad_id1' => CS_CUPS_TEAM_UNKNOWN, 'squad_id2' => CS_CUPS_TEAM_UNKNOWN, 'tree_order' => 0, 'seed1' => $seedX, 'seed2' => $seedY);
                } else {
                    /* in round X+0.5 the losers from the WB X+1 join the LB */
                    $matchnr = $gridsize + $nummatches * 2 + $i;
                    /* lb teams should be nummatches + 1 to 2 * nummatches vs 2*nummatches+1 to 3*nummatches */
                    $seedX = $nummatches + $i;
                    $seedY = 2 * $nummatches + $i;
                    /* get the wb match which should point to this one, loser joins this match */
                    $matches[$wbmatches[$seedX]]['nextmatchlb'] = $matchnr;
                    if (intval($curround + 0.5) == $maxrounds) {
                        /* we are in LB final, next match is grand final */
                        $nextmatch = 0;
                    } else {
                        /* find the LB match in the next round which has the highest seed in it */
                        foreach ($matches as $key => $match) {
                            if ($match['loserbracket'] == 1 && $match['round'] == (int) ($curround * 2.0 + 1) && ($match['seed1'] == $seedX || $match['seed2'] == $seedX)) {
                                $nextmatch = $key;
                                break;
                            }
                        }
                    }
                    $matches[$matchnr] = array('nextmatch' => $nextmatch, 'nextmatchlb' => CS_CUPS_NO_NEXTMATCH, 'loserbracket' => 1, 'round' => (int) ($curround * 2.0), 'squad_id1' => CS_CUPS_TEAM_UNKNOWN, 'squad_id2' => CS_CUPS_TEAM_UNKNOWN, 'tree_order' => 0, 'seed1' => $seedX, 'seed2' => $seedY);
                }
                $i++;
            }
            $curround -= 0.5;
        }
        /* now we generated all the LB matches, order them in the grid */
        $grid = array();
        $curround = (double) $maxrounds - 0.5;
        $nummatches = $gridsize / pow(2, (int) ($curround + 0.5));
        // K = N / (2 ^ (A+0.5))
        $matchnr = $gridsize + $nummatches * 2 + 1;
        /* order the first one */
        $n = $gridsize * 2;
        $matches[$matchnr]['tree_order'] = 1;
        $diff = 1;
        while ($curround > 1.0) {
            $even = true;
            /* uneven round, so losers from WB join the LB */
            if ($curround - floor($curround) == 0.5) {
                $n /= 2;
                $even = false;
            }
            foreach ($matches as $key => $match) {
                if ($match['loserbracket'] == 1 && $match['round'] == (int) ($curround * 2)) {
                    $prev = cs_cups_findprev($matches, $key, 1);
                    if (count($prev) == 1) {
                        /* prev round is an uneven round */
                        $nummatches = $gridsize / pow(2, (int) ($curround + 0.5));
                        // K = N / (2 ^ (A+0.5))
                        $matches[$prev[0]]['tree_order'] = $match['tree_order'] + $n;
                    } else {
                        /* prev round is an even round */
                        $matches[$prev[0]]['tree_order'] = $match['tree_order'] - $n + 1;
                        $matches[$prev[1]]['tree_order'] = $match['tree_order'] + $n - 1;
                    }
                }
            }
            $curround -= 0.5;
            $diff += 2;
        }
    }
    return $matches;
}
Esempio n. 2
0
 *
 * Special notice by Remy Wetzels <*****@*****.**>, September 14, 2010:
 * Permission is hereby granted by Wetzels Holding BV to the ClanSphere Project
 * to omit the above disclaimer in their general documentation and/or
 * ClanSphere about section of the code.
 */
$cs_lang = cs_translate('cups');
$cs_option = cs_sql_option(__FILE__, 'cups');
$datahtml = array();
$datahtml['tree'] = array();
include_once 'mods/cups/functions.php';
$cups_id = (int) $_GET['id'];
/* tree.php might set the variable below */
$gridonly = isset($gridonly) && $gridonly == true || !empty($_GET['gridonly']) ? true : false;
$cup = cs_sql_select(__FILE__, 'cups', '*', 'cups_id = ' . $cups_id);
$rounds = cs_cups_log($cup['cups_teams']);
$rounds_1 = $rounds;
if ($account['access_cups'] < $cup['cups_access'] || $cup['cups_access'] == 0) {
    echo $cs_lang['access_denied'];
    return;
}
if (defined('IN_TREE')) {
    $tree = true;
} else {
    $tree = false;
}
$width = empty($cs_option['width']) ? empty($_GET['width']) ? 600 : (int) $_GET['width'] : $cs_option['width'];
$key = 'lang=' . $account['users_lang'] . '&cup=' . $cups_id . '&lb=0&gridonly=' . ($gridonly ? 1 : 0) . '&tree=' . ($tree ? 1 : 0) . '&width=' . $width . '&access=' . $account['access_cups'];
if (function_exists('cs_datacache_load')) {
    $cachedata = cs_datacache_load('cups', 'viewtree', $key, false);
} else {
Esempio n. 3
0
if ($account['access_cups'] < $cup['cups_access'] || $cup['cups_access'] == 0) {
    echo $cs_lang['access_denied'];
    return;
}
$key = 'lang=' . $account['users_lang'] . '&cup=' . $cups_id . '&access=' . $account['access_cups'];
if (function_exists('cs_datacache_load')) {
    $cachedata = cs_datacache_load('cups', 'result', $key, false);
} else {
    $cachedata = false;
}
if ($cachedata !== false) {
    echo $cachedata;
    return;
}
$gridsize = $cup['cups_teams'];
$rounds = cs_cups_log($gridsize);
$cpos = $gridsize;
$result = array();
/* get the teams */
$teams = cs_cups_get_teams($cups_id, $cup['cups_system'], $cs_lang);
$thirdplace = null;
switch ($cup['cups_brackets']) {
    case CS_CUPS_SYSTEM_KO3RD:
        /* get the 3rd place match */
        $rwhere = 'cups_id = ' . $cups_id . ' AND cupmatches_accepted1 = 1 AND cupmatches_accepted2 = 1 AND cupmatches_round = 0';
        $thirdplace = cs_sql_select(__FILE__, 'cupmatches', '*', $rwhere, 0, 0, 1);
        /* fallthrough, almost the same as KO */
    /* fallthrough, almost the same as KO */
    case CS_CUPS_SYSTEM_KO:
        /* the position of the team who loses in a KO round is always (number of players / 2) + 1  */
        $pos2 = floor($gridsize / 2);
Esempio n. 4
0
<?php

// ClanSphere 2010 - www.clansphere.net
// $Id$
$cs_lang = cs_translate('cups');
include_once 'mods/cups/functions.php';
$cups_id = !empty($_POST['where']) ? (int) $_POST['where'] : (int) $_GET['where'];
$lb = !empty($_POST['lb']) ? 1 : (!empty($_GET['lb']) ? 1 : 0);
$maxteams = cs_sql_select(__FILE__, 'cups', 'cups_teams', 'cups_id = ' . $cups_id);
$maxrounds = cs_cups_log($maxteams['cups_teams']);
$round = !empty($_POST['round']) ? (int) $_POST['round'] : (!empty($_GET['round']) ? (int) $_GET['round'] : 1);
$start = empty($_GET['start']) ? 0 : (int) $_GET['start'];
$system = cs_sql_select(__FILE__, 'cups', 'cups_system, cups_brackets, cups_access', 'cups_id = ' . $cups_id);
if ($account['access_cups'] < $system['cups_access'] || $system['cups_access'] == 0) {
    echo $cs_lang['access_denied'];
    return;
}
if ($system['cups_system'] == CS_CUPS_TYPE_TEAMS) {
    $cs_sort[1] = 'sq1.squads_name ASC';
    $cs_sort[2] = 'sq1.squads_name DESC';
    $cs_sort[3] = 'sq2.squads_name ASC';
    $cs_sort[4] = 'sq2.squads_name DESC';
} else {
    $cs_sort[1] = 'usr1.users_nick ASC';
    $cs_sort[2] = 'usr1.users_nick DESC';
    $cs_sort[3] = 'usr2.users_nick ASC';
    $cs_sort[4] = 'usr2.users_nick DESC';
}
$cs_sort[5] = 'cm.cupmatches_loserbracket ASC, cm.cupmatches_match ASC';
$cs_sort[6] = 'cm.cupmatches_loserbracket DESC, cm.cupmatches_match ASC';
$sort = empty($_GET['sort']) ? 5 : (int) $_GET['sort'];
Esempio n. 5
0
}
$data['cup']['percentage_open'] = 100 - $data['cup']['percentage_reg'];
$data['cup']['start_date'] = cs_date('unix', $data['cup']['cups_start'], 1);
$data['cup']['checkin_date'] = cs_date('unix', $data['cup']['cups_checkin'], 1);
$data['cup']['cups_text'] = cs_secure($data['cup']['cups_text'], 1, 1);
if (file_exists('uploads/games/' . $data['cup']['games_id'] . '.gif')) {
    $data['cup']['game'] = cs_html_img('uploads/games/' . $data['cup']['games_id'] . '.gif');
} else {
    $data['cup']['game'] = '';
}
$where = "games_id = '" . $data['cup']['games_id'] . "'";
$cs_game = cs_sql_select(__FILE__, 'games', 'games_name, games_id', $where);
$id = 'id=' . $cs_game['games_id'];
$data['cup']['game'] .= ' ' . cs_link($cs_game['games_name'], 'games', 'view', $id);
$data['if']['running'] = false;
$max_rounds = cs_cups_log($data['cup']['cups_teams']);
$find_round = $data['cup']['cups_brackets'] == CS_CUPS_SYSTEM_LB ? 0 : $max_rounds;
$matchcells = 'cupmatches_round, cupmatches_score1, cupmatches_score2, cupmatches_winner, squad1_id, ';
$matchcells .= 'squad2_id, cupmatches_accepted1, cupmatches_accepted2';
$matchsel = cs_sql_select(__FILE__, 'cupmatches', $matchcells, 'cups_id = ' . $cups_id . ' AND cupmatches_loserbracket = 0 AND cupmatches_round = ' . $find_round, 0, 0, 1);
if (empty($matchsel)) {
    /* we have no cupmatches yet */
    $data['cup']['status'] = $cs_lang['upcoming'];
    $data['cup']['rounds'] = '-';
} else {
    if (!empty($matchsel['cupmatches_accepted1']) && !empty($matchsel['cupmatches_accepted2'])) {
        /* we have cupmatches, and the final is already closed */
        $data['cup']['status'] = $cs_lang['finished'];
        $data['cup']['rounds'] = '-';
    } else {
        /* we have cupmatches, and the final is not already closed */