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