TrinityCore
MailDraft Class Reference

#include <Mail.h>

Public Member Functions

 MailDraft (uint16 mailTemplateId, bool need_items=true)
 
 MailDraft (std::string const &subject, std::string const &body)
 
uint16 GetMailTemplateId () const
 
std::string const & GetSubject () const
 
uint64 GetMoney () const
 
uint64 GetCOD () const
 
std::string const & GetBody () const
 
MailDraftAddItem (Item *item)
 
MailDraftAddMoney (uint64 money)
 
MailDraftAddCOD (uint64 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)
 

Private Types

typedef std::map< ObjectGuid::LowType, 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
 
uint64 m_money
 
uint64 m_COD
 

Member Typedef Documentation

◆ MailItemMap

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

Constructor & Destructor Documentation

◆ MailDraft() [1/2]

MailDraft::MailDraft ( uint16  mailTemplateId,
bool  need_items = true 
)
inlineexplicit
130  : m_mailTemplateId(mailTemplateId), m_mailTemplateItemsNeed(need_items), m_money(0), m_COD(0)
131  { }
bool m_mailTemplateItemsNeed
Definition: Mail.h:155
uint64 m_COD
Definition: Mail.h:162
uint64 m_money
Definition: Mail.h:161
uint16 m_mailTemplateId
Definition: Mail.h:154

◆ MailDraft() [2/2]

MailDraft::MailDraft ( std::string const &  subject,
std::string const &  body 
)
inline
133  : m_mailTemplateId(0), m_mailTemplateItemsNeed(false), m_subject(subject), m_body(body), m_money(0), m_COD(0) { }
bool m_mailTemplateItemsNeed
Definition: Mail.h:155
std::string m_body
Definition: Mail.h:157
uint64 m_COD
Definition: Mail.h:162
uint64 m_money
Definition: Mail.h:161
uint16 m_mailTemplateId
Definition: Mail.h:154
std::string m_subject
Definition: Mail.h:156

Member Function Documentation

◆ AddCOD()

MailDraft& MailDraft::AddCOD ( uint64  COD)
inline
144 { m_COD = COD; return *this; }
uint64 m_COD
Definition: Mail.h:162
+ Here is the caller graph for this function:

◆ AddItem()

MailDraft & MailDraft::AddItem ( Item item)
99 {
100  m_items[item->GetGUID().GetCounter()] = item;
101  return *this;
102 }
MailItemMap m_items
Definition: Mail.h:159
LowType GetCounter() const
Definition: ObjectGuid.h:273
ObjectGuid const & GetGUID() const
Definition: Object.h:154
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AddMoney()

MailDraft& MailDraft::AddMoney ( uint64  money)
inline
143 { m_money = money; return *this; }
uint64 m_money
Definition: Mail.h:161
+ Here is the caller graph for this function:

◆ deleteIncludedItems()

void MailDraft::deleteIncludedItems ( CharacterDatabaseTransaction trans,
bool  inDB = false 
)
private
131 {
132  for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
133  {
134  Item* item = mailItemIter->second;
135 
136  if (inDB)
137  item->DeleteFromDB(trans);
138 
139  delete item;
140  }
141 
142  m_items.clear();
143 }
static void DeleteFromDB(CharacterDatabaseTransaction &trans, ObjectGuid::LowType itemGuid)
Definition: Item.cpp:1054
MailItemMap m_items
Definition: Mail.h:159
Definition: Item.h:169
+ Here is the call graph for this function:

◆ GetBody()

std::string const& MailDraft::GetBody ( ) const
inline
139 { return m_body; }
std::string m_body
Definition: Mail.h:157

◆ GetCOD()

uint64 MailDraft::GetCOD ( ) const
inline
138 { return m_COD; }
uint64 m_COD
Definition: Mail.h:162

◆ GetMailTemplateId()

uint16 MailDraft::GetMailTemplateId ( ) const
inline
135 { return m_mailTemplateId; }
uint16 m_mailTemplateId
Definition: Mail.h:154

◆ GetMoney()

uint64 MailDraft::GetMoney ( ) const
inline
137 { return m_money; }
uint64 m_money
Definition: Mail.h:161

◆ GetSubject()

std::string const& MailDraft::GetSubject ( ) const
inline
136 { return m_subject; }
std::string m_subject
Definition: Mail.h:156

◆ prepareItems()

