-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOfTheStatistician.lua
More file actions
212 lines (177 loc) · 6.88 KB
/
OfTheStatistician.lua
File metadata and controls
212 lines (177 loc) · 6.88 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
209
210
211
212
local _;
-- Prints a string with a formatted 'OTS - ' in front of it.
local function otsPrint(string)
print("|cFFFFDD00 OTS - |cFFFFFFFF" .. string);
end
-- Displays information when the addon first loads.
local function greeting()
print("|cFFFFDD00 Of The Statistician |cFFFFFFFF Loaded. Check 'github.com/naschorr/Of-The-Statistician' for the latest updates.");
end
-- Handles the initialization checks before we do anything else.
local function startup(itemName)
if(itemName == nil) then -- Sometimes, when dropping items into the AH sell panel, the tooltip will return nil.
return nil;
end
if(type(itemName) ~= "string") then -- Check to make sure we got a string.
otsPrint("String not recieved. Did you accidentally call it with an item ID?");
return nil;
end
if(not IsAddOnLoaded("Auctionator")) then -- Check to ensure that Auctionator is loaded (ideally this won't ever be necessary.)
otsPrint("Auctionator isn't loaded. Please make sure that it is working.");
return nil;
end
return true;
end
-- Merges a given amount of tables together.
local function mergeTables(tables)
if(#tables == 1) then
return tables[1];
else
for iter = 1, #tables[2] do
table.insert(tables[1], tables[2][iter]);
end
table.remove(tables, 2);
return mergeTables(tables);
end
end
-- Formats a supplied suffix by appending and prepending optional arguments.
-- Potential arguments include (suffixTable, prependString, appendString)
-- This needs improvement, function calls take waaay too much typing.
local function formatSuffixTable(args)
local suffix = {};
local prepend, append = "", "";
if(not args.suffixTable) then
otsPrint("Error with suffix table formatting -- No suffix table supplied.")
return;
else
for iter = 1, #args.suffixTable do
table.insert(suffix, args.suffixTable[iter]);
end
end
if(args.prependString) then
prepend = args.prependString;
end
if(args.appendString) then
append = args.appendString;
end
for iter = 1, #suffix do
suffix[iter] = prepend .. suffix[iter] .. append;
end
return suffix;
end
-- Picks out the appropriate suffixes for the given item's level requirement.
local function buildSuffixTable(reqLevel)
local suffixes = {};
if(reqLevel >= 1) then
local oneAttr = formatSuffixTable{prependString=" of ", suffixTable=OTS_OneAttr_Suffixes};
local resistances = formatSuffixTable{prependString=" of ", suffixTable=OTS_Resistance_Suffixes, appendString=" Resistance"};
local twoAttrNonThe = formatSuffixTable{prependString=" of ", suffixTable=OTS_TwoAttr_Suffixes_nonThe};
local twoAttr = formatSuffixTable{prependString=" of the ", suffixTable=OTS_TwoAttr_Suffixes};
local bcNonThe = formatSuffixTable{prependString=" of ", suffixTable=OTS_BC_Suffixes_nonThe};
local bc = formatSuffixTable{prependString=" of the ", suffixTable=OTS_BC_Suffixes};
suffixes = mergeTables({oneAttr, resistances, twoAttrNonThe, twoAttr, bcNonThe, bc});
end
if(reqLevel >= 55) then
local abyssal = formatSuffixTable{prependString=" of ", suffixTable=OTS_Abyssal_Suffixes};
local protection = formatSuffixTable{prependString=" of ", suffixTable=OTS_Resistance_Suffixes, appendString=" Protection"};
suffixes = mergeTables({suffixes, abyssal, protection});
end
if(reqLevel >= 70) then
local wotlk = formatSuffixTable{prependString=" of the ", suffixTable=OTS_WOTLK_Suffixes};
suffixes = mergeTables({suffixes, wotlk});
end
if(reqLevel >= 80) then
local fourAttr = formatSuffixTable{prependString=" of the ", suffixTable=OTS_FourAttr_Suffixes};
suffixes = mergeTables({suffixes, fourAttr});
end
if(reqLevel >= 90) then
local wod = formatSuffixTable{prependString=" of the ", suffixTable=OTS_WOD_Suffixes};
suffixes = mergeTables({suffixes, wod});
end
return suffixes;
end
-- Uses auctionator API to get the lowest, average, and highest price for a item-suffix combination.
local function getPriceStats(itemName, suffixTable)
local prices = {};
for iter = 1, #suffixTable do
local price = Atr_GetAuctionBuyout(itemName .. suffixTable[iter]);
if(price) then
table.insert(prices, price);
end
end
local min, avg, max = nil, nil, nil;
if(#prices >= 1) then
min = prices[1];
avg = prices[1];
max = prices[1];
for iter = 2, #prices do
local current = prices[iter];
if(current < min) then
min = current;
end
if(current > max) then
max = current;
end
avg = avg + current;
end
avg = avg/#prices;
end
return min, avg, max;
end
-- Builds a string to display the gold, silver, and copper values for a given amount with the appropriate icons.
local function getGoldString(amount)
local goldIcon = "|TInterface\\MoneyFrame\\UI-GoldIcon:12:12:4:0|t"
local silverIcon = "|TInterface\\MoneyFrame\\UI-SilverIcon:12:12:4:0|t"
local copperIcon = "|TInterface\\MoneyFrame\\UI-CopperIcon:12:12:4:0|t"
copper = math.floor(amount - math.floor(amount/100) * 100);
silver = math.floor((amount - math.floor(amount/10000) * 10000)/100);
gold = math.floor(amount/10000);
return gold .. goldIcon .. " " .. silver .. silverIcon .. " " .. copper .. copperIcon;
end
-- When a tooltip for an item is generated, try to add the OTS data if possible.
local lineAdded = false;
local function OnTooltipSetItem(tooltip, ...)
if(not lineAdded) then
local name, _ = tooltip:GetItem();
local min, avg, max = OTS(name);
if(min and avg and max) then
-- Format it to look like Auctionator's tooltip output
tooltip:AddDoubleLine("Minimum", "|cFFFFFFFF" .. getGoldString(min));
tooltip:AddDoubleLine("Average", "|cFFFFFFFF" .. getGoldString(avg));
tooltip:AddDoubleLine("Maximum", "|cFFFFFFFF" .. getGoldString(max));
lineAdded = true;
end
end
end
-- Ensures that only a single line gets added to the tooltip.
local function OnTooltipCleared(tooltip, ...)
lineAdded = false;
end
-- Hook into the tooltip, and call the previous functions when necessary.
GameTooltip:HookScript("OnTooltipSetItem", OnTooltipSetItem);
GameTooltip:HookScript("OnTooltipCleared", OnTooltipCleared);
-- Function alias for ease of use
function OTS(itemName)
return OfTheStatistician(itemName);
end
-- Main
function OfTheStatistician(itemName)
if(not startup(itemName)) then -- Ensure that Auctionator is working, and a string has been provided.
return nil, nil, nil;
end
local name, reqLevel, class;
name, _, _, _, reqLevel, class, _, _, _, _, _ = GetItemInfo(itemName); -- Get the required level, and class of the given item.
if(class ~= "Armor" and class ~= "Weapon") then -- Check to make sure that the item is a weapon or armor piece.
return nil, nil, nil;
end
local suffixes = buildSuffixTable(reqLevel); -- Build a list of possible suffixes based on the item's required level.
if(#suffixes == 0) then
return nil, nil, nil;
end
local low, avg, high = getPriceStats(name, suffixes); -- Use the item's root name and the list of suffixes to determine pricing information.
if(low and avg and high) then
return low, avg, high;
end
return nil, nil, nil;
end
greeting();