Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: addEvent, adjusted loadItems function and player wheel log #1499

Merged
merged 1 commit into from
Aug 24, 2023
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
2 changes: 1 addition & 1 deletion src/creatures/players/wheel/player_wheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ void PlayerWheel::loadDBPlayerSlotPointsOnLogin() {
uint16_t points;
if (propStream.read<uint8_t>(slot) && propStream.read<uint16_t>(points)) {
setPointsBySlotType(slot, points);
g_logger().info("Player: {}, loaded points {} to slot {}", m_player.getName(), points, slot);
g_logger().debug("Player: {}, loaded points {} to slot {}", m_player.getName(), points, slot);
}
}
}
Expand Down
62 changes: 49 additions & 13 deletions src/io/functions/iologindata_load_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@
#include "io/functions/iologindata_load_player.hpp"
#include "game/game.hpp"

void IOLoginDataLoad::loadItems(ItemsMap &itemsMap, DBResult_ptr result, Player &player) {
try {
do {
uint32_t sid = result->getNumber<uint32_t>("sid");
uint32_t pid = result->getNumber<uint32_t>("pid");
uint16_t type = result->getNumber<uint16_t>("itemtype");
uint16_t count = result->getNumber<uint16_t>("count");
unsigned long attrSize;
const char* attr = result->getStream("attributes", attrSize);
PropStream propStream;
propStream.init(attr, attrSize);

try {
Item* item = Item::CreateItem(type, count);
if (item) {
if (!item->unserializeAttr(propStream)) {
g_logger().warn("[{}] - Failed to deserialize item attributes {}, from player {}, from account id {}", __FUNCTION__, item->getID(), player.getName(), player.getAccount());
savePlayer(&player);
g_logger().info("[{}] - Deleting wrong item: {}", __FUNCTION__, item->getID());
delete item;
continue;
}
itemsMap[sid] = std::make_pair(item, pid);
} else {
g_logger().warn("[{}] - Failed to create item of type {} for player {}, from account id {}", __FUNCTION__, type, player.getName(), player.getAccount());
}
} catch (const std::exception &e) {
g_logger().warn("[{}] - Exception during the creation or deserialization of the item: {}", __FUNCTION__, e.what());
continue;
}
} while (result->next());
} catch (const std::exception &e) {
g_logger().error("[{}] - General exception during item loading: {}", __FUNCTION__, e.what());
}
}

