vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Enchantrix Addon for World of Warcraft(tm).
3 Version: 3.6.1 (Platypus)
4 Revision: $Id: EnxTooltip.lua 911 2006-06-23 10:08:24Z aradan $
5  
6 Tooltip functions.
7  
8 License:
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13  
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18  
19 You should have received a copy of the GNU General Public License
20 along with this program(see GLP.txt); if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 ]]
23  
24 -- Global functions
25 local addonLoaded -- Enchantrix.Tooltip.AddonLoaded()
26 local tooltipFormat -- Enchantrix.Tooltip.Format
27  
28 -- Local functions
29 local itemTooltip
30 local enchantTooltip
31 local hookTooltip
32  
33 function addonLoaded()
34 -- Hook in new tooltip code
35 Stubby.RegisterFunctionHook("EnhTooltip.AddTooltip", 400, hookTooltip)
36 end
37  
38 tooltipFormat = {
39 currentFormat = "fancy",
40 format = {
41 ["fancy"] = {
42 -- counts = off
43 ['off'] = " $conf% |q$name|r $rate",
44 -- counts = on
45 ['on'] = " $conf% |q$name|r $rate |e(B=$bcount L=$lcount)|r",
46 },
47 ["default"] = {
48 ['off'] = " $name: $prob% $rate",
49 ['on'] = " $name: $prob% $rate |e(B=$bcount L=$lcount)|r",
50 },
51 },
52 patterns = {
53 -- Strings
54 ["$prob"] = "", -- Probability: "75"
55 ["$conf"] = "", -- Confidence interval: "<1", "72-78", ">99"
56 ["$count"] = "", -- Local + base counts: "51"
57 ["$lcount"] = "", -- Local count: "13"
58 ["$bcount"] = "", -- Base count: "38"
59 ["$name"] = "", -- Name: "Lesser Magic Essence"
60 ["$rate"] = "", -- Avg drop amount: "x1.5"
61 -- Colors
62 ["|q"] = "", -- Quality color
63 ["|E"] = "|cffcccc33", -- Yellow ("Enchantrix" color)
64 ["|e"] = "|cff7f7f00", -- Dark yellow
65 ["|r"] = "|r", -- Reset color
66 },
67 SelectFormat = function(this, fmt)
68 if this.format[fmt] then
69 this.currentFormat = fmt
70 else
71 this.currentFormat = "default"
72 end
73 end,
74 SetFormat = function(this, fmt, val, counts)
75 if counts == nil then
76 counts = Enchantrix.Config.GetFilter('counts')
77 end
78 if counts then
79 this.format[fmt]['on'] = val
80 else
81 this.format[fmt]['off'] = val
82 end
83 end,
84 GetFormat = function(this, fmt, counts)
85 if not this.format[fmt] then return end
86 if counts == nil then
87 counts = Enchantrix.Config.GetFilter('counts')
88 end
89 if counts then
90 return this.format[fmt]['on']
91 else
92 return this.format[fmt]['off']
93 end
94 end,
95 GetString = function(this, counts)
96 local line
97 if counts == nil then
98 counts = Enchantrix.Config.GetFilter('counts')
99 end
100 if counts then
101 line = this.format[this.currentFormat]['on']
102 else
103 line = this.format[this.currentFormat]['off']
104 end
105 -- Replace patterns
106 for pat, repl in pairs(this.patterns) do
107 line = string.gsub(line, pat, repl or "")
108 end
109 return line
110 end,
111 SetPattern = function(this, pat, repl)
112 this.patterns[pat] = repl
113 end,
114 }
115  
116 function itemTooltip(funcVars, retVal, frame, name, link, quality, count)
117 local embed = Enchantrix.Config.GetFilter('embed');
118  
119 -- Check for disenchantable target
120 local id = Enchantrix.Util.GetItemIdFromLink(link)
121 if (not id or id == 0 or not Enchantrix.Util.IsDisenchantable(id)) then
122 return
123 end
124  
125 local disenchantsTo = Enchantrix.Storage.GetItemDisenchants(Enchantrix.Util.GetSigFromLink(link), name, true);
126  
127 -- Process the results
128 local totals = disenchantsTo.totals;
129 disenchantsTo.totals = nil;
130 if (totals and totals.total > 0) then
131  
132 -- Terse mode
133 if Enchantrix.Config.GetFilter('terse') and not IsControlKeyDown() then
134 if Enchantrix.Config.GetFilter('valuate-hsp') and totals.hspValue > 0 then
135 EnhTooltip.AddLine(_ENCH('FrmtValueAuctHsp'), Enchantrix.Util.Round(totals.hspValue * totals.conf, 3), embed);
136 EnhTooltip.LineColor(0.1,0.6,0.6);
137 elseif Enchantrix.Config.GetFilter('valuate-median') and totals.medValue > 0 then
138 EnhTooltip.AddLine(_ENCH('FrmtValueAuctMed'), Enchantrix.Util.Round(totals.medValue * totals.conf, 3), embed);
139 EnhTooltip.LineColor(0.1,0.6,0.6);
140 elseif Enchantrix.Config.GetFilter('valuate-baseline') and totals.mktValue > 0 then
141 EnhTooltip.AddLine(_ENCH('FrmtValueMarket'), Enchantrix.Util.Round(totals.mktValue * totals.conf, 3), embed);
142 EnhTooltip.LineColor(0.1,0.6,0.6);
143 end
144 return
145 end
146  
147 -- If it looks quirky, and we haven't disenchanted it, then ignore it...
148 if (totals.iCount + totals.biCount < 1) then return; end
149  
150 -- Header
151 local total = ""
152 if (Enchantrix.Config.GetFilter('counts')) then
153 total = string.format(" |cff7f7f00(%d)|r", totals.total)
154 end
155 EnhTooltip.AddLine(_ENCH('FrmtDisinto')..total, nil, embed);
156 EnhTooltip.LineColor(0.8,0.8,0.2);
157  
158 local lines = {}
159 for dSig, counts in pairs(disenchantsTo) do
160 local p = (counts.biCount + counts.iCount) / totals.total
161 local pmin, pmax = Enchantrix.Util.ConfidenceInterval(p, totals.total)
162  
163 -- Probabilities
164 p, pmin, pmax = math.floor(p * 100 + 0.5), math.floor(pmin * 100 + 0.5), math.floor(pmax * 100 + 0.5)
165 tooltipFormat:SetPattern("$prob", tostring(p))
166 if pmin == 0 then
167 tooltipFormat:SetPattern("$conf", "<"..max(pmax, 1))
168 elseif pmax == 100 then
169 tooltipFormat:SetPattern("$conf", ">"..min(pmin, 99))
170 else
171 if pmin ~= pmax then
172 tooltipFormat:SetPattern("$conf", pmin.."-"..pmax)
173 else
174 tooltipFormat:SetPattern("$conf", tostring(p))
175 end
176 end
177  
178 -- Counts
179 tooltipFormat:SetPattern("$count", tostring(counts.biCount + counts.iCount))
180 tooltipFormat:SetPattern("$lcount", tostring(counts.iCount))
181 tooltipFormat:SetPattern("$bcount", tostring(counts.biCount))
182  
183 -- Name and quality
184 local name, _, quality = Enchantrix.Util.GetReagentInfo(dSig)
185 local _, _, _, color = GetItemQualityColor(quality or 0)
186 tooltipFormat:SetPattern("|q", color or "|cffcccc33")
187 tooltipFormat:SetPattern("$name", name or counts.name)
188  
189 -- Rate
190 if counts.rate ~= 1 then
191 tooltipFormat:SetPattern("$rate", string.format("x%0.1f", counts.rate))
192 else
193 tooltipFormat:SetPattern("$rate", "")
194 end
195  
196 -- Store this line and sort key
197 local line = tooltipFormat:GetString(Enchantrix.Config.GetFilter('counts'))
198 table.insert(lines, {str = line, sort = pmin})
199 end
200  
201 -- Sort in order of decreasing probability before adding to tooltip
202 table.sort(lines, function(a, b) return a.sort > b.sort end)
203 for n, line in ipairs(lines) do
204 EnhTooltip.AddLine(line.str, nil, embed)
205 EnhTooltip.LineColor(0.8, 0.8, 0.2);
206 if n >= 5 then break end -- Don't add more than 5 lines
207 end
208  
209 if (Enchantrix.Config.GetFilter('valuate')) then
210 local confidence = totals.conf;
211  
212 if (Enchantrix.Config.GetFilter('valuate-hsp') and totals.hspValue > 0) then
213 EnhTooltip.AddLine(_ENCH('FrmtValueAuctHsp'), Enchantrix.Util.Round(totals.hspValue * confidence, 3), embed);
214 EnhTooltip.LineColor(0.1,0.6,0.6);
215 end
216 if (Enchantrix.Config.GetFilter('valuate-median') and totals.medValue > 0) then
217 EnhTooltip.AddLine(_ENCH('FrmtValueAuctMed'), Enchantrix.Util.Round(totals.medValue * confidence, 3), embed);
218 EnhTooltip.LineColor(0.1,0.6,0.6);
219 end
220 if (Enchantrix.Config.GetFilter('valuate-baseline') and totals.mktValue > 0) then
221 EnhTooltip.AddLine(_ENCH('FrmtValueMarket'), Enchantrix.Util.Round(totals.mktValue * confidence, 3), embed);
222 EnhTooltip.LineColor(0.1,0.6,0.6);
223 end
224 end
225 end
226 end
227  
228 local function getReagentsFromCraftFrame(craftIndex)
229 local reagentList = {}
230  
231 local numReagents = GetCraftNumReagents(craftIndex)
232 for i = 1, numReagents do
233 local link = GetCraftReagentItemLink(craftIndex, i)
234 if link then
235 local hlink = EnhTooltip.HyperlinkFromLink(link)
236 local reagentName, reagentTexture, reagentCount, playerReagentCount = GetCraftReagentInfo(craftIndex, i)
237 table.insert(reagentList, {hlink, reagentCount})
238 end
239 end
240  
241 return reagentList
242 end
243  
244 local function getReagentsFromTooltip(frame)
245 local frameName = frame:GetName()
246 local nLines = frame:NumLines()
247 local reagents
248 -- Find reagents line ("Reagents: ...")
249 for i = 1, nLines do
250 local text = getglobal(frameName.."TextLeft"..i):GetText()
251  
252 -- string.find(text, "Reagents: (.+)")
253 local _, _, r = string.find(text, _ENCH('PatReagents'))
254 if r then
255 reagents = r
256 break
257 end
258 end
259 if not reagents then return end
260  
261 local reagentList = {}
262 local name, quality, color, hlink
263 -- Process reagents separated by ","
264 for reagent in Enchantrix.Util.Spliterator(reagents, ",") do
265 -- Chomp whitespace
266 reagent = string.gsub(reagent, "^%s*", "")
267 reagent = string.gsub(reagent, "%s*$", "")
268 -- ...and color codes
269 reagent = string.gsub(reagent, "^%|c%x%x%x%x%x%x%x%x", "")
270 reagent = string.gsub(reagent, "%|r$", "")
271  
272 -- Get and chomp counts, e.g "Strange Dust (2)"
273 local _, _, count = string.find(reagent, "%((%d+)%)$")
274 if count then
275 reagent = string.gsub(reagent, "%s*%(%d+%)$", "")
276 count = tonumber(count)
277 else
278 count = 1
279 end
280  
281 hlink = Enchantrix.Util.GetLinkFromName(reagent)
282 if hlink then
283 table.insert(reagentList, {hlink, count})
284 else
285 return
286 end
287 end
288  
289 return reagentList
290 end
291  
292 function enchantTooltip(funcVars, retVal, frame, name, link)
293 local embed = Enchantrix.Config.GetFilter('embed');
294  
295 local craftIndex
296 for i = 1, GetNumCrafts() do
297 local craftName = GetCraftInfo(i)
298 if name == craftName then
299 craftIndex = i
300 break
301 end
302 end
303  
304 -- Get reagent list
305 local reagentList
306 if craftIndex then
307 reagentList = getReagentsFromCraftFrame(craftIndex)
308 else
309 reagentList = getReagentsFromTooltip(frame)
310 end
311  
312 if not reagentList or table.getn(reagentList) < 1 then
313 return
314 end
315  
316 -- Append additional reagent info
317 for _, reagent in ipairs(reagentList) do
318 local name, link, quality = Enchantrix.Util.GetReagentInfo(reagent[1])
319 local hsp, median, market = Enchantrix.Util.GetReagentPrice(reagent[1])
320 local _, _, _, color = GetItemQualityColor(quality)
321  
322 reagent[1] = name
323 table.insert(reagent, quality)
324 table.insert(reagent, color)
325 table.insert(reagent, hsp)
326 end
327  
328 local NAME, COUNT, QUALITY, COLOR, PRICE = 1, 2, 3, 4, 5
329  
330 -- Sort by rarity and price
331 table.sort(reagentList, function(a,b)
332 if (not b) or (not a) then return end
333 return ((b[QUALITY] or -1) < (a[QUALITY] or -1)) or ((b[PRICE] or 0) < (a[PRICE] or 0))
334 end)
335  
336 -- Header
337 if not embed then
338 local icon
339 if craftIndex then
340 icon = GetCraftIcon(craftIndex)
341 else
342 icon = "Interface\\Icons\\Spell_Holy_GreaterHeal"
343 end
344 EnhTooltip.SetIcon(icon)
345 EnhTooltip.AddLine(name)
346 EnhTooltip.AddLine(EnhTooltip.HyperlinkFromLink(link))
347 end
348 EnhTooltip.AddLine(_ENCH('FrmtSuggestedPrice'), nil, embed)
349 EnhTooltip.LineColor(0.8,0.8,0.2)
350  
351 local price = 0
352 local unknownPrices
353 -- Add reagent list to tooltip and sum reagent prices
354 for _, reagent in pairs(reagentList) do
355 local line = " "
356  
357 if reagent[COLOR] then
358 line = line..reagent[COLOR]
359 end
360 line = line..reagent[NAME]
361 if reagent[COLOR] then
362 line = line.."|r"
363 end
364 line = line.." x"..reagent[COUNT]
365 if reagent[COUNT] > 1 and reagent[PRICE] then
366 line = line.." "..string.format(_ENCH('FrmtPriceEach'), EnhTooltip.GetTextGSC(Enchantrix.Util.Round(reagent[PRICE], 3)))
367 EnhTooltip.AddLine(line, Enchantrix.Util.Round(reagent[PRICE] * reagent[COUNT], 3), embed)
368 price = price + reagent[PRICE] * reagent[COUNT]
369 elseif reagent[PRICE] then
370 EnhTooltip.AddLine(line, Enchantrix.Util.Round(reagent[PRICE], 3), embed)
371 price = price + reagent[PRICE]
372 else
373 EnhTooltip.AddLine(line, nil, embed)
374 unknownPrices = true
375 end
376 EnhTooltip.LineColor(0.7,0.7,0.1)
377 end
378  
379 -- Barker price
380 local margin = Enchantrix_BarkerGetConfig("profit_margin")
381 local profit = price * margin * 0.01
382 profit = math.min(profit, Enchantrix_BarkerGetConfig("highest_profit"))
383 local barkerPrice = Enchantrix_RoundPrice(price + profit)
384  
385 -- Totals
386 if price > 0 then
387 EnhTooltip.AddLine(_ENCH('FrmtTotal'), Enchantrix.Util.Round(price, 2.5), embed)
388 EnhTooltip.LineColor(0.8,0.8,0.2)
389 if Enchantrix.Config.GetFilter('barker') and (GetLocale() == "enUS") then
390 -- "Barker Price (%d%% margin)"
391 EnhTooltip.AddLine(string.format(_ENCH('FrmtBarkerPrice'), Enchantrix.Util.Round(margin)), barkerPrice, embed)
392 EnhTooltip.LineColor(0.8,0.8,0.2)
393 end
394  
395 if not Enchantrix.State.Auctioneer_Loaded then
396 EnhTooltip.AddLine(_ENCH('FrmtWarnAuctNotLoaded'))
397 EnhTooltip.LineColor(0.6,0.6,0.1)
398 end
399  
400 if unknownPrices then
401 EnhTooltip.AddLine(_ENCH('FrmtWarnPriceUnavail'))
402 EnhTooltip.LineColor(0.6,0.6,0.1)
403 end
404 else
405 EnhTooltip.AddLine(_ENCH('FrmtWarnNoPrices'))
406 EnhTooltip.LineColor(0.6,0.6,0.1)
407 end
408 end
409  
410 function hookTooltip(funcVars, retVal, frame, name, link, quality, count)
411 -- nothing to do, if enchantrix is disabled
412 if (not Enchantrix.Config.GetFilter('all')) then
413 return
414 end
415  
416 local ltype = EnhTooltip.LinkType(link)
417 if ltype == "item" then
418 itemTooltip(funcVars, retVal, frame, name, link, quality, count)
419 elseif ltype == "enchant" then
420 enchantTooltip(funcVars, retVal, frame, name, link)
421 end
422 end
423  
424 Enchantrix.Tooltip = {
425 Revision = "$Revision: 911 $",
426  
427 AddonLoaded = addonLoaded,
428 Format = tooltipFormat,
429 }