[Bots] Optimize inventory loading. (#2588)

* [Bots] Optimize inventory loading.

# Notes
- Bots previously were running 23 individual queries to load their inventory versus grabbing their inventory all at once and referencing it in memory.

* Typo.

* Update bot_database.cpp

* Update bot_database.cpp

* Update bot.cpp
This commit is contained in:
Alex King 2022-11-27 19:07:24 -05:00 committed by GitHub
parent d6db35b84e
commit 200c6cccaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 11 deletions

View File

@ -4016,11 +4016,10 @@ bool Bot::Spawn(Client* botCharacterOwner) {
ping_timer.Start(8000);
// there is something askew with spawn struct appearance fields...
// I re-enabled this until I can sort it out
uint32 item_id = 0;
const auto& m = GetBotItemSlots();
uint8 material_from_slot = 0xFF;
for (int slot_id = EQ::invslot::EQUIPMENT_BEGIN; slot_id <= EQ::invslot::EQUIPMENT_END; ++slot_id) {
item_id = GetBotItemBySlot(slot_id);
if (item_id != 0) {
if (m.find(slot_id) != m.end()) {
material_from_slot = EQ::InventoryProfile::CalcMaterialFromSlot(slot_id);
if (material_from_slot != 0xFF) {
SendWearChange(material_from_slot);
@ -4064,6 +4063,26 @@ void Bot::GetBotItems(EQ::InventoryProfile &inv, std::string* error_message)
UpdateEquipmentLight();
}
std::map<uint16, uint32> Bot::GetBotItemSlots()
{
std::map<uint16, uint32> m;
if (!GetBotID()) {
return m;
}
if (!database.botdb.LoadItemSlots(GetBotID(), m)) {
GetBotOwner()->CastToClient()->Message(
Chat::White,
fmt::format(
"Failed to load inventory slots for {}.",
GetCleanName()
).c_str()
);
}
return m;
}
// Returns the inventory record for this bot from the database for the specified equipment slot.
uint32 Bot::GetBotItemBySlot(uint16 slot_id)
{
@ -4077,8 +4096,9 @@ uint32 Bot::GetBotItemBySlot(uint16 slot_id)
GetBotOwner()->CastToClient()->Message(
Chat::White,
fmt::format(
"{}",
BotDatabase::fail::LoadItemBySlot()
"Failed to load slot ID {} for {}.",
slot_id,
GetCleanName()
).c_str()
);
}
@ -4734,11 +4754,11 @@ void Bot::PerformTradeWithClient(int16 begin_slot_id, int16 end_slot_id, Client*
return;
}
}
if (
!trade_instance->IsClassEquipable(GetClass()) ||
GetLevel() < trade_instance->GetItem()->ReqLevel ||
(!trade_instance->IsRaceEquipable(GetRace()) && !RuleB(Bot, AllowBotEquipAnyRaceGear))
(!trade_instance->IsRaceEquipable(GetRace()) && !RuleB(Bots, AllowBotEquipAnyRaceGear))
) {
if (trade_event_exists) {
event_trade.push_back(ClientTrade(trade_instance, trade_index));

View File

@ -558,9 +558,9 @@ public:
inline InspectMessage_Struct& GetInspectMessage() { return _botInspectMessage; }
inline const InspectMessage_Struct& GetInspectMessage() const { return _botInspectMessage; }
// "Quest API" Methods
// "Quest API" Methods
bool HasBotSpellEntry(uint16 spellid);
// "SET" Class Methods
void SetBotSpellID(uint32 newSpellID);
virtual void SetSpawnStatus(bool spawnStatus) { _spawnStatus = spawnStatus; }
@ -672,6 +672,7 @@ public:
uint32 augment_six = 0
);
uint32 CountBotItem(uint32 item_id);
std::map<uint16, uint32> GetBotItemSlots();
uint32 GetBotItemBySlot(uint16 slot_id);
bool HasBotItem(uint32 item_id);
void RemoveBotItem(uint32 item_id);

View File

@ -24,6 +24,8 @@
#include "../common/strings.h"
#include "../common/eqemu_logsys.h"
#include "../common/repositories/bot_inventories_repository.h"
#include "zonedb.h"
#include "../common/zone_store.h"
#include "bot.h"
@ -1360,6 +1362,30 @@ bool BotDatabase::LoadItemBySlot(const uint32 bot_id, const uint32 slot_id, uint
return true;
}
bool BotDatabase::LoadItemSlots(const uint32 bot_id, std::map<uint16, uint32>& m)
{
if (!bot_id) {
return false;
}
const auto& l = BotInventoriesRepository::GetWhere(
database,
fmt::format(
"bot_id = {}",
bot_id
)
);
if (l.empty()) {
return false;
}
for (const auto& e : l) {
m.insert(std::pair<uint16, uint32>(e.slot_id, e.item_id));
}
return true;
}
bool BotDatabase::SaveItemBySlot(Bot* bot_inst, const uint32 slot_id, const EQ::ItemInstance* item_inst)
{
if (!bot_inst || !bot_inst->GetBotID() || slot_id > EQ::invslot::EQUIPMENT_END)
@ -3276,7 +3302,6 @@ const char* BotDatabase::fail::QueryInventoryCount() { return "Failed to query i
const char* BotDatabase::fail::LoadItems() { return "Failed to load items"; }
const char* BotDatabase::fail::SaveItems() { return "Failed to save items"; }
const char* BotDatabase::fail::DeleteItems() { return "Failed to delete items"; }
const char* BotDatabase::fail::LoadItemBySlot() { return "Failed to load item by slot"; }
const char* BotDatabase::fail::SaveItemBySlot() { return "Failed to save item by slot"; }
const char* BotDatabase::fail::DeleteItemBySlot() { return "Failed to delete item by slot"; }
const char* BotDatabase::fail::LoadEquipmentColor() { return "Failed to load equipment color"; }

View File

@ -89,6 +89,7 @@ public:
bool SaveItems(Bot* bot_inst);
bool DeleteItems(const uint32 bot_id);
bool LoadItemSlots(const uint32 bot_id, std::map<uint16, uint32>& m);
bool LoadItemBySlot(Bot* bot_inst);
bool LoadItemBySlot(const uint32 bot_id, const uint32 slot_id, uint32& item_id);
bool SaveItemBySlot(Bot* bot_inst, const uint32 slot_id, const EQ::ItemInstance* item_inst);
@ -219,7 +220,6 @@ public:
static const char* LoadItems();
static const char* SaveItems();
static const char* DeleteItems();
static const char* LoadItemBySlot();
static const char* SaveItemBySlot();
static const char* DeleteItemBySlot();
static const char* LoadEquipmentColor();