annotate Modules/Events.lua @ 9:374dd1a90d02

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