Ejemplo n.º 1
0
 public static function generate(array $params)
 {
     $active = array_shift($params) !== 'past';
     $brackets = Lib\Cache::fetch(function () use($active) {
         $allBrackets = Api\Bracket::getAll();
         // Filter out active/completed brackets
         $brackets = [];
         foreach ($allBrackets as $bracket) {
             if ($active && ($bracket->state == BS_ELIMINATIONS || $bracket->state == BS_VOTING || $bracket->state == BS_NOMINATIONS)) {
                 $bracket->title = Api\Round::getBracketTitleForActiveRound($bracket);
                 $brackets[] = $bracket;
             }
             if (!$active && $bracket->state == BS_FINAL) {
                 $brackets[] = $bracket;
             }
         }
         // Check for card images
         foreach ($brackets as $bracket) {
             if (is_readable('./images/bracket_' . $bracket->id . '_card.jpg')) {
                 $bracket->cardImage = '/images/bracket_' . $bracket->id . '_card.jpg';
             } else {
                 $bracket->entrants = Api\Character::getRandomCharacters($bracket, 9);
             }
         }
         return $brackets;
     }, 'Controller::Brackets_displayBrackets_' . ($active ? 'active' : 'completed'));
     Lib\Display::addKey('page', 'brackets');
     $title = $active ? 'Current Brackets' : 'Past Brackets';
     Lib\Display::renderAndAddKey('content', 'bracketsView', ['brackets' => $brackets, 'title' => $title]);
 }
Ejemplo n.º 2
0
 /**
  * Gets a test bucket value for the given seed
  */
 public static function get($key, $seed = null)
 {
     self::initialize();
     $seed = $seed ?: self::$_seed;
     $cacheKey = 'test_' . $key . '_' . $seed;
     return Cache::fetch(function () use($key, $seed) {
         $retVal = DEFAULT_TEST_VALUE;
         $found = false;
         if (isset(self::$_tests->{$key})) {
             $test = self::$_tests->{$key};
             if (isset($test->whiteLists)) {
                 foreach ($test->whiteLists as $whiteList) {
                     if (in_array($seed, $whiteList->ids)) {
                         $retVal = $whiteList->value;
                         $found = true;
                         break;
                     }
                 }
             }
             if (!$found && isset($test->ramps)) {
                 srand($seed);
                 $rand = rand() % 100;
                 $percentTotal = 0;
                 foreach ($test->ramps as $ramp) {
                     $percentTotal += $ramp->percent;
                     if ($rand <= $percentTotal) {
                         $retVal = $ramp->value;
                         break;
                     }
                 }
             }
         }
         return $retVal;
     }, $cacheKey);
 }
Ejemplo n.º 3
0
 public static function generate(array $params)
 {
     $user = self::_checkLogin();
     self::_enableAd();
     $perma = array_shift($params);
     $bracket = Api\Bracket::getBracketByPerma($perma);
     if ($bracket->start <= time() && ($bracket->state == BS_ELIMINATIONS || $bracket->state == BS_VOTING || $bracket->state == BS_WILDCARD)) {
         $cacheKey = 'CurrentRound_' . $bracket->id . '_' . $user->id;
         $out = Lib\Cache::fetch(function () use($user, $bracket) {
             $out = new stdClass();
             $out->userId = $user->id;
             $out->round = Api\Round::getCurrentRounds($bracket->id);
             $out->title = Api\Round::getBracketTitleForActiveRound($bracket);
             return $out;
         }, $cacheKey, CACHE_MEDIUM);
         if ($out) {
             $out->bracket = $bracket;
             $template = $out->bracket->state == BS_ELIMINATIONS ? 'eliminations' : 'voting';
             if ($bracket->state != BS_ELIMINATIONS) {
                 $entrantSwap = Lib\TestBucket::get('entrantSwap');
                 if ($entrantSwap !== 'control') {
                     foreach ($out->round as $round) {
                         // Interesting side effect that I had not considered before:
                         // When TestBucket initializes, it's setting the random seed for the entire RNG (duh).
                         // That means the following random line will produce a static set of results, so the
                         // user experience won't be wonky.
                         if ($entrantSwap === 'flip' || $entrantSwap === 'random' && rand() % 2 === 0) {
                             $round = self::_flipEntrants($round);
                         }
                     }
                 }
             }
             Lib\Display::addKey('page', 'vote');
             Lib\Display::addKey('title', $bracket->name . ' - Voting' . DEFAULT_TITLE_SUFFIX);
             Lib\Display::renderAndAddKey('content', $template, $out);
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Returns random completed rounds
  */
 public static function getRandomCompletedRounds($count)
 {
     $count = is_numeric($count) ? $count : 10;
     return Lib\Cache::fetch(function () use($count) {
         $retVal = null;
         $query = 'SELECT * FROM `round` WHERE round_final = 1 AND round_tier > 0 AND round_character2_id > 1 ORDER BY RAND() LIMIT ' . $count;
         return self::_getRoundsAndCharacters($query);
     }, 'Round::getRandomCompletedRounds_' . $count, CACHE_LONG * 24);
 }
Ejemplo n.º 5
0
 /**
  * Returns a random number of characters from bracket
  */
 public static function getRandomCharacters(Bracket $bracket, $count)
 {
     if (is_numeric($count)) {
         return Lib\Cache::fetch(function () use($bracket, $count) {
             $retVal = [];
             $result = Lib\Db::Query('SELECT * FROM `character` WHERE bracket_id = :id ORDER BY RAND() LIMIT ' . $count, [':id' => $bracket->id]);
             if ($result && $result->count) {
                 while ($row = Lib\Db::Fetch($result)) {
                     $retVal[] = new Character($row);
                 }
             }
             return $retVal;
         }, 'Api::Character_getRandomCharacters_' . $bracket->id . '_' . $count);
     }
     return [];
 }
Ejemplo n.º 6
0
 /**
  * Does fuzzy searching for characters within a bracket
  */
 public static function searchBracketNominees($query, $bracketId)
 {
     return Lib\Cache::fetch(function () use($query, $bracketId) {
         $retVal = [];
         $params = [':bracketId' => $bracketId, ':queryA' => $query . '%', ':queryB' => '% ' . $query . '%'];
         $result = Lib\Db::Query('SELECT * FROM `nominee` WHERE bracket_id = :bracketId AND (nominee_name LIKE :queryA OR nominee_name LIKE :queryB)', $params);
         if ($result && $result->count) {
             while ($row = Lib\Db::Fetch($result)) {
                 $retVal[] = new Nominee($row);
             }
         }
         return $retVal;
     }, 'Api::Nominee_searchBracketNominees_' . $query . '_' . $bracketId, CACHE_MEDIUM);
 }