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

#include "Mail.h"

Public Member Functions

 MailDraft (uint16 mailTemplateId, bool need_items=true)
 
 MailDraft (std::string subject, std::string body)
 
uint16 GetMailTemplateId () const
 
std::string const & GetSubject () const
 
uint32 GetMoney () const
 
uint32 GetCOD () const
 
std::string const & GetBody () const
 
MailDraftAddItem (Item *item)
 
MailDraftAddMoney (uint32 money)
 
MailDraftAddCOD (uint32 COD)
 
void SendReturnToSender (uint32 sender_acc, ObjectGuid::LowType sender_guid, ObjectGuid::LowType receiver_guid, CharacterDatabaseTransaction trans)
 
void SendMailTo (CharacterDatabaseTransaction trans, MailReceiver const &receiver, MailSender const &sender, MailCheckMask checked=MAIL_CHECK_MASK_NONE, uint32 deliver_delay=0, uint32 custom_expiration=0, bool deleteMailItemsFromDB=false, bool sendMail=true)
 

Private Types

typedef std::map< ObjectGuid, Item * > MailItemMap
 

Private Member Functions

void deleteIncludedItems (CharacterDatabaseTransaction trans, bool inDB=false)
 
void prepareItems (Player *receiver, CharacterDatabaseTransaction trans)
 

Private Attributes

uint16 m_mailTemplateId
 
bool m_mailTemplateItemsNeed
 
std::string m_subject
 
std::string m_body
 
MailItemMap m_items
 
uint32 m_money
 
uint32 m_COD
 

Detailed Description

Member Typedef Documentation

◆ MailItemMap

typedef std::map<ObjectGuid, Item*> MailDraft::MailItemMap
private

Constructor & Destructor Documentation

◆ MailDraft() [1/2]

MailDraft::MailDraft ( uint16  mailTemplateId,
bool  need_items = true 
)
inlineexplicit
124 : m_mailTemplateId(mailTemplateId), m_mailTemplateItemsNeed(need_items), m_money(0), m_COD(0)
125 {}
uint32 m_COD
Definition: Mail.h:156
uint32 m_money
Definition: Mail.h:155
uint16 m_mailTemplateId
Definition: Mail.h:148
bool m_mailTemplateItemsNeed
Definition: Mail.h:149

◆ MailDraft() [2/2]

MailDraft::MailDraft ( std::string  subject,
std::string  body 
)
inline
127 : m_mailTemplateId(0), m_mailTemplateItemsNeed(false), m_subject(std::move(subject)), m_body(std::move(body)), m_money(0), m_COD(0) {}
std::string m_subject
Definition: Mail.h:150
std::string m_body
Definition: Mail.h:151

Member Function Documentation

◆ AddCOD()

MailDraft & MailDraft::AddCOD ( uint32  COD)
inline
138{ m_COD = COD; return *this; }

References m_COD.

Referenced by WorldSession::HandleSendMail().

◆ AddItem()

◆ AddMoney()

◆ deleteIncludedItems()

void MailDraft::deleteIncludedItems ( CharacterDatabaseTransaction  trans,
bool  inDB = false 
)
private
133{
134 for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
135 {
136 Item* item = mailItemIter->second;
137
138 if (inDB)
139 {
141 stmt->SetData(0, item->GetGUID().GetCounter());
142 trans->Append(stmt);
143 }
144
145 delete item;
146 }
147
148 m_items.clear();
149}
@ CHAR_DEL_ITEM_INSTANCE
Definition: CharacterDatabase.h:127
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: Item.h:220
LowType GetCounter() const
Definition: ObjectGuid.h:145

References CHAR_DEL_ITEM_INSTANCE, CharacterDatabase, ObjectGuid::GetCounter(), Object::GetGUID(), m_items, and PreparedStatementBase::SetData().

Referenced by SendMailTo(), and SendReturnToSender().

◆ GetBody()

std::string const & MailDraft::GetBody ( ) const
inline
133{ return m_body; }

References m_body.

Referenced by SendMailTo().

◆ GetCOD()

uint32 MailDraft::GetCOD ( ) const
inline
132{ return m_COD; }

References m_COD.

Referenced by SendMailTo().

◆ GetMailTemplateId()

uint16 MailDraft::GetMailTemplateId ( ) const
inline
129{ return m_mailTemplateId; }

References m_mailTemplateId.

Referenced by SendMailTo().

