I am building double elimination system when match completes in winner bracket the loser is transferred to loser bracket but I am having a problem how to figure out in which round that player needs to be moved i am really depressed as I am unable to figure out I have created a function where it checks on how many slots are available in the current round
if 4 slots available in round 2 8 players can only be accommodated and in round 3 if 2 lots are available and 6 players are available the system will accommodate winners from previous round of loser bracket and move the remaining players which are losers from winner bracket to the next round and if in next round slot is available
The problem is I am not sure how do I get the round number I have created a function but still confused
Function to check the slots in each round and players expected
public function calculate_loser_bracket_rounds($tournamentID = 76, $matchRound = 2) {
$winnerBracketData = $this->process_data->get_bracket($tournamentID);
$tournamentData = $this->process_data->get_data('tournament', array('id' => $tournamentID));
$totalRounds = count($this->process_data->get_bracket($tournamentID));
$looserBracketRounds = $totalRounds + 1;
$bracket = array();
$key = 0;
$winnerBracketData = $this->process_data->get_bracket($tournamentID);
$numLoosers = 0;
$reservedPlayers = 0;
unset($winnerBracketData[$totalRounds]);
for($round = 1; $round <= $looserBracketRounds; $round++) {
foreach($winnerBracketData as $winnerBracket):
if($round == $winnerBracket['round']) {
$numLosers = count($winnerBracket['bracketData']);
break;
} else {
$numLosers = 0;
}
endforeach;
$divideCurrentSlots = false;
if ($round == 1) {
$emptySlots = ($numLosers / 2);
$winnersForNextRound = $emptySlots;
$previousRoundMatchesCount[$round] = $emptySlots;
$totalSlots = $emptySlots;
} else {
//First create slots of winners from previous round
//First get no of winners from previous round
$loserBracketWinners = $winnersForNextRound;
$winnerbracketLosers = $numLosers;
$reservePlayersPrevRound = $reservedPlayers;
//Create Matches From Loser bracket winners
$winnerSlots = $loserBracketWinners / 2;
$loserSlots = $winnerbracketLosers / 2;
$totalSlots = $winnerSlots + $loserSlots;
echo 'Round : ' . $round . ' ) <br />';
$slotsForThisRound = $previousRoundMatchesCount[$round - 1];
if(isset($previousRoundMatchesCount[$round - 1]) && isset($previousRoundMatchesCount[$round - 2])) {
$previousRoundSlots = $previousRoundMatchesCount[$round - 1];
$previoustwoRoundSlots = $previousRoundMatchesCount[$round - 2];
if($previousRoundSlots == $previoustwoRoundSlots) {
$slotsForThisRound = $previoustwoRoundSlots / 2;
}
}
$totalStandingPlayers = ($loserBracketWinners + $winnerbracketLosers + $reservedPlayers);
echo "Total Players Standing In This Round : " . $totalStandingPlayers . '<br />';
$returnData[$round]['availablePlayers'] = $totalStandingPlayers;
if($totalSlots != $slotsForThisRound) {
$reserveSlotForNextRound = $totalSlots - $slotsForThisRound;
$totalSlots = $totalSlots - $reserveSlotForNextRound;
$reservedPlayers = $numLosers;
}
$usedPlayersWinnerBracketCurrentRound = ($totalSlots * 2) - $loserBracketWinners - $reservePlayersPrevRound;
echo "Slots For This Round : " . $totalSlots . '<br />';
echo "Required Players For This Round : " . ($totalSlots * 2) . '<br />';
echo "Reserved Players From Previous Round : " . $reservePlayersPrevRound . '<br />';
echo "Reserved Players For Next Round : " . $reservedPlayers . '<br />';
echo "Players Used From Previous Round (Loser Bracket Winner) : " . $loserBracketWinners . '<br />';
echo "Players Used From current Round (Winner Bracket Loser) : " . $usedPlayersWinnerBracketCurrentRound . '<br />';
echo "Losers From winner bracket : " . $winnerbracketLosers . '<br />';
echo "Winners From Previous round : " . $loserBracketWinners . '<br />';
echo "<hr />";
$returnData[$round]['round'] = $round;
$returnData[$round]['slots'] = $totalSlots;
$returnData[$round]['reqPlayersCount'] = ($totalSlots * 2);
$returnData[$round]['pushedNextRound'] = $reservedPlayers;
$returnData[$round]['playersUsedCurrentRound'] = ($loserBracketWinners + $reservePlayersPrevRound) - ($totalSlots * 2);
$previousRoundMatchesCount[$round] = $totalSlots;
$winnersForNextRound = $totalSlots;
}
$numLosers = 0;
$key++;
}
return $roundToReturn;
}
Now when a match ends there is a function to create new match and save in the database I need to add based on which round that player needs to be moved in.