bool IOLoginDataLoad::preLoadPlayer(Player* player, const std::string &name) {
Database &db = Database::getInstance();

Expand Down Expand Up @@ -463,14 +499,14 @@ void IOLoginDataLoad::loadPlayerInventoryItems(Player* player, DBResult_ptr resu
std::ostringstream query;
query << "SELECT `pid`, `sid`, `itemtype`, `count`, `attributes` FROM `player_items` WHERE `player_id` = " << player->getGUID() << " ORDER BY `sid` DESC";

InventoryItemsMap inventoryItems;
ItemsMap inventoryItems;
std::vector<std::pair<uint8_t, Container*>> openContainersList;

try {
if ((result = db.storeQuery(query.str()))) {
loadItems(inventoryItems, result, *player);

for (InventoryItemsMap::const_reverse_iterator it = inventoryItems.rbegin(), end = inventoryItems.rend(); it != end; ++it) {
for (ItemsMap::const_reverse_iterator it = inventoryItems.rbegin(), end = inventoryItems.rend(); it != end; ++it) {
const std::pair<Item*, int32_t> &pair = it->second;
Item* item = pair.first;
if (!item) {
Expand All @@ -483,7 +519,7 @@ void IOLoginDataLoad::loadPlayerInventoryItems(Player* player, DBResult_ptr resu
player->internalAddThing(pid, item);
item->startDecaying();
} else {
InventoryItemsMap::const_iterator it2 = inventoryItems.find(pid);
ItemsMap::const_iterator it2 = inventoryItems.find(pid);
if (it2 == inventoryItems.end()) {
continue;
}
Expand Down Expand Up @@ -547,7 +583,7 @@ void IOLoginDataLoad::loadRewardItems(Player* player) {
return;
}

RewardItemsMap rewardItems;
ItemsMap rewardItems;
std::ostringstream query;
query.str(std::string());
query << "SELECT `pid`, `sid`, `itemtype`, `count`, `attributes` FROM `player_rewards` WHERE `player_id` = "
Expand All @@ -566,13 +602,13 @@ void IOLoginDataLoad::loadPlayerDepotItems(Player* player, DBResult_ptr result)
}

Database &db = Database::getInstance();
DepotItemsMap depotItems;
ItemsMap depotItems;
std::ostringstream query;
query << "SELECT `pid`, `sid`, `itemtype`, `count`, `attributes` FROM `player_depotitems` WHERE `player_id` = " << player->getGUID() << " ORDER BY `sid` DESC";
if ((result = db.storeQuery(query.str()))) {

loadItems(depotItems, result, *player);
for (DepotItemsMap::const_reverse_iterator it = depotItems.rbegin(), end = depotItems.rend(); it != end; ++it) {
for (ItemsMap::const_reverse_iterator it = depotItems.rbegin(), end = depotItems.rend(); it != end; ++it) {
const std::pair<Item*, int32_t> &pair = it->second;
Item* item = pair.first;

Expand All @@ -584,7 +620,7 @@ void IOLoginDataLoad::loadPlayerDepotItems(Player* player, DBResult_ptr result)
item->startDecaying();
}
} else {
DepotItemsMap::const_iterator it2 = depotItems.find(pid);
ItemsMap::const_iterator it2 = depotItems.find(pid);
if (it2 == depotItems.end()) {
continue;
}
Expand All @@ -610,18 +646,18 @@ void IOLoginDataLoad::loadPlayerInboxItems(Player* player, DBResult_ptr result)
query << "SELECT `pid`, `sid`, `itemtype`, `count`, `attributes` FROM `player_inboxitems` WHERE `player_id` = " << player->getGUID() << " ORDER BY `sid` DESC";
if ((result = db.storeQuery(query.str()))) {

InboxItemsMap inboxItems;
ItemsMap inboxItems;
loadItems(inboxItems, result, *player);

for (InboxItemsMap::const_reverse_iterator it = inboxItems.rbegin(), end = inboxItems.rend(); it != end; ++it) {
for (ItemsMap::const_reverse_iterator it = inboxItems.rbegin(), end = inboxItems.rend(); it != end; ++it) {
const std::pair<Item*, int32_t> &pair = it->second;
Item* item = pair.first;
int32_t pid = pair.second;
if (pid >= 0 && pid < 100) {
player->getInbox()->internalAddThing(item);
item->startDecaying();
} else {
InboxItemsMap::const_iterator it2 = inboxItems.find(pid);
ItemsMap::const_iterator it2 = inboxItems.find(pid);
if (it2 == inboxItems.end()) {
continue;
}
Expand Down Expand Up @@ -818,7 +854,7 @@ void IOLoginDataLoad::loadPlayerBosstiary(Player* player, DBResult_ptr result) {
}
}

void IOLoginDataLoad::bindRewardBag(Player* player, RewardItemsMap &rewardItemsMap) {
void IOLoginDataLoad::bindRewardBag(Player* player, ItemsMap &rewardItemsMap) {
if (!player) {
g_logger().warn("[IOLoginData::loadPlayer] - Player nullptr: {}", __FUNCTION__);
return;
Expand All @@ -837,7 +873,7 @@ void IOLoginDataLoad::bindRewardBag(Player* player, RewardItemsMap &rewardItemsM
}
}

void IOLoginDataLoad::insertItemsIntoRewardBag(const RewardItemsMap &rewardItemsMap) {
void IOLoginDataLoad::insertItemsIntoRewardBag(const ItemsMap &rewardItemsMap) {
for (const auto &it : std::views::reverse(rewardItemsMap)) {
const std::pair<Item*, int32_t> &pair = it.second;
Item* item = pair.first;
Expand All @@ -846,7 +882,7 @@ void IOLoginDataLoad::insertItemsIntoRewardBag(const RewardItemsMap &rewardItems
break;
}

RewardItemsMap::const_iterator it2 = rewardItemsMap.find(pid);
ItemsMap::const_iterator it2 = rewardItemsMap.find(pid);
if (it2 == rewardItemsMap.end()) {
continue;
}
Expand Down
47 changes: 4 additions & 43 deletions src/io/functions/iologindata_load_player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,10 @@ class IOLoginDataLoad : public IOLoginData {
static void loadPlayerUpdateSystem(Player* player);

private:
using InventoryItemsMap = std::map<uint32_t, std::pair<Item*, uint32_t>>;
using RewardItemsMap = std::map<uint32_t, std::pair<Item*, uint32_t>>;
using DepotItemsMap = std::map<uint32_t, std::pair<Item*, uint32_t>>;
using InboxItemsMap = std::map<uint32_t, std::pair<Item*, uint32_t>>;
using ItemsMap = std::map<uint32_t, std::pair<Item*, uint32_t>>;

static void bindRewardBag(Player* player, RewardItemsMap &rewardItemsMap);
static void insertItemsIntoRewardBag(const RewardItemsMap &rewardItemsMap);
static void bindRewardBag(Player* player, ItemsMap &rewardItemsMap);
static void insertItemsIntoRewardBag(const ItemsMap &rewardItemsMap);

template <typename T>
static void loadItems(T &container, DBResult_ptr result, Player &player) {
try {
do {
uint32_t sid = result->getNumber<uint32_t>("sid");
uint32_t pid = result->getNumber<uint32_t>("pid");
uint16_t type = result->getNumber<uint16_t>("itemtype");
uint16_t count = result->getNumber<uint16_t>("count");
unsigned long attrSize;
const char* attr = result->getStream("attributes", attrSize);
PropStream propStream;
propStream.init(attr, attrSize);

try {
Item* item = Item::CreateItem(type, count);
if (item) {
if (!item->unserializeAttr(propStream)) {
g_logger().warn("[IOLoginData::loadItems] - Falha ao desserializar os atributos do item {}, do jogador {}, da conta id {}", item->getID(), player.getName(), player.getAccount());
savePlayer(&player);
g_logger().info("[IOLoginData::loadItems] - Deletando item defeituoso: {}", item->getID());
delete item; // Delete o item defeituoso
continue;
}
std::pair<Item*, uint32_t> pair(item, pid);
container[sid] = pair;
} else {
g_logger().warn("[IOLoginData::loadItems] - Falha ao criar o item do tipo {} para o jogador {}, da conta id {}", type, player.getName(), player.getAccount());
}
} catch (const std::exception &e) {
g_logger().warn("[IOLoginData::loadItems] - Exceção durante a criação ou desserialização do item: {}", e.what());
continue;
}
} while (result->next());
} catch (const std::exception &e) {
g_logger().error("[IOLoginData::loadItems] - Exceção geral durante o carregamento do item: {}", e.what());
}
}
static void loadItems(ItemsMap &itemsMap, DBResult_ptr result, Player &player);
};
2 changes: 1 addition & 1 deletion src/lua/functions/core/game/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ int GlobalFunctions::luaAddEvent(lua_State* L) {
lua_rawgeti(L, -1, 't');

LuaData_t type = getNumber<LuaData_t>(L, -1);
if (type != LuaData_t::Unknown && type != LuaData_t::Tile && type != LuaData_t::Position) {
if (type != LuaData_t::Unknown && type <= LuaData_t::Npc) {
indexes.push_back({ i, type });
}
lua_pop(globalState, 2);
Expand Down
2 changes: 1 addition & 1 deletion src/lua/lua_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ enum class LuaData_t : uint8_t {
Teleport,
Player,
Monster,
MonsterType,
Npc,
MonsterType,
NpcType,
Tile,
Variant,
Expand Down