function createDataStructure() { global $db; // delete old structure $db->delete("tournamentencounters", "tournamentid=" . $this->tournamentId); // only start if there are 2 or more if (count($this->participants) < 2) { return false; } // make a list of all playerids $playerlist = array(); foreach ($this->participants as $participant) { $playerlist[] = $participant->getId(); } shuffle($playerlist); // calculate max, rounds and stuff $count = count($playerlist); $straightCount = (int) ceil($count / 2) * 2; $unevenCount = $straightCount != $count; $roundsMax = $straightCount - 1; $matchesPerRound = $straightCount / 2 - 1; $matches = array(); // set encounters for ($round = 0; $round < $roundsMax; $round++) { //$dummy = array(); $p1 = $playerlist[$round]; $p2 = $unevenCount ? "-1" : $playerlist[$roundsMax]; $curEnc = new EliminationEncounter($this->tournamentId, $round, 0, true); $curEnc->setPlayers($p1, $p2); if ($unevenCount) { $curEnc->setPoints(1, 0); } //$dummy[0]['player1'] = $playerlist[$round]; //$dummy[0]['player2'] = $unevenCount?"-1":$playerlist[$roundsMax]; for ($i = 1; $i <= $matchesPerRound; $i++) { $p1 = $playerlist[($round + $i) % $roundsMax]; $p2 = $playerlist[($round - $i + $roundsMax) % $roundsMax]; $curEnc = new EliminationEncounter($this->tournamentId, $round, $i, true); $curEnc->setPlayers($p1, $p2); } //$matches[] = $dummy; } $this->createMapCycle($roundsMax); return true; }
function createDataStructure() { global $db; $db->delete("tournamentencounters", "tournamentid=" . $this->tournamentId); if (count($this->participants) < 4) { return false; } $playerList = array(); foreach ($this->participants as $player) { $playerList[] = $player->getId(); } shuffle($playerList); $size = ceil(count($playerList) / 2) * 2; $gotafree = $size != count($playerList); for ($playernr = 0; $playernr < $size; $playernr += 2) { $curEnc = new EliminationEncounter($this->tournamentId, 0, $playernr / 2, true); $curEnc->setPlayers($playerList[$playernr], isset($playerList[$playernr + 1]) ? $playerList[$playernr + 1] : -1); } if ($gotafree) { $curEnc->setPoints(0, 0); $this->forwardPlayers($curEnc, 0, $curEnc->getId()); $this->setEncTimes(1); } $this->createMapCycle($this->numberOfRounds); $this->setEncTimes(0); return true; }
private function setEncPoints($roundId, $encId, $p1points, $p2points) { $enc = new EliminationEncounter($this->tournamentId, $roundId, $encId); if ($roundId < $this->roundCount() - 1) { // not in last round $nextEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, (int) $enc->getAlias() / 2); if ($this->thirdplayoff && $roundId == $this->roundCount() - 2) { $firstAtNext = $enc->getId() % 2 == 0; } else { $firstAtNext = $enc->getAlias() % 2 == 0; } } // check which player has won and forward this player $enc->setPoints($p1points, $p2points); if (isset($nextEnc)) { if ($enc->winner() == 1) { if ($firstAtNext) { $nextEnc->setPlayer1id($enc->getPlayer1id()); } else { $nextEnc->setPlayer2id($enc->getPlayer1id()); } } else { if ($firstAtNext) { $nextEnc->setPlayer1id($enc->getPlayer2id()); } else { $nextEnc->setPlayer2id($enc->getPlayer2id()); } } } // /w 3rd playoff forward loosers as well if ($this->thirdplayoff && $roundId == $this->roundCount() - 2) { $thirdPlayoffEnc = new EliminationEncounter($this->tournamentId, $roundId + 1, 1); if ($enc->winner() == 2) { if ($firstAtNext) { $thirdPlayoffEnc->setPlayer1id($enc->getPlayer1id()); } else { $thirdPlayoffEnc->setPlayer2id($enc->getPlayer1id()); } } else { if ($firstAtNext) { $thirdPlayoffEnc->setPlayer1id($enc->getPlayer2id()); } else { $thirdPlayoffEnc->setPlayer2id($enc->getPlayer2id()); } } // Check if there is a bye in the 3rd playoff if ($thirdPlayoffEnc->getPlayer1id() == -1 or $thirdPlayoffEnc->getPlayer2id() == -1) { $thirdPlayoffEnc->setPoints(0, 0); } } }
private function setEncPoints($roundId, $encId, $p1points, $p2points) { $enc = new EliminationEncounter($this->tournamentId, $roundId, $encId); 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 ($p2points >= $p1points && $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 global $db; global $notify; global $lang; $db->update('tournamentlist', "`state`=3", "`tournamentid`=" . $this->tournamentId); $notify->add($lang->get('tournament'), $lang->get('ntfy_end_game')); } } // check which player has won and forward this player $enc->setPoints($p1points, $p2points); if (isset($nextEnc)) { if ($enc->winner() == 1) { if ($firstAtNext) { $nextEnc->setPlayer1id($enc->getPlayer1id()); } else { $nextEnc->setPlayer2id($enc->getPlayer1id()); } } else { if ($firstAtNext) { $nextEnc->setPlayer1id($enc->getPlayer2id()); } else { $nextEnc->setPlayer2id($enc->getPlayer2id()); } } } if (isset($loserNextEnc)) { if ($enc->winner() == 1) { if ($loserFirstAtNext) { $loserNextEnc->setPlayer1id($enc->getPlayer2id()); } else { $loserNextEnc->setPlayer2id($enc->getPlayer2id()); } } else { if ($loserFirstAtNext) { $loserNextEnc->setPlayer1id($enc->getPlayer1id()); } else { $loserNextEnc->setPlayer2id($enc->getPlayer1id()); } } } // Forward looser if he has a bye if (isset($loserNextEnc)) { $p1id = $loserNextEnc->getPlayer1id(); $p2id = $loserNextEnc->getPlayer2id(); if ($p1id == -1 && $p2id > 0 || $p2id == -1 && $p1id > 0 || $p1id == -1 && $p2id == -1) { $loserNextEnc->setPoints(0, 0); $this->setEncPoints($roundId + 1, $loserNextEnc->getId(), 0, 0); } } }