annotate Modules/Events.lua @ 6:5dddd73b2220

Removed dependency on postal. It was only being used to determine the mail type which has been moved into ItemAuditor so I can support CODPayments. Paying for COD items works, but getting the payments back can't reliably associate the payment with an item yet.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 26 May 2010 22:41:47 -0700
parents 7d0f4ebedf8c
children bbba2fae0f69
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@6 30 for mailType, collection in pairs(self.lastMailScan) do
Asa@6 31 for item, total in pairs(collection) do
Asa@3 32
Asa@6 33 diff = total - (newScan[mailType][item] or 0)
Asa@6 34 if diff ~= 0 then
Asa@6 35 self:SaveValue(item, diff)
Asa@6 36 end
Asa@6 37
Asa@3 38 end
Asa@3 39 end
Asa@3 40
Asa@3 41 self.lastMailScan = newScan
Asa@3 42 end
Asa@3 43
Asa@4 44 function addon:UNIT_SPELLCAST_START(event, target, spell)
Asa@5 45 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@4 46 self:UnwatchBags()
Asa@4 47 self:UpdateCurrentInventory()
Asa@4 48 self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 49 self:RegisterEvent("LOOT_CLOSED")
Asa@3 50 end
Asa@3 51 end
Asa@3 52
Asa@4 53 --[[
Asa@4 54 The item should be destroyed before this point, so the last inventory check
Asa@4 55 needs to be kept so it can be combined with the up coming loot.
Asa@4 56 ]]
Asa@4 57 function addon:LOOT_CLOSED()
Asa@4 58 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 59 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 60 local inventory = self.lastInventory
Asa@4 61 self:WatchBags()
Asa@4 62 self.lastInventory = inventory
Asa@4 63 end
Asa@3 64
Asa@4 65 function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell)
Asa@5 66 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@4 67 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 68 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 69 self:WatchBags()
Asa@4 70 end
Asa@4 71 end
Asa@4 72
Asa@4 73 function addon:UpdateCurrentInventory()
Asa@4 74 self.lastInventory = self:GetCurrentInventory()
Asa@3 75 end
Asa@3 76
Asa@3 77 function addon:UpdateAudit()
Asa@3 78 self:Debug("UpdateAudit")
Asa@3 79 local currentInventory = self:GetCurrentInventory()
Asa@3 80 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 81 -- this is only here for debugging
Asa@3 82 self.lastdiff = diff
Asa@3 83
Asa@5 84 local positive, negative = {}, {}
Asa@5 85 local positiveCount, negativeCount = 0, 0
Asa@5 86 for item, count in pairs(diff.items) do
Asa@5 87 if count > 0 then
Asa@5 88 positive[item] = count
Asa@5 89 positiveCount = positiveCount + count
Asa@5 90 elseif count < 0 then
Asa@5 91 negative[item] = count
Asa@5 92 negativeCount = negativeCount + abs(count)
Asa@5 93 end
Asa@5 94 end
Asa@5 95
Asa@5 96 if diff.money > 0 and utils:tcount(positive) > 0 and utils:tcount(negative) == 0 then
Asa@5 97 self:Debug("loot")
Asa@5 98 elseif abs(diff.money) > 0 and utils:tcount(diff.items) == 1 then
Asa@3 99 self:Debug("purchase or sale")
Asa@3 100
Asa@3 101 for itemName, count in pairs(diff.items) do
Asa@3 102 self:SaveValue(itemName, diff.money)
Asa@3 103 end
Asa@3 104 elseif utils:tcount(diff.items) > 1 then
Asa@3 105
Asa@3 106 if utils:tcount(positive) > 0 and utils:tcount(negative) > 0 then
Asa@3 107 -- we must have created/converted something
Asa@3 108 self:Debug("conversion")
Asa@3 109 local totalChange = 0
Asa@3 110 for itemName, change in pairs(negative) do
Asa@3 111 local _, itemCost, count = self:GetItemCost(itemName, change)
Asa@3 112 self:SaveValue(itemName, abs(itemCost * change))
Asa@3 113
Asa@3 114 totalChange = totalChange + abs(itemCost * change)
Asa@3 115 end
Asa@3 116
Asa@3 117 self:Debug("totalChange")
Asa@3 118 self:Debug(totalChange)
Asa@3 119
Asa@3 120 local valuePerItem = totalChange / positiveCount
Asa@3 121 self:Debug(valuePerItem )
Asa@3 122 for itemName, change in pairs(positive) do
Asa@3 123 self:Debug(itemName)
Asa@3 124 self:Debug(0-abs(valuePerItem * change))
Asa@3 125 self:SaveValue(itemName, 0-abs(valuePerItem * change))
Asa@3 126 end
Asa@3 127 end
Asa@3 128 end
Asa@3 129
Asa@3 130 self.lastInventory = currentInventory
Asa@4 131 addon:WatchBags()
Asa@3 132 end