AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
LootItemStorage Class Reference

#include "LootItemStorage.h"

Public Member Functions

void LoadStorageFromDB ()
 
void RemoveEntryFromDB (ObjectGuid containerGUID, uint32 itemid, uint32 count, uint32 itemIndex)
 
void AddNewStoredLoot (Loot *loot, Player *player)
 
bool LoadStoredLoot (Item *item, Player *player)
 
void RemoveStoredLootItem (ObjectGuid containerGUID, uint32 itemid, uint32 count, Loot *loot, uint32 itemIndex)
 
void RemoveStoredLootMoney (ObjectGuid containerGUID, Loot *loot)
 
void RemoveStoredLoot (ObjectGuid containerGUID)
 

Static Public Member Functions

static LootItemStorageinstance ()
 

Private Member Functions

 LootItemStorage ()
 
 ~LootItemStorage ()
 

Private Attributes

LootItemContainer lootItemStore
 

Detailed Description

Constructor & Destructor Documentation

◆ LootItemStorage()

LootItemStorage::LootItemStorage ( )
private
25{
26}

◆ ~LootItemStorage()

LootItemStorage::~LootItemStorage ( )
private
29{
30}

Member Function Documentation

◆ AddNewStoredLoot()

void LootItemStorage::AddNewStoredLoot ( Loot loot,
Player player 
)
83{
84 if (lootItemStore.find(loot->containerGUID) != lootItemStore.end())
85 {
86 LOG_INFO("misc", "LootItemStorage::AddNewStoredLoot (A1) - {}!", loot->containerGUID.ToString());
87 return;
88 }
89
90 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
92
94
95 // Gold at first
96 if (loot->gold)
97 {
98 itemList.emplace_back(0, 0, loot->gold, 0, 0, false, false, false, false, false, false, 0);
99
100 uint8 index = 0;
101 stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_ITEMCONTAINER_SINGLE_ITEM);
102 stmt->SetData(index++, loot->containerGUID.GetCounter());
103 stmt->SetData(index++, 0);
104 stmt->SetData(index++, 0);
105 stmt->SetData(index++, loot->gold);
106 stmt->SetData(index++, 0);
107 stmt->SetData(index++, 0);
108 stmt->SetData(index++, false);
109 stmt->SetData(index++, false);
110 stmt->SetData(index++, false);
111 stmt->SetData(index++, false);
112 stmt->SetData(index++, false);
113 stmt->SetData(index++, false);
114 stmt->SetData(index++, 0);
115 trans->Append(stmt);
116 }
117
118 // And normal items
119 if (!loot->isLooted())
120 for (LootItemList::const_iterator li = loot->items.begin(); li != loot->items.end(); li++)
121 {
122 // Even if an item is not available for a specific player, it doesn't mean that
123 // we are not able to trade this container to another player that is able to loot that item
124 // if we don't save it then the item will be lost at player re-login.
125 //if (!li->AllowedForPlayer(player))
126 // continue;
127
128 ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(li->itemid);
129 if (!itemTemplate || itemTemplate->IsCurrencyToken())
130 continue;
131
132 uint32 conditionLootId = 0;
133 if (!li->conditions.empty())
134 {
135 conditionLootId = li->conditions.front()->SourceGroup;
136 }
137
138 itemList.emplace_back(li->itemid, li->itemIndex, li->count, li->randomPropertyId, li->randomSuffix, li->follow_loot_rules, li->freeforall, li->is_blocked, li->is_counted,
139 li->is_underthreshold, li->needs_quest, conditionLootId);
140
141 uint8 index = 0;
142 stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_ITEMCONTAINER_SINGLE_ITEM);
143 stmt->SetData(index++, loot->containerGUID.GetCounter());
144 stmt->SetData(index++, li->itemid);
145 stmt->SetData(index++, li->itemIndex);
146 stmt->SetData(index++, li->count);
147 stmt->SetData (index++, li->randomPropertyId);
148 stmt->SetData(index++, li->randomSuffix);
149 stmt->SetData(index++, li->follow_loot_rules);
150 stmt->SetData(index++, li->freeforall);
151 stmt->SetData(index++, li->is_blocked);
152 stmt->SetData(index++, li->is_counted);
153 stmt->SetData(index++, li->is_underthreshold);
154 stmt->SetData(index++, li->needs_quest);
155 stmt->SetData(index++, conditionLootId);
156
157 trans->Append(stmt);
158 }
159
160 CharacterDatabase.CommitTransaction(trans);
161}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
std::uint8_t uint8
Definition: Define.h:109
std::uint32_t uint32
Definition: Define.h:107
#define sObjectMgr
Definition: ObjectMgr.h:1635
std::list< StoredLootItem > StoredLootItemList
Definition: LootItemStorage.h:47
@ CHAR_INS_ITEMCONTAINER_SINGLE_ITEM
Definition: CharacterDatabase.h:490
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
Definition: DatabaseEnvFwd.h:69
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
Definition: PreparedStatement.h:157
Acore::Types::is_default< T > SetData(const uint8 index, T value)
Definition: PreparedStatement.h:77
Definition: ItemTemplate.h:619
bool IsCurrencyToken() const
Definition: ItemTemplate.h:727
LowType GetCounter() const
Definition: ObjectGuid.h:145
std::string ToString() const
Definition: ObjectGuid.cpp:47
LootItemContainer lootItemStore
Definition: LootItemStorage.h:70
ObjectGuid containerGUID
Definition: LootMgr.h:329
bool isLooted() const
Definition: LootMgr.h:368
uint32 gold
Definition: LootMgr.h:322
std::vector< LootItem > items
Definition: LootMgr.h:320

