private static function _updateCharacter(Api\Bracket $bracket) { $out = new stdClass(); $out->success = false; $id = Lib\Url::Post('characterId', true); $name = Lib\Url::Post('name'); $source = Lib\Url::Post('source'); $action = Lib\Url::Post('action'); if ($id && $name && $action) { $out->action = $action; $character = Api\Character::getById($id); if ($character && $character->bracketId == $bracket->id) { if ($action == 'update') { $character->name = $name; $character->source = $source; if ($character->sync()) { $out->success = true; } else { $out->message = 'Error updating database'; } } else { if ($action == 'delete') { if ($bracket->state == BS_NOMINATIONS || $bracket->state == BS_ELIMINATIONS) { if ($character->delete()) { $out->success = true; } else { $out->message = 'Delete failed'; } } else { $out->message = 'Cannot delete characters after voting has started'; } } else { $out->message = 'Unknown action'; } } } else { $out->message = 'Character does not belong to this bracket'; } } else { $out->message = 'Missing fields'; } Lib\Display::renderJson($out); }
/** * Returns the character object for the winner of the current round */ public function getWinner($useEliminations = false) { $retVal = null; $params = []; if (!$useEliminations) { $query = 'SELECT COUNT(1) AS votes, character_id FROM votes WHERE round_id = :roundId GROUP BY character_id'; $params[':roundId'] = $this->id; } else { $query = 'SELECT COUNT(1) AS votes, character_id FROM votes WHERE round_id IN (SELECT round_id FROM round WHERE bracket_id = :bracketId AND round_tier = 0 AND round_character1_id IN (:char1, :char2)) GROUP BY character_id'; $params[':char1'] = $this->character1Id; $params[':char2'] = $this->character2Id; $params[':bracketId'] = $this->bracketId; } $result = Lib\Db::Query($query, $params); if ($result && $result->count === 2) { $highestVotes = 0; $winner = 0; while ($row = Lib\Db::Fetch($result)) { if ((int) $row->votes > $highestVotes) { $highestVotes = (int) $row->votes; $winner = (int) $row->character_id; // Seed determines the outcome of a tie } else { if ((int) $row->votes === $highestVotes && !$useEliminations) { $character1 = Character::getById($this->character1Id); $character2 = Character::getById($this->character2Id); $winner = $character1->seed < $character2->seed ? $character1->id : $character2->id; } } } $retVal = Character::getById($winner); } else { // Somehow, one person managed to get no votes, so fallback on tie breaker rules (technically that's what it is after all) $retVal = $this->getWinner(true); } return $retVal; }
/** * Returns the character object for the winner of the current round */ public function getWinner() { return Character::getById($this->getWinnerId()); }
/** * Override for getAll to include the winner character object */ public static function getAll($force = false) { $cacheKey = 'Api:Bracket:getAll_' . BRACKET_SOURCE; $retVal = Lib\Cache::Get($cacheKey); if (false === $retVal || $force) { $brackets = parent::queryReturnAll(['source' => BRACKET_SOURCE, 'state' => ['ne' => BS_HIDDEN]], ['score' => 'desc', 'state' => 'desc', 'start' => 'desc']); $retVal = []; foreach ($brackets as $bracket) { if ($bracket->winnerCharacterId) { $bracket->winner = Character::getById($bracket->winnerCharacterId); } if ($bracket->start <= time() || $force) { $retVal[] = $bracket; } } Lib\Cache::Set($cacheKey, $retVal, 3600); } return $retVal; }