◆ GetMoney()

uint32 MailDraft::GetMoney ( ) const
inline
131{ return m_money; }

References m_money.

Referenced by SendMailTo().

◆ GetSubject()

std::string const & MailDraft::GetSubject ( ) const
inline
130{ return m_subject; }

References m_subject.

Referenced by SendMailTo().

◆ prepareItems()

void MailDraft::prepareItems ( Player receiver,
CharacterDatabaseTransaction  trans 
)
private
100{
102 return;
103
105
106 // The mail sent after turning in the quest The Wrath of Neptulon contains 100g
107 // Only quest in the game up to BFA which sends raw gold through mail. So would just be overkill to introduce a new column in the database.
108 if (m_mailTemplateId == 123)
109 {
110 m_money = 1000000;
111 }
112
113 Loot mailLoot;
114
115 // can be empty
116 mailLoot.FillLoot(m_mailTemplateId, LootTemplates_Mail, receiver, true, true);
117
118 uint32 max_slot = mailLoot.GetMaxSlotInLootFor(receiver);
119 for (uint32 i = 0; m_items.size() < MAX_MAIL_ITEMS && i < max_slot; ++i)
120 {
121 if (LootItem* lootitem = mailLoot.LootItemInSlot(i, receiver))
122 {
123 if (Item* item = Item::CreateItem(lootitem->itemid, lootitem->count, receiver))
124 {
125 item->SaveToDB(trans); // save for prevent lost at next mail load, if send fail then item will deleted
126 AddItem(item);
127 }
128 }
129 }
130}
std::uint32_t uint32
Definition: Define.h:107
LootStore LootTemplates_Mail("mail_loot_template", "mail template id", false)
#define MAX_MAIL_ITEMS
Definition: Mail.h:33
static Item * CreateItem(uint32 item, uint32 count, Player const *player=nullptr, bool clone=false, uint32 randomPropertyId=0)
Definition: Item.cpp:1087
Definition: LootMgr.h:154
Definition: LootMgr.h:313
uint32 GetMaxSlotInLootFor(Player *player) const
Definition: LootMgr.cpp:915
LootItem * LootItemInSlot(uint32 lootslot, Player *player, QuestItem **qitem=nullptr, QuestItem **ffaitem=nullptr, QuestItem **conditem=nullptr)
Definition: LootMgr.cpp:850
bool FillLoot(uint32 lootId, LootStore const &store, Player *lootOwner, bool personal, bool noEmptyError=false, uint16 lootMode=LOOT_MODE_DEFAULT, WorldObject *lootSource=nullptr)
Definition: LootMgr.cpp:570
MailDraft & AddItem(Item *item)
Definition: Mail.cpp:93

References AddItem(), Item::CreateItem(), Loot::FillLoot(), Loot::GetMaxSlotInLootFor(), Loot::LootItemInSlot(), LootTemplates_Mail, m_items, m_mailTemplateId, m_mailTemplateItemsNeed, m_money, and MAX_MAIL_ITEMS.

Referenced by SendMailTo().

◆ SendMailTo()

