From 79dbddd56e8556694d618688340ca9ca802e9b87 Mon Sep 17 00:00:00 2001 From: Akkadius Date: Mon, 30 Mar 2020 21:32:59 -0500 Subject: [PATCH] Decouple temporary merchant list call [skip ci] --- common/database_instances.cpp | 17 ++-- common/eqemu_logsys.h | 2 + common/eqemu_logsys_log_aliases.h | 16 ++++ zone/spawn2.cpp | 6 +- zone/waypoints.cpp | 4 +- zone/zone.cpp | 137 ++++++++++++++++++------------ zone/zone.h | 6 +- 7 files changed, 115 insertions(+), 73 deletions(-) diff --git a/common/database_instances.cpp b/common/database_instances.cpp index b84924192..68496b61d 100644 --- a/common/database_instances.cpp +++ b/common/database_instances.cpp @@ -472,21 +472,16 @@ void Database::AssignRaidToInstance(uint32 raid_id, uint32 instance_id) } void Database::BuryCorpsesInInstance(uint16 instance_id) { - std::string query = StringFormat( - "UPDATE `character_corpses` " - "SET `is_buried` = 1, " - "`instance_id` = 0 " - "WHERE " - "`instance_id` = %u ", - instance_id - ); - auto results = QueryDatabase(query); + QueryDatabase( + fmt::format( + "UPDATE character_corpses SET is_buried = 1, instance_id = 0 WHERE instance_id = {}", + instance_id + ) + ); } void Database::DeleteInstance(uint16 instance_id) { - // TODO: BOUNDARY REWRITE - std::string query = StringFormat("DELETE FROM instance_list WHERE id=%u", instance_id); QueryDatabase(query); diff --git a/common/eqemu_logsys.h b/common/eqemu_logsys.h index cb880e038..51ec48bfc 100644 --- a/common/eqemu_logsys.h +++ b/common/eqemu_logsys.h @@ -115,6 +115,7 @@ namespace Logs { Flee, Aura, HotReload, + Merchants, MaxCategoryID /* Don't Remove this */ }; @@ -189,6 +190,7 @@ namespace Logs { "Flee", "Aura", "HotReload", + "Merchants", }; } diff --git a/common/eqemu_logsys_log_aliases.h b/common/eqemu_logsys_log_aliases.h index 2ca5127b6..42f973e79 100644 --- a/common/eqemu_logsys_log_aliases.h +++ b/common/eqemu_logsys_log_aliases.h @@ -571,6 +571,16 @@ OutF(LogSys, Logs::Detail, Logs::HotReload, __FILE__, __func__, __LINE__, message, ##__VA_ARGS__);\ } while (0) +#define LogMerchants(message, ...) do {\ + if (LogSys.log_settings[Logs::Merchants].is_category_enabled == 1)\ + OutF(LogSys, Logs::General, Logs::Merchants, __FILE__, __func__, __LINE__, message, ##__VA_ARGS__);\ +} while (0) + +#define LogMerchantsDetail(message, ...) do {\ + if (LogSys.log_settings[Logs::Merchants].is_category_enabled == 1)\ + OutF(LogSys, Logs::Detail, Logs::Merchants, __FILE__, __func__, __LINE__, message, ##__VA_ARGS__);\ +} while (0) + #define Log(debug_level, log_category, message, ...) do {\ if (LogSys.log_settings[log_category].is_category_enabled == 1)\ LogSys.Out(debug_level, log_category, __FILE__, __func__, __LINE__, message, ##__VA_ARGS__);\ @@ -910,6 +920,12 @@ #define LogHotReloadDetail(message, ...) do {\ } while (0) +#define LogMerchants(message, ...) do {\ +} while (0) + +#define LogMerchantsDetail(message, ...) do {\ +} while (0) + #define Log(debug_level, log_category, message, ...) do {\ } while (0) diff --git a/zone/spawn2.cpp b/zone/spawn2.cpp index d362c8f44..74ec8d588 100644 --- a/zone/spawn2.cpp +++ b/zone/spawn2.cpp @@ -480,7 +480,7 @@ bool ZoneDatabase::PopulateZoneSpawnListClose(uint32 zoneid, LinkedList zone_name, version ); - results = QueryDatabase(query); + results = database.QueryDatabase(query); if (!results.Success()) { return false; @@ -547,7 +547,7 @@ bool ZoneDatabase::PopulateZoneSpawnList(uint32 zoneid, LinkedList &spa "WHERE instance_id = %u", zone->GetInstanceID() ); - auto results = QueryDatabase(spawn_query); + auto results = database.QueryDatabase(spawn_query); for (auto row = results.begin(); row != results.end(); ++row) { uint32 start_duration = atoi(row[1]) > 0 ? atoi(row[1]) : 0; uint32 end_duration = atoi(row[2]) > 0 ? atoi(row[2]) : 0; @@ -989,7 +989,7 @@ bool SpawnConditionManager::LoadSpawnConditions(const char* zone_name, uint32 in query = StringFormat("SELECT id, value FROM spawn_condition_values " "WHERE zone = '%s' AND instance_id = %u", zone_name, instance_id); - results = content_db.QueryDatabase(query); + results = database.QueryDatabase(query); if (!results.Success()) { spawn_conditions.clear(); return false; diff --git a/zone/waypoints.cpp b/zone/waypoints.cpp index 47c293f12..fe470bd78 100644 --- a/zone/waypoints.cpp +++ b/zone/waypoints.cpp @@ -575,7 +575,7 @@ void NPC::AssignWaypoints(int32 grid_id, int start_wp) Waypoints.clear(); roamer = false; - auto grid_entry = GridRepository::GetGrid(zone->grids, grid_id); + auto grid_entry = GridRepository::GetGrid(zone->zone_grids, grid_id); if (grid_entry.id == 0) { return; } @@ -588,7 +588,7 @@ void NPC::AssignWaypoints(int32 grid_id, int start_wp) roamer = true; max_wp = 0; // Initialize it; will increment it for each waypoint successfully added to the list - for (auto &entry : zone->grid_entries) { + for (auto &entry : zone->zone_grid_entries) { if (entry.gridid == grid_id) { wplist new_waypoint{}; new_waypoint.index = max_wp; diff --git a/zone/zone.cpp b/zone/zone.cpp index 1bbdd411e..5c60e06fc 100755 --- a/zone/zone.cpp +++ b/zone/zone.cpp @@ -419,48 +419,79 @@ uint32 Zone::GetTempMerchantQuantity(uint32 NPCID, uint32 Slot) { return 0; } -void Zone::LoadTempMerchantData() { +void Zone::LoadTempMerchantData() +{ LogInfo("Loading Temporary Merchant Lists"); - std::string query = StringFormat( - "SELECT " - "DISTINCT ml.npcid, " - "ml.slot, " - "ml.charges, " - "ml.itemid " - "FROM " - "merchantlist_temp ml, " - "spawnentry se, " - "spawn2 s2 " - "WHERE " - "ml.npcid = se.npcid " - "AND se.spawngroupid = s2.spawngroupid " - "AND s2.zone = '%s' AND s2.version = %i " - "ORDER BY ml.slot ", GetShortName(), GetInstanceVersion()); - auto results = content_db.QueryDatabase(query); - if (!results.Success()) { - return; - } - std::map >::iterator cur; - uint32 npcid = 0; + + auto results = content_db.QueryDatabase( + fmt::format( + SQL( + SELECT + DISTINCT npc_types.id + FROM + npc_types + JOIN spawnentry ON spawnentry.npcID = npc_types.id + JOIN spawn2 ON spawn2.spawngroupID = spawnentry.spawngroupID + WHERE + spawn2.zone = '{}' + AND spawn2.version = {} + ), + GetShortName(), + GetInstanceVersion() + ) + ); + + std::vector npc_ids; for (auto row = results.begin(); row != results.end(); ++row) { - TempMerchantList ml; - ml.npcid = atoul(row[0]); - if (npcid != ml.npcid){ - cur = tmpmerchanttable.find(ml.npcid); - if (cur == tmpmerchanttable.end()) { - std::list empty; - tmpmerchanttable[ml.npcid] = empty; - cur = tmpmerchanttable.find(ml.npcid); - } - npcid = ml.npcid; - } - ml.slot = atoul(row[1]); - ml.charges = atoul(row[2]); - ml.item = atoul(row[3]); - ml.origslot = ml.slot; - cur->second.push_back(ml); + npc_ids.push_back(row[0]); + } + + results = database.QueryDatabase( + fmt::format( + SQL( + npcid, + slot, + charges, + itemid + FROM merchantlist_temp + WHERE npcid IN ({}) + ), + implode(", ", npc_ids) + ) + ); + + std::map >::iterator temp_merchant_table_entry; + + uint32 npc_id = 0; + for (auto row = results.begin(); row != results.end(); ++row) { + TempMerchantList temp_merchant_list; + temp_merchant_list.npcid = atoul(row[0]); + if (npc_id != temp_merchant_list.npcid) { + temp_merchant_table_entry = tmpmerchanttable.find(temp_merchant_list.npcid); + if (temp_merchant_table_entry == tmpmerchanttable.end()) { + std::list empty; + tmpmerchanttable[temp_merchant_list.npcid] = empty; + temp_merchant_table_entry = tmpmerchanttable.find(temp_merchant_list.npcid); + } + npc_id = temp_merchant_list.npcid; + } + + temp_merchant_list.slot = atoul(row[1]); + temp_merchant_list.charges = atoul(row[2]); + temp_merchant_list.item = atoul(row[3]); + temp_merchant_list.origslot = temp_merchant_list.slot; + + LogMerchants( + "[LoadTempMerchantData] Loading merchant temp items npc_id [{}] slot [{}] charges [{}] item [{}] origslot [{}]", + npc_id, + temp_merchant_list.slot, + temp_merchant_list.charges, + temp_merchant_list.item, + temp_merchant_list.origslot + ); + + temp_merchant_table_entry->second.push_back(temp_merchant_list); } - pQueuedMerchantsWorkID = 0; } void Zone::LoadNewMerchantData(uint32 merchantid) { @@ -807,7 +838,6 @@ Zone::Zone(uint32 in_zoneid, uint32 in_instanceid, const char* in_short_name) pgraveyard_id = 0; pgraveyard_zoneid = 0; pMaxClients = 0; - pQueuedMerchantsWorkID = 0; pvpzone = false; if(database.GetServerType() == 1) pvpzone = true; @@ -907,16 +937,18 @@ Zone::~Zone() { //Modified for timezones. bool Zone::Init(bool iStaticZone) { SetStaticZone(iStaticZone); - - //load the zone config file. - if (!LoadZoneCFG(zone->GetShortName(), zone->GetInstanceVersion())) // try loading the zone name... - LoadZoneCFG(zone->GetFileName(), zone->GetInstanceVersion()); // if that fails, try the file name, then load defaults - if(RuleManager::Instance()->GetActiveRulesetID() != default_ruleset) - { + //load the zone config file. + if (!LoadZoneCFG(zone->GetShortName(), zone->GetInstanceVersion())) { // try loading the zone name... + LoadZoneCFG( + zone->GetFileName(), + zone->GetInstanceVersion() + ); + } // if that fails, try the file name, then load defaults + + if (RuleManager::Instance()->GetActiveRulesetID() != default_ruleset) { std::string r_name = RuleManager::Instance()->GetRulesetName(&database, default_ruleset); - if(r_name.size() > 0) - { + if (r_name.size() > 0) { RuleManager::Instance()->LoadRules(&database, r_name.c_str(), false); } } @@ -980,12 +1012,11 @@ bool Zone::Init(bool iStaticZone) { LogInfo("Flushing old respawn timers"); database.QueryDatabase("DELETE FROM `respawn_times` WHERE (`start` + `duration`) < UNIX_TIMESTAMP(NOW())"); - //load up the zone's doors (prints inside) zone->LoadZoneDoors(zone->GetShortName(), zone->GetInstanceVersion()); zone->LoadZoneBlockedSpells(zone->GetZoneID()); //clear trader items if we are loading the bazaar - if(strncasecmp(short_name,"bazaar",6)==0) { + if (strncasecmp(short_name, "bazaar", 6) == 0) { database.DeleteTraderItem(0); database.DeleteBuyLines(0); } @@ -2487,6 +2518,6 @@ void Zone::SetQuestHotReloadQueued(bool in_quest_hot_reload_queued) void Zone::LoadGrids() { - grids = GridRepository::GetZoneGrids(GetZoneID()); - grid_entries = GridEntriesRepository::GetZoneGridEntries(GetZoneID()); -} + zone_grids = GridRepository::GetZoneGrids(GetZoneID()); + zone_grid_entries = GridEntriesRepository::GetZoneGridEntries(GetZoneID()); +} \ No newline at end of file diff --git a/zone/zone.h b/zone/zone.h index 9dfe2c36e..e0dbfe38e 100755 --- a/zone/zone.h +++ b/zone/zone.h @@ -205,8 +205,8 @@ public: std::unordered_map> aa_abilities; std::unordered_map> aa_ranks; - std::vector grids; - std::vector grid_entries; + std::vector zone_grids; + std::vector zone_grid_entries; time_t weather_timer; Timer spawn2_timer; @@ -372,8 +372,6 @@ private: uint32 pMaxClients; uint32 zoneid; uint32 m_last_ucss_update; - uint32 pQueuedMerchantsWorkID; - uint32 pQueuedTempMerchantsWorkID; GlobalLootManager m_global_loot; LinkedList client_auth_list;