Asa@3: local addonName, addonTable = ...; Asa@3: local addon = _G[addonName] Asa@3: Asa@3: local utils = addonTable.utils Asa@3: Asa@3: function addon:PLAYER_ENTERING_WORLD() Asa@3: self:RegisterEvent("MAIL_SHOW") Asa@4: self:RegisterEvent("UNIT_SPELLCAST_START") Asa@24: addon:UpdateCurrentInventory() Asa@3: self:WatchBags() Asa@9: Asa@13: -- addon:ConvertItems() Asa@3: end Asa@3: Asa@3: function addon:MAIL_SHOW() Asa@3: self:Debug("MAIL_SHOW") Asa@24: addon:UpdateCurrentInventory() Asa@3: self.lastMailScan = self:ScanMail() Asa@7: Asa@3: self:UnregisterEvent("MAIL_SHOW") Asa@3: self:RegisterEvent("MAIL_CLOSED") Asa@3: self:RegisterEvent("MAIL_INBOX_UPDATE") Asa@3: end Asa@3: Asa@3: function addon:MAIL_CLOSED() Asa@23: self:Debug("MAIL_CLOSED") Asa@3: addon:UnregisterEvent("MAIL_CLOSED") Asa@7: self:MAIL_INBOX_UPDATE() Asa@3: self:UnregisterEvent("MAIL_INBOX_UPDATE") Asa@3: self:RegisterEvent("MAIL_SHOW") Asa@3: end Asa@3: Asa@26: local storedCountDiff Asa@3: function addon:MAIL_INBOX_UPDATE() Asa@23: self:Debug("MAIL_INBOX_UPDATE") Asa@3: local newScan = addon:ScanMail() Asa@3: local diff Asa@6: for mailType, collection in pairs(self.lastMailScan) do Asa@7: newScan[mailType] = (newScan[mailType] or {}) Asa@26: for itemName, data in pairs(collection) do Asa@26: newScan[mailType][itemName] = (newScan[mailType][itemName] or {total=0,count=0}) Asa@26: local totalDiff = data.total - newScan[mailType][itemName].total Asa@26: local countDiff = data.count - newScan[mailType][itemName].count Asa@26: --[[ Asa@26: In one update the item will be taken and in the following update the invoice Asa@26: will be gone. I need to store the item difference in order ot pass it into Asa@26: SaveValue. Asa@26: ]] Asa@26: if countDiff ~= 0 then Asa@26: storedCountDiff = countDiff Asa@26: end Asa@26: Asa@26: if totalDiff ~= 0 then Asa@26: self:SaveValue(itemName, totalDiff, storedCountDiff) Asa@26: storedCountDiff = 0 Asa@6: end Asa@6: Asa@3: end Asa@3: end Asa@3: Asa@3: self.lastMailScan = newScan Asa@3: end Asa@3: Asa@4: function addon:UNIT_SPELLCAST_START(event, target, spell) Asa@5: if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then Asa@23: self:Debug(event .. " " .. spell) Asa@4: self:UnwatchBags() Asa@4: self:UpdateCurrentInventory() Asa@4: self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED") Asa@4: self:RegisterEvent("LOOT_CLOSED") Asa@3: end Asa@3: end Asa@3: Asa@4: --[[ Asa@4: The item should be destroyed before this point, so the last inventory check Asa@4: needs to be kept so it can be combined with the up coming loot. Asa@4: ]] Asa@4: function addon:LOOT_CLOSED() Asa@23: self:Debug("LOOT_CLOSED") Asa@4: self:UnregisterEvent("LOOT_CLOSED") Asa@4: self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED") Asa@4: local inventory = self.lastInventory Asa@4: self:WatchBags() Asa@4: self.lastInventory = inventory Asa@4: end Asa@3: Asa@4: function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell) Asa@5: if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then Asa@23: self:Debug(event .. " " .. spell) Asa@4: self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED") Asa@4: self:UnregisterEvent("LOOT_CLOSED") Asa@4: self:WatchBags() Asa@4: end Asa@4: end Asa@4: Asa@4: function addon:UpdateCurrentInventory() Asa@4: self.lastInventory = self:GetCurrentInventory() Asa@3: end Asa@3: Asa@3: function addon:UpdateAudit() Asa@23: -- self:Debug("UpdateAudit " .. event) Asa@3: local currentInventory = self:GetCurrentInventory() Asa@3: local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory) Asa@3: Asa@5: local positive, negative = {}, {} Asa@5: local positiveCount, negativeCount = 0, 0 Asa@5: for item, count in pairs(diff.items) do Asa@5: if count > 0 then Asa@5: positive[item] = count Asa@5: positiveCount = positiveCount + count Asa@5: elseif count < 0 then Asa@5: negative[item] = count Asa@5: negativeCount = negativeCount + abs(count) Asa@5: end Asa@5: end Asa@5: Asa@23: if positiveCount + negativeCount == 0 then Asa@33: --[[ Asa@33: Nothing needs to be done, but this will prevent mistakenly attributing Asa@33: the cost of flights to the first item you pick up. Asa@33: ]] Asa@33: elseif diff.money > 0 and self:tcount(positive) > 0 and self:tcount(negative) == 0 then Asa@15: self:Debug("loot") Asa@20: elseif abs(diff.money) > 0 and self:tcount(diff.items) == 1 then Asa@15: self:Debug("purchase or sale") Asa@3: Asa@9: for link, count in pairs(diff.items) do Asa@26: self:SaveValue(link, 0 - diff.money, count) Asa@3: end Asa@23: elseif self:tcount(diff.items) > 1 and self:tcount(positive) > 0 and self:tcount(negative) > 0 then Asa@23: -- we must have created/converted something Asa@23: self:Debug("conversion") Asa@3: Asa@23: local totalChange = 0 Asa@23: for link, change in pairs(negative) do Asa@23: local _, itemCost, count = self:GetItemCost(link, change) Asa@26: self:SaveValue(link, itemCost * change, change) Asa@10: Asa@23: totalChange = totalChange + (itemCost * abs(change)) Asa@3: end Asa@23: Asa@23: local valuePerItem = totalChange / positiveCount Asa@23: Asa@23: for link, change in pairs(positive) do Asa@26: self:SaveValue(link, valuePerItem * change, change) Asa@23: end Asa@23: else Asa@23: self:Debug("No match in UpdateAudit.") Asa@3: end Asa@3: Asa@3: self.lastInventory = currentInventory Asa@4: addon:WatchBags() Asa@3: end