annotate Modules/Events.lua @ 7:bbba2fae0f69

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