void MailDraft::SendMailTo ( CharacterDatabaseTransaction  trans,
MailReceiver const &  receiver,
MailSender const &  sender,
MailCheckMask  checked = MAIL_CHECK_MASK_NONE,
uint32  deliver_delay = 0,
uint32  custom_expiration = 0,
bool  deleteMailItemsFromDB = false,
bool  sendMail = true 
)
186{
187 sScriptMgr->OnBeforeMailDraftSendMailTo(this, receiver, sender, checked, deliver_delay, custom_expiration, deleteMailItemsFromDB, sendMail);
188
189 if (deleteMailItemsFromDB) // can be changed in the hook
190 deleteIncludedItems(trans, true);
191
192 if (!sendMail) // can be changed in the hook
193 return;
194
195 Player* pReceiver = receiver.GetPlayer(); // can be nullptr
196
197 if (pReceiver)
198 prepareItems(pReceiver, trans); // generate mail template items
199
200 uint32 mailId = sObjectMgr->GenerateMailID();
201
202 time_t deliver_time = GameTime::GetGameTime().count() + deliver_delay;
203
204 //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
205 uint32 expire_delay;
206
207 // auction mail without any items and money
208 if (sender.GetMailMessageType() == MAIL_AUCTION && m_items.empty() && !m_money)
209 expire_delay = sWorld->getIntConfig(CONFIG_MAIL_DELIVERY_DELAY);
210 // mail from battlemaster (rewardmarks) should last only one day
211 else if (sender.GetMailMessageType() == MAIL_CREATURE && sBattlegroundMgr->GetBattleMasterBG(sender.GetSenderId()) != BATTLEGROUND_TYPE_NONE)
212 expire_delay = DAY;
213 // default case: expire time if COD 3 days, if no COD 30 days (or 90 days if sender is a game master)
214 else if (m_COD)
215 expire_delay = 3 * DAY;
216 else if (custom_expiration > 0)
217 expire_delay = custom_expiration * DAY;
218 else
219 {
220 Player* pSender = ObjectAccessor::FindPlayerByLowGUID(sender.GetSenderId());
221 expire_delay = pSender && pSender->GetSession()->GetSecurity() ? 90 * DAY : 30 * DAY;
222 }
223
224 time_t expire_time = deliver_time + expire_delay;
225
226 // Add to DB
227 uint8 index = 0;
229 stmt->SetData( index, mailId);
230 stmt->SetData(++index, uint8(sender.GetMailMessageType()));
231 stmt->SetData(++index, int8(sender.GetStationery()));
232 stmt->SetData(++index, GetMailTemplateId());
233 stmt->SetData(++index, sender.GetSenderId());
234 stmt->SetData(++index, receiver.GetPlayerGUIDLow());
235 stmt->SetData(++index, GetSubject());
236 stmt->SetData(++index, GetBody());
237 stmt->SetData(++index, !m_items.empty());
238 stmt->SetData(++index, uint32(expire_time));
239 stmt->SetData(++index, uint32(deliver_time));
240 stmt->SetData(++index, m_money);
241 stmt->SetData(++index, m_COD);
242 stmt->SetData(++index, uint8(checked));
243 trans->Append(stmt);
244
245 for (MailItemMap::const_iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
246 {
247 stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_MAIL_ITEM);
248 stmt->SetData(0, mailId);
249 stmt->SetData(1, mailItemIter->second->GetGUID().GetCounter());
250 stmt->SetData(2, receiver.GetPlayerGUIDLow());
251 trans->Append(stmt);
252 }
253
254 sCharacterCache->IncreaseCharacterMailCount(ObjectGuid(HighGuid::Player, receiver.GetPlayerGUIDLow()));
255
256 // For online receiver update in game mail status and data
257 if (pReceiver)
258 {
259 pReceiver->AddNewMailDeliverTime(deliver_time);
260
261 Mail* m = new Mail;
262 m->messageID = mailId;
264 m->subject = GetSubject();
265 m->body = GetBody();
266 m->money = GetMoney();
267 m->COD = GetCOD();
268
269 for (MailItemMap::const_iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
270 {
271 Item* item = mailItemIter->second;
272 m->AddItem(item->GetGUID().GetCounter(), item->GetEntry());
273 }
274
275 m->messageType = sender.GetMailMessageType();
276 m->stationery = sender.GetStationery();
277 m->sender = sender.GetSenderId();
278 m->receiver = receiver.GetPlayerGUIDLow();
279 m->expire_time = expire_time;
280 m->deliver_time = deliver_time;
281 m->checked = checked;
283
284 pReceiver->AddMail(m); // to insert new mail to beginning of maillist
285
286 if (!m_items.empty())
287 {
288 for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
289 {
290 pReceiver->AddMItem(mailItemIter->second);
291 }
292 }
293 }
294 else if (!m_items.empty())
295 {
297 }
298}
constexpr auto DAY
Definition: Common.h:49
std::uint8_t uint8
Definition: Define.h:109
std::int8_t int8
Definition: Define.h:105
@ BATTLEGROUND_TYPE_NONE
Definition: SharedDefines.h:3480
#define sObjectMgr
Definition: ObjectMgr.h:1635
@ CONFIG_MAIL_DELIVERY_DELAY
Definition: IWorld.h:263
#define sBattlegroundMgr
Definition: BattlegroundMgr.h:186
#define sCharacterCache
Definition: CharacterCache.h:83
@ MAIL_STATE_UNCHANGED
Definition: Mail.h:68
@ MAIL_AUCTION
Definition: Mail.h:38
@ MAIL_CREATURE
Definition: Mail.h:39
#define sScriptMgr
Definition: ScriptMgr.h:709
@ CHAR_INS_MAIL_ITEM
Definition: CharacterDatabase.h:111
@ CHAR_INS_MAIL
Definition: CharacterDatabase.h:109
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
Definition: DatabaseEnvFwd.h:69
#define sWorld
Definition: World.h:443
Player * FindPlayerByLowGUID(ObjectGuid::LowType lowguid)
Definition: ObjectAccessor.cpp:251
Seconds GetGameTime()
Definition: GameTime.cpp:38
uint32 GetEntry() const
Definition: Object.h:115
Definition: ObjectGuid.h:118
Definition: Player.h:1081
void AddNewMailDeliverTime(time_t deliver_time)
Definition: Player.cpp:2904
void AddMItem(Item *it)
Definition: Player.h:1681
WorldSession * GetSession() const
Definition: Player.h:1998
void AddMail(Mail *mail)
Definition: Player.h:1656
void deleteIncludedItems(CharacterDatabaseTransaction trans, bool inDB=false)
Definition: Mail.cpp:132
uint32 GetCOD() const
Definition: Mail.h:132
uint32 GetMoney() const
Definition: Mail.h:131
std::string const & GetSubject() const
Definition: Mail.h:130
uint16 GetMailTemplateId() const
Definition: Mail.h:129
void prepareItems(Player *receiver, CharacterDatabaseTransaction trans)
Definition: Mail.cpp:99
std::string const & GetBody() const
Definition: Mail.h:133
Definition: Mail.h:167
ObjectGuid::LowType receiver
Definition: Mail.h:173
uint8 messageType
Definition: Mail.h:169
uint32 messageID
Definition: Mail.h:168
time_t expire_time
Definition: Mail.h:178
uint32 sender
Definition: Mail.h:172
uint8 stationery
Definition: Mail.h:170
std::string subject
Definition: Mail.h:174
std::string body
Definition: Mail.h:175
void AddItem(ObjectGuid::LowType itemGuidLow, uint32 item_template)
Definition: Mail.h:185
time_t deliver_time
Definition: Mail.h:179
uint32 COD
Definition: Mail.h:181
uint32 checked
Definition: Mail.h:182
MailState state
Definition: Mail.h:183
uint32 money
Definition: Mail.h:180
uint16 mailTemplateId
Definition: Mail.h:171
AccountTypes GetSecurity() const
Definition: WorldSession.h:359

References Mail::AddItem(), Player::AddMail(), Player::AddMItem(), Player::AddNewMailDeliverTime(), BATTLEGROUND_TYPE_NONE, Mail::body, CHAR_INS_MAIL, CHAR_INS_MAIL_ITEM, CharacterDatabase, Mail::checked, Mail::COD, CONFIG_MAIL_DELIVERY_DELAY, DAY, deleteIncludedItems(), Mail::deliver_time, Mail::expire_time, ObjectAccessor::FindPlayerByLowGUID(), GetBody(), GetCOD(), ObjectGuid::GetCounter(), Object::GetEntry(), GameTime::GetGameTime(), Object::GetGUID(), MailSender::GetMailMessageType(), GetMailTemplateId(), GetMoney(), MailReceiver::GetPlayer(), MailReceiver::GetPlayerGUIDLow(), WorldSession::GetSecurity(), MailSender::GetSenderId(), Player::GetSession(), MailSender::GetStationery(), GetSubject(), m_COD, m_items, m_money, MAIL_AUCTION, MAIL_CREATURE, MAIL_STATE_UNCHANGED, Mail::mailTemplateId, Mail::messageID, Mail::messageType, Mail::money, prepareItems(), Mail::receiver, sBattlegroundMgr, sCharacterCache, Mail::sender, PreparedStatementBase::SetData(), sObjectMgr, sScriptMgr, Mail::state, Mail::stationery, Mail::subject, and sWorld.

Referenced by Player::_LoadBrewOfTheMonth(), Player::_LoadInventory(), Player::AutoUnequipOffhandIfNeed(), AchievementMgr::CompletedAchievement(), Player::GiveLevel(), WorldSession::HandleAuctionRemoveItem(), item_commandscript::HandleItemRefundCommand(), item_commandscript::HandleItemRestoreCommand(), WorldSession::HandleMailTakeItem(), quest_commandscript::HandleQuestComplete(), quest_commandscript::HandleQuestReward(), spell_gen_spirit_of_competition_participant::HandleScript(), spell_gen_spirit_of_competition_winner::HandleScript(), send_commandscript::HandleSendItemsCommand(), WorldSession::HandleSendMail(), send_commandscript::HandleSendMailCommand(), send_commandscript::HandleSendMoneyCommand(), CalendarMgr::RemoveEvent(), Player::RewardQuest(), AuctionHouseMgr::SendAuctionCancelledToBidderMail(), AuctionHouseMgr::SendAuctionExpiredMail(), AuctionHouseMgr::SendAuctionOutbiddedMail(), AuctionHouseMgr::SendAuctionSalePendingMail(), AuctionHouseMgr::SendAuctionSuccessfulMail(), AuctionHouseMgr::SendAuctionWonMail(), Player::SendItemRetrievalMail(), npc_minigob_manabonk::SendMailToPlayer(), SendReturnToSender(), and ObjectMgr::SendServerMail().

◆ SendReturnToSender()

void MailDraft::SendReturnToSender ( uint32  sender_acc,
ObjectGuid::LowType  sender_guid,
ObjectGuid::LowType  receiver_guid,
CharacterDatabaseTransaction  trans 
)
152{
153 Player* receiver = ObjectAccessor::FindPlayerByLowGUID(receiver_guid);
154
155 if (!receiver && !sCharacterCache->GetCharacterAccountIdByGuid(ObjectGuid(HighGuid::Player, receiver_guid)))
156 {
157 deleteIncludedItems(trans, true);
158 return;
159 }
160
161 if (!m_items.empty())
162 {
163 // if item send to character at another account, then apply item delivery delay
164 //needItemDelay = sender_acc != rc_account;
165
166 // set owner to new receiver (to prevent delete item with sender char deleting)
167 for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
168 {
169 Item* item = mailItemIter->second;
170 item->SaveToDB(trans); // item not in inventory and can be save standalone
171 // owner in data will set at mail receive and item extracting
173 stmt->SetData(0, receiver_guid);
174 stmt->SetData(1, item->GetGUID().GetCounter());
175 trans->Append(stmt);
176 }
177 }
178
179 // xinef: WowWiki: "Return mail arrives immediately."
180
181 // will delete item or place to receiver mail list
182 SendMailTo(trans, MailReceiver(receiver, receiver_guid), MailSender(MAIL_NORMAL, sender_guid), MAIL_CHECK_MASK_RETURNED, 0);
183}
@ MAIL_CHECK_MASK_RETURNED
Definition: Mail.h:48
@ MAIL_NORMAL
Definition: Mail.h:37
@ CHAR_UPD_ITEM_OWNER
Definition: CharacterDatabase.h:118
virtual void SaveToDB(CharacterDatabaseTransaction trans)
Definition: Item.cpp:336
Definition: Mail.h:83
Definition: Mail.h:105
void SendMailTo(CharacterDatabaseTransaction trans, MailReceiver const &receiver, MailSender const &sender, MailCheckMask checked=MAIL_CHECK_MASK_NONE, uint32 deliver_delay=0, uint32 custom_expiration=0, bool deleteMailItemsFromDB=false, bool sendMail=true)
Definition: Mail.cpp:185

References CHAR_UPD_ITEM_OWNER, CharacterDatabase, deleteIncludedItems(), ObjectAccessor::FindPlayerByLowGUID(), ObjectGuid::GetCounter(), Object::GetGUID(), m_items, MAIL_CHECK_MASK_RETURNED, MAIL_NORMAL, Item::SaveToDB(), sCharacterCache, SendMailTo(), and PreparedStatementBase::SetData().

Referenced by Player::DeleteFromDB(), and WorldSession::HandleMailReturnToSender().

Member Data Documentation

◆ m_body

std::string MailDraft::m_body
private

Referenced by GetBody().

◆ m_COD

uint32 MailDraft::m_COD
private

Referenced by AddCOD(), GetCOD(), and SendMailTo().

◆ m_items

◆ m_mailTemplateId

uint16 MailDraft::m_mailTemplateId
private

Referenced by GetMailTemplateId(), and prepareItems().

◆ m_mailTemplateItemsNeed

bool MailDraft::m_mailTemplateItemsNeed
private

Referenced by prepareItems().

◆ m_money

uint32 MailDraft::m_money
private

◆ m_subject

std::string MailDraft::m_subject
private

Referenced by GetSubject().