annotate Modules/Events.lua @ 40:5273038322d7 release

Updated the way postage was counted so that if you mail multiple items at once, they will all have the postage counted. I also removed the 30c for postage from how QA thresholds are calculated now that they will be counted every time.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Mon, 19 Jul 2010 20:55:50 -0700
parents 003de902ae64
children 1ff3b83b104c
rev   line source
Asa@3 1 local addonName, addonTable = ...;
Asa@3 2 local addon = _G[addonName]
Asa@3 3
Asa@38 4 function addon:OnEnable()
Asa@3 5 self:RegisterEvent("MAIL_SHOW")
Asa@4 6 self:RegisterEvent("UNIT_SPELLCAST_START")
Asa@24 7 addon:UpdateCurrentInventory()
Asa@3 8 self:WatchBags()
Asa@9 9
Asa@38 10 self:SetEnabled(nil, self.db.profile.addon_enabled)
Asa@38 11 end
Asa@38 12
Asa@38 13 function addon:OnDisable()
Asa@38 14 self:UnwatchBags()
Asa@38 15 self:UnregisterAllEvents()
Asa@38 16 addon:HideAllFrames()
Asa@3 17 end
Asa@3 18
Asa@3 19 function addon:MAIL_SHOW()
Asa@3 20 self:Debug("MAIL_SHOW")
Asa@39 21 self:UnwatchBags()
Asa@24 22 addon:UpdateCurrentInventory()
Asa@3 23 self.lastMailScan = self:ScanMail()
Asa@7 24
Asa@3 25 self:UnregisterEvent("MAIL_SHOW")
Asa@3 26 self:RegisterEvent("MAIL_CLOSED")
Asa@3 27 self:RegisterEvent("MAIL_INBOX_UPDATE")
Asa@39 28
Asa@39 29 self:GenerateBlankOutbox()
Asa@39 30
Asa@39 31 self:RegisterEvent("MAIL_SUCCESS")
Asa@39 32 end
Asa@39 33
Asa@39 34 function addon:GenerateBlankOutbox()
Asa@39 35 self.mailOutbox = {
Asa@39 36 from = UnitName("player"),
Asa@39 37 to = "",
Asa@39 38 subject = "",
Asa@39 39 link = '',
Asa@39 40 count = 0,
Asa@39 41 COD = 0,
Asa@39 42 key = random(10000),
Asa@39 43 sent = 0,
Asa@39 44 }
Asa@39 45
Asa@39 46 if self.db.factionrealm.outbound_cod[self.mailOutbox.key] ~= nil then
Asa@39 47 return self:GenerateBlankOutbox()
Asa@39 48 end
Asa@39 49 end
Asa@39 50
Asa@40 51 local attachedItems = {}
Asa@39 52 local Orig_SendMail = SendMail
Asa@39 53
Asa@39 54 function SendMail(recipient, subject, body, ...)
Asa@39 55 local self = ItemAuditor
Asa@39 56 self:GenerateBlankOutbox()
Asa@39 57
Asa@39 58 self:Debug(format("[To: %s] [Subject: %s]", recipient, subject))
Asa@39 59
Asa@39 60 self.mailOutbox.COD = GetSendMailCOD()
Asa@39 61
Asa@40 62 attachedItems = {}
Asa@40 63 local totalStacks = 0
Asa@39 64 local link
Asa@40 65 for index = 1, ATTACHMENTS_MAX_SEND do
Asa@39 66 local itemName, _, itemCount = GetSendMailItem(index)
Asa@39 67 local newLink = GetSendMailItemLink(index)
Asa@39 68
Asa@40 69 if newLink ~= nil then
Asa@40 70 newLink = self:GetSafeLink(newLink)
Asa@40 71 totalStacks = totalStacks + 1
Asa@40 72 attachedItems[newLink] = (attachedItems[newLink] or {stacks = 0, count = 0})
Asa@40 73 attachedItems[newLink].stacks = attachedItems[newLink].stacks + 1
Asa@40 74 attachedItems[newLink].count = attachedItems[newLink].count + itemCount
Asa@40 75 attachedItems[newLink].price = 0 -- This is a placeholder for below.
Asa@39 76 end
Asa@40 77 end
Asa@40 78 local pricePerStack = GetSendMailPrice() / totalStacks
Asa@40 79 for link, data in pairs(attachedItems) do
Asa@40 80 data.price = pricePerStack * data.stacks
Asa@40 81 end
Asa@40 82
Asa@40 83 if self.mailOutbox.COD > 0 then
Asa@40 84 if self:tcount(attachedItems) > 1 then
Asa@40 85 self:Print("ERROR: ItemAuditor can't track COD mail with more than one item type.")
Asa@39 86 self:GenerateBlankOutbox()
Asa@40 87 -- I need to make a prompt so the user can send the mail without interference
Asa@39 88 return
Asa@39 89 end
Asa@40 90 self:Debug("COD mail")
Asa@39 91
Asa@40 92 subject = format("[IA: %s] %s", self.mailOutbox.key, subject)
Asa@40 93 self.mailOutbox.subject = subject
Asa@40 94 self.mailOutbox.to = recipient
Asa@40 95
Asa@40 96 -- At this point we know there is only one item
Asa@40 97 for link, data in pairs(attachedItems) do
Asa@40 98 self.mailOutbox.link = link
Asa@40 99 self.mailOutbox.count = data.count
Asa@40 100 end
Asa@40 101 else
Asa@40 102 self:Debug("Non-COD mail")
Asa@39 103 end
Asa@40 104
Asa@39 105 return Orig_SendMail(recipient, subject, body, ...)
Asa@39 106 end
Asa@39 107
Asa@39 108 function addon:MAIL_SUCCESS(event)
Asa@40 109 for link, data in pairs(attachedItems) do
Asa@40 110 self:SaveValue(link, data.price, data.count)
Asa@40 111 end
Asa@39 112 if self.mailOutbox.COD > 0 then
Asa@39 113 self:Debug(format("MAIL_SUCCESS %d [To: %s] [Subject: %s] [COD: %s]", self.mailOutbox.key, self.mailOutbox.to, self.mailOutbox.subject, self.mailOutbox.COD))
Asa@39 114
Asa@39 115 self.mailOutbox.sent = time()
Asa@39 116 self.db.factionrealm.outbound_cod[self.mailOutbox.key] = self.mailOutbox
Asa@39 117 end
Asa@39 118
Asa@40 119
Asa@40 120 self:GenerateBlankOutbox()
Asa@3 121 end
Asa@3 122
Asa@3 123 function addon:MAIL_CLOSED()
Asa@23 124 self:Debug("MAIL_CLOSED")
Asa@3 125 addon:UnregisterEvent("MAIL_CLOSED")
Asa@7 126 self:MAIL_INBOX_UPDATE()
Asa@3 127 self:UnregisterEvent("MAIL_INBOX_UPDATE")
Asa@3 128 self:RegisterEvent("MAIL_SHOW")
Asa@39 129 self:WatchBags()
Asa@3 130 end
Asa@3 131
Asa@26 132 local storedCountDiff
Asa@3 133 function addon:MAIL_INBOX_UPDATE()
Asa@23 134 self:Debug("MAIL_INBOX_UPDATE")
Asa@3 135 local newScan = addon:ScanMail()
Asa@3 136 local diff
Asa@39 137
Asa@6 138 for mailType, collection in pairs(self.lastMailScan) do
Asa@7 139 newScan[mailType] = (newScan[mailType] or {})
Asa@26 140 for itemName, data in pairs(collection) do
Asa@26 141 newScan[mailType][itemName] = (newScan[mailType][itemName] or {total=0,count=0})
Asa@26 142 local totalDiff = data.total - newScan[mailType][itemName].total
Asa@26 143 local countDiff = data.count - newScan[mailType][itemName].count
Asa@26 144 --[[
Asa@26 145 In one update the item will be taken and in the following update the invoice
Asa@26 146 will be gone. I need to store the item difference in order ot pass it into
Asa@26 147 SaveValue.
Asa@26 148 ]]
Asa@26 149 if countDiff ~= 0 then
Asa@26 150 storedCountDiff = countDiff
Asa@26 151 end
Asa@26 152
Asa@26 153 if totalDiff ~= 0 then
Asa@39 154 if mailType == "CODPayment" then
Asa@39 155 local trackID
Asa@39 156 trackID, itemName= strsplit("|", itemName, 2)
Asa@39 157 self.db.factionrealm.outbound_cod[tonumber(trackID)] = nil
Asa@39 158 self:Debug("Removing COD Tracker: " .. trackID)
Asa@39 159 end
Asa@26 160 self:SaveValue(itemName, totalDiff, storedCountDiff)
Asa@26 161 storedCountDiff = 0
Asa@6 162 end
Asa@6 163
Asa@3 164 end
Asa@3 165 end
Asa@3 166
Asa@3 167 self.lastMailScan = newScan
Asa@3 168 end
Asa@3 169
Asa@4 170 function addon:UNIT_SPELLCAST_START(event, target, spell)
Asa@5 171 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@23 172 self:Debug(event .. " " .. spell)
Asa@4 173 self:UnwatchBags()
Asa@4 174 self:UpdateCurrentInventory()
Asa@4 175 self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 176 self:RegisterEvent("LOOT_CLOSED")
Asa@3 177 end
Asa@3 178 end
Asa@3 179
Asa@4 180 --[[
Asa@4 181 The item should be destroyed before this point, so the last inventory check
Asa@4 182 needs to be kept so it can be combined with the up coming loot.
Asa@4 183 ]]
Asa@4 184 function addon:LOOT_CLOSED()
Asa@23 185 self:Debug("LOOT_CLOSED")
Asa@4 186 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 187 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 188 local inventory = self.lastInventory
Asa@4 189 self:WatchBags()
Asa@4 190 self.lastInventory = inventory
Asa@4 191 end
Asa@3 192
Asa@4 193 function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell)
Asa@5 194 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@23 195 self:Debug(event .. " " .. spell)
Asa@4 196 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 197 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 198 self:WatchBags()
Asa@4 199 end
Asa@4 200 end
Asa@4 201
Asa@4 202 function addon:UpdateCurrentInventory()
Asa@4 203 self.lastInventory = self:GetCurrentInventory()
Asa@3 204 end
Asa@3 205
Asa@3 206 function addon:UpdateAudit()
Asa@23 207 -- self:Debug("UpdateAudit " .. event)
Asa@3 208 local currentInventory = self:GetCurrentInventory()
Asa@3 209 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 210
Asa@5 211 local positive, negative = {}, {}
Asa@5 212 local positiveCount, negativeCount = 0, 0
Asa@5 213 for item, count in pairs(diff.items) do
Asa@5 214 if count > 0 then
Asa@5 215 positive[item] = count
Asa@5 216 positiveCount = positiveCount + count
Asa@5 217 elseif count < 0 then
Asa@5 218 negative[item] = count
Asa@5 219 negativeCount = negativeCount + abs(count)
Asa@5 220 end
Asa@5 221 end
Asa@5 222
Asa@23 223 if positiveCount + negativeCount == 0 then
Asa@33 224 --[[
Asa@33 225 Nothing needs to be done, but this will prevent mistakenly attributing
Asa@33 226 the cost of flights to the first item you pick up.
Asa@33 227 ]]
Asa@33 228 elseif diff.money > 0 and self:tcount(positive) > 0 and self:tcount(negative) == 0 then
Asa@15 229 self:Debug("loot")
Asa@20 230 elseif abs(diff.money) > 0 and self:tcount(diff.items) == 1 then
Asa@15 231 self:Debug("purchase or sale")
Asa@3 232
Asa@9 233 for link, count in pairs(diff.items) do
Asa@26 234 self:SaveValue(link, 0 - diff.money, count)
Asa@3 235 end
Asa@23 236 elseif self:tcount(diff.items) > 1 and self:tcount(positive) > 0 and self:tcount(negative) > 0 then
Asa@23 237 -- we must have created/converted something
Asa@23 238 self:Debug("conversion")
Asa@3 239
Asa@23 240 local totalChange = 0
Asa@23 241 for link, change in pairs(negative) do
Asa@23 242 local _, itemCost, count = self:GetItemCost(link, change)
Asa@26 243 self:SaveValue(link, itemCost * change, change)
Asa@10 244
Asa@23 245 totalChange = totalChange + (itemCost * abs(change))
Asa@3 246 end
Asa@23 247
Asa@23 248 local valuePerItem = totalChange / positiveCount
Asa@23 249
Asa@23 250 for link, change in pairs(positive) do
Asa@26 251 self:SaveValue(link, valuePerItem * change, change)
Asa@23 252 end
Asa@23 253 else
Asa@23 254 self:Debug("No match in UpdateAudit.")
Asa@3 255 end
Asa@3 256
Asa@3 257 self.lastInventory = currentInventory
Asa@4 258 addon:WatchBags()
Asa@3 259 end