comparison Modules/Events.lua @ 92:71053b1f6c1c

Fixed a bug where postage was being counted more than once if you send mail and then open mail in the same session.\nAdded a warning when sending items with value to characters on other accounts.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 11 Aug 2010 21:09:59 -0700
parents e2dab8e34b32
children 028c02f1d47f
comparison
equal deleted inserted replaced
91:e2dab8e34b32 92:71053b1f6c1c
1 local ItemAuditor = select(2, ...) 1 local ItemAuditor = select(2, ...)
2 local Events = ItemAuditor:NewModule("Events", "AceEvent-3.0") 2 local Events = ItemAuditor:NewModule("Events", "AceEvent-3.0")
3
4 local Utils = ItemAuditor:GetModule("Utils")
3 5
4 function ItemAuditor:OnEnable() 6 function ItemAuditor:OnEnable()
5 self:RegisterEvent("MAIL_SHOW") 7 self:RegisterEvent("MAIL_SHOW")
6 self:RegisterEvent("UNIT_SPELLCAST_START") 8 self:RegisterEvent("UNIT_SPELLCAST_START")
7 9
53 end 55 end
54 56
55 local attachedItems = {} 57 local attachedItems = {}
56 local Orig_SendMail = SendMail 58 local Orig_SendMail = SendMail
57 local skipCODTracking = false 59 local skipCODTracking = false
60 local skipCODCheck = false
58 61
59 StaticPopupDialogs["ItemAuditor_Send_COD_without_tracking_number"] = { 62 StaticPopupDialogs["ItemAuditor_Send_COD_without_tracking_number"] = {
60 text = "ItemAuditor cannot track COD mail with multiple item types attached. Do you want to send this mail without tracking?", 63 text = "ItemAuditor cannot track COD mail with multiple item types attached. Do you want to send this mail without tracking?",
61 button1 = "Yes", 64 button1 = "Yes",
62 button2 = "No", 65 button2 = "No",
66 timeout = 0, 69 timeout = 0,
67 whileDead = true, 70 whileDead = true,
68 hideOnEscape = true, 71 hideOnEscape = true,
69 } 72 }
70 73
74 StaticPopupDialogs["ItemAuditor_Insufficient_COD"] = {
75 text = "The COD on this mail is less than the value of items attached. Are you sure you want to send this?|nTotal value (including postage): %s",
76 button1 = "Yes",
77 button2 = "No",
78 OnAccept = function()
79 skipCODCheck = true
80 end,
81 timeout = 0,
82 whileDead = true,
83 hideOnEscape = true,
84 }
85
71 function SendMail(recipient, subject, body, ...) 86 function SendMail(recipient, subject, body, ...)
72 local self = ItemAuditor 87 local self = ItemAuditor
73 self:GenerateBlankOutbox() 88 self:GenerateBlankOutbox()
74 89
75 self:Debug(format("[To: %s] [Subject: %s]", recipient, subject)) 90 self:Debug(format("[To: %s] [Subject: %s]", recipient, subject))
76 91
77 self.mailOutbox.COD = GetSendMailCOD() 92 self.mailOutbox.COD = GetSendMailCOD()
78 93
79 attachedItems = {} 94 wipe(attachedItems)
80 local totalStacks = 0 95 local totalStacks = 0
81 local link 96 local link
82 for index = 1, ATTACHMENTS_MAX_SEND do 97 for index = 1, ATTACHMENTS_MAX_SEND do
83 local itemName, _, itemCount = GetSendMailItem(index) 98 local itemName, _, itemCount = GetSendMailItem(index)
84 local newLink = GetSendMailItemLink(index) 99 local newLink = GetSendMailItemLink(index)
90 attachedItems[newLink].stacks = attachedItems[newLink].stacks + 1 105 attachedItems[newLink].stacks = attachedItems[newLink].stacks + 1
91 attachedItems[newLink].count = attachedItems[newLink].count + itemCount 106 attachedItems[newLink].count = attachedItems[newLink].count + itemCount
92 attachedItems[newLink].price = 0 -- This is a placeholder for below. 107 attachedItems[newLink].price = 0 -- This is a placeholder for below.
93 end 108 end
94 end 109 end
110 local attachedValue = 0
95 for link, data in pairs(attachedItems) do 111 for link, data in pairs(attachedItems) do
96 data.price = 30 * data.stacks 112 data.price = 30 * data.stacks
97 end 113 attachedValue = attachedValue + data.price + (select(2, ItemAuditor:GetItemCost(link)) * data.stacks)
98 114 end
99 if self.mailOutbox.COD > 0 and skipCODTracking then 115
116 local cross_account_mail = true
117 for name, _ in pairs(DataStore:GetCharacters()) do
118 if recipient == name then
119 cross_account_mail = false
120 break
121 end
122 end
123 if cross_account_mail and attachedValue > self.mailOutbox.COD and not skipCODCheck then
124 self:GenerateBlankOutbox()
125 skipCODCheck = false;
126 local vararg = ...
127 StaticPopupDialogs["ItemAuditor_Insufficient_COD"].OnAccept = function()
128 skipCODCheck = true
129 SendMail(recipient, subject, body, vararg)
130 skipCODCheck = false
131 end
132 StaticPopup_Show ("ItemAuditor_Insufficient_COD", Utils.FormatMoney(attachedValue));
133 return
134 elseif self.mailOutbox.COD > 0 and skipCODTracking then
100 135
101 elseif self.mailOutbox.COD > 0 then 136 elseif self.mailOutbox.COD > 0 then
102 if self:tcount(attachedItems) > 1 then 137 if self:tcount(attachedItems) > 1 then
103 self:GenerateBlankOutbox() 138 self:GenerateBlankOutbox()
104 local vararg = ... 139 local vararg = ...
128 return Orig_SendMail(recipient, subject, body, ...) 163 return Orig_SendMail(recipient, subject, body, ...)
129 end 164 end
130 165
131 function ItemAuditor:MAIL_SUCCESS(event) 166 function ItemAuditor:MAIL_SUCCESS(event)
132 skipCODTracking = false 167 skipCODTracking = false
168 skipCODCheck = false
169
133 for link, data in pairs(attachedItems) do 170 for link, data in pairs(attachedItems) do
134 self:SaveValue(link, data.price, data.count) 171 self:SaveValue(link, data.price, data.count)
135 end 172 end
136 if self.mailOutbox.COD > 0 then 173 if self.mailOutbox.COD > 0 then
137 self:Debug(format("MAIL_SUCCESS %d [To: %s] [Subject: %s] [COD: %s]", self.mailOutbox.key, self.mailOutbox.to, self.mailOutbox.subject, self.mailOutbox.COD)) 174 self:Debug(format("MAIL_SUCCESS %d [To: %s] [Subject: %s] [COD: %s]", self.mailOutbox.key, self.mailOutbox.to, self.mailOutbox.subject, self.mailOutbox.COD))
138 175
139 self.mailOutbox.sent = time() 176 self.mailOutbox.sent = time()
140 self.db.factionrealm.outbound_cod[self.mailOutbox.key] = self.mailOutbox 177 self.db.factionrealm.outbound_cod[self.mailOutbox.key] = self.mailOutbox
141 end 178 end
142 179
143 180 wipe(attachedItems)
144 self:GenerateBlankOutbox() 181 self:GenerateBlankOutbox()
145 end 182 end
146 183
147 function ItemAuditor:MAIL_CLOSED() 184 function ItemAuditor:MAIL_CLOSED()
148 self:Debug("MAIL_CLOSED") 185 self:Debug("MAIL_CLOSED")