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; }
function createDataStructure() { global $db; global $notify; global $lang; // if there was a structure built before, delete it $db->delete("tournamentencounters", "tournamentid=" . $this->tournamentId); // only start game if more than 4 players participate if (count($this->participants) < 4) { return false; } // make a list containing of 2^n elements where n is the number of // rounds. also shuffle the players $size = pow(2, $this->roundCount()); $playerList = array(); foreach ($this->participants as $player) { $playerList[] = $player->getId(); } shuffle($playerList); $firstRoundEncounters = array(); $secondRoundPlayers = array(); $aliases = $this->createPossibleAliases(1); for ($i = 0; $i < $size / 2; $i++) { //Put a player into every game $firstRoundEncounters[$i][0] = $playerList[$i]; $firstRoundEncounters[$i]['alias'] = $aliases[$i]; } for ($i = $size / 2; $i < $size; $i++) { if ($i < count($playerList)) { $firstRoundEncounters[$i - $size / 2][1] = $playerList[$i]; } else { $firstRoundEncounters[$i - $size / 2][1] = -1; $secondRoundPlayers[] = array($firstRoundEncounters[$i - $size / 2][0], $firstRoundEncounters[$i - $size / 2]['alias'], (int) $i - $size / 2); } } //first Round foreach ($firstRoundEncounters as $encID => $encounter) { $curEnc = new EliminationEncounter($this->tournamentId, 0, $encID, true); $curEnc->setPlayers($encounter[0], $encounter[1]); if ($encounter[0] == -1 || $encounter[1] == -1) { $curEnc->setPoints(0, 0); } $curEnc->setAlias($encounter['alias']); } $aliases = $this->createPossibleAliases(2); //second Round $secondRoundEncounters = array(); //Put the winning players in their encounter foreach ($secondRoundPlayers as $players) { $secondRoundEncounters[(int) $players[1] / 2][(int) $players[1] % 2] = $players[0]; } // Create 2nd round matches for ($i = 0; $i < $size / 4; $i++) { $curEnc = new EliminationEncounter($this->tournamentId, 1, $i, true); $curEnc->setPlayers(0, 0); $curEnc->setAlias($aliases[$i]); } //Update second round encounters foreach ($secondRoundEncounters as $encID => $encounter) { $curEnc = new EliminationEncounter($this->tournamentId, 1, $encID, false); $curEnc->setPlayers(isset($encounter[0]) ? $encounter[0] : 0, isset($encounter[1]) ? $encounter[1] : 0); $curEnc->setAlias($aliases[$encID]); } //Create the remaining matches for ($round = 2; $round < $this->roundCount(); $round++) { $aliases = $this->createPossibleAliases($round + 1); for ($encID = 0; $encID < pow(2, $this->roundCount() - $round - 1); $encID++) { $curEnc = new EliminationEncounter($this->tournamentId, $round, $encID, true); $curEnc->setPlayers(0, 0); // prevent endgame and 3d playoff from shuffling if ($round < $this->roundCount() - 1) { $curEnc->setAlias($aliases[$encID]); } } } // /w 3rd playoff if ($this->thirdplayoff) { $curEnc = new EliminationEncounter($this->tournamentId, $this->roundCount() - 1, 1, true); if ($this->roundCount() > 2 || count($this->participants) > 3) { $curEnc->setPlayers(0, 0); } else { $curEnc->setPlayers(0, -1); } } $this->createMapCycle($this->roundCount()); $this->setEncTimes(); return true; }
function createDataStructure() { global $db; // if there was a structure built before, delete it $db->delete(MYSQL_TABLE_PREFIX . "tournamentencounters", "tournamentid=" . $this->tournamentId); unset($this->firstRoundParts); unset($this->lowerTreeEncOffset); if (count($this->participants) < 4) { return false; } $size = $this->numberOfEncounters(1, DOUBLEELIMINATION_UPPER); $playerList = array(); foreach ($this->participants as $player) { $playerList[] = $player->getId(); } shuffle($playerList); // create round 1 - 3, fill with participants and dummies // get some stuff $firstRoundEncounters = array(); $secondRoundUpper = array(); $secondRoundLower = array(); $thirdRoundLower = array(); $aliases = $this->createPossibleAliases(1, DOUBLEELIMINATION_UPPER); $aliases2u = $this->createPossibleAliases(2, DOUBLEELIMINATION_UPPER); $aliases2l = $this->createPossibleAliases(2, DOUBLEELIMINATION_LOWER); $aliases3l = $this->createPossibleAliases(3, DOUBLEELIMINATION_LOWER); // initialize rounds 2 and 3 (1 will be initialized when filling) for ($i = 0; $i < $size / 2; $i++) { $secondRoundUpper[$i] = new EliminationEncounter($this->tournamentId, 2, $i, TRUE); $secondRoundUpper[$i]->setPlayer1Id(0); $secondRoundUpper[$i]->setPlayer2Id(0); $secondRoundUpper[$i]->setAlias($aliases2u[$i]); $secondRoundLower[$i] = new EliminationEncounter($this->tournamentId, 2, $i + $this->lowerEncOffset(), TRUE); $secondRoundLower[$i]->setPlayer1Id(0); $secondRoundLower[$i]->setPlayer2Id(0); $secondRoundLower[$i]->setAlias($aliases2l[$i]); $thirdRoundLower[$i] = new EliminationEncounter($this->tournamentId, 3, $i + $this->lowerEncOffset(), TRUE); $thirdRoundLower[$i]->setPlayer1Id(0); $thirdRoundLower[$i]->setPlayer2Id(0); $thirdRoundLower[$i]->setAlias($aliases3l[$i]); } // initialize and fill first players of round 1 for ($i = 0; $i < $size; $i++) { $firstRoundEncounters[$i] = new EliminationEncounter($this->tournamentId, 1, $i, TRUE); $firstRoundEncounters[$i]->setPlayer1Id($playerList[$i]); unset($playerList[$i]); } // shuffle remaining participants and dummies for ($i = count($this->participants); $i < $this->firstRoundPartCount(); $i++) { $playerList[] = -1; } shuffle($playerList); // fill second players of round 1 for ($i = 0; $i < $size; $i++) { $firstRoundEncounters[$i]->setPlayer2Id($playerList[$i]); // advance dummy players if ($playerList[$i] == -1) { $firstRoundEncounters[$i]->setPoints(0, 0); if ($i % 2 == 0) { $secondRoundUpper[$i / 2]->setPlayer1Id($firstRoundEncounters[$i]->getPlayer1Id()); $secondRoundLower[$i / 2]->setPlayer1Id(-1); $addThird = $secondRoundLower[$i / 2]->getPlayer2Id() == -1; if ($addThird) { $secondRoundLower[$i / 2]->setPoints(0, 0); } } else { $secondRoundUpper[($i - 1) / 2]->setPlayer2Id($firstRoundEncounters[$i]->getPlayer1Id()); $secondRoundLower[($i - 1) / 2]->setPlayer2Id(-1); $addThird = $secondRoundLower[($i - 1) / 2]->getPlayer1Id() == -1; if ($addThird) { $secondRoundLower[($i - 1) / 2]->setPoints(0, 0); } } if ($addThird) { $thirdRoundLower[floor($i / 2)]->setPlayer2Id(-1); } } } // create remaining empty encounters for ($i = 4; $i <= $this->lastRound(); $i++) { for ($j = 0; $j < $this->numberOfEncounters($i, DOUBLEELIMINATION_UPPER); $j++) { $enc = new EliminationEncounter($this->tournamentId, $i, $j, TRUE); $enc->setPlayers(0, 0); $enc->setAlias($j); } for ($j = 0; $j < $this->numberOfEncounters($i, DOUBLEELIMINATION_LOWER); $j++) { $enc = new EliminationEncounter($this->tournamentId, $i, $j + $this->lowerEncOffset(), TRUE); $enc->setPlayers(0, 0); $enc->setAlias($j); } } $this->createMapCycle($this->lastRound()); $this->setEncTimes(); return true; }