annotate Modules/Events.lua @ 22:66b7c3f5937e

I have removed the ability to show debug messages in the chat window and have replaced it with a new debug frame. /ia debug will show all of the debug messages and everything that IA has printed. This commit also has a small fix so that if you sell something for your exact cost you don't get a message that you made a profit of 0c.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Thu, 08 Jul 2010 19:30:30 -0700
parents ff9a698caebc
children 819bfdc5d73c
rev   line source
Asa@3 1 local addonName, addonTable = ...;
Asa@3 2 local addon = _G[addonName]
Asa@3 3
Asa@3 4 local utils = addonTable.utils
Asa@3 5
Asa@3 6 function addon:PLAYER_ENTERING_WORLD()
Asa@3 7 self:RegisterEvent("MAIL_SHOW")
Asa@4 8 self:RegisterEvent("UNIT_SPELLCAST_START")
Asa@3 9 self:WatchBags()
Asa@9 10
Asa@13 11 -- addon:ConvertItems()
Asa@3 12 end
Asa@3 13
Asa@3 14 function addon:MAIL_SHOW()
Asa@3 15 self:Debug("MAIL_SHOW")
Asa@3 16 self.lastMailScan = self:ScanMail()
Asa@7 17
Asa@3 18 self:UnregisterEvent("MAIL_SHOW")
Asa@3 19 self:RegisterEvent("MAIL_CLOSED")
Asa@3 20 self:RegisterEvent("MAIL_INBOX_UPDATE")
Asa@3 21 self:Debug("MAIL_SHOW complete")
Asa@3 22 end
Asa@3 23
Asa@3 24 function addon:MAIL_CLOSED()
Asa@3 25 addon:UnregisterEvent("MAIL_CLOSED")
Asa@7 26 self:MAIL_INBOX_UPDATE()
Asa@3 27 self:UnregisterEvent("MAIL_INBOX_UPDATE")
Asa@3 28 self:RegisterEvent("MAIL_SHOW")
Asa@3 29 end
Asa@3 30
Asa@3 31 function addon:MAIL_INBOX_UPDATE()
Asa@3 32 local newScan = addon:ScanMail()
Asa@3 33 local diff
Asa@6 34 for mailType, collection in pairs(self.lastMailScan) do
Asa@7 35 newScan[mailType] = (newScan[mailType] or {})
Asa@6 36 for item, total in pairs(collection) do
Asa@3 37
Asa@6 38 diff = total - (newScan[mailType][item] or 0)
Asa@6 39 if diff ~= 0 then
Asa@6 40 self:SaveValue(item, diff)
Asa@6 41 end
Asa@6 42
Asa@3 43 end
Asa@3 44 end
Asa@3 45
Asa@3 46 self.lastMailScan = newScan
Asa@3 47 end
Asa@3 48
Asa@4 49 function addon:UNIT_SPELLCAST_START(event, target, spell)
Asa@5 50 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@4 51 self:UnwatchBags()
Asa@4 52 self:UpdateCurrentInventory()
Asa@4 53 self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 54 self:RegisterEvent("LOOT_CLOSED")
Asa@3 55 end
Asa@3 56 end
Asa@3 57
Asa@4 58 --[[
Asa@4 59 The item should be destroyed before this point, so the last inventory check
Asa@4 60 needs to be kept so it can be combined with the up coming loot.
Asa@4 61 ]]
Asa@4 62 function addon:LOOT_CLOSED()
Asa@4 63 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 64 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 65 local inventory = self.lastInventory
Asa@4 66 self:WatchBags()
Asa@4 67 self.lastInventory = inventory
Asa@4 68 end
Asa@3 69
Asa@4 70 function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell)
Asa@5 71 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@4 72 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 73 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 74 self:WatchBags()
Asa@4 75 end
Asa@4 76 end
Asa@4 77
Asa@4 78 function addon:UpdateCurrentInventory()
Asa@4 79 self.lastInventory = self:GetCurrentInventory()
Asa@3 80 end
Asa@3 81
Asa@3 82 function addon:UpdateAudit()
Asa@10 83 -- self:Debug("UpdateAudit")
Asa@3 84 local currentInventory = self:GetCurrentInventory()
Asa@3 85 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 86 -- this is only here for debugging
Asa@3 87 self.lastdiff = diff
Asa@3 88
Asa@5 89 local positive, negative = {}, {}
Asa@5 90 local positiveCount, negativeCount = 0, 0
Asa@5 91 for item, count in pairs(diff.items) do
Asa@5 92 if count > 0 then
Asa@5 93 positive[item] = count
Asa@5 94 positiveCount = positiveCount + count
Asa@5 95 elseif count < 0 then
Asa@5 96 negative[item] = count
Asa@5 97 negativeCount = negativeCount + abs(count)
Asa@5 98 end
Asa@5 99 end
Asa@5 100
Asa@15 101 if diff.money > 0 and self:tcount(positive) > 0 and self:tcount(negative) == 0 then
Asa@15 102 self:Debug("loot")
Asa@20 103 elseif abs(diff.money) > 0 and self:tcount(diff.items) == 1 then
Asa@15 104 self:Debug("purchase or sale")
Asa@3 105
Asa@9 106 for link, count in pairs(diff.items) do
Asa@9 107 self:SaveValue(link, 0 - diff.money)
Asa@3 108 end
Asa@15 109 elseif self:tcount(diff.items) > 1 then
Asa@3 110
Asa@15 111 if self:tcount(positive) > 0 and self:tcount(negative) > 0 then
Asa@3 112 -- we must have created/converted something
Asa@15 113 self:Debug("conversion")
Asa@10 114
Asa@3 115 local totalChange = 0
Asa@9 116 for link, change in pairs(negative) do
Asa@9 117 local _, itemCost, count = self:GetItemCost(link, change)
Asa@9 118 self:SaveValue(link, itemCost * change)
Asa@3 119
Asa@9 120 totalChange = totalChange + (itemCost * abs(change))
Asa@3 121 end
Asa@3 122
Asa@7 123 local valuePerItem = totalChange / positiveCount
Asa@3 124
Asa@9 125 for link, change in pairs(positive) do
Asa@9 126 self:SaveValue(link, valuePerItem * change)
Asa@3 127 end
Asa@3 128 end
Asa@3 129 end
Asa@3 130
Asa@3 131 self.lastInventory = currentInventory
Asa@4 132 addon:WatchBags()
Asa@3 133 end