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; }
/** * 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; }
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; }
/** * 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::getById($bracket->id); Api\Bracket::getBracketByPerma($bracket->perma); Api\Round::getCurrentRounds($bracket->id); $bracket->getResults(); } Lib\Cache::setDisabled(false); }