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
 public static function generate(array $params)
 {
     $code = Lib\Url::Get('code', null);
     $action = array_shift($params);
     if ($action === 'logout') {
         $user = Api\User::getCurrentUser();
         if ($user) {
             $user->logout();
             header('Location: /brackets/');
         }
     }
     if ($code) {
         $success = Api\User::authenticateUser($code);
         if ($success) {
             $redirect = Lib\Url::Get('state', '/');
             header('Location: ' . $redirect);
             exit;
         } else {
             Lib\Display::addKey('content', 'We were unable to verify your account at this time or your account age does not meet the requirements.');
         }
     } else {
         $obj = new stdClass();
         $obj->loginUrl = Api\User::getLoginUrl(Lib\Url::Get('redirect'));
         // Do a mobile check
         if (preg_match('/iphone|android|windows phone/i', $_SERVER['HTTP_USER_AGENT'])) {
             $obj->loginUrl = str_replace('authorize', 'authorize.compact', $obj->loginUrl);
         }
         $obj->originalUrl = Lib\Url::Get('redirect');
         Lib\Display::addKey('page', 'login');
         Lib\Display::addKey('title', 'Login' . DEFAULT_TITLE_SUFFIX);
         Lib\Display::renderAndAddKey('content', 'login', $obj);
     }
 }
Ejemplo n.º 3
0
 public static function generate(array $params)
 {
     $bracket = self::_getBracket(array_shift($params));
     if ($bracket) {
         // Create the bracket on POST
         if ($_POST) {
             $id = Lib\Url::Post('bracketId', true);
             $name = Lib\Url::Post('bracketName');
             $rules = Lib\Url::Post('rules');
             if ($name && $rules) {
                 $bracket->name = trim($name);
                 $bracket->rules = $rules;
                 $bracket->nameLabel = Lib\Url::Post('nameLabel');
                 $sourceOn = Lib\Url::Post('hideSource') !== 'on';
                 $bracket->sourceLabel = $sourceOn ? Lib\Url::Post('sourceLabel') : 'NO_SOURCE';
                 $advanceHour = Lib\Url::Post('advanceHour', true);
                 $advanceHour = null !== $advanceHour ? $advanceHour : -1;
                 $bracket->advanceHour = $advanceHour;
                 if ($bracket->sync()) {
                     // Clear the generic bracket related caches
                     self::_refreshCaches($bracket);
                     header('Location: /me/?edited');
                     exit;
                 }
             }
         }
         $bracket->sourceHidden = $bracket->sourceLabel === 'NO_SOURCE';
         $bracket->times = self::_generateAdvanceTimes($bracket->advanceHour);
         Lib\Display::renderAndAddKey('content', 'admin/bracket', $bracket);
     }
 }
Ejemplo n.º 4
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)]);
     }
 }
Ejemplo n.º 5
0
 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]);
     }
 }
Ejemplo n.º 6
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]);
     }
 }
Ejemplo n.º 7
0
 public static function generate(array $params)
 {
     $page = array_shift($params);
     $page = $page ?: 'about';
     $path = VIEW_PATH . '/' . $page . '.md';
     $out = 'Uh oh... that doesn\'t seem to exist...';
     if (is_readable($path)) {
         $file = file_get_contents($path);
         $out = Lib\Michelf\Markdown::defaultTransform($file);
     }
     Lib\Display::addKey('page', 'static');
     Lib\Display::renderAndAddKey('content', 'static', $out);
 }
Ejemplo n.º 8
0
 public static function generate(array $params)
 {
     $bracket = self::_getBracket(array_shift($params));
     if ($bracket) {
         $stats = Api\Round::getVotingStats($bracket->id);
         if ($stats) {
             $out = new stdClass();
             $out->bracket = $bracket;
             $out->stats = $stats;
             Lib\Display::renderAndAddKey('content', 'admin/stats', $out);
         }
     }
 }
Ejemplo n.º 9
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);
     }
 }
Ejemplo n.º 10
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);
     }
 }
Ejemplo n.º 11
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);
 }
