annotate Modules/Events.lua @ 4:c940b527ccab

Fixed Milling. Disenchating will probably have to be fixed the same way
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sat, 22 May 2010 15:23:11 -0700
parents bbcf81868171
children 7d0f4ebedf8c
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@3 30 for item, total in pairs(self.lastMailScan) do
Asa@3 31
Asa@3 32 if newScan[item] == nil then
Asa@3 33 newScan[item] = 0
Asa@3 34 end
Asa@3 35 diff = total - newScan[item]
Asa@3 36 if diff ~= 0 then
Asa@3 37 self:SaveValue(item, diff)
Asa@3 38 end
Asa@3 39
Asa@3 40 end
Asa@3 41
Asa@3 42 self.lastMailScan = newScan
Asa@3 43 end
Asa@3 44
Asa@4 45 function addon:UNIT_SPELLCAST_START(event, target, spell)
Asa@4 46 if target == "player" and spell == "Milling" then
Asa@4 47 self:UnwatchBags()
Asa@4 48 self:UpdateCurrentInventory()
Asa@4 49 self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 50 self:RegisterEvent("LOOT_CLOSED")
Asa@3 51 end
Asa@3 52 end
Asa@3 53
Asa@4 54 --[[
Asa@4 55 The item should be destroyed before this point, so the last inventory check
Asa@4 56 needs to be kept so it can be combined with the up coming loot.
Asa@4 57 ]]
Asa@4 58 function addon:LOOT_CLOSED()
Asa@4 59 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 60 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 61 local inventory = self.lastInventory
Asa@4 62 self:WatchBags()
Asa@4 63 self.lastInventory = inventory
Asa@4 64 end
Asa@3 65
Asa@4 66 function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell)
Asa@4 67 if target == "player" and spell == "Milling" then
Asa@4 68 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 69 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 70 self:WatchBags()
Asa@4 71 end
Asa@4 72 end
Asa@4 73
Asa@4 74 function addon:UpdateCurrentInventory()
Asa@4 75 self.lastInventory = self:GetCurrentInventory()
Asa@3 76 end
Asa@3 77
Asa@3 78 function addon:UpdateAudit()
Asa@3 79 self:Debug("UpdateAudit")
Asa@3 80 local currentInventory = self:GetCurrentInventory()
Asa@3 81 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 82 -- this is only here for debugging
Asa@3 83 self.lastdiff = diff
Asa@3 84
Asa@3 85 if abs(diff.money) > 0 and utils:tcount(diff.items) == 1 then
Asa@3 86 self:Debug("purchase or sale")
Asa@3 87
Asa@3 88 for itemName, count in pairs(diff.items) do
Asa@3 89 self:SaveValue(itemName, diff.money)
Asa@3 90 end
Asa@3 91 elseif utils:tcount(diff.items) > 1 then
Asa@3 92 local positive, negative = {}, {}
Asa@3 93 local positiveCount, negativeCount = 0, 0
Asa@3 94 for item, count in pairs(diff.items) do
Asa@3 95 if count > 0 then
Asa@3 96 positive[item] = count
Asa@3 97 positiveCount = positiveCount + count
Asa@3 98 elseif count < 0 then
Asa@3 99 negative[item] = count
Asa@3 100 negativeCount = negativeCount + abs(count)
Asa@3 101 end
Asa@3 102 end
Asa@3 103
Asa@3 104 if utils:tcount(positive) > 0 and utils:tcount(negative) > 0 then
Asa@3 105 -- we must have created/converted something
Asa@3 106 self:Debug("conversion")
Asa@3 107 local totalChange = 0
Asa@3 108 for itemName, change in pairs(negative) do
Asa@3 109 local _, itemCost, count = self:GetItemCost(itemName, change)
Asa@3 110 self:SaveValue(itemName, abs(itemCost * change))
Asa@3 111
Asa@3 112 totalChange = totalChange + abs(itemCost * change)
Asa@3 113 end
Asa@3 114
Asa@3 115 self:Debug("totalChange")
Asa@3 116 self:Debug(totalChange)
Asa@3 117
Asa@3 118 local valuePerItem = totalChange / positiveCount
Asa@3 119 self:Debug(valuePerItem )
Asa@3 120 for itemName, change in pairs(positive) do
Asa@3 121 self:Debug(itemName)
Asa@3 122 self:Debug(0-abs(valuePerItem * change))
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