Skip to content

Commit

Permalink
Achievement progress is now done as a bulk command rather than indivi…
Browse files Browse the repository at this point in the history
…dual db row commands
  • Loading branch information
Arcidev committed Aug 23, 2024
1 parent 5afd543 commit e6d61a5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
22 changes: 14 additions & 8 deletions Server/Database/DbAchievementCommandHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cstring>
#include <map>
#include <sstream>
#include <string>
#include "DbCommandHandler.h"

Expand Down Expand Up @@ -78,15 +79,20 @@ DbUserCriteriaMap DbCommandHandler::GetUserAchievementProgress(uint32_t userId)
return userCriterias;
}

void DbCommandHandler::SetUserAchievementProgress(uint32_t userId, uint32_t criteriaId, uint32_t progress) const
void DbCommandHandler::SetUserAchievementProgress(std::map<std::tuple<uint32_t, uint32_t>, uint32_t> const& userAchievements) const
{
std::string userStr = std::to_string(userId);
std::string criteriaStr = std::to_string(criteriaId);
std::string progressStr = std::to_string(progress);
PreparedStatement stmt("INSERT INTO user_achievement_criterias (user_id, criteria_id, progress) VALUES ($1, $2, $3) ON CONFLICT (user_id, criteria_id) DO UPDATE SET progress = $3, last_modified = CURRENT_TIMESTAMP;");
stmt.AddParameter(userStr);
stmt.AddParameter(criteriaStr);
stmt.AddParameter(progressStr);
if (userAchievements.empty())
return;

std::stringstream ss;
ss << "INSERT INTO user_achievement_criterias(user_id, criteria_id, progress) VALUES ";

for (auto const& item : userAchievements)
ss << '(' << std::get<0>(item.first) << ", " << std::get<1>(item.first) << ", " << item.second << "),";

ss.seekp(-1, std::ios_base::end);
ss << " ON CONFLICT (user_id, criteria_id) DO UPDATE SET progress = excluded.progress, last_modified = CURRENT_TIMESTAMP;";

PreparedStatement stmt(ss.str());
dbHandler.ExecuteCommand(stmt, nullptr);
}
2 changes: 1 addition & 1 deletion Server/Database/DbCommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DbCommandHandler

DbAchievementMap GetAchievements() const;
DbUserCriteriaMap GetUserAchievementProgress(uint32_t userId) const;
void SetUserAchievementProgress(uint32_t userId, uint32_t criteriaId, uint32_t progress) const;
void SetUserAchievementProgress(std::map<std::tuple<uint32_t, uint32_t>, uint32_t> const& userAchievements) const;

void ExecuteCommand(PreparedStatement const& statement, std::function<void(PGresult const*)> callback) const { dbHandler.ExecuteCommand(statement, callback); }
};
4 changes: 1 addition & 3 deletions Server/Server/Achievements/AchievementMgr.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <chrono>
#include "AchievementMgr.h"
#include "../../Shared/SharedDefines.h"

Expand Down Expand Up @@ -40,8 +39,7 @@ void AchievementManager::updateUserAchievementProgress()
m_userAchievementProgress.clear();
m_locker.unlock();

for (auto const& item : mapCopy)
DatabaseInstance::GetDbCommandHandler().SetUserAchievementProgress(std::get<0>(item.first), std::get<1>(item.first), item.second);
DatabaseInstance::GetDbCommandHandler().SetUserAchievementProgress(mapCopy);
}
DEBUG_LOG("Achievement update process ended\r\n");
}
Expand Down

0 comments on commit e6d61a5

Please sign in to comment.