tekkub / nutcounter

WoW Addon - 1, 2... 3?

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