Esempio n. 1
0
<?php

require_once 'app-config.php';
chdir(CORE_LOCATION);
require_once 'config.php';
require_once 'lib/aal.php';
// Get all brackets that need to be updated
$brackets = Api\Bracket::queryReturnAll(['state' => ['in' => [BS_NOMINATIONS, BS_ELIMINATIONS, BS_VOTING]]]);
$bot = new Api\Reddit(RB_BOT);
for ($i = 0, $count = count($brackets); $i < $count; $i++) {
    $bracket = $brackets[$i];
    $cacheKey = 'CRON::reddit-poster_' . $bracket->id;
    $title = Api\Round::getBracketTitleForActiveRound($bracket);
    $oldTitle = Lib\Cache::Get($cacheKey);
    if ($title != $oldTitle) {
        $fullTitle = $bracket->name . ' - ' . $title;
        $url = $bracket->state == BS_NOMINATIONS ? 'nominate' : 'vote';
        $message = '[Vote on today\'s round](http://animebracket.com/' . $url . '/' . $bracket->perma . ')' . PHP_EOL . PHP_EOL . '[View bracket results](http://animebracket.com/results/' . $bracket->perma . ')';
        $out = null;
        echo 'Creating post for "', $fullTitle, '"... ';
        if ($bot->Submit($fullTitle, $message, 'r/AnimeBracket', 'self', $out)) {
            $bracket->externalId = $out->data->id;
            $bracket->sync();
            echo $bracket->externalId;
        } else {
            if (isset($out->ratelimit)) {
                echo 'RATE LIMIT';
                sleep(ceil($out->ratelimit));
                $i--;
            } else {
                echo 'FAIL';
}
// Gets the percentage of times loyalists voted for a particular character in a matchup
function getPercentTimesVotedFor($char, $users)
{
    $retVal = 0;
    $query = 'SELECT round_id FROM round WHERE round_character1_id = :id OR round_character2_id = :id';
    $result = Lib\Db::Query('SELECT COUNT(1) AS total, SUM(CASE WHEN character_id = :id THEN 1 ELSE 0 END) AS character_votes FROM votes WHERE round_id IN (' . $query . ') AND user_id IN (' . implode(',', $users) . ')', [':id' => $char->id]);
    if ($result && $result->count) {
        $row = Lib\Db::Fetch($result);
        $retVal = (int) $row->character_votes / (int) $row->total;
    }
    return $retVal;
}
$projections = [];
// Get the current voting round
$rounds = Api\Round::getCurrentRounds(AI_BRACKET_ID);
foreach ($rounds as $round) {
    // Data gathering
    $char1 = Api\Character::getById($round->character1Id);
    $char2 = Api\Character::getById($round->character2Id);
    $nonLoyalists = getNonLoyalists($char1, $char2);
    $char1->loyalists = getLoyalists($char1, $char2);
    $char2->loyalists = getLoyalists($char2, $char1);
    $char1->sourceStrength = getSourceStrength($char1, $nonLoyalists);
    $char2->sourceStrength = getSourceStrength($char2, $nonLoyalists);
    $char1->percentOfTimesVotedFor = getPercentTimesVotedFor($char1, $nonLoyalists);
    $char2->percentOfTimesVotedFor = getPercentTimesVotedFor($char2, $nonLoyalists);
    // Below is the scientific explanation as penned by Chris Hackmann
    // The percentage of voters who are not loyal to one character or the other
    $nonLoyalistPercentage = 1 - ($char1->loyalists->percent + $char2->loyalists->percent);
    // Determine which character is voted for more often overall