annotate Modules/Events.lua @ 5:7d0f4ebedf8c

work in progress: I added Prospecting and Disenchanting and AHOutbid mail and I also changed the initialization to quit forcing debug every time ItemAuditor loads and have begun to implement the COD mail scanning.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 26 May 2010 21:26:24 -0700
parents c940b527ccab
children 5dddd73b2220
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@3 10 end
Asa@3 11
Asa@3 12 function addon:MAIL_SHOW()
Asa@3 13 self:Debug("MAIL_SHOW")
Asa@3 14 self.lastMailScan = self:ScanMail()
Asa@3 15 self:UnregisterEvent("MAIL_SHOW")
Asa@3 16 self:RegisterEvent("MAIL_CLOSED")
Asa@3 17 self:RegisterEvent("MAIL_INBOX_UPDATE")
Asa@3 18 self:Debug("MAIL_SHOW complete")
Asa@3 19 end
Asa@3 20
Asa@3 21 function addon:MAIL_CLOSED()
Asa@3 22 addon:UnregisterEvent("MAIL_CLOSED")
Asa@3 23 self:UnregisterEvent("MAIL_INBOX_UPDATE")
Asa@3 24 self:RegisterEvent("MAIL_SHOW")
Asa@3 25 end
Asa@3 26
Asa@3 27 function addon:MAIL_INBOX_UPDATE()
Asa@3 28 local newScan = addon:ScanMail()
Asa@3 29 local diff
Asa@3 30 for item, total in pairs(self.lastMailScan) do
Asa@3 31
Asa@3 32 if newScan[item] == nil then
Asa@3 33 newScan[item] = 0
Asa@3 34 end
Asa@3 35 diff = total - newScan[item]
Asa@3 36 if diff ~= 0 then
Asa@3 37 self:SaveValue(item, diff)
Asa@3 38 end
Asa@3 39
Asa@3 40 end
Asa@3 41
Asa@3 42 self.lastMailScan = newScan
Asa@3 43 end
Asa@3 44
Asa@4 45 function addon:UNIT_SPELLCAST_START(event, target, spell)
Asa@5 46 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@4 47 self:UnwatchBags()
Asa@4 48 self:UpdateCurrentInventory()
Asa@4 49 self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 50 self:RegisterEvent("LOOT_CLOSED")
Asa@3 51 end
Asa@3 52 end
Asa@3 53
Asa@4 54 --[[
Asa@4 55 The item should be destroyed before this point, so the last inventory check
Asa@4 56 needs to be kept so it can be combined with the up coming loot.
Asa@4 57 ]]
Asa@4 58 function addon:LOOT_CLOSED()
Asa@4 59 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 60 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 61 local inventory = self.lastInventory
Asa@4 62 self:WatchBags()
Asa@4 63 self.lastInventory = inventory
Asa@4 64 end
Asa@3 65
Asa@4 66 function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell)
Asa@5 67 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@4 68 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 69 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 70 self:WatchBags()
Asa@4 71 end
Asa@4 72 end
Asa@4 73
Asa@4 74 function addon:UpdateCurrentInventory()
Asa@4 75 self.lastInventory = self:GetCurrentInventory()
Asa@3 76 end
Asa@3 77
Asa@3 78 function addon:UpdateAudit()
Asa@3 79 self:Debug("UpdateAudit")
Asa@3 80 local currentInventory = self:GetCurrentInventory()
Asa@3 81 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 82 -- this is only here for debugging
Asa@3 83 self.lastdiff = diff
Asa@3 84
Asa@5 85 local positive, negative = {}, {}
Asa@5 86 local positiveCount, negativeCount = 0, 0
Asa@5 87 for item, count in pairs(diff.items) do
Asa@5 88 if count > 0 then
Asa@5 89 positive[item] = count
Asa@5 90 positiveCount = positiveCount + count
Asa@5 91 elseif count < 0 then
Asa@5 92 negative[item] = count
Asa@5 93 negativeCount = negativeCount + abs(count)
Asa@5 94 end
Asa@5 95 end
Asa@5 96
Asa@5 97 if diff.money > 0 and utils:tcount(positive) > 0 and utils:tcount(negative) == 0 then
Asa@5 98 self:Debug("loot")
Asa@5 99 elseif abs(diff.money) > 0 and utils:tcount(diff.items) == 1 then
Asa@3 100 self:Debug("purchase or sale")
Asa@3 101
Asa@3 102 for itemName, count in pairs(diff.items) do
Asa@3 103 self:SaveValue(itemName, diff.money)
Asa@3 104 end
Asa@3 105 elseif utils:tcount(diff.items) > 1 then
Asa@3 106
Asa@3 107 if utils:tcount(positive) > 0 and utils:tcount(negative) > 0 then
Asa@3 108 -- we must have created/converted something
Asa@3 109 self:Debug("conversion")
Asa@3 110 local totalChange = 0
Asa@3 111 for itemName, change in pairs(negative) do
Asa@3 112 local _, itemCost, count = self:GetItemCost(itemName, change)
Asa@3 113 self:SaveValue(itemName, abs(itemCost * change))
Asa@3 114
Asa@3 115 totalChange = totalChange + abs(itemCost * change)
Asa@3 116 end
Asa@3 117
Asa@3 118 self:Debug("totalChange")
Asa@3 119 self:Debug(totalChange)
Asa@3 120
Asa@3 121 local valuePerItem = totalChange / positiveCount
Asa@3 122 self:Debug(valuePerItem )
Asa@3 123 for itemName, change in pairs(positive) do
Asa@3 124 self:Debug(itemName)
Asa@3 125 self:Debug(0-abs(valuePerItem * change))
Asa@3 126 self:SaveValue(itemName, 0-abs(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