annotate Modules/Events.lua @ 3:bbcf81868171

Code cleanup.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sat, 22 May 2010 11:34:19 -0700
parents
children c940b527ccab
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@3 8 self:WatchBags()
Asa@3 9 end
Asa@3 10
Asa@3 11 function addon:MAIL_SHOW()
Asa@3 12 self:Debug("MAIL_SHOW")
Asa@3 13 self.lastMailScan = self:ScanMail()
Asa@3 14 self:UnregisterEvent("MAIL_SHOW")
Asa@3 15 self:RegisterEvent("MAIL_CLOSED")
Asa@3 16 self:RegisterEvent("MAIL_INBOX_UPDATE")
Asa@3 17 self:Debug("MAIL_SHOW complete")
Asa@3 18 end
Asa@3 19
Asa@3 20 function addon:MAIL_CLOSED()
Asa@3 21 addon:UnregisterEvent("MAIL_CLOSED")
Asa@3 22 self:UnregisterEvent("MAIL_INBOX_UPDATE")
Asa@3 23 self:RegisterEvent("MAIL_SHOW")
Asa@3 24 end
Asa@3 25
Asa@3 26 function addon:MAIL_INBOX_UPDATE()
Asa@3 27 local newScan = addon:ScanMail()
Asa@3 28 local diff
Asa@3 29 for item, total in pairs(self.lastMailScan) do
Asa@3 30
Asa@3 31 if newScan[item] == nil then
Asa@3 32 newScan[item] = 0
Asa@3 33 end
Asa@3 34 diff = total - newScan[item]
Asa@3 35 if diff ~= 0 then
Asa@3 36 self:SaveValue(item, diff)
Asa@3 37 end
Asa@3 38
Asa@3 39 end
Asa@3 40
Asa@3 41 self.lastMailScan = newScan
Asa@3 42 end
Asa@3 43
Asa@3 44 function addon:UNIT_SPELLCAST_SENT(target, spell)
Asa@3 45 if target == "player" and spell = "Milling" then
Asa@3 46
Asa@3 47 end
Asa@3 48 end
Asa@3 49
Asa@3 50 function addon:UNIT_SPELLCAST_INTERRUPTED(target, spell)
Asa@3 51
Asa@3 52 end
Asa@3 53
Asa@3 54 function addon:UpdateAudit()
Asa@3 55 self:Debug("UpdateAudit")
Asa@3 56 local currentInventory = self:GetCurrentInventory()
Asa@3 57 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 58 -- this is only here for debugging
Asa@3 59 self.lastdiff = diff
Asa@3 60
Asa@3 61 if abs(diff.money) > 0 and utils:tcount(diff.items) == 1 then
Asa@3 62 self:Debug("purchase or sale")
Asa@3 63
Asa@3 64 for itemName, count in pairs(diff.items) do
Asa@3 65 self:SaveValue(itemName, diff.money)
Asa@3 66 end
Asa@3 67 elseif utils:tcount(diff.items) > 1 then
Asa@3 68 local positive, negative = {}, {}
Asa@3 69 local positiveCount, negativeCount = 0, 0
Asa@3 70 for item, count in pairs(diff.items) do
Asa@3 71 if count > 0 then
Asa@3 72 positive[item] = count
Asa@3 73 positiveCount = positiveCount + count
Asa@3 74 elseif count < 0 then
Asa@3 75 negative[item] = count
Asa@3 76 negativeCount = negativeCount + abs(count)
Asa@3 77 end
Asa@3 78 end
Asa@3 79
Asa@3 80 if utils:tcount(positive) > 0 and utils:tcount(negative) > 0 then
Asa@3 81 -- we must have created/converted something
Asa@3 82 self:Debug("conversion")
Asa@3 83 local totalChange = 0
Asa@3 84 for itemName, change in pairs(negative) do
Asa@3 85 local _, itemCost, count = self:GetItemCost(itemName, change)
Asa@3 86 self:SaveValue(itemName, abs(itemCost * change))
Asa@3 87
Asa@3 88 totalChange = totalChange + abs(itemCost * change)
Asa@3 89 end
Asa@3 90
Asa@3 91 self:Debug("totalChange")
Asa@3 92 self:Debug(totalChange)
Asa@3 93
Asa@3 94 local valuePerItem = totalChange / positiveCount
Asa@3 95 self:Debug(valuePerItem )
Asa@3 96 for itemName, change in pairs(positive) do
Asa@3 97 self:Debug(itemName)
Asa@3 98 self:Debug(0-abs(valuePerItem * change))
Asa@3 99 self:SaveValue(itemName, 0-abs(valuePerItem * change))
Asa@3 100 end
Asa@3 101 end
Asa@3 102 end
Asa@3 103
Asa@3 104 self.lastInventory = currentInventory
Asa@3 105 end