diff --git a/dGame/LeaderboardManager.cpp b/dGame/LeaderboardManager.cpp index 83703724..44ef17cc 100644 --- a/dGame/LeaderboardManager.cpp +++ b/dGame/LeaderboardManager.cpp @@ -64,7 +64,7 @@ bool Leaderboard::GetRankingQuery(std::string& lookupReturn) const { void Leaderboard::SetupLeaderboard() { std::string queryBase = - R"QUERY( + R"QUERY( WITH leaderboardsRanked AS ( SELECT leaderboard.*, charinfo.name, RANK() OVER @@ -278,8 +278,8 @@ std::string FormatInsert(const std::string& columns, const std::string& format, const char* updateClause = "UPDATE"; const char* queryType = useUpdate ? updateClause : insertClause; - const char* insertFilter = ", character_id = ?, game_id = ?"; - const char* updateFilter = "WHERE character_id = ? AND game_id = ?"; + const char* insertFilter = ", character_id = ?, game_id = ?, timesPlayed = 1"; + const char* updateFilter = ", timesPlayed = timesPlayed + 1 WHERE character_id = ? AND game_id = ?"; const char* usedFilter = useUpdate ? updateFilter : insertFilter; 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) { - // Increment the numTimes this player has played this game. - std::unique_ptr 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 selectedColumns; std::string insertFormat; @@ -342,10 +336,7 @@ void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Lead insertFormat = "score=%i"; break; } - case Leaderboard::Type::None: { - Game::logger->Log("LeaderboardManager", "Warning: Saving leaderboard of type None. Are you sure this is intended?"); - break; - } + case Leaderboard::Type::None: default: { Game::logger->Log("LeaderboardManager", "Unknown leaderboard type %i. Cannot save score!", leaderboardType); return; @@ -485,12 +476,19 @@ void LeaderboardManager::SaveScore(const LWOOBJID& playerID, GameID gameID, Lead } else { saveQuery = FormatInsert(selectedColumns, insertFormat, argsCopy, false); } - Game::logger->Log("LeaderboardManager", "%s", saveQuery.c_str()); if (!saveQuery.empty()) { + Game::logger->Log("LeaderboardManager", "%s", saveQuery.c_str()); std::unique_ptr insertQuery(Database::CreatePreppedStmt(saveQuery)); insertQuery->setInt(1, playerID); insertQuery->setInt(2, gameID); 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 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); } diff --git a/migrations/dlu/9_Update_Leaderboard_Storage.sql b/migrations/dlu/9_Update_Leaderboard_Storage.sql index 498e833c..c90ba4a2 100644 --- a/migrations/dlu/9_Update_Leaderboard_Storage.sql +++ b/migrations/dlu/9_Update_Leaderboard_Storage.sql @@ -7,6 +7,6 @@ ALTER TABLE leaderboard MODIFY time FLOAT NOT NULL DEFAULT 0; 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; diff --git a/tests/dGameTests/LeaderboardTests.cpp b/tests/dGameTests/LeaderboardTests.cpp index 8e6cb4e6..a4b7b9c0 100644 --- a/tests/dGameTests/LeaderboardTests.cpp +++ b/tests/dGameTests/LeaderboardTests.cpp @@ -75,19 +75,19 @@ TEST_F(LeaderboardTests, LeaderboardSpeedTest) { RunTests(1864, Leaderboard::Type::ShootingGallery , Leaderboard::InfoType::Top); // RunTests(1864, Leaderboard::Type::ShootingGallery, Leaderboard::InfoType::MyStanding); // 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); - 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); - 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); - 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); - 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); - 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); - 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); - LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::Donations, 1, 300000); + LeaderboardManager::Instance().SaveScore(14231, 0, Leaderboard::Type::Donations, 1, 300001); }