-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuyCheap.lua
More file actions
433 lines (405 loc) · 15 KB
/
BuyCheap.lua
File metadata and controls
433 lines (405 loc) · 15 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
function BuyCheap_OnLoad()
buycheap_button = CreateFrame("Button", "MyButton", UIParent, "UIPanelButtonTemplate")
buycheap_button:SetText("Best Price")
buycheap_button:SetScript("OnClick", BuyCheap)
buycheap_button:Hide()
buycheap_amount = CreateFrame("EditBox", "MyBox", UIParent, "InputBoxTemplate")
buycheap_amount:SetNumeric()
buycheap_amount:SetScript("OnEnterPressed", BuyCheap)
buycheap_amount:SetAutoFocus(false)
buycheap_amount:Hide()
StaticPopupDialogs["BUYCHEAP_POPUP_SUCCESS"] = {
text = "Best price found for %s item%s:",
hasMoneyFrame = true,
OnShow = function(self, amount, formatter)
MoneyFrame_Update(self.moneyFrame, BuyCheap_itemstobuy_price);
end,
button1 = "Buy",
button2 = "Cancel",
OnAccept = function()
StaticPopup_Show("BUYCHEAP_POPUP_WAIT", tostring(buycheap_global_amount_bought) .. "/" .. tostring(buycheap_amount_global));
BuyCheap_BuyItems(-1, 1)
end,
OnCancel = function()
BuyCheap_running = false
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
StaticPopupDialogs["BUYCHEAP_POPUP_FAILURE"] = {
text = "Could not find a way to purchase exactly %s item%s!",
OnShow = function(self, amount, formatter)
BuyCheap_running = false
end,
button1 = "Ok",
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
StaticPopupDialogs["BUYCHEAP_POPUP_ONEPAGEITEMS"] = {
text = "Buy quantity %s for:",
button1 = "Ok",
button2 = "Skip",
timeout = 0,
hasMoneyFrame = true,
OnShow = function(self, amount)
StaticPopup_Hide("BUYCHEAP_POPUP_WAIT");
MoneyFrame_Update(self.moneyFrame, BuyCheap_buyoutPrice);
end,
OnAccept = function()
local tempwaittime = getn(BuyCheap_itemstobid)
for i = 1, getn(BuyCheap_itemstobid) do
PlaceAuctionBid("list", BuyCheap_itemstobid[i][2], BuyCheap_itemstobid[i][1])
BuyCheap_itemi = BuyCheap_itemi - 1
end
buycheap_global_amount_bought = buycheap_global_amount_bought + buycheap_global_count_all
if BuyCheap_itemi == -1 then
StaticPopup_Show("BUYCHEAP_POPUP_END", "succesfully");
else
StaticPopup_Show("BUYCHEAP_POPUP_WAIT", tostring(buycheap_global_amount_bought) .. "/" .. tostring(buycheap_amount_global));
BuyCheap_wait(1.1 * tempwaittime + 2.0, BuyCheap_BuyQuery, BuyCheap_cur_page)
end
end,
OnCancel = function()
StaticPopup_Show("BUYCHEAP_POPUP_WAIT", tostring(buycheap_global_amount_bought) .. "/" .. tostring(buycheap_amount_global));
BuyCheap_itemi = BuyCheap_itemi - getn(BuyCheap_itemstobid)
BuyCheap_wait(1.5, BuyCheap_BuyQuery, BuyCheap_cur_page + 1)
end,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
StaticPopupDialogs["BUYCHEAP_POPUP_WAIT"] = {
text = "Scanning AH for your items\nYou have currently purchased %s items.",
OnShow = function(self, amount)
end,
button1 = "Cancel",
OnAccept = function()
BuyCheap_control = true
BuyCheap_running = false
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
StaticPopupDialogs["BUYCHEAP_POPUP_END"] = {
text = "Process finished %s!",
OnShow = function(self, didfail)
local tempwaittime = getn(BuyCheap_itemstobid)
BuyCheap_wait(1.1 * tempwaittime + 2.0, function () BuyCheap_running = false end)
end,
button1 = "Ok",
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
StaticPopupDialogs["BUYCHEAP_POPUP_CANCEL"] = {
text = "BuyCheap is scanning the Auction House...",
button1 = "Cancel",
timeout = 0,
OnAccept = function()
BuyCheap_control = true
BuyCheap_running = false
end,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
end
function BuyCheap_EventHandler(self, event, arg1)
if event == "ADDON_LOADED" and arg1 == "BuyCheap" then
if BuyCheap_waitDuration == nil then
BuyCheap_waitDuration = 4
end
if BuyCheap_overSearch == nil then
BuyCheap_overSearch = 1.1
end
BuyCheap_BlizzardOptions()
end
if event == "AUCTION_HOUSE_SHOW" then
buycheap_button:SetParent("AuctionFrameBrowse")
buycheap_button:SetPoint("TOP", "AuctionFrameBrowse", buycheap_button:GetParent():GetWidth() / 3.3, -buycheap_button:GetParent():GetHeight() / 12)
buycheap_button:SetSize(buycheap_button:GetParent():GetWidth() / 10 , buycheap_button:GetParent():GetHeight() / 22)
buycheap_button:Show()
buycheap_amount:SetParent("AuctionFrameBrowse")
buycheap_amount:SetFrameLevel(buycheap_amount:GetParent():GetFrameLevel() + 1)
buycheap_amount:SetPoint("TOP", "AuctionFrameBrowse", buycheap_amount:GetParent():GetWidth() / 3.1, -buycheap_amount:GetParent():GetHeight() / 7.5)
buycheap_amount:SetSize(buycheap_amount:GetParent():GetWidth() / 13 , buycheap_amount:GetParent():GetHeight() / 25)
buycheap_amount:SetNumber(1)
buycheap_amount:Show()
else
buycheap_button:Hide()
buycheap_amount:Hide()
BuyCheap_running = false
StaticPopup_Hide("BUYCHEAP_POPUP_CANCEL")
StaticPopup_Hide("BUYCHEAP_POPUP_SUCCESS")
StaticPopup_Hide("BUYCHEAP_POPUP_FAILURE")
StaticPopup_Hide("BUYCHEAP_POPUP_ONEITEM")
StaticPopup_Hide("BUYCHEAP_POPUP_END")
end
end
function BuyCheap_roundFormat(a, b)
a = a * 10 ^ b
if a >= floor(a) + 0.5 then
a = ceil(a)
else
a = floor(a)
end
return tostring((a - a % 10 ^ b) / (10 ^ b)) .. "." .. tostring(floor(a % (10 ^ b)))
end
function BuyCheap_percentFormat(a)
return tostring(floor((a - 1) * 100)) .. "%"
end
function BuyCheap_BlizzardOptions()
-- Create main frame for information text
local BuyCheap_Options = CreateFrame("FRAME", "BuyCheap_Options")
BuyCheap_Options.refresh = function (self) BuyCheap_Slider:SetValue(floor((BuyCheap_waitDuration - 2) / 0.2 + 0.4)) end
BuyCheap_Options.name = GetAddOnMetadata("BuyCheap", "Title")
InterfaceOptions_AddCategory(BuyCheap_Options)
local BuyCheap_OptionsHeader = BuyCheap_Options:CreateFontString(nil, "ARTWORK")
BuyCheap_OptionsHeader:SetFontObject(GameFontNormalLarge)
BuyCheap_OptionsHeader:SetJustifyH("LEFT")
BuyCheap_OptionsHeader:SetJustifyV("TOP")
BuyCheap_OptionsHeader:ClearAllPoints()
BuyCheap_OptionsHeader:SetPoint("TOPLEFT", 16, -16)
BuyCheap_OptionsHeader:SetText("BuyCheap Options")
local BuyCheap_Slider = CreateFrame("Slider", "BuyCheap_Slider", BuyCheap_Options, "OptionsSliderTemplate")
BuyCheap_Slider:SetWidth(300)
BuyCheap_Slider:SetHeight(20)
BuyCheap_Slider:SetOrientation('HORIZONTAL')
BuyCheap_Slider.tooltipText = 'Duration Between AH page loading (the lower, the faster will the procedure be but it may fail)'
getglobal(BuyCheap_Slider:GetName() .. 'Low'):SetText('2');
getglobal(BuyCheap_Slider:GetName() .. 'High'):SetText('8');
getglobal(BuyCheap_Slider:GetName() .. 'Text'):SetText("Page Update Every " .. BuyCheap_roundFormat(BuyCheap_waitDuration, 1) .. " s");
BuyCheap_Slider:SetMinMaxValues(0, 30)
BuyCheap_Slider:SetValue(floor((BuyCheap_waitDuration - 2) / 0.2 + 0.4))
BuyCheap_Slider:SetValueStep(1)
BuyCheap_Slider:SetPoint("TOPLEFT", BuyCheap_OptionsHeader, "BOTTOMLEFT", 0, -50)
BuyCheap_Slider:SetScript("OnValueChanged",
function ()
getglobal(BuyCheap_Slider:GetName() .. 'Text'):SetText("Page Update Every " .. BuyCheap_roundFormat(2 + 0.2 * BuyCheap_Slider:GetValue(), 1) .. " s");
BuyCheap_waitDuration = 2 + 0.2 * BuyCheap_Slider:GetValue()
end
)
BlizzardOptionsPanel_Slider_Enable(BuyCheap_Slider)
local BuyCheap_Slider2 = CreateFrame("Slider", "BuyCheap_Slider2", BuyCheap_Options, "OptionsSliderTemplate")
BuyCheap_Slider2:SetWidth(300)
BuyCheap_Slider2:SetHeight(20)
BuyCheap_Slider2:SetOrientation('HORIZONTAL')
BuyCheap_Slider2.tooltipText = 'Percentage of extra item quantity to search'
getglobal(BuyCheap_Slider2:GetName() .. 'Low'):SetText('0%');
getglobal(BuyCheap_Slider2:GetName() .. 'High'):SetText('100%');
getglobal(BuyCheap_Slider2:GetName() .. 'Text'):SetText("Search for +" .. BuyCheap_percentFormat(BuyCheap_overSearch) .. " items");
BuyCheap_Slider2:SetMinMaxValues(0, 100)
BuyCheap_Slider2:SetValue(floor(((BuyCheap_overSearch - 1) * 100)))
BuyCheap_Slider2:SetValueStep(1)
BuyCheap_Slider2:SetPoint("TOPLEFT", BuyCheap_Slider, "BOTTOMLEFT", 0, -50)
BuyCheap_Slider2:SetScript("OnValueChanged",
function ()
BuyCheap_overSearch = 1 + BuyCheap_Slider2:GetValue() / 100
getglobal(BuyCheap_Slider2:GetName() .. 'Text'):SetText("Search for +" .. BuyCheap_percentFormat(BuyCheap_overSearch) .. " items");
end
)
BlizzardOptionsPanel_Slider_Enable(BuyCheap_Slider2)
end
-- logic
function BuyCheap()
if BuyCheap_running then return nil end
local amount = buycheap_amount:GetNumber()
buycheap_amount_global = amount
buycheap_global_amount_bought = 0
BuyCheap_control = false
BuyCheap_running = true
BuyCheap_itemname = BrowseName:GetText()
StaticPopup_Show("BUYCHEAP_POPUP_CANCEL")
BuyCheap_Query({}, {}, 0, amount)
end
function BuyCheap_Query(prices, weights, page, amount)
if BuyCheap_control then return nil end
QueryAuctionItems(BuyCheap_itemname, nil, nil, 0, 0, 0, page, 0, false)
BuyCheap_wait(BuyCheap_waitDuration, BuyCheap_FindAllItems, prices, weights, page, amount)
end
function BuyCheap_FindAllItems(prices, weights, page, amount)
local current, total = GetNumAuctionItems("list")
local cur_num = page * 50
for i = 0, current - 1 do
local itemname, _, count, _, _, _, _, _, buyoutPrice, _, _, _, _ = GetAuctionItemInfo("list", i + 1)
if strlower(itemname) == strlower(BuyCheap_itemname) then
prices[cur_num + i] = buyoutPrice
else
prices[cur_num + i] = 0
end
weights[cur_num + i] = count
end
if cur_num + current == total then
StaticPopup_Hide("BUYCHEAP_POPUP_CANCEL")
BuyCheap_itemstobuy, BuyCheap_itemstobuy_price, BuyCheap_itemstobuy_len, BuyCheap_itemstobuy_num = BuyCheap_Knapsack(weights, prices, amount, total)
BuyCheap_weights = weights
BuyCheap_prices = prices
local formatter = ""
if not (BuyCheap_itemstobuy_num == nil) and (BuyCheap_itemstobuy_num > 1) then
formatter = "s"
elseif amount > 1 then
formatter = "s"
end
if BuyCheap_itemstobuy == nil then
StaticPopup_Show("BUYCHEAP_POPUP_FAILURE", tostring(amount), formatter)
else
BuyCheap_totalitems = total
BuyCheap_itemi = BuyCheap_itemstobuy_len - 1
StaticPopup_Show("BUYCHEAP_POPUP_SUCCESS", tostring(BuyCheap_itemstobuy_num), formatter)
end
else
BuyCheap_Query(prices, weights, page + 1, amount)
end
end
function BuyCheap_BuyQuery(page)
QueryAuctionItems(BuyCheap_itemname, nil, nil, 0, 0, 0, page, 0, false)
BuyCheap_wait(BuyCheap_waitDuration, BuyCheap_BuyItems, page)
end
function BuyCheap_booltoint(b)
if b then return 1 else return 0 end
end
function BuyCheap_BuyItems(cur_page)
if BuyCheap_itemi == -1 then
StaticPopup_Show("BUYCHEAP_POPUP_END", "succesfully")
return nil
end
local pageitems, _ = GetNumAuctionItems("list")
if pageitems == 0 then
StaticPopup_Show("BUYCHEAP_POPUP_END", "unsuccesfully")
return nil
end
local itemstobid = {}
local itemstobid_i = 1
local count_all = 0
BuyCheap_buyoutPrice = 0
local item_i = BuyCheap_itemi
for i = 1, pageitems do
local itemname, _, count, _, _, _, _, _, buyoutPrice, _, _, _, _ = GetAuctionItemInfo("list", i)
if count == BuyCheap_weights[BuyCheap_itemstobuy[item_i]] and buyoutPrice == BuyCheap_prices[BuyCheap_itemstobuy[item_i]] and strlower(itemname) == strlower(BuyCheap_itemname) then
count_all = count_all + count
BuyCheap_buyoutPrice = BuyCheap_buyoutPrice + buyoutPrice
itemstobid[itemstobid_i] = {buyoutPrice, i}
itemstobid_i = itemstobid_i + 1
item_i = item_i - 1
end
end
if count_all > 0 then
BuyCheap_itemstobid = itemstobid
BuyCheap_cur_page = cur_page
buycheap_global_count_all = count_all
StaticPopup_Show("BUYCHEAP_POPUP_ONEPAGEITEMS", count_all)
else
BuyCheap_BuyQuery(cur_page + 1)
end
end
-- Knapsack function
function BuyCheap_Knapsack(weights, prices, W, n)
local dpt = {}
local prev = 0
local cur = 0
local cur1 = 0
for item = 0, n do
dpt[item] = {}
for weight = 0, ceil(W * BuyCheap_overSearch) do
if item == 0 or weight == 0 then
dpt[item][weight] = {0, false}
elseif prices[item - 1] == 0 then
dpt[item][weight] = {dpt[item-1][weight][1], false}
else
prev = dpt[item - 1][weight][1]
if weights[item - 1] > weight then
dpt[item][weight] = {prev, false}
else
cur = dpt[item - 1][weight - weights[item - 1]][1] + prices[item - 1]
cur1 = dpt[item - 1][weight - weights[item - 1]][1]
if prev == 0 and cur1 == 0 then
if weights[item - 1] == weight then
dpt[item][weight] = {cur, true}
else
dpt[item][weight] = {0, false}
end
elseif prev == 0 and cur1 ~= 0 then
dpt[item][weight] = {cur, true}
elseif prev ~= 0 and cur1 == 0 then
if weights[item - 1] == weight and cur < prev then
dpt[item][weight] = {cur, true}
else dpt[item][weight] = {prev, false}
end
else
if cur < prev then
dpt[item][weight] = {cur, true}
else
dpt[item][weight] = {prev, false}
end
end
end
end
end
end
local minimumvalue = -1
local minimumvalueindex = W
for i = W, ceil(W * BuyCheap_overSearch) do
if not (dpt[n][i][1] == 0) then
if (dpt[n][i][1] < minimumvalue) or minimumvalue == -1 then
minimumvalue = dpt[n][i][1]
minimumvalueindex = i
end
end
end
if minimumvalue == -1 then
return nil, nil
end
local indices = {}
local i = n
local j = minimumvalueindex
local c = 0
while i > 0 do
if dpt[i][j][2] then
indices[c] = i - 1
c = c + 1
j = j - weights[i - 1]
end
i = i - 1
end
if c == 0 then
return nil, nil
end
return indices, dpt[n][minimumvalueindex][1], c, minimumvalueindex
end
-- wait function below
local waitTable = {};
local waitFrame = nil;
function BuyCheap_wait(delay, func, ...)
if(type(delay)~="number" or type(func)~="function") then
return false;
end
if(waitFrame == nil) then
waitFrame = CreateFrame("Frame","WaitFrame", UIParent);
waitFrame:SetScript("onUpdate",function (self,elapse)
local count = #waitTable;
local i = 1;
while(i<=count) do
local waitRecord = tremove(waitTable,i);
local d = tremove(waitRecord,1);
local f = tremove(waitRecord,1);
local p = tremove(waitRecord,1);
if(d>elapse) then
tinsert(waitTable,i,{d-elapse,f,p});
i = i + 1;
else
count = count - 1;
f(unpack(p));
end
end
end);
end
tinsert(waitTable,{delay,func,{...}});
return true;
end