Simplify snprintfs

so much better to read
This commit is contained in:
EmosewaMC 2023-05-04 14:28:53 -07:00
parent 8de528e77c
commit 2ab995b9c3

View File

@ -111,50 +111,53 @@ void Leaderboard::SetupLeaderboard() {
char baseStandingBuffer[1024]; char baseStandingBuffer[1024];
char lookupBuffer[MAX_QUERY_LENGTH]; char lookupBuffer[MAX_QUERY_LENGTH];
std::string orderBase;
std::string selectBase;
switch (leaderboardType) { switch (leaderboardType) {
case Type::ShootingGallery: { case Type::ShootingGallery: {
const char* orderBase = "score DESC, streak DESC, hitPercentage DESC"; orderBase = "score DESC, streak DESC, hitPercentage DESC";
const char* selectBase = "hitPercentage, score, streak"; selectBase = "hitPercentage, score, streak";
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), orderBase, selectBase);
if (isFriendsQuery) snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), orderBase, friendsQuery, selectBase);
else snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), orderBase, "", selectBase);
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), orderBase);
break; break;
} }
case Type::Racing: case Type::Racing:
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "bestTime ASC, bestLapTime ASC, numWins DESC", "bestLapTime, bestTime, numWins"); orderBase = "bestTime ASC, bestLapTime ASC, numWins DESC";
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "bestTime ASC, bestLapTime ASC, numWins DESC"); selectBase = "bestLapTime, bestTime, numWins";
break; break;
case Type::UnusedLeaderboard4: case Type::UnusedLeaderboard4:
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "score DESC", "score"); orderBase = "score DESC";
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "score DESC"); selectBase = "score";
break; break;
case Type::MonumentRace: case Type::MonumentRace:
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "bestTime ASC", "bestTime"); orderBase = "bestTime ASC";
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "bestTime ASC"); selectBase = "bestTime";
break; break;
case Type::FootRace: case Type::FootRace:
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "bestTime DESC", "bestTime"); orderBase = "bestTime DESC";
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "bestTime DESC"); selectBase = "bestTime";
break; break;
case Type::Survival: case Type::Survival:
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "score DESC, bestTime DESC", "score, bestTime"); orderBase = "score DESC, bestTime DESC";
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "score DESC, bestTime DESC"); selectBase = "score, bestTime";
// If the config option default_survival_scoring is 1, reverse the order of the points and time columns // If the config option default_survival_scoring is 1, reverse the order of the points and time columns
break; break;
case Type::SurvivalNS: case Type::SurvivalNS:
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "bestTime DESC, score DESC", "bestTime, score"); orderBase = "bestTime DESC, score DESC";
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "bestTime DESC, score DESC"); selectBase = "bestTime, score";
break; break;
case Type::Donations: case Type::Donations:
snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), "score DESC", "score"); orderBase = "score DESC";
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), "score DESC"); selectBase = "score";
break; break;
case Type::None: case Type::None:
Game::logger->Log("LeaderboardManager", "Attempting to get leaderboard for type none. Is this intended?"); Game::logger->Log("LeaderboardManager", "Attempting to get leaderboard for type none. Is this intended?");
// This type is included here simply to resolve a compiler warning on mac about unused enum types // This type is included here simply to resolve a compiler warning on mac about unused enum types
break; break;
} }
if (isFriendsQuery) snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), orderBase.c_str(), friendsQuery, selectBase.c_str());
else snprintf(lookupBuffer, MAX_QUERY_LENGTH, queryBase.c_str(), orderBase.c_str(), "", selectBase.c_str());
if (isTopQuery) snprintf(baseStandingBuffer, 1024, baseLookupStr.c_str(), orderBase.c_str());
Game::logger->Log("LeaderboardManager", "lookup query is %s", (!isTopQuery) ? baseLookupStr.c_str() : baseStandingBuffer); Game::logger->Log("LeaderboardManager", "lookup query is %s", (!isTopQuery) ? baseLookupStr.c_str() : baseStandingBuffer);
std::unique_ptr<sql::PreparedStatement> baseQuery(Database::CreatePreppedStmt((!isTopQuery) ? baseLookupStr : baseStandingBuffer)); std::unique_ptr<sql::PreparedStatement> baseQuery(Database::CreatePreppedStmt((!isTopQuery) ? baseLookupStr : baseStandingBuffer));
baseQuery->setInt(1, this->gameID); baseQuery->setInt(1, this->gameID);