I’ve been using the PHP fixtures generator from bluebones (see below) and it’s working great. However, I need to add additional functionality to it to suit my local pool league.
The issue is around multiple teams from the same venue. Basically if one venue only has one pool table then both those teams can’t play from home at the same week. Other software I’ve seen has a ‘required opposites’ function where Team X and Team Y can’t be at home on the same week, and hence Team Y is switched to away.
I am struggling to replicate that in my PHP code (see below)
function getMatches($teams) {
shuffle($teams);
return call_user_func_array('array_combine', array_chunk($teams, sizeof($teams) / 2));
}
function nums($n) {
$ns = array();
for ($i = 1; $i <= $n; $i++) {
$ns[] = $i;
}
return $ns;
}
function show_fixtures($names) {
$teams = sizeof($names);
// Generate the fixtures using the cyclic algorithm.
$totalRounds = $teams - 1;
$matchesPerRound = $teams / 2;
$rounds = array();
for ($i = 0; $i < $totalRounds; $i++) {
$rounds[$i] = array();
}
for ($round = 0; $round < $totalRounds; $round++) {
for ($match = 0; $match < $matchesPerRound; $match++) {
$home = ($round + $match) % ($teams - 1);
$away = ($teams - 1 - $match + $round) % ($teams - 1);
// Last team stays in the same place while the others
// rotate around it.
if ($match == 0) {
$away = $teams - 1;
}
$rounds[$round][$match] = team_name($home + 1, $names)
. "v" . team_name($away + 1, $names);
}
}
// Interleave so that home and away games are fairly evenly dispersed.
$interleaved = array();
for ($i = 0; $i < $totalRounds; $i++) {
$interleaved[$i] = array();
}
$evn = 0;
$odd = ($teams / 2);
for ($i = 0; $i < sizeof($rounds); $i++) {
if ($i % 2 == 0) {
$interleaved[$i] = $rounds[$evn++];
} else {
$interleaved[$i] = $rounds[$odd++];
}
}
$rounds = $interleaved;
// Last team can't be away for every game so flip them
// to home on odd rounds.
for ($round = 0; $round < sizeof($rounds); $round++) {
if ($round % 2 == 1) {
$rounds[$round][0] = flip($rounds[$round][0]);
}
}
// Display the fixtures
for ($i = 0; $i < sizeof($rounds); $i++) {
//print "<p>Round " . ($i + 1) . "</p>n";
foreach ($rounds[$i] as $r) {
//print $r . "<br />";
$exround = ($i + 1);
$players = explode('v', $r);
echo('Match -> ' . $players[0] . ' vs '.$players[1].' (Week '.$exround.' / '.$r.')<br>');
// Insert into DB here
}
print "<br />";
}
// LEAGUE
// Basically this just flips around what was done above!
$round_counter = sizeof($rounds) + 1;
for ($i = sizeof($rounds) - 1; $i >= 0; $i--) {
//print "<p>Round " . $round_counter . "</p>n";
foreach ($rounds[$i] as $r) {
$r = flip($r);
$exround = $round_counter;
$players = explode('v', $r);
echo('Match -> ' . $players[0] . ' vs '.$players[1].' (Week '.$exround.' / '.$r.')<br>');
// Insert into DB here
}
$round_counter += 1;
print "<br />";
}
}
function team_name($num, $names) {
$i = $num - 1;
if (sizeof($names) > $i && strlen(trim($names[$i])) > 0) {
return trim($names[$i]);
} else {
return $num;
}
}
function flip($match) {
$components = explode('v', $match);
return trim($components[1]) . "v" . trim($components[0]);
}
show_fixtures(NO OF TEAMS);
Any advice appreciated.