void MailDraft::prepareItems ( Player receiver,
CharacterDatabaseTransaction trans 
)
private
105 {
107  return;
108 
109  m_mailTemplateItemsNeed = false;
110 
111  Loot mailLoot;
112 
113  // can be empty
115 
116  uint32 max_slot = mailLoot.GetMaxSlotInLootFor(receiver);
117  for (uint32 i = 0; m_items.size() < MAX_MAIL_ITEMS && i < max_slot; ++i)
118  {
119  if (LootItem* lootitem = mailLoot.LootItemInSlot(i, receiver))
120  {
121  if (Item* item = Item::CreateItem(lootitem->itemid, lootitem->count, lootitem->context, receiver))
122  {
123  item->SaveToDB(trans); // save for prevent lost at next mail load, if send fail then item will deleted
124  AddItem(item);
125  }
126  }
127  }
128 }
static Item * CreateItem(uint32 itemEntry, uint32 count, ItemContext context, Player const *player=nullptr)
Definition: Item.cpp:1546
MailItemMap m_items
Definition: Mail.h:159
bool m_mailTemplateItemsNeed
Definition: Mail.h:155
uint32 GetMaxSlotInLootFor(Player *player) const
Definition: Loot.cpp:367
Definition: SharedDefines.h:69
LootStore LootTemplates_Mail("mail_loot_template", "mail template id", false)
LootItem * LootItemInSlot(uint32 lootslot, Player *player, NotNormalLootItem **qitem=nullptr, NotNormalLootItem **ffaitem=nullptr, NotNormalLootItem **conditem=nullptr)
Definition: Loot.cpp:304
Definition: Loot.h:207
Definition: Item.h:169
uint32_t uint32
Definition: Define.h:152
uint16 m_mailTemplateId
Definition: Mail.h:154
Definition: Loot.h:134
MailDraft & AddItem(Item *item)
Definition: Mail.cpp:98
#define MAX_MAIL_ITEMS
Definition: Mail.h:35
bool FillLoot(uint32 lootId, LootStore const &store, Player *lootOwner, bool personal, bool noEmptyError=false, uint16 lootMode=LOOT_MODE_DEFAULT, ItemContext context=ItemContext::NONE)
Definition: Loot.cpp:210
+ Here is the call graph for this function:

◆ SendMailTo()

