TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 constGetSubject () const
 
uint64 GetMoney () const
 
uint64 GetCOD () const
 
std::string constGetBody () const
 
MailDraftAddItem (Item *item)
 
MailDraftAddMoney (uint64 money)
 
MailDraftAddCOD (uint64 COD)
 
void SendReturnToSender (uint32 sender_acc, ObjectGuid::LowType sender_guid, ObjectGuid::LowType receiver_guid, SQLTransaction &trans)
 
void SendMailTo (SQLTransaction &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 (SQLTransaction &trans, bool inDB=false)
 
void prepareItems (Player *receiver, SQLTransaction &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

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

Constructor & Destructor Documentation

MailDraft::MailDraft ( uint16  mailTemplateId,
bool  need_items = true 
)
inlineexplicit
124  : m_mailTemplateId(mailTemplateId), m_mailTemplateItemsNeed(need_items), m_money(0), m_COD(0)
125  { }
bool m_mailTemplateItemsNeed
Definition: Mail.h:149
uint64 m_COD
Definition: Mail.h:156
uint64 m_money
Definition: Mail.h:155
uint16 m_mailTemplateId
Definition: Mail.h:148
MailDraft::MailDraft ( std::string const subject,
std::string const body 
)
inline
127  : m_mailTemplateId(0), m_mailTemplateItemsNeed(false), m_subject(subject), m_body(body), m_money(0), m_COD(0) { }
bool m_mailTemplateItemsNeed
Definition: Mail.h:149
std::string m_body
Definition: Mail.h:151
uint64 m_COD
Definition: Mail.h:156
uint64 m_money
Definition: Mail.h:155
uint16 m_mailTemplateId
Definition: Mail.h:148
std::string m_subject
Definition: Mail.h:150

Member Function Documentation

MailDraft& MailDraft::AddCOD ( uint64  COD)
inline
138 { m_COD = COD; return *this; }
uint64 m_COD
Definition: Mail.h:156

+ Here is the caller graph for this function:

MailDraft & MailDraft::AddItem ( Item item)
80 {
81  m_items[item->GetGUID().GetCounter()] = item;
82  return *this;
83 }
MailItemMap m_items
Definition: Mail.h:153
ObjectGuid const & GetGUID() const
Definition: Object.h:105
LowType GetCounter() const
Definition: ObjectGuid.h:221

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

MailDraft& MailDraft::AddMoney ( uint64  money)
inline
137 { m_money = money; return *this; }
uint64 m_money
Definition: Mail.h:155

+ Here is the caller graph for this function:

void MailDraft::deleteIncludedItems ( SQLTransaction trans,
bool  inDB = false 
)
private
112 {
113  for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
114  {
115  Item* item = mailItemIter->second;
116 
117  if (inDB)
118  {
120  stmt->setUInt64(0, item->GetGUID().GetCounter());
121  trans->Append(stmt);
122  }
123 
124  delete item;
125  }
126 
127  m_items.clear();
128 }
MailItemMap m_items
Definition: Mail.h:153
Definition: PreparedStatement.h:74
Definition: CharacterDatabase.h:145
Definition: Item.h:259
PreparedStatement * GetPreparedStatement(PreparedStatementIndex index)
Definition: DatabaseWorkerPool.h:263
ObjectGuid const & GetGUID() const
Definition: Object.h:105
void setUInt64(const uint8 index, const uint64 value)
Definition: PreparedStatement.cpp:124
CharacterDatabaseWorkerPool CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
LowType GetCounter() const
Definition: ObjectGuid.h:221

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

+ Here is the caller graph for this function:

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

+ Here is the caller graph for this function:

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

+ Here is the caller graph for this function:

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

+ Here is the caller graph for this function:

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

+ Here is the caller graph for this function:

void MailDraft::prepareItems ( Player receiver,
SQLTransaction trans 
)
private
86 {
88  return;
89 
91 
92  Loot mailLoot;
93 
94  // can be empty
95  mailLoot.FillLoot(m_mailTemplateId, LootTemplates_Mail, receiver, true, true);
96 
97  uint32 max_slot = mailLoot.GetMaxSlotInLootFor(receiver);
98  for (uint32 i = 0; m_items.size() < MAX_MAIL_ITEMS && i < max_slot; ++i)
99  {
100  if (LootItem* lootitem = mailLoot.LootItemInSlot(i, receiver))
101  {
102  if (Item* item = Item::CreateItem(lootitem->itemid, lootitem->count, receiver))
103  {
104  item->SaveToDB(trans); // save for prevent lost at next mail load, if send fail then item will deleted
105  AddItem(item);
106  }
107  }
108  }
109 }
LootItem * LootItemInSlot(uint32 lootslot, Player *player, QuestItem **qitem=NULL, QuestItem **ffaitem=NULL, QuestItem **conditem=NULL)
Definition: LootMgr.cpp:734
MailItemMap m_items
Definition: Mail.h:153
uint32 GetMaxSlotInLootFor(Player *player) const
Definition: LootMgr.cpp:797
bool m_mailTemplateItemsNeed
Definition: Mail.h:149
Definition: LootMgr.h:314
Definition: Item.h:259
uint32_t uint32
Definition: Define.h:150
static Item * CreateItem(uint32 itemEntry, uint32 count, Player const *player=NULL)
Definition: Item.cpp:1068
uint16 m_mailTemplateId
Definition: Mail.h:148
Definition: LootMgr.h:157
MailDraft & AddItem(Item *item)
Definition: Mail.cpp:79
LootStore LootTemplates_Mail("mail_loot_template","mail template id", false)
#define MAX_MAIL_ITEMS
Definition: Mail.h:34
bool FillLoot(uint32 lootId, LootStore const &store, Player *lootOwner, bool personal, bool noEmptyError=false, uint16 lootMode=LOOT_MODE_DEFAULT)
Definition: LootMgr.cpp:450

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MailDraft::SendMailTo ( SQLTransaction trans,
MailReceiver const receiver,
MailSender const sender,
MailCheckMask  checked = MAIL_CHECK_MASK_NONE,
uint32  deliver_delay = 0 
)
174 {
175  Player* pReceiver = receiver.GetPlayer(); // can be NULL
176  Player* pSender = sObjectMgr->GetPlayerByLowGUID(sender.GetSenderId());
177 
178  if (pReceiver)
179  prepareItems(pReceiver, trans); // generate mail template items
180 
181  uint32 mailId = sObjectMgr->GenerateMailID();
182 
183  time_t deliver_time = time(NULL) + deliver_delay;
184 
185  //expire time if COD 3 days, if no COD 30 days, if auction sale pending 1 hour
186  uint32 expire_delay;
187 
188  // auction mail without any items and money
189  if (sender.GetMailMessageType() == MAIL_AUCTION && m_items.empty() && !m_money)
190  expire_delay = sWorld->getIntConfig(CONFIG_MAIL_DELIVERY_DELAY);
191  // default case: expire time if COD 3 days, if no COD 30 days (or 90 days if sender is a game master)
192  else
193  {
194  if (m_COD)
195  expire_delay = 3 * DAY;
196  else
197  expire_delay = pSender && pSender->IsGameMaster() ? 90 * DAY : 30 * DAY;
198  }
199 
200  time_t expire_time = deliver_time + expire_delay;
201 
202  // Add to DB
203  uint8 index = 0;
205  stmt->setUInt32( index, mailId);
206  stmt->setUInt8 (++index, uint8(sender.GetMailMessageType()));
207  stmt->setInt8 (++index, int8(sender.GetStationery()));
208  stmt->setUInt16(++index, GetMailTemplateId());
209  stmt->setUInt64(++index, sender.GetSenderId());
210  stmt->setUInt64(++index, receiver.GetPlayerGUIDLow());
211  stmt->setString(++index, GetSubject());
212  stmt->setString(++index, GetBody());
213  stmt->setBool (++index, !m_items.empty());
214  stmt->setUInt64(++index, uint64(expire_time));
215  stmt->setUInt64(++index, uint64(deliver_time));
216  stmt->setUInt64(++index, m_money);
217  stmt->setUInt64(++index, m_COD);
218  stmt->setUInt8 (++index, uint8(checked));
219  trans->Append(stmt);
220 
221  for (MailItemMap::const_iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
222  {
223  Item* pItem = mailItemIter->second;
225  stmt->setUInt32(0, mailId);
226  stmt->setUInt64(1, pItem->GetGUID().GetCounter());
227  stmt->setUInt64(2, receiver.GetPlayerGUIDLow());
228  trans->Append(stmt);
229  }
230 
231  // For online receiver update in game mail status and data
232  if (pReceiver)
233  {
234  pReceiver->AddNewMailDeliverTime(deliver_time);
235 
236  if (pReceiver->IsMailsLoaded())
237  {
238  Mail* m = new Mail;
239  m->messageID = mailId;
241  m->subject = GetSubject();
242  m->body = GetBody();
243  m->money = GetMoney();
244  m->COD = GetCOD();
245 
246  for (MailItemMap::const_iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
247  {
248  Item* item = mailItemIter->second;
249  m->AddItem(item->GetGUID().GetCounter(), item->GetEntry());
250  }
251 
252  m->messageType = sender.GetMailMessageType();
253  m->stationery = sender.GetStationery();
254  m->sender = sender.GetSenderId();
255  m->receiver = receiver.GetPlayerGUIDLow();
256  m->expire_time = expire_time;
257  m->deliver_time = deliver_time;
258  m->checked = checked;
260 
261  pReceiver->AddMail(m); // to insert new mail to beginning of maillist
262 
263  if (!m_items.empty())
264  {
265  for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
266  pReceiver->AddMItem(mailItemIter->second);
267  }
268  }
269  else if (!m_items.empty())
270  {
272  deleteIncludedItems(temp);
273  }
274  }
275  else if (!m_items.empty())
276  {
278  deleteIncludedItems(temp);
279  }
280 }
MailItemMap m_items
Definition: Mail.h:153
uint16 mailTemplateId
Definition: Mail.h:171
uint16 GetMailTemplateId() const
Definition: Mail.h:129
Definition: Mail.h:39
uint8 messageType
Definition: Mail.h:169
MailState state
Definition: Mail.h:183
void setUInt8(const uint8 index, const uint8 value)
Definition: PreparedStatement.cpp:97
int8_t int8
Definition: g3dmath.h:163
uint32 checked
Definition: Mail.h:182
void setString(const uint8 index, const std::string &value)
Definition: PreparedStatement.cpp:187
uint8 stationery
Definition: Mail.h:170
arena_t NULL
Definition: jemalloc_internal.h:624
uint64 COD
Definition: Mail.h:181
ObjectGuid::LowType receiver
Definition: Mail.h:173
uint64_t uint64
Definition: g3dmath.h:170
#define sWorld
Definition: World.h:887
uint64 m_COD
Definition: Mail.h:156
#define sObjectMgr
Definition: ObjectMgr.h:1567
Definition: PreparedStatement.h:74
uint64 m_money
Definition: Mail.h:155
uint64 money
Definition: Mail.h:180
std::string body
Definition: Mail.h:175
Definition: Mail.h:166
void setBool(const uint8 index, const bool value)
Definition: PreparedStatement.cpp:88
Definition: CharacterDatabase.h:128
uint64 GetMoney() const
Definition: Mail.h:131
Definition: Common.h:99
Definition: Item.h:259
void setUInt16(const uint8 index, const uint16 value)
Definition: PreparedStatement.cpp:106
std::string const & GetBody() const
Definition: Mail.h:133
uint32_t uint32
Definition: Define.h:150
void deleteIncludedItems(SQLTransaction &trans, bool inDB=false)
Definition: Mail.cpp:111
time_t deliver_time
Definition: Mail.h:179
Definition: World.h:267
Definition: CharacterDatabase.h:126
PreparedStatement * GetPreparedStatement(PreparedStatementIndex index)
Definition: DatabaseWorkerPool.h:263
void setUInt32(const uint8 index, const uint32 value)
Definition: PreparedStatement.cpp:115
std::string const & GetSubject() const
Definition: Mail.h:130
void setInt8(const uint8 index, const int8 value)
Definition: PreparedStatement.cpp:133
uint8_t uint8
Definition: g3dmath.h:164
ObjectGuid const & GetGUID() const
Definition: Object.h:105
time_t expire_time
Definition: Mail.h:178
std::string subject
Definition: Mail.h:174
void AddItem(ObjectGuid::LowType itemGuidLow, uint32 item_template)
Definition: Mail.h:185
void setUInt64(const uint8 index, const uint64 value)
Definition: PreparedStatement.cpp:124
uint8_t uint8
Definition: Define.h:152
void prepareItems(Player *receiver, SQLTransaction &trans)
Definition: Mail.cpp:85
CharacterDatabaseWorkerPool CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
uint32 GetEntry() const
Definition: Object.h:107
uint32 messageID
Definition: Mail.h:168
uint64 GetCOD() const
Definition: Mail.h:132
LowType GetCounter() const
Definition: ObjectGuid.h:221
std::shared_ptr< Transaction > SQLTransaction
Definition: Transaction.h:58
Definition: Mail.h:69
ObjectGuid::LowType sender
Definition: Mail.h:172

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void MailDraft::SendReturnToSender ( uint32  sender_acc,
ObjectGuid::LowType  sender_guid,
ObjectGuid::LowType  receiver_guid,
SQLTransaction trans 
)
131 {
132  ObjectGuid receiverGuid = ObjectGuid::Create<HighGuid::Player>(receiver_guid);
133  Player* receiver = ObjectAccessor::FindConnectedPlayer(receiverGuid);
134 
135  uint32 rc_account = 0;
136  if (!receiver)
137  rc_account = ObjectMgr::GetPlayerAccountIdByGUID(receiverGuid);
138 
139  if (!receiver && !rc_account) // sender not exist
140  {
141  deleteIncludedItems(trans, true);
142  return;
143  }
144 
145  // prepare mail and send in other case
146  bool needItemDelay = false;
147 
148  if (!m_items.empty())
149  {
150  // if item send to character at another account, then apply item delivery delay
151  needItemDelay = sender_acc != rc_account;
152 
153  // set owner to new receiver (to prevent delete item with sender char deleting)
154  for (MailItemMap::iterator mailItemIter = m_items.begin(); mailItemIter != m_items.end(); ++mailItemIter)
155  {
156  Item* item = mailItemIter->second;
157  item->SaveToDB(trans); // item not in inventory and can be save standalone
158  // owner in data will set at mail receive and item extracting
160  stmt->setUInt64(0, receiver_guid);
161  stmt->setUInt64(1, item->GetGUID().GetCounter());
162  trans->Append(stmt);
163  }
164  }
165 
166  // If theres is an item, there is a one hour delivery delay.
167  uint32 deliver_delay = needItemDelay ? sWorld->getIntConfig(CONFIG_MAIL_DELIVERY_DELAY) : 0;
168 
169  // will delete item or place to receiver mail list
170  SendMailTo(trans, MailReceiver(receiver, receiver_guid), MailSender(MAIL_NORMAL, sender_guid), MAIL_CHECK_MASK_RETURNED, deliver_delay);
171 }
MailItemMap m_items
Definition: Mail.h:153
Definition: Mail.h:38
#define sWorld
Definition: World.h:887
Definition: PreparedStatement.h:74
void SendMailTo(SQLTransaction &trans, MailReceiver const &receiver, MailSender const &sender, MailCheckMask checked=MAIL_CHECK_MASK_NONE, uint32 deliver_delay=0)
Definition: Mail.cpp:173
virtual void SaveToDB(SQLTransaction &trans)
Definition: Item.cpp:311
Definition: Item.h:259
uint32_t uint32
Definition: Define.h:150
void deleteIncludedItems(SQLTransaction &trans, bool inDB=false)
Definition: Mail.cpp:111
Definition: World.h:267
PreparedStatement * GetPreparedStatement(PreparedStatementIndex index)
Definition: DatabaseWorkerPool.h:263
ObjectGuid const & GetGUID() const
Definition: Object.h:105
void setUInt64(const uint8 index, const uint64 value)
Definition: PreparedStatement.cpp:124
static uint32 GetPlayerAccountIdByGUID(ObjectGuid const &guid)
Definition: ObjectMgr.cpp:2302
CharacterDatabaseWorkerPool CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
Definition: ObjectGuid.h:189
Definition: Mail.h:49
TC_GAME_API Player * FindConnectedPlayer(ObjectGuid const &)
Definition: ObjectAccessor.cpp:215
Definition: Mail.h:104
Definition: Mail.h:83
LowType GetCounter() const
Definition: ObjectGuid.h:221
Definition: CharacterDatabase.h:136

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

std::string MailDraft::m_body
private
uint64 MailDraft::m_COD
private
MailItemMap MailDraft::m_items
private
uint16 MailDraft::m_mailTemplateId
private
bool MailDraft::m_mailTemplateItemsNeed
private
uint64 MailDraft::m_money
private
std::string MailDraft::m_subject
private

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