References CHAR_INS_ITEMCONTAINER_SINGLE_ITEM, CharacterDatabase, Loot::containerGUID, ObjectGuid::GetCounter(), Loot::gold, ItemTemplate::IsCurrencyToken(), Loot::isLooted(), Loot::items, LOG_INFO, lootItemStore, PreparedStatementBase::SetData(), sObjectMgr, and ObjectGuid::ToString().

◆ instance()

LootItemStorage * LootItemStorage::instance ( )
static
33{
35 return &instance;
36}
Definition: LootItemStorage.h:51
static LootItemStorage * instance()
Definition: LootItemStorage.cpp:32

References instance().

Referenced by instance().

◆ LoadStorageFromDB()

void LootItemStorage::LoadStorageFromDB ( )
39{
40 uint32 oldMSTime = getMSTime();
41 lootItemStore.clear();
42
44 PreparedQueryResult result = CharacterDatabase.Query(stmt);
45 if (!result)
46 {
47 LOG_WARN("server.loading", ">> Loaded 0 stored items!");
48 LOG_INFO("server.loading", " ");
49 return;
50 }
51
52 uint32 count = 0;
53 do
54 {
55 Field* fields = result->Fetch();
56
57 StoredLootItemList& itemList = lootItemStore[ObjectGuid::Create<HighGuid::Item>(fields[0].Get<uint32>())];
58 itemList.emplace_back(fields[1].Get<uint32>(), fields[2].Get<uint32>(), fields[3].Get<uint32>(), fields[4].Get<int32>(), fields[5].Get<uint32>(), fields[6].Get<bool>(),
59 fields[7].Get<bool>(), fields[8].Get<bool>(), fields[9].Get<bool>(), fields[10].Get<bool>(), fields[11].Get<bool>(), fields[12].Get<uint32>());
60
61 ++count;
62 } while (result->NextRow());
63
64 LOG_INFO("server.loading", ">> Loaded {} stored items in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
65 LOG_INFO("server.loading", " ");
66}
#define LOG_WARN(filterType__,...)
Definition: Log.h:161
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:131
uint32 getMSTime()
Definition: Timer.h:103
@ CHAR_SEL_ITEMCONTAINER_ITEMS
Definition: CharacterDatabase.h:488
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: DatabaseEnvFwd.h:45
Class used to access individual fields of database query result.
Definition: Field.h:98

References CHAR_SEL_ITEMCONTAINER_ITEMS, CharacterDatabase, getMSTime(), GetMSTimeDiffToNow(), LOG_INFO, LOG_WARN, and lootItemStore.

◆ LoadStoredLoot()

bool LootItemStorage::LoadStoredLoot ( Item item,
Player player 
)
164{
165 ItemTemplate const* proto = sObjectMgr->GetItemTemplate(item->GetEntry());
166 if (!proto)
167 {
168 return false;
169 }
170
171 Loot* loot = &item->loot;
172 LootItemContainer::iterator itr = lootItemStore.find(loot->containerGUID);
173 if (itr == lootItemStore.end())
174 return false;
175
176 StoredLootItemList& itemList = itr->second;
177 for (StoredLootItemList::iterator it2 = itemList.begin(); it2 != itemList.end(); ++it2)
178 {
179 if (it2->itemid == 0)
180 {
181 loot->gold = it2->count;
182 continue;
183 }
184
185 if (LootTemplate const* lt = LootTemplates_Item.GetLootFor(item->GetEntry()))
186 {
187 LootItem li;
188 li.itemid = it2->itemid;
189 li.itemIndex = it2->itemIndex;
190 li.count = it2->count;
191 li.follow_loot_rules = it2->follow_loot_rules;
192 li.freeforall = it2->freeforall;
193 li.is_blocked = it2->is_blocked;
194 li.is_counted = it2->is_counted;
195 li.is_underthreshold = it2->is_underthreshold;
196 li.is_looted = false;
197 li.needs_quest = it2->needs_quest;
198 li.randomPropertyId = it2->randomPropertyId;
199 li.randomSuffix = it2->randomSuffix;
201 li.groupid = 0;
202
203 // Copy the extra loot conditions from the item in the loot template
204 lt->CopyConditions(&li, it2->conditionLootId);
205
206 if (li.needs_quest)
207 {
208 loot->quest_items.push_back(li);
209 }
210 else
211 {
212 loot->items.push_back(li);
213 }
214
215 // non-conditional one-player only items are counted here,
216 // free for all items are counted in FillFFALoot(),
217 // non-ffa conditionals are counted in FillNonQuestNonFFAConditionalLoot()
218 if ((!li.needs_quest && li.conditions.empty() && !proto->HasFlag(ITEM_FLAG_MULTI_DROP)) || li.is_counted)
219 {
220 ++loot->unlootedCount;
221 }
222 }
223 }
224
225 if (loot->unlootedCount)
226 {
227 loot->FillNotNormalLootFor(player);
228 }
229
230 // Mark the item if it has loot so it won't be generated again on open
231 item->m_lootGenerated = true;
232 return true;
233}
@ ITEM_FLAG_MULTI_DROP
Definition: ItemTemplate.h:158
LootStore LootTemplates_Item("item_loot_template", "item entry", true)
bool m_lootGenerated
Definition: Item.h:321
Loot loot
Definition: Item.h:320
bool HasFlag(ItemFlags flag) const
Definition: ItemTemplate.h:827
uint32 GetEntry() const
Definition: Object.h:115
static ObjectGuid const Empty
Definition: ObjectGuid.h:120
Definition: LootMgr.h:154
uint32 itemid
Definition: LootMgr.h:155
bool is_blocked
Definition: LootMgr.h:164
uint8 groupid
Definition: LootMgr.h:170
ObjectGuid rollWinnerGUID
Definition: LootMgr.h:161
bool needs_quest
Definition: LootMgr.h:168
bool follow_loot_rules
Definition: LootMgr.h:169
bool is_underthreshold
Definition: LootMgr.h:166
int32 randomPropertyId
Definition: LootMgr.h:158
uint32 itemIndex
Definition: LootMgr.h:156
uint8 count
Definition: LootMgr.h:162
bool is_looted
Definition: LootMgr.h:163
ConditionList conditions
Definition: LootMgr.h:159
bool freeforall
Definition: LootMgr.h:165
uint32 randomSuffix
Definition: LootMgr.h:157
bool is_counted
Definition: LootMgr.h:167
LootTemplate const * GetLootFor(uint32 loot_id) const
Definition: LootMgr.cpp:247
Definition: LootMgr.h:245
Definition: LootMgr.h:313
void FillNotNormalLootFor(Player *player)
Definition: LootMgr.cpp:626
uint8 unlootedCount
Definition: LootMgr.h:323
std::vector< LootItem > quest_items
Definition: LootMgr.h:321

References LootItem::conditions, Loot::containerGUID, LootItem::count, ObjectGuid::Empty, Loot::FillNotNormalLootFor(), LootItem::follow_loot_rules, LootItem::freeforall, Object::GetEntry(), LootStore::GetLootFor(), Loot::gold, LootItem::groupid, ItemTemplate::HasFlag(), LootItem::is_blocked, LootItem::is_counted, LootItem::is_looted, LootItem::is_underthreshold, ITEM_FLAG_MULTI_DROP, LootItem::itemid, LootItem::itemIndex, Loot::items, Item::loot, lootItemStore, LootTemplates_Item, Item::m_lootGenerated, LootItem::needs_quest, Loot::quest_items, LootItem::randomPropertyId, LootItem::randomSuffix, LootItem::rollWinnerGUID, sObjectMgr, and Loot::unlootedCount.

◆ RemoveEntryFromDB()

void LootItemStorage::RemoveEntryFromDB ( ObjectGuid  containerGUID,
uint32  itemid,
uint32  count,
uint32  itemIndex 
)
69{
70 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
71
73 stmt->SetData(0, containerGUID.GetCounter());
74 stmt->SetData(1, itemid);
75 stmt->SetData(2, count);
76 stmt->SetData(3, itemIndex);
77 trans->Append(stmt);
78
79 CharacterDatabase.CommitTransaction(trans);
80}
@ CHAR_DEL_ITEMCONTAINER_SINGLE_ITEM
Definition: CharacterDatabase.h:489

References CHAR_DEL_ITEMCONTAINER_SINGLE_ITEM, CharacterDatabase, ObjectGuid::GetCounter(), and PreparedStatementBase::SetData().

Referenced by RemoveStoredLootItem(), and RemoveStoredLootMoney().

◆ RemoveStoredLoot()

void LootItemStorage::RemoveStoredLoot ( ObjectGuid  containerGUID)
278{
279 lootItemStore.erase(containerGUID);
280
281 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
282
284 stmt->SetData(0, containerGUID.GetCounter());
285 trans->Append(stmt);
286
287 CharacterDatabase.CommitTransaction(trans);
288}
@ CHAR_DEL_ITEMCONTAINER_CONTAINER
Definition: CharacterDatabase.h:491

References CHAR_DEL_ITEMCONTAINER_CONTAINER, CharacterDatabase, ObjectGuid::GetCounter(), lootItemStore, and PreparedStatementBase::SetData().

◆ RemoveStoredLootItem()

void LootItemStorage::RemoveStoredLootItem ( ObjectGuid  containerGUID,
uint32  itemid,
uint32  count,
Loot loot,
uint32  itemIndex 
)
236{
237 LootItemContainer::iterator itr = lootItemStore.find(containerGUID);
238 if (itr == lootItemStore.end())
239 return;
240
241 StoredLootItemList& itemList = itr->second;
242 for (StoredLootItemList::iterator it2 = itemList.begin(); it2 != itemList.end(); ++it2)
243 if (it2->itemid == itemid && it2->count == count)
244 {
245 RemoveEntryFromDB(containerGUID, itemid, count, itemIndex);
246 itemList.erase(it2);
247 break;
248 }
249
250 // loot with empty itemList but unlootedCount > 0
251 // must be deleted manually by the player or traded
252 if (!loot->unlootedCount)
253 lootItemStore.erase(itr);
254}
void RemoveEntryFromDB(ObjectGuid containerGUID, uint32 itemid, uint32 count, uint32 itemIndex)
Definition: LootItemStorage.cpp:68

References lootItemStore, RemoveEntryFromDB(), and Loot::unlootedCount.

◆ RemoveStoredLootMoney()

void LootItemStorage::RemoveStoredLootMoney ( ObjectGuid  containerGUID,
Loot loot 
)
257{
258 LootItemContainer::iterator itr = lootItemStore.find(containerGUID);
259 if (itr == lootItemStore.end())
260 return;
261
262 StoredLootItemList& itemList = itr->second;
263 for (StoredLootItemList::iterator it2 = itemList.begin(); it2 != itemList.end(); ++it2)
264 if (it2->itemid == 0)
265 {
266 RemoveEntryFromDB(containerGUID, 0, it2->count, 0);
267 itemList.erase(it2);
268 break;
269 }
270
271 // loot with empty itemList but unlootedCount > 0
272 // must be deleted manually by the player or traded
273 if (!loot->unlootedCount)
274 lootItemStore.erase(itr);
275}

References lootItemStore, RemoveEntryFromDB(), and Loot::unlootedCount.

Member Data Documentation

◆ lootItemStore