Skip to content

Commit

Permalink
improve: some reworks in load player
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Nov 2, 2024
1 parent 0673f4d commit 5a10d43
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 385 deletions.
41 changes: 26 additions & 15 deletions src/creatures/players/components/player_forge_history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ void PlayerForgeHistory::add(const ForgeHistory &history) {

void PlayerForgeHistory::remove(int historyId) {
m_removedHistoryIds.push_back(historyId);
m_history.erase(
std::remove_if(m_history.begin(), m_history.end(), [historyId](const ForgeHistory &h) { return h.id == historyId; }),
m_history.end()
);
auto it = std::ranges::remove_if(m_history, [historyId](const ForgeHistory &h) {
return h.id == historyId;
});
m_history.erase(it.begin(), it.end());
}

bool PlayerForgeHistory::load() {
auto playerGUID = m_player.getGUID();
Database &db = Database::getInstance();
auto query = fmt::format("SELECT * FROM forge_history WHERE player_id = {}", playerGUID);
const DBResult_ptr &result = db.storeQuery(query);
const DBResult_ptr &result = g_database().storeQuery(query);
if (!result) {
g_logger().debug("Failed to load forge history for player with ID: {}", playerGUID);
return false;
Expand Down Expand Up @@ -66,40 +65,52 @@ bool PlayerForgeHistory::save() {
auto removedHistoryIds = m_removedHistoryIds;
auto modifiedHistory = m_modifiedHistory;

auto forgeHistorySaveTask = [playerGUID, removedHistoryIds, modifiedHistory]() mutable {
Database &db = Database::getInstance();

auto deleteForgeHistoryEntries = [playerGUID, removedHistoryIds]() mutable {
if (!removedHistoryIds.empty()) {
std::string idsToDelete = fmt::format("{}", fmt::join(removedHistoryIds, ", "));
std::string deleteQuery = fmt::format(
"DELETE FROM `forge_history` WHERE `player_id` = {} AND `id` IN ({})",
playerGUID, idsToDelete
);

if (!db.executeQuery(deleteQuery)) {
if (!g_database().executeQuery(deleteQuery)) {
g_logger().error("Failed to delete forge history entries for player with ID: {}", playerGUID);
return;
return false;
}

removedHistoryIds.clear();
}

return true;
};

auto insertModifiedHistory = [playerGUID, modifiedHistory]() mutable {
DBInsert insertQuery("INSERT INTO `forge_history` (`id`, `player_id`, `action_type`, `description`, `done_at`, `is_success`) VALUES ");
insertQuery.upsert({ "action_type", "description", "done_at", "is_success" });

for (const auto &history : modifiedHistory) {
auto row = fmt::format("{}, {}, {}, {}, {}, {}", history.id, playerGUID, history.actionType, db.escapeString(history.description), history.createdAt, history.success ? 1 : 0);

auto row = fmt::format("{}, {}, {}, {}, {}, {}", history.id, playerGUID, history.actionType, g_database().escapeString(history.description), history.createdAt, history.success ? 1 : 0);
if (!insertQuery.addRow(row)) {
g_logger().warn("Failed to add forge history entry for player with ID: {}", playerGUID);
return;
return false;
}

g_logger().debug("Added forge history entry date: {}, for player with ID: {}", formatDate(history.createdAt / 1000), playerGUID);
}

if (!insertQuery.execute()) {
g_logger().error("Failed to execute insertion for forge history entries for player with ID: {}", playerGUID);
return false;
}

return true;
};

auto forgeHistorySaveTask = [deleteForgeHistoryEntries, insertModifiedHistory]() mutable {
if (!deleteForgeHistoryEntries()) {
return;
}

if (!insertModifiedHistory()) {
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/players/components/player_forge_history.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct ForgeHistory {

class PlayerForgeHistory {
public:
PlayerForgeHistory(Player &player);
explicit PlayerForgeHistory(Player &player);

const std::vector<ForgeHistory> &get() const;
void add(const ForgeHistory &history);
Expand Down
23 changes: 20 additions & 3 deletions src/creatures/players/components/player_stash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool PlayerStash::save() {
auto removedItems = m_removedItems;
auto modifiedItems = m_modifiedItems;

auto stashSaveTask = [playerGUID, removedItems, modifiedItems]() mutable {
auto deleteStashItems = [playerGUID, removedItems]() mutable {
Database &db = Database::getInstance();

if (!removedItems.empty()) {
Expand All @@ -92,25 +92,42 @@ bool PlayerStash::save() {

if (!db.executeQuery(deleteQuery)) {
g_logger().error("[PlayerStash::save] - Failed to delete removed items for player: {}", playerGUID);
return;
return false;
}

removedItems.clear();
}

return true;
};

auto insertModifiedStashItems = [playerGUID, modifiedItems]() mutable {
Database &db = Database::getInstance();

Check warning on line 105 in src/creatures/players/components/player_stash.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/creatures/players/components/player_stash.cpp#L105

Variable 'db' can be declared with const
Raw output
src/creatures/players/components/player_stash.cpp:105:Variable 'db' can be declared with const

Check warning on line 105 in src/creatures/players/components/player_stash.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-24.04-linux-debug

unused variable ‘db’ [-Wunused-variable]

Check warning on line 105 in src/creatures/players/components/player_stash.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

unused variable ‘db’ [-Wunused-variable]
DBInsert insertQuery("INSERT INTO `player_stash` (`player_id`, `item_id`, `item_count`) VALUES ");
insertQuery.upsert({ "item_count" });

for (const auto &[itemId, itemCount] : modifiedItems) {
auto row = fmt::format("{}, {}, {}", playerGUID, itemId, itemCount);
if (!insertQuery.addRow(row)) {
g_logger().warn("[PlayerStash::save] - Failed to add row for stash item: {}", itemId);
return;
return false;
}
}

if (!insertQuery.execute()) {
g_logger().error("[PlayerStash::save] - Failed to execute insertion for modified stash items for player: {}", playerGUID);
return false;
}

return true;
};

auto stashSaveTask = [deleteStashItems, insertModifiedStashItems]() mutable {
if (!deleteStashItems()) {
return;
}

if (!insertModifiedStashItems()) {
return;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/players/components/player_stash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Player;

class PlayerStash {
public:
PlayerStash(Player &player);
explicit PlayerStash(Player &player);

void add(uint16_t itemId, uint32_t count = 1);

Expand Down
35 changes: 27 additions & 8 deletions src/creatures/players/components/player_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ bool PlayerStorage::save() {
return true;
}

auto saveTask = [removedKeys = m_removedKeys, modifiedKeys = m_modifiedKeys, playerGUID = m_player.getGUID(), playerName = m_player.getName(), storageMap = m_storageMap]() mutable {
auto playerGUID = m_player.getGUID();
auto playerName = m_player.getName();
auto removedKeys = m_removedKeys;
auto modifiedKeys = m_modifiedKeys;
auto storageMap = m_storageMap;

auto deleteStorageKeys = [playerGUID, playerName, removedKeys]() mutable {
if (!removedKeys.empty()) {
std::string keysList = fmt::format("{}", fmt::join(removedKeys, ", "));
std::string deleteQuery = fmt::format(
Expand All @@ -114,11 +120,13 @@ bool PlayerStorage::save() {

if (!g_database().executeQuery(deleteQuery)) {
g_logger().error("[SaveManager::playerStorageSaveTask] - Failed to delete storage keys for player: {}", playerName);
return;
return false;
}
removedKeys.clear();
}
return true;
};

auto insertModifiedStorageKeys = [playerGUID, playerName, modifiedKeys, storageMap]() mutable {
if (!modifiedKeys.empty()) {
DBInsert storageQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES ");
storageQuery.upsert({ "value" });
Expand All @@ -127,16 +135,25 @@ bool PlayerStorage::save() {
auto row = fmt::format("{}, {}, {}", playerGUID, key, storageMap.at(key));
if (!storageQuery.addRow(row)) {
g_logger().warn("[SaveManager::playerStorageSaveTask] - Failed to add row for player storage: {}", playerName);
return;
return false;
}
}

if (!storageQuery.execute()) {
g_logger().error("[SaveManager::playerStorageSaveTask] - Failed to execute storage insertion for player: {}", playerName);
return;
return false;
}
}
return true;
};

modifiedKeys.clear();
auto saveTask = [deleteStorageKeys, insertModifiedStorageKeys]() mutable {
if (!deleteStorageKeys()) {
return;
}

if (!insertModifiedStorageKeys()) {
return;
}
};

Expand Down Expand Up @@ -166,15 +183,17 @@ void PlayerStorage::getReservedRange() {
// Generate outfits range
uint32_t outfits_key = PSTRG_OUTFITS_RANGE_START;
for (const auto &entry : m_player.outfits) {
uint32_t key = ++outfits_key;
outfits_key++;
uint32_t key = outfits_key;
m_storageMap[key] = (entry.lookType << 16) | entry.addons;
m_modifiedKeys.insert(key); // Track the key for saving
}

// Generate familiars range
uint32_t familiar_key = PSTRG_FAMILIARS_RANGE_START;
for (const auto &entry : m_player.familiars) {
uint32_t key = ++familiar_key;
familiar_key++;
uint32_t key = familiar_key;
m_storageMap[key] = (entry.lookType << 16);
m_modifiedKeys.insert(key); // Track the key for saving
}
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,9 +981,9 @@ void Player::closeContainer(uint8_t cid) {
if (container && container->isAnyKindOfRewardChest() && !hasOtherRewardContainerOpen(container)) {
removeEmptyRewards();
}

// Be careful when using the "container" after this, it may crash since the iterator has been invalidated
openContainers.erase(it);
if (container && container->getID() == ITEM_BROWSEFIELD) {
}
}

void Player::removeEmptyRewards() {
Expand Down
12 changes: 9 additions & 3 deletions src/game/scheduling/save_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,17 @@ void SaveManager::executeTasks() {
}
}

threadPool.detach_task([tasks = std::move(m_tasks)] {
for (const auto &task : tasks) {
if (g_configManager().getBoolean(TOGGLE_SAVE_ASYNC)) {
threadPool.detach_task([tasks = std::move(m_tasks)] {
for (const auto &task : tasks) {
task.execute();
}
});
} else {
for (const auto &task : m_tasks) {
task.execute();
}
});
}

m_tasks.clear();
}
Loading

0 comments on commit 5a10d43

Please sign in to comment.