[10969] Some cleanups in MailDraft API

* Use Set* names for cases when function replace old value by new (instead Add*)
* Prevent hidden MailDraft copy create becase if draft have items its can't be just shared
  and need preoprtly cloned, but item close is high price operation (guid use, DB tiuched and etc)
  So this must be explictly operation. In next commits will be added clone function for this.
* Some MailDraft overwrite by assign cases rewrited to more clean way.
This commit is contained in:
VladimirMangos 2011-01-06 02:07:41 +03:00
parent 22115a8d04
commit 231c6d77ce
8 changed files with 39 additions and 31 deletions

View file

@ -128,7 +128,7 @@ void WorldSession::SendAuctionOutbiddedMail(AuctionEntry *auction, uint32 newPri
newPrice, auction->GetAuctionOutBid(), auction->item_template);
MailDraft(msgAuctionOutbiddedSubject.str(), "") // TODO: fix body
.AddMoney(auction->bid)
.SetMoney(auction->bid)
.SendMailTo(MailReceiver(oldBidder, oldBidder_guid), auction, MAIL_CHECK_MASK_COPIED);
}
}
@ -150,7 +150,7 @@ void WorldSession::SendAuctionCancelledToBidderMail( AuctionEntry* auction )
msgAuctionCancelledSubject << auction->item_template << ":0:" << AUCTION_CANCELLED_TO_BIDDER << ":0:0";
MailDraft(msgAuctionCancelledSubject.str(), "") // TODO: fix body
.AddMoney(auction->bid)
.SetMoney(auction->bid)
.SendMailTo(MailReceiver(bidder, bidder_guid), auction, MAIL_CHECK_MASK_COPIED);
}
}

View file

@ -234,7 +234,7 @@ void AuctionHouseMgr::SendAuctionSuccessfulMail( AuctionEntry * auction )
}
MailDraft(msgAuctionSuccessfulSubject.str(), auctionSuccessfulBody.str())
.AddMoney(profit)
.SetMoney(profit)
.SendMailTo(MailReceiver(owner, owner_guid), auction, MAIL_CHECK_MASK_COPIED, HOUR);
}
}

View file

@ -1763,14 +1763,10 @@ bool ChatHandler::HandleSendMailCommand(char* args)
if (!msgText)
return false;
// msgSubject, msgText isn't NUL after prev. check
std::string subject = msgSubject;
std::string text = msgText;
// from console show nonexistent sender
MailSender sender(MAIL_NORMAL, m_session ? m_session->GetPlayer()->GetObjectGuid().GetCounter() : 0, MAIL_STATIONERY_GM);
MailDraft(subject, text)
MailDraft(msgSubject, msgText)
.SendMailTo(MailReceiver(target, target_guid),sender);
std::string nameLink = playerLink(target_name);

View file

@ -6514,10 +6514,6 @@ bool ChatHandler::HandleSendItemsCommand(char* args)
if (!msgText)
return false;
// msgSubject, msgText isn't NUL after prev. check
std::string subject = msgSubject;
std::string text = msgText;
// extract items
typedef std::pair<uint32,uint32> ItemPair;
typedef std::list< ItemPair > ItemPairs;
@ -6575,7 +6571,7 @@ bool ChatHandler::HandleSendItemsCommand(char* args)
MailSender sender(MAIL_NORMAL, m_session ? m_session->GetPlayer()->GetObjectGuid().GetCounter() : 0, MAIL_STATIONERY_GM);
// fill mail
MailDraft draft(subject, text);
MailDraft draft(msgSubject, msgText);
for(ItemPairs::const_iterator itr = items.begin(); itr != items.end(); ++itr)
{
@ -6619,15 +6615,11 @@ bool ChatHandler::HandleSendMoneyCommand(char* args)
if (money <= 0)
return false;
// msgSubject, msgText isn't NUL after prev. check
std::string subject = msgSubject;
std::string text = msgText;
// from console show nonexistent sender
MailSender sender(MAIL_NORMAL, m_session ? m_session->GetPlayer()->GetObjectGuid().GetCounter() : 0, MAIL_STATIONERY_GM);
MailDraft(subject, text)
.AddMoney(money)
MailDraft(msgSubject, msgText)
.SetMoney(money)
.SendMailTo(MailReceiver(receiver, receiver_guid),sender);
std::string nameLink = playerLink(receiver_name);

View file

@ -272,8 +272,8 @@ void WorldSession::HandleSendMail(WorldPacket & recv_data )
// will delete item or place to receiver mail list
draft
.AddMoney(money)
.AddCOD(COD)
.SetMoney(money)
.SetCOD(COD)
.SendMailTo(MailReceiver(receive, rc), pl, body.empty() ? MAIL_CHECK_MASK_COPIED : MAIL_CHECK_MASK_HAS_BODY, deliver_delay);
CharacterDatabase.BeginTransaction();
@ -389,9 +389,11 @@ void WorldSession::HandleMailReturnToSender(WorldPacket & recv_data )
// send back only to existing players and simple drop for other cases
if (m->messageType == MAIL_NORMAL && m->sender)
{
MailDraft draft(m->subject, m->body);
MailDraft draft;
if (m->mailTemplateId)
draft = MailDraft(m->mailTemplateId, false); // items already included
draft.SetMailTemplate(m->mailTemplateId, false);// items already included
else
draft.SetSubjectAndBody(m->subject, m->body);
if(m->HasItems())
{
@ -404,7 +406,7 @@ void WorldSession::HandleMailReturnToSender(WorldPacket & recv_data )
}
}
draft.AddMoney(m->money).SendReturnToSender(GetAccountId(), m->receiverGuid, ObjectGuid(HIGHGUID_PLAYER, m->sender));
draft.SetMoney(m->money).SendReturnToSender(GetAccountId(), m->receiverGuid, ObjectGuid(HIGHGUID_PLAYER, m->sender));
}
delete m; // we can deallocate old mail
@ -484,7 +486,7 @@ void WorldSession::HandleMailTakeItem(WorldPacket & recv_data )
if(sender || sender_accId)
{
MailDraft(m->subject, "")
.AddMoney(m->COD)
.SetMoney(m->COD)
.SendMailTo(MailReceiver(sender, sender_guid), _player, MAIL_CHECK_MASK_COD_PAYMENT);
}

