function getAllNextEncounters()
{
    global $db;
    global $lang;
    $tournaments = $db->selectList("tournamentlist", "*", "`state`=2");
    $allNextEncs = array();
    foreach ($tournaments as $tourney) {
        switch ($tourney['mode']) {
            // Single Elimination
            case 1:
                // Double Elimination
            // Double Elimination
            case 2:
                // KotH
            // KotH
            case 6:
                $allEnc = $db->selectList("tournamentencounters", "*", "`tournamentid`=" . $tourney['tournamentid'] . " AND `state`=0 AND (`player1id`>0 OR `player2id`>0)");
                require_once "mod/default/tournament/tournament.eliminationencounter.class.php";
                foreach ($allEnc as $enc) {
                    $curEnc = new EliminationEncounter($enc['tournamentid'], $enc['roundid'], $enc['encounterid']);
                    $allNextEncs[] = array('time' => $curEnc->getEncTimeState(), 'round' => makeRoundNr($curEnc->getRoundId(), $tourney['mode']), 'encounterid' => $curEnc->getId(), 'player1id' => makePlayerUrl($curEnc->getPlayer1Id(), $tourney['playerperteam'] == 1), 'player2id' => makePlayerUrl($curEnc->getPlayer2Id(), $tourney['playerperteam'] == 1), 'title' => $tourney['title']);
                }
                break;
                // Groups
            // Groups
            case 3:
                $allEnc = $db->selectList("tournamentgroupenc", "*", "`tournamentid`=" . $tourney['tournamentid'] . " GROUP BY `round`, `group` HAVING MIN(rank)=0");
                foreach ($allEnc as $enc) {
                    $participants = $db->selectList("tournamentgroupenc", "*", "`tournamentid`=" . $enc['tournamentid'] . " AND `round`=" . $enc['round'] . " AND `group`=" . $enc['group']);
                    $allParticipants = array();
                    foreach ($participants as $participant) {
                        $allParticipants[] = makePlayerUrl($participant['participantid']);
                    }
                    $encounterUrl = makeHTMLUrl($lang->get('group') . " " . chr(65 + $enc['group']), makeURL('tournament', array('mode' => 'table', 'tournamentid' => $enc['tournamentid'], 'action' => 'submit', 'group' => $enc['group'], 'round' => $enc['round'])));
                    $allNextEncs[] = array('time' => '', 'round' => makeRoundNr($enc['round'] - 1, $tourney['mode']), 'encounterid' => $enc['group'], 'player1id' => $encounterUrl, 'player2id' => '', 'title' => $tourney['title'], 'participants' => $allParticipants);
                }
                break;
                // Points
            // Points
            case 4:
                break;
                // Randomize
            // Randomize
            case 5:
                break;
        }
    }
    usort($allNextEncs, "sortNextEncounters");
    return $allNextEncs;
}
 public function undoSetEncounter($roundId, $encId)
 {
     $enc = new EliminationEncounter($this->tournamentId, $roundId, $encId, false);
     // do not allow encounters with byes to be reset
     if ($enc->getPlayer1id() < 0 || $enc->getPlayer2id() < 0) {
         return false;
     }
     // do not create encounters if it is the final match
     if ($roundId <= $this->roundCount() - 2) {
         $nextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, (int) $enc->getAlias() / 2, false);
         if (!$this->isNextEncUndoable($nextEnc)) {
             return false;
         }
         $resetThird = false;
         if ($this->thirdplayoff && $roundId == $this->roundCount() - 2) {
             $resetThird = true;
             $thirdPlayoff = new EliminationEncounter($this->tournamentId, $roundId + 1, 1, false);
             if (!$this->isNextEncUndoable($thirdPlayoff)) {
                 return false;
             }
         }
         // reset encounters
         $firstAtNext = $enc->getAlias() % 2 == 0;
         if ($firstAtNext) {
             $nextEnc->setPlayer1id(0);
             if ($resetThird) {
                 $thirdPlayoff->setPlayer1id(0);
             }
         } else {
             $nextEnc->setPlayer2id(0);
             if ($resetThird) {
                 $thirdPlayoff->setPlayer2id(0);
             }
         }
     }
     $enc->unsetPoints();
     return true;
 }
 function encounter($roundId, $encId)
 {
     global $breadcrumbs, $template_dir, $smarty, $tournamentList, $notify, $lang;
     $tournament = $tournamentList->getTournament($this->tournamentId);
     $smarty->assign('tournament', $tournament);
     $enc = new EliminationEncounter($this->tournamentId, $roundId, $encId);
     foreach ($this->participants as $participant) {
         if ($participant->getId() == $enc->getPlayer1id()) {
             $player1 = $participant;
         }
         if ($participant->getId() == $enc->getPlayer2id()) {
             $player2 = $participant;
         }
     }
     if (!isset($player1) || !isset($player2)) {
         $notify->add($lang->get('encounter'), $lang->get('ntfy_submit_err_player'));
     } else {
         $breadcrumbs->addElement($player1->getName() . " vs. " . $player2->getName(), makeURL('tournament', array('tournamentid' => $_GET['tournamentid'], 'encid' => $_GET['encid'], 'roundid' => $_GET['roundid'], 'mode' => 'view')));
         $smarty->assign('path', $template_dir . "/eliminationencounter_detail.tpl");
         $smarty->assign('player1', array('name' => $player1->getName(), 'url' => $player1->getUrl()));
         $smarty->assign('player2', array('name' => $player2->getName(), 'url' => $player2->getUrl()));
         $enc_arr['points1'] = $enc->getPoints1();
         $enc_arr['points2'] = $enc->getPoints2();
         $enc_arr['finished'] = $enc->isFinished();
         $enc_arr['winner'] = $enc->getPoints1() > $enc->getPoints2() ? 1 : 2;
         $enc_arr['map'] = $this->getMap($_GET['roundid'] + 1);
         if ($enc->getStart() != 0) {
             $enc_arr['start'] = $enc->getStart() > time() ? timeLeft($enc->getStart()) : formatTime($enc->getStart());
             $tEnd = $enc->getStart() + $enc->getDuration();
             $enc_arr['end'] = $enc->getStart() < time() && $tEnd > time() ? timeLeft($tEnd) : formatTime($tEnd);
             $enc_arr['duration'] = $enc->getDuration();
         }
         $smarty->assign('enc', $enc_arr);
         $smarty->assign('user_can_submit', $this->checkSubmitRights($enc, $player1, $player2));
     }
     $smarty->assign('path', $template_dir . "/eliminationencounter_detail.tpl");
     $smarty->assign('_GET', $_GET);
 }
 public function undoSetEncounter($roundId, $encId)
 {
     $enc = new EliminationEncounter($this->tournamentId, $roundId, $encId, false);
     // do not allow encounters with byes to be reset
     if ($enc->getPlayer1id() < 0 || $enc->getPlayer2id() < 0) {
         return false;
     }
     if ($roundId < $this->lastRound() - 1) {
         // normal and looser tier not merged yet
         if ($enc->getId() >= $this->lowerEncOffset()) {
             // in looser tier
             if ($roundId == $this->lastRound() - 2) {
                 // winner will get in first final match
                 $nextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, 0);
                 $firstAtNext = false;
             } else {
                 if ($roundId % 2 == 0) {
                     $nextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, $enc->getId());
                     $firstAtNext = false;
                 } else {
                     $nextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, (int) $enc->getAlias() / 2 + $this->lowerEncOffset());
                     $firstAtNext = $enc->getAlias() % 2 == 0;
                 }
             }
         } else {
             // in winner tier
             if ($roundId == 1) {
                 $nextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, (int) $enc->getAlias() / 2);
                 $firstAtNext = $enc->getAlias() % 2 == 0;
                 $loserNextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, (int) $enc->getAlias() / 2 + $this->lowerEncOffset());
                 $loserFirstAtNext = $enc->getAlias() % 2 == 0;
             } else {
                 $nextEnc = new EliminationEncounter($this->tournamentId, $roundId + 2, (int) $enc->getAlias() / 2);
                 $firstAtNext = $enc->getAlias() % 2 == 0;
                 $loserNextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, (int) $enc->getAlias() + $this->lowerEncOffset());
                 $loserFirstAtNext = true;
             }
         }
     } else {
         if ($roundId == $this->lastRound() - 1) {
             // create second final match
             $nextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, 0, false);
             $loserNextEnc = $nextEnc;
             $firstAtNext = true;
             $loserFirstAtNext = false;
         } else {
             // final match
         }
     }
     // Check if the encounters are undoable
     if (isset($nextEnc)) {
         if (!$this->isNextEncUndoable($nextEnc)) {
             return false;
         }
     }
     if (isset($loserNextEnc)) {
         if (!$this->isNextEncUndoable($loserNextEnc)) {
             return false;
         }
     }
     if (isset($nextEnc)) {
         if ($firstAtNext) {
             $nextEnc->setPlayer1id(0);
         } else {
             $nextEnc->setPlayer2id(0);
         }
     }
     if (isset($loserNextEnc)) {
         if ($loserFirstAtNext) {
             $loserNextEnc->setPlayer1id(0);
         } else {
             $loserNextEnc->setPlayer2id(0);
         }
     }
     $enc->unsetPoints();
     return true;
 }