annotate Modules/Events.lua @ 35:aaa716c93fb2 v0.1.1

Added the ability to change the price of an item. You can click the Total Invested or the Invested Each to change the value.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sun, 18 Jul 2010 16:43:03 -0700
parents f5d384fe7e4a
children e27d13095b49
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@26 33 local storedCountDiff
Asa@3 34 function addon:MAIL_INBOX_UPDATE()
Asa@23 35 self:Debug("MAIL_INBOX_UPDATE")
Asa@3 36 local newScan = addon:ScanMail()
Asa@3 37 local diff
Asa@6 38 for mailType, collection in pairs(self.lastMailScan) do
Asa@7 39 newScan[mailType] = (newScan[mailType] or {})
Asa@26 40 for itemName, data in pairs(collection) do
Asa@26 41 newScan[mailType][itemName] = (newScan[mailType][itemName] or {total=0,count=0})
Asa@26 42 local totalDiff = data.total - newScan[mailType][itemName].total
Asa@26 43 local countDiff = data.count - newScan[mailType][itemName].count
Asa@26 44 --[[
Asa@26 45 In one update the item will be taken and in the following update the invoice
Asa@26 46 will be gone. I need to store the item difference in order ot pass it into
Asa@26 47 SaveValue.
Asa@26 48 ]]
Asa@26 49 if countDiff ~= 0 then
Asa@26 50 storedCountDiff = countDiff
Asa@26 51 end
Asa@26 52
Asa@26 53 if totalDiff ~= 0 then
Asa@26 54 self:SaveValue(itemName, totalDiff, storedCountDiff)
Asa@26 55 storedCountDiff = 0
Asa@6 56 end
Asa@6 57
Asa@3 58 end
Asa@3 59 end
Asa@3 60
Asa@3 61 self.lastMailScan = newScan
Asa@3 62 end
Asa@3 63
Asa@4 64 function addon:UNIT_SPELLCAST_START(event, target, spell)
Asa@5 65 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@23 66 self:Debug(event .. " " .. spell)
Asa@4 67 self:UnwatchBags()
Asa@4 68 self:UpdateCurrentInventory()
Asa@4 69 self:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 70 self:RegisterEvent("LOOT_CLOSED")
Asa@3 71 end
Asa@3 72 end
Asa@3 73
Asa@4 74 --[[
Asa@4 75 The item should be destroyed before this point, so the last inventory check
Asa@4 76 needs to be kept so it can be combined with the up coming loot.
Asa@4 77 ]]
Asa@4 78 function addon:LOOT_CLOSED()
Asa@23 79 self:Debug("LOOT_CLOSED")
Asa@4 80 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 81 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 82 local inventory = self.lastInventory
Asa@4 83 self:WatchBags()
Asa@4 84 self.lastInventory = inventory
Asa@4 85 end
Asa@3 86
Asa@4 87 function addon:UNIT_SPELLCAST_INTERRUPTED(event, target, spell)
Asa@5 88 if target == "player" and spell == "Milling" or spell == "Prospecting" or spell == "Disenchanting" then
Asa@23 89 self:Debug(event .. " " .. spell)
Asa@4 90 self:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED")
Asa@4 91 self:UnregisterEvent("LOOT_CLOSED")
Asa@4 92 self:WatchBags()
Asa@4 93 end
Asa@4 94 end
Asa@4 95
Asa@4 96 function addon:UpdateCurrentInventory()
Asa@4 97 self.lastInventory = self:GetCurrentInventory()
Asa@3 98 end
Asa@3 99
Asa@3 100 function addon:UpdateAudit()
Asa@23 101 -- self:Debug("UpdateAudit " .. event)
Asa@3 102 local currentInventory = self:GetCurrentInventory()
Asa@3 103 local diff = addon:GetInventoryDiff(self.lastInventory, currentInventory)
Asa@3 104
Asa@5 105 local positive, negative = {}, {}
Asa@5 106 local positiveCount, negativeCount = 0, 0
Asa@5 107 for item, count in pairs(diff.items) do
Asa@5 108 if count > 0 then
Asa@5 109 positive[item] = count
Asa@5 110 positiveCount = positiveCount + count
Asa@5 111 elseif count < 0 then
Asa@5 112 negative[item] = count
Asa@5 113 negativeCount = negativeCount + abs(count)
Asa@5 114 end
Asa@5 115 end
Asa@5 116
Asa@23 117 if positiveCount + negativeCount == 0 then
Asa@33 118 --[[
Asa@33 119 Nothing needs to be done, but this will prevent mistakenly attributing
Asa@33 120 the cost of flights to the first item you pick up.
Asa@33 121 ]]
Asa@33 122 elseif diff.money > 0 and self:tcount(positive) > 0 and self:tcount(negative) == 0 then
Asa@15 123 self:Debug("loot")
Asa@20 124 elseif abs(diff.money) > 0 and self:tcount(diff.items) == 1 then
Asa@15 125 self:Debug("purchase or sale")
Asa@3 126
Asa@9 127 for link, count in pairs(diff.items) do
Asa@26 128 self:SaveValue(link, 0 - diff.money, count)
Asa@3 129 end
Asa@23 130 elseif self:tcount(diff.items) > 1 and self:tcount(positive) > 0 and self:tcount(negative) > 0 then
Asa@23 131 -- we must have created/converted something
Asa@23 132 self:Debug("conversion")
Asa@3 133
Asa@23 134 local totalChange = 0
Asa@23 135 for link, change in pairs(negative) do
Asa@23 136 local _, itemCost, count = self:GetItemCost(link, change)
Asa@26 137 self:SaveValue(link, itemCost * change, change)
Asa@10 138
Asa@23 139 totalChange = totalChange + (itemCost * abs(change))
Asa@3 140 end
Asa@23 141
Asa@23 142 local valuePerItem = totalChange / positiveCount
Asa@23 143
Asa@23 144 for link, change in pairs(positive) do
Asa@26 145 self:SaveValue(link, valuePerItem * change, change)
Asa@23 146 end
Asa@23 147 else
Asa@23 148 self:Debug("No match in UpdateAudit.")
Asa@3 149 end
Asa@3 150
Asa@3 151 self.lastInventory = currentInventory
Asa@4 152 addon:WatchBags()
Asa@3 153 end