/** * Roast DE Matches for a Tournament * @param KTournament $tournament * @param bool $forced * @return string */ public function roastDoubleElimination(KTournament $tournament, $forced = false) { $tour = $tournament; //If tournament can show brackets if (!$tour->canShowBrackets()) { return "Tournament {$tour->id} cannot show bracket\n"; } // if already roasted if ($tour->matches()->first() && $forced == false) { return "Tournament {$tour->id} already roasted\n"; } // If forced then deleted the roasted matches first if ($forced == true) { $tour->matches()->delete(); } // Turn to not eligible all teams having less than minimum required players. foreach ($tour->teams()->qualified()->get() as $teamx) { if ($teamx->isFull()) { continue; } $teamx->team_status = 3; //Not Eligible $teamx->save(); } // Team taking PART (only their respected IDs) $competitors = $tour->teams()->qualified()->get(['id'])->lists('id')->toArray(); // Total no. of rounds to be played $rounds = log(count($competitors), 2) + 1; // round one for ($i = 0; $i < log(count($competitors), 2); $i++) { $seeded = array(); foreach ($competitors as $competitor) { $splice = pow(2, $i); $seeded = array_merge($seeded, array_splice($competitors, 0, $splice)); $seeded = array_merge($seeded, array_splice($competitors, -$splice)); } $competitors = $seeded; } $events = array_chunk($seeded, 2); if ($rounds > 2) { $round_index = count($events); // second round for ($i = 0; $i < count($competitors) / 2; $i++) { array_push($events, array(array('from_event_index' => $i, 'from_event_rank' => 1), array('from_event_index' => ++$i, 'from_event_rank' => 1))); } $round_matchups = array(); for ($i = 0; $i < count($competitors) / 2; $i++) { array_push($round_matchups, array(array('from_event_index' => $i, 'from_event_rank' => 2), array('from_event_index' => ++$i, 'from_event_rank' => 2))); } $events = array_merge($events, $round_matchups); for ($i = 0; $i < count($round_matchups); $i++) { array_push($events, array(array('from_event_index' => $i + count($competitors) / 2, 'from_event_rank' => 2), array('from_event_index' => $i + count($competitors) / 2 + count($competitors) / 2 / 2, 'from_event_rank' => 1))); } } if ($rounds > 3) { // subsequent rounds for ($i = 0; $i < $rounds - 3; $i++) { $round_events = pow(2, $rounds - 3 - $i); $total_events = count($events); for ($j = 0; $j < $round_events; $j++) { array_push($events, array(array('from_event_index' => $j + $round_index, 'from_event_rank' => 1), array('from_event_index' => ++$j + $round_index, 'from_event_rank' => 1))); } for ($j = 0; $j < $round_events; $j++) { array_push($events, array(array('from_event_index' => $j + $round_index + $round_events * 2, 'from_event_rank' => 1), array('from_event_index' => ++$j + $round_index + $round_events * 2, 'from_event_rank' => 1))); } for ($j = 0; $j < $round_events / 2; $j++) { array_push($events, array(array('from_event_index' => $j + $total_events, 'from_event_rank' => 2), array('from_event_index' => $j + $total_events + $round_events / 2, 'from_event_rank' => 1))); } $round_index = $total_events; } } if ($rounds > 1) { // finals array_push($events, array(array('from_event_index' => count($events) - 3, 'from_event_rank' => 1), array('from_event_index' => count($events) - 1, 'from_event_rank' => 1))); } // Now save this to DB $starts_at_glob = $tournament->tournament_starts_at->addHour(1); $i = 0; foreach (array_chunk($events, 2) as $eventx) { foreach ($eventx as $event) { //Create new Match; $match = new KMatch(); // Time $match->starts_at = $starts_at_glob; $starts_at_glob->addHour(2); //Adding of TEAM-1 of Match $team1 = $event[0]; //If team is directly given ID -- in Round 1 generally if (!is_array($team1)) { $match->k_team1_id = $team1; } else { $match->team1_from_match_rank = $team1['from_event_rank']; $match->team1_from_match_index = $team1['from_event_index']; } //Adding of TEAM-2 of Match $team2 = $event[1]; //If team is directly given ID -- in Round 1 generally if (!is_array($team2)) { $match->k_team2_id = $team2; } else { $match->team2_from_match_rank = $team2['from_event_rank']; $match->team2_from_match_index = $team2['from_event_index']; } $match->match_index = $i++; $tournament->matches()->create($match->toArray()); } $starts_at_glob = $starts_at_glob->addDay(); $starts_at_glob = $starts_at_glob->subHour(4); } return "New DE tournaments roasted!\n"; }