Ejemplo n.º 12
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.º 13
0
 private static function _displayCharacters(Api\Bracket $bracket)
 {
     $out = new stdClass();
     Lib\Cache::setDisabled(true);
     $out->characters = Api\Character::getByBracketId($bracket->id);
     $out->bracket = $bracket;
     Lib\Display::renderAndAddKey('content', 'admin/characters', $out);
     Lib\Cache::setDisabled(false);
 }
Ejemplo n.º 14
0
 public static function _generateBracket(Api\Bracket $bracket)
 {
     $retVal = null;
     if ($bracket) {
         $availableEntrants = Api\Round::getRoundCountForTier($bracket, 0);
         // Can't have much of a bracket with only two entrants...
         if ($availableEntrants < 2) {
             $message = self::_createMessage('error', 'There are not enough entrants to generate a bracket :(');
             self::_main($message);
         } else {
             if (count($_POST) > 0) {
                 $entrants = Lib\Url::Post('entrants', true);
                 $groups = Lib\Url::Post('groups', true);
                 if ($entrants && $groups) {
                     // Verify that the entrants/groups combo doesn't exceed to number of available entrants
                     if ($entrants * $groups > $availableEntrants) {
                         $message = self::_createMessage('error', 'Cannot generate a bracket of that size');
                         self::_main($message);
                     } else {
                         $bracket->advance();
                         if ($bracket->createBracketFromEliminations($entrants * $groups, $groups)) {
                             $message = self::_createMessage('success', 'Voting for bracket "' . $bracket->name . '" has successfully started!');
                             self::_refreshCaches($bracket);
                             self::_main($message);
                         } else {
                             $message = self::_createMessage('error', 'There are not enough entrants to create a bracket of that size');
                             self::_main($message);
                         }
                     }
                 } else {
                     $message = self::_createMessage('error', 'There was an error starting the bracket');
                     self::_main($message);
                 }
             } else {
                 $out = (object) ['bracket' => $bracket, 'count' => $availableEntrants];
                 Lib\Display::renderAndAddKey('content', 'admin/start_bracket', $out);
             }
         }
     }
 }
Ejemplo n.º 15
0
 protected static function _main($message = null, $force = false)
 {
     $out = new stdClass();
     $out->brackets = Api\Bracket::getUserOwnedBrackets(self::$_user, $force);
     if ($out->brackets) {
         // Check for card images
         foreach ($out->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);
             }
         }
         // Sort the brackets by reverse date
         usort($out->brackets, function ($a, $b) {
             return $a->state == BS_FINAL || $a->state > $b->state ? 1 : -1;
         });
         // Decorate each bracket with some information about what phase it can
         // safely move to. Mostly this is for eliminations
         foreach ($out->brackets as $bracket) {
             $bracket->title = Api\Round::getBracketTitleForActiveRound($bracket);
             $bracket->nextIsFinal = $bracket->title === 'Title Match';
             // Get the title of the next round
             $nextRounds = Api\Round::getNextRounds($bracket);
             $bracket->nextTitle = null;
             if ($nextRounds) {
                 $bracket->nextTitle = str_replace(['Voting - ', 'Eliminations - '], '', Api\Round::getBracketTitleForRound($bracket, $nextRounds[0]));
             }
             // This is a dumb catch all while I work out issues in the stored procedure
             $bracket->nextTitle = $bracket->nextTitle ?: 'Next Round';
             if ($bracket->state == BS_ELIMINATIONS) {
                 // Should query all the brackets at once, but I'm feeling lazy tonight...
                 $result = Lib\Db::Query('SELECT MIN(round_group) AS current_group, MAX(round_group) AS last_group FROM `round` WHERE bracket_id = :bracketId AND round_final = 0', [':bracketId' => $bracket->id]);
                 if ($result && $result->count) {
                     $row = Lib\Db::Fetch($result);
                     // If the eliminations are on the last group, don't show the
                     // advance button
                     if ($row->current_group == $row->last_group) {
                         $bracket->showStart = true;
                     } else {
                         $bracket->showAdvance = true;
                     }
                 }
             }
         }
     }
     if ($message) {
         $out->message = $message;
     }
     Lib\Display::renderAndAddKey('content', 'admin/brackets', $out);
 }