View file

@ -111,6 +111,8 @@ enum MailAuctionAnswers
class MailSender
{
public: // Constructors
MailSender() : m_messageType(MAIL_NORMAL), m_senderId(0), m_stationery(MAIL_STATIONERY_DEFAULT) {}
/**
* Creates a new MailSender object.
*
@ -181,6 +183,12 @@ class MailDraft
typedef std::map<uint32, Item*> MailItemMap;
public: // Constructors
/**
* Creates a new blank MailDraft object
*
*/
MailDraft()
: m_mailTemplateId(0), m_mailTemplateItemsNeed(false), m_money(0), m_COD(0) {}
/**
* Creates a new MailDraft object using mail template id.
*
@ -211,23 +219,31 @@ class MailDraft
/// Returns the Cost of delivery of this MailDraft.
uint32 GetCOD() const { return m_COD; }
public: // modifiers
// this two modifiers expected to be applied in normal case to blank draft and exclusively, it will work and with mixed cases but this will be not normal way use.
MailDraft& SetSubjectAndBody(std::string subject, std::string body) { m_subject = subject; m_body = body; return *this; }
MailDraft& SetMailTemplate(uint16 mailTemplateId, bool need_items = true) { m_mailTemplateId = mailTemplateId, m_mailTemplateItemsNeed = need_items; return *this; }
MailDraft& AddItem(Item* item);
/**
* Modifies the amount of money in a MailDraft.
*
* @param money The amount of money included in this MailDraft.
*/
MailDraft& AddMoney(uint32 money) { m_money = money; return *this; }
MailDraft& SetMoney(uint32 money) { m_money = money; return *this; }
/**
* Modifies the cost of delivery of the MailDraft.
*
* @param COD the amount to which the cod should be set.
*/
MailDraft& AddCOD(uint32 COD) { m_COD = COD; return *this; }
MailDraft& SetCOD(uint32 COD) { m_COD = COD; return *this; }
public: // finishers
void SendReturnToSender(uint32 sender_acc, ObjectGuid sender_guid, ObjectGuid receiver_guid);
void SendMailTo(MailReceiver const& receiver, MailSender const& sender, MailCheckMask checked = MAIL_CHECK_MASK_NONE, uint32 deliver_delay = 0);
private:
MailDraft(MailDraft const&); // trap decl, no body, mail draft must cloned only explicitly...
MailDraft& operator=(MailDraft const&); // trap decl, no body, ...because items clone is high price operation
void deleteIncludedItems(bool inDB = false);
void prepareItems(Player* receiver); ///< called from SendMailTo for generate mailTemplateBase items

View file

@ -4233,9 +4233,11 @@ void Player::DeleteFromDB(ObjectGuid playerguid, uint32 accountId, bool updateRe
continue;
}
MailDraft draft(subject, body);
MailDraft draft;
if (mailTemplateId)
draft = MailDraft(mailTemplateId, false); // items already included
draft.SetMailTemplate(mailTemplateId, false);// items already included
else
draft.SetSubjectAndBody(subject, body);
if (has_items)
{
@ -4278,7 +4280,7 @@ void Player::DeleteFromDB(ObjectGuid playerguid, uint32 accountId, bool updateRe
uint32 pl_account = sObjectMgr.GetPlayerAccountIdByGUID(playerguid);
draft.AddMoney(money).SendReturnToSender(pl_account, playerguid, ObjectGuid(HIGHGUID_PLAYER, sender));
draft.SetMoney(money).SendReturnToSender(pl_account, playerguid, ObjectGuid(HIGHGUID_PLAYER, sender));
}
while (resultMail->NextRow());

View file

@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__
#define __REVISION_NR_H__
#define REVISION_NR "10968"
#define REVISION_NR "10969"
#endif // __REVISION_NR_H__