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@3: self:WatchBags() Asa@9: Asa@9: addon:ConvertItems() Asa@3: end Asa@3: Asa@3: function addon:MAIL_SHOW() Asa@3: self:Debug("MAIL_SHOW") 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: self:Debug("MAIL_SHOW complete") Asa@3: end Asa@3: Asa@3: function addon: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@3: function addon: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@6: for item, total in pairs(collection) do Asa@3: Asa@6: diff = total - (newScan[mailType][item] or 0) Asa@6: if diff ~= 0 then Asa@6: self:SaveValue(item, diff) 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@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@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@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@9: self:Debug("UpdateAudit") Asa@3: local currentInventory = self:GetCurrentInventory() Asa@3: local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory) Asa@3: -- this is only here for debugging Asa@3: self.lastdiff = diff 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@5: if diff.money > 0 and utils:tcount(positive) > 0 and utils:tcount(negative) == 0 then Asa@7: -- self:Debug("loot") Asa@7: elseif utils:tcount(diff.items) == 1 then Asa@8: self:Debug("purchase or sale") Asa@3: Asa@9: for link, count in pairs(diff.items) do Asa@9: self:SaveValue(link, 0 - diff.money) Asa@3: end Asa@3: elseif utils:tcount(diff.items) > 1 then Asa@3: Asa@3: if utils:tcount(positive) > 0 and utils:tcount(negative) > 0 then Asa@3: -- we must have created/converted something Asa@7: -- self:Debug("conversion") Asa@3: local totalChange = 0 Asa@9: for link, change in pairs(negative) do Asa@9: local _, itemCost, count = self:GetItemCost(link, change) Asa@9: self:SaveValue(link, itemCost * change) Asa@3: Asa@9: totalChange = totalChange + (itemCost * abs(change)) Asa@3: end Asa@3: Asa@7: local valuePerItem = totalChange / positiveCount Asa@3: Asa@9: for link, change in pairs(positive) do Asa@9: self:SaveValue(link, valuePerItem * change) Asa@3: end Asa@3: end Asa@3: end Asa@3: Asa@3: self.lastInventory = currentInventory Asa@4: addon:WatchBags() Asa@3: end