Fix various bugs and make code cleaner.

Still have work to go.
This commit is contained in:
David Markowitz 2023-05-08 03:55:10 -07:00
parent 0f307ac4be
commit 1c7ac93d4b
3 changed files with 21 additions and 23 deletions

View File

@ -278,8 +278,8 @@ std::string FormatInsert(const std::string& columns, const std::string& format,
const char* updateClause = "UPDATE"; const char* updateClause = "UPDATE";
const char* queryType = useUpdate ? updateClause : insertClause; const char* queryType = useUpdate ? updateClause : insertClause;
const char* insertFilter = ", character_id = ?, game_id = ?"; const char* insertFilter = ", character_id = ?, game_id = ?, timesPlayed = 1";
const char* updateFilter = "WHERE character_id = ? AND game_id = ?"; const char* updateFilter = ", timesPlayed = timesPlayed + 1 WHERE character_id = ? AND game_id = ?";
const char* usedFilter = useUpdate ? updateFilter : insertFilter; const char* usedFilter = useUpdate ? updateFilter : insertFilter;
constexpr uint16_t STRING_LENGTH = 400; constexpr uint16_t STRING_LENGTH = 400;
@ -293,12 +293,6 @@ std::string FormatInsert(const std::string& columns, const std::string& format,
} }
void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, va_list args) { void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Leaderboard::Type leaderboardType, va_list args) {
// Increment the numTimes this player has played this game.
std::unique_ptr<sql::PreparedStatement> incrementStatement(Database::CreatePreppedStmt("UPDATE leaderboard SET timesPlayed = timesPlayed + 1 WHERE character_id = ? AND game_id = ?;"));
incrementStatement->setInt(1, playerID);
incrementStatement->setInt(2, gameID);
incrementStatement->executeUpdate();
std::string insertStatement; std::string insertStatement;
std::string selectedColumns; std::string selectedColumns;
std::string insertFormat; std::string insertFormat;
@ -342,10 +336,7 @@ void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Lead
insertFormat = "score=%i"; insertFormat = "score=%i";
break; break;
} }
case Leaderboard::Type::None: { case Leaderboard::Type::None:
Game::logger->Log("LeaderboardManager", "Warning: Saving leaderboard of type None. Are you sure this is intended?");
break;
}
default: { default: {
Game::logger->Log("LeaderboardManager", "Unknown leaderboard type %i. Cannot save score!", leaderboardType); Game::logger->Log("LeaderboardManager", "Unknown leaderboard type %i. Cannot save score!", leaderboardType);
return; return;
@ -485,12 +476,19 @@ void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Lead
} else { } else {
saveQuery = FormatInsert(selectedColumns, insertFormat, argsCopy, false); saveQuery = FormatInsert(selectedColumns, insertFormat, argsCopy, false);
} }
Game::logger->Log("LeaderboardManager", "%s", saveQuery.c_str());
if (!saveQuery.empty()) { if (!saveQuery.empty()) {
Game::logger->Log("LeaderboardManager", "%s", saveQuery.c_str());
std::unique_ptr<sql::PreparedStatement> insertQuery(Database::CreatePreppedStmt(saveQuery)); std::unique_ptr<sql::PreparedStatement> insertQuery(Database::CreatePreppedStmt(saveQuery));
insertQuery->setInt(1, playerID); insertQuery->setInt(1, playerID);
insertQuery->setInt(2, gameID); insertQuery->setInt(2, gameID);
insertQuery->execute(); insertQuery->execute();
} else {
Game::logger->Log("LeaderboardManager", "No new score to save, incrementing numTimesPlayed");
// Increment the numTimes this player has played this game.
std::unique_ptr<sql::PreparedStatement> incrementStatement(Database::CreatePreppedStmt("UPDATE leaderboard SET timesPlayed = timesPlayed + 1 WHERE character_id = ? AND game_id = ?;"));
incrementStatement->setInt(1, playerID);
incrementStatement->setInt(2, gameID);
incrementStatement->executeUpdate();
} }
va_end(argsCopy); va_end(argsCopy);
} }

View File

@ -7,6 +7,6 @@ ALTER TABLE leaderboard
MODIFY time FLOAT NOT NULL DEFAULT 0; MODIFY time FLOAT NOT NULL DEFAULT 0;
ALTER TABLE leaderboard CHANGE time bestTime float; ALTER TABLE leaderboard CHANGE time bestTime float;
ALTER TABLE leaderboard CHANGE last_played TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(); ALTER TABLE leaderboard CHANGE last_played last_played TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP();
UPDATE leaderboard SET streak = bestTime where game_id = 1864; UPDATE leaderboard SET streak = bestTime where game_id = 1864;

View File

@ -75,19 +75,19 @@ TEST_F(LeaderboardTests, LeaderboardSpeedTest) {
RunTests(1864, Leaderboard::Type::ShootingGallery , Leaderboard::InfoType::Top); RunTests(1864, Leaderboard::Type::ShootingGallery , Leaderboard::InfoType::Top);
// RunTests(1864, Leaderboard::Type::ShootingGallery, Leaderboard::InfoType::MyStanding); // RunTests(1864, Leaderboard::Type::ShootingGallery, Leaderboard::InfoType::MyStanding);
// RunTests(1864, Leaderboard::Type::ShootingGallery, Leaderboard::InfoType::Friends); // RunTests(1864, Leaderboard::Type::ShootingGallery, Leaderboard::InfoType::Friends);
LeaderboardManager::Instance().SaveScore(14231, 1864, Leaderboard::Type::ShootingGallery, 3, 53001, 15.0f, 100); LeaderboardManager::Instance().SaveScore(14231, 1864, Leaderboard::Type::ShootingGallery, 3, 53002, 15.0f, 100);
// RunTests(0, Leaderboard::Type::Racing); // RunTests(0, Leaderboard::Type::Racing);
LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::Racing, 3, 260.0f, 250.0f, true); LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::Racing, 3, 259.0f, 250.0f, true);
// RunTests(0, Leaderboard::Type::MonumentRace); // RunTests(0, Leaderboard::Type::MonumentRace);
LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::MonumentRace, 1, 150); LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::MonumentRace, 1, 149);
// RunTests(0, Leaderboard::Type::FootRace); // RunTests(0, Leaderboard::Type::FootRace);
LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::FootRace, 1, 150); LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::FootRace, 1, 151);
// RunTests(0, Leaderboard::Type::UnusedLeaderboard4); // RunTests(0, Leaderboard::Type::UnusedLeaderboard4);
LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::UnusedLeaderboard4, 1, 100); LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::UnusedLeaderboard4, 1, 101);
// RunTests(0, Leaderboard::Type::Survival); // RunTests(0, Leaderboard::Type::Survival);
LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::Survival, 2, 3000, 15); LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::Survival, 2, 3001, 15);
// RunTests(0, Leaderboard::Type::SurvivalNS); // RunTests(0, Leaderboard::Type::SurvivalNS);
LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::SurvivalNS, 2, 300, 15); LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::SurvivalNS, 2, 301, 15);
// RunTests(0, Leaderboard::Type::Donations); // RunTests(0, Leaderboard::Type::Donations);
LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::Donations, 1, 300000); LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::Donations, 1, 300001);
} }