annotate Modules/Events.lua @ 24:554b30908b33 v0.1

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