annotate Modules/Events.lua @ 23:819bfdc5d73c

More debug messages and added the ability to scroll the debug window.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Thu, 08 Jul 2010 23:55:05 -0700
parents ff9a698caebc
children 554b30908b33
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 end
Asa@3 22
Asa@3 23 function addon:MAIL_CLOSED()
Asa@23 24 self:Debug("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@23 32 self:Debug("MAIL_INBOX_UPDATE")
Asa@3 33 local newScan = addon:ScanMail()
Asa@3 34 local diff
Asa@6 35 for mailType, collection in pairs(self.lastMailScan) do
Asa@7 36 newScan[mailType] = (newScan[mailType] or {})
Asa@6 37 for item, total in pairs(collection) do
Asa@3 38
Asa@6 39 diff = total - (newScan[mailType][item] or 0)
Asa@6 40 if diff ~= 0 then
Asa@6 41 self:SaveValue(item, diff)
Asa@6 42 end
Asa@6 43
Asa@3 44 end
Asa@3 45 end
Asa@3 46
Asa@3 47 self.lastMailScan = newScan
Asa@3 48 end
Asa@3 49
Asa@4 50 function addon:UNIT_SPELLCAST_START(event, target, spell)
Asa@5 51 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@23 52 self:Debug(event .. " " .. spell)
Asa@4 53 self:UnwatchBags()
Asa@4 54 self:UpdateCurrentInventory()
Asa@4 55 self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 56 self:RegisterEvent("LOOT_CLOSED")
Asa@3 57 end
Asa@3 58 end
Asa@3 59
Asa@4 60 --[[
Asa@4 61 The item should be destroyed before this point, so the last inventory check
Asa@4 62 needs to be kept so it can be combined with the up coming loot.
Asa@4 63 ]]
Asa@4 64 function addon:LOOT_CLOSED()
Asa@23 65 self:Debug("LOOT_CLOSED")
Asa@4 66 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 67 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 68 local inventory = self.lastInventory
Asa@4 69 self:WatchBags()
Asa@4 70 self.lastInventory = inventory
Asa@4 71 end
Asa@3 72
Asa@4 73 function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell)
Asa@5 74 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@23 75 self:Debug(event .. " " .. spell)
Asa@4 76 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 77 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 78 self:WatchBags()
Asa@4 79 end
Asa@4 80 end
Asa@4 81
Asa@4 82 function addon:UpdateCurrentInventory()
Asa@4 83 self.lastInventory = self:GetCurrentInventory()
Asa@3 84 end
Asa@3 85
Asa@3 86 function addon:UpdateAudit()
Asa@23 87 -- self:Debug("UpdateAudit " .. event)
Asa@3 88 local currentInventory = self:GetCurrentInventory()
Asa@3 89 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 90
Asa@5 91 local positive, negative = {}, {}
Asa@5 92 local positiveCount, negativeCount = 0, 0
Asa@5 93 for item, count in pairs(diff.items) do
Asa@5 94 if count > 0 then
Asa@5 95 positive[item] = count
Asa@5 96 positiveCount = positiveCount + count
Asa@5 97 elseif count < 0 then
Asa@5 98 negative[item] = count
Asa@5 99 negativeCount = negativeCount + abs(count)
Asa@5 100 end
Asa@5 101 end
Asa@5 102
Asa@23 103 if positiveCount + negativeCount == 0 then
Asa@23 104 return
Asa@23 105 end
Asa@23 106
Asa@15 107 if diff.money > 0 and self:tcount(positive) > 0 and self:tcount(negative) == 0 then
Asa@15 108 self:Debug("loot")
Asa@20 109 elseif abs(diff.money) > 0 and self:tcount(diff.items) == 1 then
Asa@15 110 self:Debug("purchase or sale")
Asa@3 111
Asa@9 112 for link, count in pairs(diff.items) do
Asa@9 113 self:SaveValue(link, 0 - diff.money)
Asa@3 114 end
Asa@23 115 elseif self:tcount(diff.items) > 1 and self:tcount(positive) > 0 and self:tcount(negative) > 0 then
Asa@23 116 -- we must have created/converted something
Asa@23 117 self:Debug("conversion")
Asa@3 118
Asa@23 119 local totalChange = 0
Asa@23 120 for link, change in pairs(negative) do
Asa@23 121 local _, itemCost, count = self:GetItemCost(link, change)
Asa@23 122 self:SaveValue(link, itemCost * change)
Asa@10 123
Asa@23 124 totalChange = totalChange + (itemCost * abs(change))
Asa@3 125 end
Asa@23 126
Asa@23 127 local valuePerItem = totalChange / positiveCount
Asa@23 128
Asa@23 129 for link, change in pairs(positive) do
Asa@23 130 self:SaveValue(link, valuePerItem * change)
Asa@23 131 end
Asa@23 132 else
Asa@23 133 self:Debug("No match in UpdateAudit.")
Asa@3 134 end
Asa@3 135
Asa@3 136 self.lastInventory = currentInventory
Asa@4 137 addon:WatchBags()
Asa@3 138 end