void MailDraft::SendMailTo ( CharacterDatabaseTransaction trans,
MailReceiver const &  receiver,
MailSender const &  sender,
MailCheckMask  checked = MAIL_CHECK_MASK_NONE,
uint32  deliver_delay = 0 
)
189 {
190  Player* pReceiver = receiver.GetPlayer(); // can be NULL
191  Player* pSender = sender.GetMailMessageType() == MAIL_NORMAL ? ObjectAccessor::FindPlayer(ObjectGuid::Create<HighGuid::Player>(sender.GetSenderId())) : nullptr;
192 
193  if (pReceiver)
194  prepareItems(pReceiver, trans); // generate mail template items
195 
196  uint32 mailId = sObjectMgr->GenerateMailID();
197 
198  time_t deliver_time = GameTime::GetGameTime() + deliver_delay;
199 
200  //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
201  uint32 expire_delay;
202 
203  // auction mail without any items and money
204  if (sender.GetMailMessageType() == MAIL_AUCTION && m_items.empty() && !m_money)
205  expire_delay = sWorld->getIntConfig(CONFIG_MAIL_DELIVERY_DELAY);
206  // default case: expire time if COD 3 days, if no COD 30 days (or 90 days if sender is a game master)
207  else
208  {
209  if (m_COD)
210  expire_delay = 3 * DAY;
211  else
212  expire_delay = pSender && pSender->IsGameMaster() ? 90 * DAY : 30 * DAY;
213  }
214 
215  time_t expire_time = deliver_time + expire_delay;
216 
217  // Add to DB
218  uint8 index = 0;
220  stmt->setUInt32( index, mailId);
221  stmt->setUInt8 (++index, uint8(sender.GetMailMessageType()));
222  stmt->setInt8 (++index, int8(sender.GetStationery()));
223  stmt->setUInt16(++index, GetMailTemplateId());
224  stmt->setUInt64(++index, sender.GetSenderId());
225  stmt->setUInt64(++index, receiver.GetPlayerGUIDLow());
226  stmt->setString(++index, GetSubject());
227  stmt->setString(++index, GetBody());
228  stmt->setBool (++index, !m_items.empty());
229  stmt->setInt64 (++index, expire_time);
230  stmt->setInt64 (++index, deliver_time);
231  stmt->setUInt64(++index, m_money);
232  stmt->setUInt64(++index, m_COD);
233  stmt->setUInt8 (++index, uint8(checked));
234  trans->Append(stmt);
235 
236  for (MailItemMap::const_iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
237  {
238  Item* pItem = mailItemIter->second;
239  stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_MAIL_ITEM);
240  stmt->setUInt32(0, mailId);
241  stmt->setUInt64(1, pItem->GetGUID().GetCounter());
242  stmt->setUInt64(2, receiver.GetPlayerGUIDLow());
243  trans->Append(stmt);
244  }
245 
246  // For online receiver update in game mail status and data
247  if (pReceiver)
248  {
249  pReceiver->AddNewMailDeliverTime(deliver_time);
250 
251  if (pReceiver->IsMailsLoaded())
252  {
253  Mail* m = new Mail;
254  m->messageID = mailId;
256  m->subject = GetSubject();
257  m->body = GetBody();
258  m->money = GetMoney();
259  m->COD = GetCOD();
260 
261  for (MailItemMap::const_iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
262  {
263  Item* item = mailItemIter->second;
264  m->AddItem(item->GetGUID().GetCounter(), item->GetEntry());
265  }
266 
267  m->messageType = sender.GetMailMessageType();
268  m->stationery = sender.GetStationery();
269  m->sender = sender.GetSenderId();
270  m->receiver = receiver.GetPlayerGUIDLow();
271  m->expire_time = expire_time;
272  m->deliver_time = deliver_time;
273  m->checked = checked;
275 
276  pReceiver->AddMail(m); // to insert new mail to beginning of maillist
277 
278  if (!m_items.empty())
279  {
280  for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
281  pReceiver->AddMItem(mailItemIter->second);
282  }
283  }
284  else if (!m_items.empty())
285  {
287  deleteIncludedItems(temp);
288  }
289  }
290  else if (!m_items.empty())
291  {
293  deleteIncludedItems(temp);
294  }
295 }
bool IsMailsLoaded() const
Definition: Player.h:1697
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
void deleteIncludedItems(CharacterDatabaseTransaction &trans, bool inDB=false)
Definition: Mail.cpp:130
void setInt8(const uint8 index, const int8 value)
Definition: PreparedStatement.cpp:132
int8_t int8
Definition: Define.h:150
MailItemMap m_items
Definition: Mail.h:159
uint16 mailTemplateId
Definition: Mail.h:177
Definition: Mail.h:40
std::string const & GetSubject() const
Definition: Mail.h:136
uint16 GetMailTemplateId() const
Definition: Mail.h:135
uint8 messageType
Definition: Mail.h:175
void AddMItem(Item *it)
Definition: Player.cpp:22093
Definition: CharacterDatabase.h:144
MailState state
Definition: Mail.h:189
uint32 checked
Definition: Mail.h:188
LowType GetCounter() const
Definition: ObjectGuid.h:273
uint8 stationery
Definition: Mail.h:176
uint64 COD
Definition: Mail.h:187
void setInt64(const uint8 index, const int64 value)
Definition: PreparedStatement.cpp:156
Definition: Mail.h:39
ObjectGuid::LowType receiver
Definition: Mail.h:179
void setUInt8(const uint8 index, const uint8 value)
Definition: PreparedStatement.cpp:100
void prepareItems(Player *receiver, CharacterDatabaseTransaction &trans)
Definition: Mail.cpp:104
#define sWorld
Definition: World.h:926
void AddNewMailDeliverTime(time_t deliver_time)
Definition: Player.cpp:2709
void AddMail(Mail *mail)
Definition: Player.h:1701
uint64 m_COD
Definition: Mail.h:162
#define sObjectMgr
Definition: ObjectMgr.h:1880
Definition: DatabaseEnvFwd.h:40
uint64 m_money
Definition: Mail.h:161
Definition: Common.h:64
uint64 money
Definition: Mail.h:186
uint64 GetMoney() const
Definition: Mail.h:137
std::string body
Definition: Mail.h:181
void setUInt16(const uint8 index, const uint16 value)
Definition: PreparedStatement.cpp:108
Definition: Mail.h:172
TC_GAME_API Player * FindPlayer(ObjectGuid const &)
Definition: ObjectAccessor.cpp:264
ObjectGuid const & GetGUID() const
Definition: Object.h:154
void setBool(const uint8 index, const bool value)
Definition: PreparedStatement.cpp:92
time_t GetGameTime()
Definition: GameTime.cpp:39
bool IsGameMaster() const
Definition: Player.h:1154
void setUInt32(const uint8 index, const uint32 value)
Definition: PreparedStatement.cpp:116
Definition: Item.h:169
uint32_t uint32
Definition: Define.h:152
time_t deliver_time
Definition: Mail.h:185
Definition: World.h:284
void setString(const uint8 index, const std::string &value)
Definition: PreparedStatement.cpp:180
Definition: CharacterDatabase.h:142
time_t expire_time
Definition: Mail.h:184
uint32 GetEntry() const
Definition: Object.h:155
std::string subject
Definition: Mail.h:180
void AddItem(ObjectGuid::LowType itemGuidLow, uint32 item_template)
Definition: Mail.h:191
std::string const & GetBody() const
Definition: Mail.h:139
uint8_t uint8
Definition: Define.h:154
uint64 GetCOD() const
Definition: Mail.h:138
void setUInt64(const uint8 index, const uint64 value)
Definition: PreparedStatement.cpp:124
uint32 messageID
Definition: Mail.h:174
Definition: Player.h:1081
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
Definition: DatabaseEnvFwd.h:72
Definition: Mail.h:72
ObjectGuid::LowType sender
Definition: Mail.h:178
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SendReturnToSender()

void MailDraft::SendReturnToSender ( uint32  sender_acc,
ObjectGuid::LowType  sender_guid,
ObjectGuid::LowType  receiver_guid,
CharacterDatabaseTransaction trans 
)
146 {
147  ObjectGuid receiverGuid = ObjectGuid::Create<HighGuid::Player>(receiver_guid);
148  Player* receiver = ObjectAccessor::FindConnectedPlayer(receiverGuid);
149 
150  uint32 rc_account = 0;
151  if (!receiver)
152  rc_account = sCharacterCache->GetCharacterAccountIdByGuid(receiverGuid);
153 
154  if (!receiver && !rc_account) // sender not exist
155  {
156  deleteIncludedItems(trans, true);
157  return;
158  }
159 
160  // prepare mail and send in other case
161  bool needItemDelay = false;
162 
163  if (!m_items.empty())
164  {
165  // if item send to character at another account, then apply item delivery delay
166  needItemDelay = sender_acc != rc_account;
167 
168  // set owner to new receiver (to prevent delete item with sender char deleting)
169  for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
170  {
171  Item* item = mailItemIter->second;
172  item->SaveToDB(trans); // item not in inventory and can be save standalone
173  // owner in data will set at mail receive and item extracting
175  stmt->setUInt64(0, receiver_guid);
176  stmt->setUInt64(1, item->GetGUID().GetCounter());
177  trans->Append(stmt);
178  }
179  }
180 
181  // If theres is an item, there is a one hour delivery delay.
182  uint32 deliver_delay = needItemDelay ? sWorld->getIntConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
183 
184  // will delete item or place to receiver mail list
185  SendMailTo(trans, MailReceiver(receiver, receiver_guid), MailSender(MAIL_NORMAL, sender_guid), MAIL_CHECK_MASK_RETURNED, deliver_delay);
186 }
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
void deleteIncludedItems(CharacterDatabaseTransaction &trans, bool inDB=false)
Definition: Mail.cpp:130
void SendMailTo(CharacterDatabaseTransaction &trans, MailReceiver const &receiver, MailSender const &sender, MailCheckMask checked=MAIL_CHECK_MASK_NONE, uint32 deliver_delay=0)
Definition: Mail.cpp:188
MailItemMap m_items
Definition: Mail.h:159
LowType GetCounter() const
Definition: ObjectGuid.h:273
Definition: Mail.h:39
#define sWorld
Definition: World.h:926
Definition: DatabaseEnvFwd.h:40
#define sCharacterCache
Definition: CharacterCache.h:73
ObjectGuid const & GetGUID() const
Definition: Object.h:154
virtual void SaveToDB(CharacterDatabaseTransaction &trans)
Definition: Item.cpp:539
Definition: Item.h:169
uint32_t uint32
Definition: Define.h:152
Definition: World.h:284
Definition: CharacterDatabase.h:152
Definition: ObjectGuid.h:248
Definition: Mail.h:52
void setUInt64(const uint8 index, const uint64 value)
Definition: PreparedStatement.cpp:124
Definition: Player.h:1081
TC_GAME_API Player * FindConnectedPlayer(ObjectGuid const &)
Definition: ObjectAccessor.cpp:285
Definition: Mail.h:109
Definition: Mail.h:86
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_body

std::string MailDraft::m_body
private

◆ m_COD

uint64 MailDraft::m_COD
private

◆ m_items

MailItemMap MailDraft::m_items
private

◆ m_mailTemplateId

uint16 MailDraft::m_mailTemplateId
private

◆ m_mailTemplateItemsNeed

bool MailDraft::m_mailTemplateItemsNeed
private

◆ m_money

uint64 MailDraft::m_money
private

◆ m_subject

std::string MailDraft::m_subject
private

The documentation for this class was generated from the following files: