Example #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]);
 }
Example #2
0
 public static function generate(array $params)
 {
     $bracket = Api\Bracket::getBracketByPerma(array_shift($params));
     if ($bracket) {
         Lib\Display::addKey('page', 'characters');
         $content = Lib\Display::renderAndAddKey('content', 'characters', (object) ['bracket' => $bracket, 'characters' => Api\Character::getByBracketId($bracket->id)]);
     }
 }
 public static function generate(array $params)
 {
     $bracket = Api\Bracket::getBracketByPerma(array_shift($params));
     if ($bracket) {
         Lib\Display::addKey('page', 'characters');
         Lib\Display::addKey('title', $bracket->name . ' Entrants' . DEFAULT_TITLE_SUFFIX);
         $hasSource = $bracket->hasSourceLabel();
         $hasSeed = $bracket->state == BS_VOTING || $bracket->state == BS_FINAL;
         $content = Lib\Display::renderAndAddKey('content', 'characters', (object) ['bracket' => $bracket, 'characters' => Api\Character::getByBracketId($bracket->id), 'hasSource' => $hasSource, 'hasSeed' => $hasSeed, 'hasSorter' => $hasSource || $hasSeed]);
     }
 }
Example #4
0
 public static function generate(array $params)
 {
     $perma = array_shift($params);
     $bracket = Api\Bracket::getBracketByPerma($perma);
     if ($bracket) {
         // TODO - get full voting stats
         $entrantStats = Api\Stats::getEntrantPerformanceStats($bracket);
         Lib\Display::addKey('page', 'stats');
         Lib\Display::addKey('title', 'Stats for ' . $bracket->name . DEFAULT_TITLE_SUFFIX);
         Lib\Display::renderAndAddKey('content', 'stats', ['entrants' => $entrantStats, 'bracket' => $bracket]);
     }
 }
Example #5
0
 public static function generate(array $params)
 {
     // Create the bracket on POST
     if ($_POST) {
         $name = Lib\Url::Post('name');
         $rules = Lib\Url::Post('rules');
         if ($name && $rules) {
             $bracket = new Api\Bracket();
             $bracket->name = trim($name);
             $bracket->rules = $rules;
             $bracket->state = 0;
             $bracket->start = time();
             $bracket->generatePerma();
             $bracket->nameLabel = Lib\Url::Post('nameLabel');
             $bracket->minAge = Lib\Url::Post('minAge', true);
             $hideSource = Lib\Url::Post('hideSource') === 'on';
             $bracket->sourceLabel = $hideSource ? 'NO_SOURCE' : Lib\Url::Post('sourceLabel');
             $advanceHour = Lib\Url::Post('advanceHour', true);
             if ($advanceHour !== null) {
                 $utcOffset = Lib\Url::Post('utcOffset', true);
                 $advanceHour += $utcOffset !== null ? $utcOffset : 0;
             } else {
                 $advanceHour = -1;
             }
             $bracket->advanceHour = $advanceHour;
             if ($bracket->sync()) {
                 $bracket->addUser(self::$_user);
                 self::_refreshCaches();
                 // Clear the generic bracket related caches
                 header('Location: /me/?created');
                 exit;
             }
         }
     }
     // Or display the form
     $_POST['times'] = self::_generateAdvanceTimes();
     $_POST['ages'] = self::_generateAges(REDDIT_MINAGE);
     Lib\Display::renderAndAddKey('content', 'admin/bracket', $_POST);
 }
Example #6
0
 public static function generate(array $params)
 {
     $perma = array_shift($params);
     $bracket = Api\Bracket::getBracketByPerma($perma);
     if ($bracket) {
         $bracket->results = $bracket->getResults();
         $user = Api\User::getCurrentUser();
         if ($user) {
             $bracket->userVotes = $bracket->getVotesForUser($user);
         }
         Lib\Display::addKey('page', 'results');
         Lib\Display::renderAndAddKey('content', 'results', $bracket);
     }
 }
Example #7
0
 public static function generate(array $params)
 {
     self::_checkLogin();
     $bracket = Api\Bracket::getBracketByPerma(array_shift($params));
     self::_enableAd();
     if ($bracket) {
         $bracket->nameLabel = $bracket->nameLabel ?: 'Character name';
         $bracket->sourceLabel = $bracket->sourceLabel ?: 'Source';
         $bracket->sourceLabel = $bracket->sourceLabel === 'NO_SOURCE' ? false : $bracket->sourceLabel;
         $out = (object) ['rules' => Lib\Michelf\Markdown::defaultTransform($bracket->rules), 'bracket' => $bracket];
         Lib\Display::addKey('page', 'nominate');
         Lib\Display::renderAndAddKey('content', 'nominate', $out);
     }
 }
Example #8
0
 private static function _getBracketCharacters()
 {
     $retVal = null;
     $bracketId = Lib\Url::GetInt('bracketId', null);
     $count = Lib\Url::GetInt('count', null);
     if ($bracketId) {
         //If $count has a value, get random characters from the given bracket
         if ($count) {
             $bracket = \Api\Bracket::getById($bracketId);
             if ($bracket) {
                 //3 levels of IFs. This is getting rediculous
                 $retVal = \Api\Character::getRandomCharacters($bracket, $count);
             }
         } else {
             $retVal = \Api\Character::getByBracketId($bracketId);
         }
     }
     return $retVal;
 }
Example #9
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);
         }
     }
 }
Example #10
0
 /**
  * Gets other characters in this bracket with similar names (checks for Japanese and Western naming order)
  */
 public static function getBySimilarName($name)
 {
     $retVal = null;
     $query = 'SELECT * FROM `character` WHERE ';
     $params = [];
     $name = explode(' ', $name);
     if (count($name) === 2) {
         $params[':nameA'] = $name[0] . '%' . $name[1];
         $params[':nameB'] = $name[1] . '%' . $name[0];
         $query .= '(character_name LIKE :nameA OR character_name LIKE :nameB)';
     } else {
         $params[':name'] = implode('%', $name);
         $query .= 'character_name LIKE :name';
     }
     $result = Lib\Db::Query($query, $params);
     if ($result && $result->count) {
         $retVal = [];
         while ($row = Lib\Db::Fetch($result)) {
             $obj = new Character($row);
             $obj->bracket = Bracket::getById($obj->bracketId);
             $retVal[] = $obj;
         }
     }
     return $retVal;
 }
Example #11
0
 private static function _vote($user)
 {
     $out = new stdClass();
     $out->success = false;
     $bracketId = Lib\Url::Post('bracketId', true);
     $bracket = Api\Bracket::getById($bracketId);
     if ($bracket) {
         $state = $bracket ? (int) $bracket->state : null;
         if ($bracket->isLocked()) {
             $out->message = 'Voting is closed for this round. Please refresh to see the latest round.';
         } else {
             if ($state === BS_ELIMINATIONS || $state === BS_VOTING) {
                 if (self::_verifyAccountAge($user, $bracket)) {
                     // Break the votes down into an array of round/character objects
                     $votes = [];
                     foreach ($_POST as $key => $val) {
                         if (strpos($key, 'round:') === 0) {
                             $key = str_replace('round:', '', $key);
                             $obj = new stdClass();
                             $obj->roundId = (int) $key;
                             $obj->characterId = (int) $val;
                             $votes[] = $obj;
                         }
                     }
                     $count = count($votes);
                     if ($count > 0) {
                         $query = 'INSERT INTO `votes` (`user_id`, `vote_date`, `round_id`, `character_id`, `bracket_id`) VALUES ';
                         $params = [':userId' => $user->id, ':date' => time(), ':bracketId' => $bracketId];
                         $insertCount = 0;
                         // Only run an insert for rounds that haven't been voted on
                         $rounds = Api\Votes::getOpenRounds($user, $votes);
                         for ($i = 0; $i < $count; $i++) {
                             if (!isset($rounds[$votes[$i]->roundId])) {
                                 $query .= '(:userId, :date, :round' . $i . ', :character' . $i . ', :bracketId),';
                                 $params[':round' . $i] = $votes[$i]->roundId;
                                 $params[':character' . $i] = $votes[$i]->characterId;
                                 $insertCount++;
                                 $rounds[$votes[$i]->roundId] = true;
                             }
                         }
                         if ($insertCount > 0) {
                             $query = substr($query, 0, strlen($query) - 1);
                             if (Lib\Db::Query($query, $params)) {
                                 $out->success = true;
                                 // I am vehemently against putting markup in the controller, but there's much refactor needed to make this right
                                 // So, that's a note that it will be changed in the future
                                 $out->message = 'Your votes were successfully submitted! <a href="/results/' . $bracket->perma . '">View Results</a>';
                                 // Oops, I did it again...
                                 if ($bracket->externalId) {
                                     $out->message .= ' or <a href="http://redd.it/' . $bracket->externalId . '" target="_blank">discuss on reddit</a>.';
                                 }
                                 // Clear any user related caches
                                 $round = Api\Round::getById($votes[0]->roundId);
                                 Lib\Cache::Set('GetBracketRounds_' . $bracketId . '_' . $round->tier . '_' . $round->group . '_' . $user->id, false);
                                 Lib\Cache::Set('GetBracketRounds_' . $bracketId . '_' . $round->tier . '_all_' . $user->id, false);
                                 Lib\Cache::Set('CurrentRound_' . $bracketId . '_' . $user->id, false);
                                 $bracket->getVotesForUser($user, true);
                             } else {
                                 $out->message = 'There was an unexpected error. Please try again in a few moments.';
                             }
                         } else {
                             $out->message = 'Voting for this round has closed';
                             $out->code = 'closed';
                         }
                     } else {
                         $out->message = 'No votes were submitted';
                     }
                 } else {
                     $out->message = 'Your reddit account is not old enough to vote in this bracket';
                 }
             } else {
                 $out->message = 'Voting is closed on this bracket';
                 $out->code = 'closed';
             }
         }
     } else {
         $out->message = 'Invalid parameters';
     }
     return $out;
 }
Example #12
0
 /**
  * Refreshes various generic caches. This is expensive; use sparingly
  */
 protected static function _refreshCaches(Api\Bracket $bracket = null)
 {
     Lib\Cache::setDisabled(true);
     // Refresh the main collections
     Api\Bracket::getAll();
     Api\Bracket::getUserOwnedBrackets(self::$_user);
     \Controller\Brackets::generate(['past']);
     \Controller\Brackets::generate([]);
     // Refresh a single bracket if specified
     if ($bracket) {
         Api\Bracket::getBracketByPerma($bracket->perma);
         Api\Round::getCurrentRounds($bracket->id);
         $bracket->getResults();
     }
     Lib\Cache::setDisabled(false);
 }