[8768] Rewrite code for prepare and send mails.
[getmangos.git] / src / game / Mail.h
blobb46370e63f7aa6748c70c6fa9ae494ec247535f6
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #ifndef MANGOS_MAIL_H
19 #define MANGOS_MAIL_H
21 #include "Common.h"
22 #include <map>
24 struct AuctionEntry;
25 class Item;
27 #define MAIL_BODY_ITEM_TEMPLATE 8383 // - plain letter, A Dusty Unsent Letter: 889
28 #define MAX_MAIL_ITEMS 12
30 enum MailMessageType
32 MAIL_NORMAL = 0,
33 MAIL_AUCTION = 2,
34 MAIL_CREATURE = 3, // client send CMSG_CREATURE_QUERY on this mailmessagetype
35 MAIL_GAMEOBJECT = 4, // client send CMSG_GAMEOBJECT_QUERY on this mailmessagetype
36 MAIL_ITEM = 5, // client send CMSG_ITEM_QUERY on this mailmessagetype
39 enum MailState
41 MAIL_STATE_UNCHANGED = 1,
42 MAIL_STATE_CHANGED = 2,
43 MAIL_STATE_DELETED = 3
46 enum MailAuctionAnswers
48 AUCTION_OUTBIDDED = 0,
49 AUCTION_WON = 1,
50 AUCTION_SUCCESSFUL = 2,
51 AUCTION_EXPIRED = 3,
52 AUCTION_CANCELLED_TO_BIDDER = 4,
53 AUCTION_CANCELED = 5,
54 AUCTION_SALE_PENDING = 6
57 class MailSender
59 public: // Constructors
60 MailSender(MailMessageType messageType, uint32 sender_guidlow_or_entry, MailStationery stationery = MAIL_STATIONERY_NORMAL)
61 : m_messageType(messageType), m_senderId(sender_guidlow_or_entry), m_stationery(stationery)
64 MailSender(Object* sender, MailStationery stationery = MAIL_STATIONERY_NORMAL);
65 MailSender(AuctionEntry* sender);
66 public: // Accessors
67 MailMessageType GetMailMessageType() const { return m_messageType; }
68 uint32 GetSenderId() const { return m_senderId; }
69 MailStationery GetStationery() const { return m_stationery; }
70 private:
71 MailMessageType m_messageType;
72 uint32 m_senderId; // player low guid or other object entry
73 MailStationery m_stationery;
76 class MailReceiver
78 public: // Constructors
79 explicit MailReceiver(uint32 receiver_lowguid) : m_receiver(NULL), m_receiver_lowguid(receiver_lowguid) {}
80 MailReceiver(Player* receiver);
81 MailReceiver(Player* receiver,uint32 receiver_lowguid);
82 public: // Accessors
83 Player* GetPlayer() const { return m_receiver; }
84 uint32 GetPlayerGUIDLow() const { return m_receiver_lowguid; }
85 private:
86 Player* m_receiver;
87 uint32 m_receiver_lowguid;
90 class MailDraft
92 typedef std::map<uint32, Item*> MailItemMap;
94 public: // Constructors
95 explicit MailDraft(uint16 mailTemplateId, bool need_items = true)
96 : m_mailTemplateId(mailTemplateId), m_mailTemplateItemsNeed(need_items), m_bodyId(0), m_money(0), m_COD(0)
98 MailDraft(std::string subject, uint32 itemTextId = 0)
99 : m_mailTemplateId(0), m_mailTemplateItemsNeed(false), m_subject(subject), m_bodyId(itemTextId), m_money(0), m_COD(0) {}
100 public: // Accessors
101 uint16 GetMailTemplateId() const { return m_mailTemplateId; }
102 std::string const& GetSubject() const { return m_subject; }
103 uint32 GetBodyId() const { return m_bodyId; }
104 uint32 GetMoney() const { return m_money; }
105 uint32 GetCOD() const { return m_COD; }
106 public: // modifiers
107 MailDraft& AddItem(Item* item);
108 MailDraft& AddMoney(uint32 money) { m_money = money; return *this; }
109 MailDraft& AddCOD(uint32 COD) { m_COD = COD; return *this; }
110 public: // finishers
111 void SendReturnToSender(uint32 sender_acc, uint32 sender_guid, uint32 receiver_guid);
112 void SendMailTo(MailReceiver const& receiver, MailSender const& sender, MailCheckMask checked = MAIL_CHECK_MASK_NONE, uint32 deliver_delay = 0);
113 private:
114 void deleteIncludedItems(bool inDB = false);
115 void prepareItems(Player* receiver); // called from SendMailTo for generate mailTemplateBase items
117 uint16 m_mailTemplateId;
118 bool m_mailTemplateItemsNeed;
119 std::string m_subject;
120 uint32 m_bodyId;
122 MailItemMap m_items; // Keep the items in a map to avoid duplicate guids (which can happen), store only low part of guid
124 uint32 m_money;
125 uint32 m_COD;
128 struct MailItemInfo
130 uint32 item_guid;
131 uint32 item_template;
134 struct Mail
136 uint32 messageID;
137 uint8 messageType;
138 uint8 stationery;
139 uint16 mailTemplateId;
140 uint32 sender;
141 uint32 receiver;
142 std::string subject;
143 uint32 itemTextId;
144 std::vector<MailItemInfo> items;
145 std::vector<uint32> removedItems;
146 time_t expire_time;
147 time_t deliver_time;
148 uint32 money;
149 uint32 COD;
150 uint32 checked;
151 MailState state;
153 void AddItem(uint32 itemGuidLow, uint32 item_template)
155 MailItemInfo mii;
156 mii.item_guid = itemGuidLow;
157 mii.item_template = item_template;
158 items.push_back(mii);
161 bool RemoveItem(uint32 item_guid)
163 for(std::vector<MailItemInfo>::iterator itr = items.begin(); itr != items.end(); ++itr)
165 if(itr->item_guid == item_guid)
167 items.erase(itr);
168 return true;
171 return false;
174 bool HasItems() const { return !items.empty(); }
177 #endif