Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/io/iologindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,11 @@ bool IOLoginData::loadPlayer(const std::shared_ptr<Player> &player, const DBResu
// Load instant spells list
IOLoginDataLoad::loadPlayerInstantSpellList(player, result);

if (disableIrrelevantInfo) {
return true;
if (!disableIrrelevantInfo) {
// Load additional data only if the player is online (e.g., forge, bosstiary)
loadOnlyDataForOnlinePlayer(player, result);
}

// load forge history
IOLoginDataLoad::loadPlayerForgeHistory(player, result);

// load bosstiary
IOLoginDataLoad::loadPlayerBosstiary(player, result);

IOLoginDataLoad::loadPlayerInitializeSystem(player);
IOLoginDataLoad::loadPlayerUpdateSystem(player);

return true;
} catch (const std::system_error &error) {
g_logger().warn("[{}] Error while load player: {}", __FUNCTION__, error.what());
Expand All @@ -192,6 +184,13 @@ bool IOLoginData::loadPlayer(const std::shared_ptr<Player> &player, const DBResu
}
}

void IOLoginData::loadOnlyDataForOnlinePlayer(const std::shared_ptr<Player> &player, const DBResult_ptr &result) {
IOLoginDataLoad::loadPlayerForgeHistory(player, result);
IOLoginDataLoad::loadPlayerBosstiary(player, result);
IOLoginDataLoad::loadPlayerInitializeSystem(player);
IOLoginDataLoad::loadPlayerUpdateSystem(player);
}

bool IOLoginData::savePlayer(const std::shared_ptr<Player> &player) {
try {
bool success = DBTransaction::executeWithinTransaction([player]() {
Expand Down Expand Up @@ -259,6 +258,18 @@ bool IOLoginData::savePlayerGuard(const std::shared_ptr<Player> &player) {
throw DatabaseException("[IOLoginDataSave::savePlayerTaskHuntingClass] - Failed to save player task hunting class: " + player->getName());
}

// Saves data components that are only valid if the player is online.
// Skips execution entirely if the player is offline to avoid overwriting unloaded data.
saveOnlyDataForOnlinePlayer(player);

return true;
}

void IOLoginData::saveOnlyDataForOnlinePlayer(const std::shared_ptr<Player> &player) {
if (player->isOffline()) {
return;
}

if (!IOLoginDataSave::savePlayerForgeHistory(player)) {
throw DatabaseException("[IOLoginDataSave::savePlayerForgeHistory] - Failed to save player forge history: " + player->getName());
}
Expand All @@ -279,8 +290,6 @@ bool IOLoginData::savePlayerGuard(const std::shared_ptr<Player> &player) {
if (!IOLoginDataSave::savePlayerStorage(player)) {
throw DatabaseException("[IOLoginDataSave::savePlayerStorage] - Failed to save player storage: " + player->getName());
}

return true;
}

std::string IOLoginData::getNameByGuid(uint32_t guid) {
Expand Down
32 changes: 32 additions & 0 deletions src/io/iologindata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,39 @@ class IOLoginData {
static bool loadPlayerById(const std::shared_ptr<Player> &player, uint32_t id, bool disableIrrelevantInfo = true);
static bool loadPlayerByName(const std::shared_ptr<Player> &player, const std::string &name, bool disableIrrelevantInfo = true);
static bool loadPlayer(const std::shared_ptr<Player> &player, const std::shared_ptr<DBResult> &result, bool disableIrrelevantInfo = false);

/**
* @brief Loads data components that are only relevant when the player is online.
*
* This function is responsible for initializing systems that are not needed when the player is offline,
* such as the forge history, bosstiary, and various runtime systems.
*
* If the player is offline, this function returns early and avoids loading these systems.
* This helps optimize memory usage and prevents unnecessary initialization of unused features.
*
* @param player A shared pointer to the Player instance. Must not be nullptr.
* @param result The database result containing the player's data.
*/
static void loadOnlyDataForOnlinePlayer(const std::shared_ptr<Player> &player, const std::shared_ptr<DBResult> &result);

static bool savePlayer(const std::shared_ptr<Player> &player);

/**
* @brief Saves data components that are only relevant when the player is online.
*
* This function is responsible for persisting player-related systems that are only loaded
* when the player is online, such as the forge history, bosstiary progress, and wheel data.
*
* If the player is offline, this function will return immediately without performing any save operations.
* This prevents overwriting existing database values with empty or uninitialized data that may result
* from partial player loading (common during offline operations).
*
* @note This function throws DatabaseException if any of the internal save operations fail.
* It should be called after all always-loaded data has been saved.
*
* @param player A shared pointer to the Player instance. Must not be nullptr.
*/
static void saveOnlyDataForOnlinePlayer(const std::shared_ptr<Player> &player);
static uint32_t getGuidByName(const std::string &name);
static bool getGuidByNameEx(uint32_t &guid, bool &specialVip, std::string &name);
static std::string getNameByGuid(uint32_t guid);
Expand Down
Loading