public
Description: WoW Addon - 1, 2... 3?
Homepage: http://www.tekkub.net/addons/NutCounter
Clone URL: git://github.com/tekkub/nutcounter.git
Click here to lend your support to: nutcounter and make a donation at www.pledgie.com !
nutcounter / NutCounter.lua
100644 208 lines (162 sloc) 5.863 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
----------------------
-- Locals --
----------------------
 
local db
 
 
------------------------------
-- Util Functions --
------------------------------
 
local function Print(...) print("|cFF33FF99NutCounter|r:", ...) end
 
local debugf = tekDebug and tekDebug:GetFrame("NutCounter")
local function Debug(...) if debugf then debugf:AddMessage(string.join(", ", tostringall(...))) end end
 
 
-----------------------------
-- Event Handler --
-----------------------------
 
local f = CreateFrame("frame")
f:SetScript("OnEvent", function(self, event, ...) if self[event] then return self[event](self, event, ...) end end)
f:RegisterEvent("ADDON_LOADED")
 
 
function f:ADDON_LOADED(event, addon)
if addon ~= "NutCounter" then return end
 
local dbkey = GetRealmName().. " ".. UnitFactionGroup("player")
 
NutCounterDB = NutCounterDB or {}
NutCounterDB[dbkey] = NutCounterDB[dbkey] or {min = {}, max = {}, last = {}, sold = {}, failed = {}}
db = NutCounterDB[dbkey]
 
LibStub("tekKonfig-AboutPanel").new(nil, "NutCounter")
 
self:RegisterEvent("MAIL_INBOX_UPDATE")
 
self:UnregisterEvent("ADDON_LOADED")
self.ADDON_LOADED = nil
end
 
 
-----------------------
-- Buttons --
-----------------------
 
local function GrabItem(i)
AutoLootMailItem(i)
InboxFrame.openMailID = i
OpenMailFrame.updateButtonPositions = true
OpenMail_Update()
ShowUIPanel(OpenMailFrame)
InboxFrame_Update()
end
 
 
local nutting, shining
local function GatherShinies()
shining = true
for i=1,GetInboxNumItems() do
local _, _, _, subject = GetInboxHeaderInfo(i)
if subject:match("^Auction successful:") then
Debug("Grabbing cash", i, subject)
return GrabItem(i)
end
end
shining = false
Debug("Done gathering shinies")
end
 
local function CollectNuts()
nutting = true
local free = 0
for i=0,4 do free = free + GetContainerNumFreeSlots(i) end
if free > 1 then
for i=1,GetInboxNumItems() do
local _, _, _, subject = GetInboxHeaderInfo(i)
if subject:match("^Auction expired:") then
Debug("Grabbing nuts", i, subject)
return GrabItem(i)
end
end
end
nutting = false
Debug("Done collecting nuts")
end
 
local nutterbutter = LibStub("tekKonfig-Button").new_small(InboxFrame, "TOPRIGHT", -35, -56)
nutterbutter:SetWidth(30) nutterbutter:SetHeight(18)
nutterbutter:SetText("Nuts")
nutterbutter.tiptext = "Collect failed auctions"
nutterbutter:SetScript("OnClick", CollectNuts)
 
local shinybutt = LibStub("tekKonfig-Button").new_small(nutterbutter, "TOPRIGHT", nutterbutter, "BOTTOMRIGHT")
shinybutt:SetWidth(45) shinybutt:SetHeight(18)
shinybutt:SetText("Shinies")
shinybutt.tiptext = "Collect successful auctions"
shinybutt:SetScript("OnClick", GatherShinies)
 
 
-----------------------
-- Tracker --
-----------------------
 
local lastcount, lastitem, lastprice, cashingout, lastexpire
 
local orig = AutoLootMailItem
function AutoLootMailItem(i, ...)
Debug("AutoLootMailItem", i, ...)
 
local _, _, _, subject = GetInboxHeaderInfo(i)
if subject:match("^Auction expired:") then lastexpire = GetInboxItem(i, 1)
else cashingout = i end
return orig(i, ...)
end
 
 
function f:MAIL_INBOX_UPDATE()
local grabnextshiney, grabnextnut
local count = GetInboxNumItems()
Debug("MAIL_INBOX_UPDATE", count)
 
if cashingout then
local invoiceType, itemName, _, bid, buyout = GetInboxInvoiceInfo(cashingout)
Debug("Detected cashout", invoiceType, bid, buyout)
if invoiceType then
if invoiceType == "seller" then
lastitem, lastprice = itemName, buyout or bid
end
cashingout = nil
end
elseif lastexpire and lastcount and count == lastcount - 1 then
Debug("Detected expired mail removal", lastexpire)
db.failed[lastexpire] = (db.failed[lastexpire] or 0) + 1
lastexpire = nil
grabnextnut = nutting
elseif lastprice and count == lastcount - 1 then
Debug("Detected mail removal", lastprice, lastitem)
db.min[lastitem] = math.min(lastprice, db.min[lastitem] or math.huge)
db.max[lastitem] = math.max(lastprice, db.max[lastitem] or 0)
db.last[lastitem] = lastprice
db.sold[lastitem] = (db.sold[lastitem] or 0) + 1
lastprice, lastitem = nil
grabnextshiney = shining
end
 
lastcount = count
 
if grabnextnut then
Debug("Still collecting nuts")
return CollectNuts()
end
if grabnextshiney then
Debug("Still gathering shinies")
return GatherShinies()
end
end
 
 
-----------------------------
-- Tooltip stuff --
-----------------------------
 
local function GS(cash)
if not cash then return end
cash = cash/100
local s = floor(cash%100)
local g = floor(cash/100)
if g > 0 then return string.format("|cffffd700%d.|cffc7c7cf%02d", g, s)
else return string.format("|cffc7c7cf%d", s) end
end
 
 
local origs = {}
local OnTooltipSetItem = function(frame, ...)
assert(frame, "arg 1 is nil, someone isn't hooking correctly")
 
local name = frame:GetItem()
if name then
local min, max, last, sold, failed = db.min[name], db.max[name], db.last[name], db.sold[name], db.failed[name]
if min then frame:AddDoubleLine("Previous sales:", max and max ~= min and (GS(min).." - "..GS(max)) or GS(min)) end
if last then frame:AddDoubleLine("Last sale:", GS(last)) end
if sold or failed then frame:AddDoubleLine("Sellthrough:", string.format("(%d/%d) %d%%", sold or 0, (sold or 0) + (failed or 0), (sold or 0)/((sold or 0) + (failed or 0))*100)) end
end
 
if origs[frame] then return origs[frame](frame, ...) end
end
 
for i,frame in pairs{GameTooltip, ItemRefTooltip} do
origs[frame] = frame:GetScript("OnTooltipSetItem")
frame:SetScript("OnTooltipSetItem", OnTooltipSetItem)
end
 
 
--------------------------------
-- GetAuctionBuyout --
--------------------------------
 
local ids = LibStub("tekIDmemo")
local orig = GetAuctionBuyout
function GetAuctionBuyout(item)
local id = ids[item]
return orig and orig(item) or id and db.last[GetItemInfo(id)]
end