forked from evl/auctionlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuyFrame.lua
More file actions
1987 lines (1694 loc) · 56.8 KB
/
BuyFrame.lua
File metadata and controls
1987 lines (1694 loc) · 56.8 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-------------------------------------------------------------------------------
-- BuyFrame.lua
--
-- Implements the "Buy" tab.
-------------------------------------------------------------------------------
local L = LibStub("AceLocale-3.0"):GetLocale("AuctionLite", false)
-- Constants for display elements.
local BUY_DISPLAY_SIZE = 17;
local ROW_HEIGHT = 21;
local EXPAND_ROWS = 4;
-- Various states for the buy frame.
local BUY_MODE_NONE = 1;
local BUY_MODE_INTRO = 2;
local BUY_MODE_SEARCH = 3;
local BUY_MODE_SCAN = 4;
local BUY_MODE_DEALS = 5;
local BUY_MODE_FAVORITES = 6;
local BUY_MODE_MY_AUCTIONS = 7;
-- Current state of the buy frame.
local BuyMode = BUY_MODE_NONE;
-- Current sorting state.
local SummarySort = {
sort = "ItemSummary",
flipped = false,
justFlipped = false,
sorted = false,
};
local DetailSort = {
sort = "BuyoutEach",
flipped = false,
justFlipped = false,
sorted = false,
};
-- Height of expandable frame.
local ExpandHeight = 0;
-- Data to be shown in detail view.
local DetailLink = nil;
local DetailData = {};
local SavedOffset = 0;
-- Save the last selected item.
local DetailLinkPrev = nil;
-- Selected item in detail view and index of last item clicked.
local SelectedItems = {};
local LastClick = nil;
-- Data to be shown in summary view.
local SummaryData = {};
local SummaryDataByLink = {};
local NoResults = false;
-- Info about current purchase for display in expandable frame.
local PurchaseOrder = nil;
-- Information about current search progress.
local StartTime = nil;
local LastTime = nil;
local LastRemaining = nil;
local Progress = nil;
local GetAll = nil;
local Scanning = nil;
-- Stored scan data from the latest full scan.
local ScanData = nil;
-- Links and data for multi-item scan.
local MultiScanCurrent = nil;
local MultiScanOriginal = {};
local MultiScanItems = {};
local MultiScanData = {};
-- Link for context menu.
local ContextLink = nil;
-- Static popup advertising AL's fast scan.
StaticPopupDialogs["AL_FAST_SCAN"] = {
text = L["FAST_SCAN_AD"],
button1 = L["Enable"],
button2 = L["Disable"],
OnAccept = function(self)
AuctionLite:Print(L["Fast auction scan enabled."]);
AuctionLite.db.profile.fastScanAd2 = true;
AuctionLite.db.profile.getAll = true;
AuctionLite:StartFullScan();
end,
OnCancel = function(self)
AuctionLite:Print(L["Fast auction scan disabled."]);
AuctionLite.db.profile.fastScanAd2 = true;
AuctionLite.db.profile.getAll = false;
AuctionLite:StartFullScan();
end,
showAlert = 1,
timeout = 0,
exclusive = 1,
hideOnEscape = 1
};
-- Set current item to be shown in detail view, and update dependent data.
function AuctionLite:SetDetailLink(link)
-- If there's no data to show, then just set the link to nil.
if link ~= nil and table.getn(SummaryDataByLink[link].data) == 0 then
link = nil;
end
-- If we're leaving the summary view, save our offset.
if DetailLink == nil then
SavedOffset = FauxScrollFrame_GetOffset(BuyScrollFrame);
end
-- Set the new detail link, if any.
DetailLink = link;
local offset;
if DetailLink ~= nil then
DetailData = SummaryDataByLink[DetailLink].data;
offset = 0;
else
DetailData = {};
offset = SavedOffset;
end
DetailSort.sorted = false;
-- Return to our saved offset. We have to set the inner scroll bar
-- manually, because otherwise the two values will become inconsistent.
-- We also set the max values to make sure that the value we're setting
-- is not ignored.
FauxScrollFrame_SetOffset(BuyScrollFrame, offset);
BuyScrollFrameScrollBar:SetMinMaxValues(0, offset * BuyButton1:GetHeight());
BuyScrollFrameScrollBar:SetValue(offset * BuyButton1:GetHeight());
SelectedItems = {};
LastClick = nil;
end
-- Set the data for the scrolling frame.
function AuctionLite:SetBuyData(results)
SummaryData = {};
local count = 0;
local last = nil;
local foundPrev = false;
-- Sort everything and assemble the summary data.
for link, result in pairs(results) do
table.insert(SummaryData, result);
result.link = link;
result.name = self:SplitLink(link);
local hist = self:GetHistoricalPrice(link);
if hist ~= nil then
result.histPrice = hist.price;
else
result.histPrice = 0;
end
count = count + 1;
last = link;
if DetailLinkPrev == link then
foundPrev = true;
end
end
SummaryDataByLink = results;
-- Sort our data by name/profit.
if BuyMode == BUY_MODE_DEALS or
BuyMode == BUY_MODE_SCAN then
SummarySort.sort = "Historical";
SummarySort.flipped = true;
else
SummarySort.sort = "ItemSummary";
SummarySort.flipped = false;
end
SummarySort.sorted = false;
SummarySort.justFlipped = false;
-- Set up warning markers for undercuts.
self:SetWarnings();
-- If we found our last-selected item, then select it again.
-- If we found only one item, select it. Otherwise, select nothing.
local newLink = nil;
if foundPrev then
newLink = DetailLinkPrev;
elseif count == 1 then
newLink = last;
end
DetailLinkPrev = nil;
-- Reset current query info.
self:ResetSearch();
-- Save our data and set our detail link, if we only got one kind of item.
SummaryDataByLink = results;
NoResults = (count == 0);
self:SetDetailLink(newLink);
-- Clean up the display.
BuyMessageText:Hide();
BuyStatus:Hide();
-- Start a mass buyout, if necessary.
self:StartMassBuyout();
-- Repaint.
self:AuctionFrameBuy_Update();
end
-- Handle results for a full scan. Make a list of the deals.
function AuctionLite:SetScanData(results)
ScanData = {};
-- Search through all scanned items.
for link, result in pairs(results) do
local hist = self:GetHistoricalPrice(link);
-- Find the lowest buyout.
local min = 0;
for _, listing in ipairs(result.data) do
if min == 0 or (0 < listing.buyout and listing.buyout < min) then
min = listing.buyout;
end
end
-- If it meets a bunch of conditions below, it's considered a deal.
if min > 0 and hist ~= nil and
min < hist.price - (10000 * self.db.profile.minProfit) and
min < hist.price * (1 - self.db.profile.minDiscount) and
hist.listings / hist.scans > 1.2 then
result.profit = hist.price - min;
ScanData[link] = result;
end
end
-- Display our list of deals.
DetailLinkPrev = nil;
self:SetBuyData(ScanData, true);
end
-- Helper that finds user's listings. Return value indicates whether we've
-- been undercut. (If that's all you care about, omit the last three args.)
function AuctionLite:FindMyListings(listings, list, name, onlyUndercut)
-- Sort by per-item buyout price.
table.sort(listings,
function(a, b) return a.buyout / a.count < b.buyout / b.count end);
-- We may have messed up the current sort, so make sure it gets re-sorted.
DetailSort.sorted = false;
-- Iterate over listings.
local undercut = false;
local foundOther = false;
local listing;
for _, listing in ipairs(listings) do
if listing.buyout > 0 then
if listing.owner == UnitName("player") then
if foundOther then
undercut = true;
if list == nil then
break;
end
end
if list ~= nil then
if foundOther or not onlyUndercut then
listing.name = name;
table.insert(list, listing);
end
end
else
foundOther = true;
end
end
end
return undercut;
end
-- Set undercut warnings for all auctions.
function AuctionLite:SetWarnings()
if BuyMode == BUY_MODE_MY_AUCTIONS then
for link, result in pairs(SummaryDataByLink) do
-- Sort by per-item buyout price.
table.sort(result.data,
function(a, b) return a.buyout / a.count < b.buyout / b.count end);
-- Invalidate the current sort.
DetailSort.sorted = false;
-- Find the first listing with non-zero buyout. If it's not ours,
-- we've been undercut.
result.warning = self:FindMyListings(result.data);
end
end
end
-- Determine whether the selected items are biddable/buyable.
function AuctionLite:GetSelectionStatus()
local biddable = true;
local buyable = true;
local cancellable = true;
local found = false;
-- Helper to determine whether we have any listings.
local findMine = function(data)
for _, listing in ipairs(data) do
if listing.owner == UnitName("player") then
return true;
end
end
return false;
end
if DetailLink ~= nil then
-- Check all selected items.
for listing, _ in pairs(SelectedItems) do
-- We can't bid/buy our own auctions.
if listing.owner == UnitName("player") then
biddable = false;
buyable = false;
else
cancellable = false;
end
-- To buy, we must have a buyout price listed.
if listing.buyout == 0 then
buyable = false;
end
-- We must find at least one selected item.
found = true;
end
-- Nothing's selected, so check all items.
if not found then
cancellable = findMine(DetailData);
end
elseif SummaryDataByLink ~= nil then
-- We're at the summary screen, so check all items.
cancellable = false;
for link, result in pairs(SummaryDataByLink) do
if findMine(result.data) then
cancellable = true;
break;
end
end
else
cancellable = false;
end
return (found and biddable), (found and buyable), cancellable;
end
-- Cancel the selected items.
function AuctionLite:CancelItems(onlyUndercut, link)
-- List of items to be cancelled.
local list = {};
-- If we didn't specify data, try using DetailData.
if link == nil then
link = DetailLink;
end
-- Now find the items to cancel.
if link ~= nil then
local data = SummaryDataByLink[link].data;
local name = self:SplitLink(link);
-- Add information about each selected item.
local i;
for listing, _ in pairs(SelectedItems) do
assert(listing.owner == UnitName("player"));
listing.name = name;
table.insert(list, listing);
end
-- If we don't have any selected items, cancel all.
if table.getn(list) == 0 then
self:FindMyListings(data, list, name, onlyUndercut);
end
else
-- We're at the summary screen, so do this for all items.
for link, result in pairs(SummaryDataByLink) do
local name = self:SplitLink(link);
self:FindMyListings(result.data, list, name, onlyUndercut);
end
end
-- If we found some items to cancel, do it.
if table.getn(list) > 0 then
self:CancelAuctions(list);
end
end
-- Update the display now that we've finished cancelling.
function AuctionLite:CancelComplete()
-- Go through all listings and remove the ones that were cancelled.
for link, summary in pairs(SummaryDataByLink) do
local data = summary.data;
local i = table.getn(data);
while i > 0 do
local listing = data[i];
if listing.cancelled then
table.remove(data, i);
summary.itemsAll = summary.itemsAll - listing.count;
summary.itemsMine = summary.itemsMine - listing.count;
summary.listingsAll = summary.listingsAll - 1;
summary.listingsMine = summary.listingsMine - 1;
end
i = i - 1;
end
end
-- Reevaluate "warning" status.
self:SetWarnings();
-- Cleanup.
if table.getn(DetailData) == 0 then
self:SetDetailLink(nil);
end
SelectedItems = {};
-- Update the display.
self:AuctionFrameBuy_Update();
end
-- Create a purchase order based on the current selection. The first
-- argument indicates whether we're bidding or buying, and the second
-- argument (optional) indicates the actual number of items the user wants.
function AuctionLite:CreateOrder(isBuyout, requested)
if DetailLink ~= nil then
-- Create purchase order object to be filled out.
local order = { list = {}, price = 0, count = 0,
batch = 1, isBuyout = isBuyout };
-- Add information about each selected item.
local i;
for listing, _ in pairs(SelectedItems) do
assert(listing.owner ~= UnitName("player"));
local price;
if isBuyout then
price = listing.buyout;
else
price = listing.bid;
end
table.insert(order.list, listing);
order.count = order.count + listing.count;
order.price = order.price + price;
end
-- If we found any selected items and we have enough money, proceed.
if order.price > GetMoney() then
self:Print(L["|cffff0000[Error]|r Insufficient funds."]);
elseif order.count > 0 then
-- If the second argument wasn't specified, the user wants exactly
-- the number of items selected.
if requested == nil then
requested = order.count;
end
-- If we overshot, figure out how much we can resell the excess for.
if order.count > requested then
order.resell = order.count - requested;
local price = SummaryDataByLink[DetailLink].price;
order.resellPrice = math.floor(order.resell * price);
order.netPrice = order.price - order.resellPrice;
end
-- Get a historical comparison.
local hist = self:GetHistoricalPrice(DetailLink);
if hist ~= nil then
order.histPrice = math.floor(requested * hist.price);
else
order.histPrice = 0;
end
local name = self:SplitLink(DetailLink);
-- Submit the query. If it goes through, save it here too.
local query = {
name = name,
wait = true,
list = order.list,
isBuyout = isBuyout,
finish = function() AuctionLite:PurchaseComplete() end,
};
if self:StartQuery(query) then
PurchaseOrder = order;
end
end
end
end
-- Called after a search query ends in order to start a mass buyout.
function AuctionLite:StartMassBuyout()
-- See if the user requested a specific quantity.
local requested = BuyQuantity:GetNumber();
if DetailLink ~= nil and table.getn(DetailData) > 0 and requested > 0 then
-- Clear our selected items. (Should already be done, but what the hey.)
SelectedItems = {};
-- Make a list of all items from other sellers with nonzero buyout.
-- Also figure out how many items we have available in all.
local available = {};
local cheapest = nil;
local cheapestOwned = nil;
local total = 0;
local listing;
for _, listing in ipairs(DetailData) do
if listing.buyout > 0 then
local price = listing.buyout / listing.count;
if listing.owner == UnitName("player") then
-- Remember the cheapest auction that we own.
if cheapestOwned == nil or price < cheapestOwned then
cheapestOwned = price;
end
else
-- Remember the cheapest auction that we don't own.
if cheapest == nil or price < cheapest then
cheapest = price;
end
-- Keep track of all other auctions with buyouts.
table.insert(available, listing);
total = total + listing.count;
end
end
end
-- Are there enough items for sale to satisfy the request?
if total <= requested then
-- Nope. Just take everything we can.
local listing;
for _, listing in ipairs(available) do
SelectedItems[listing] = true;
end
else
-- Yep. So now we have to figure out what to buy. We use an
-- adaptation of the standard dynamic programming algorithm for
-- 0-1 knapsack.
-- The state for this algorithm is as follows:
-- results: For any n, what's the best price we can get for exactly
-- n items, and what listings do we have to buy to do so?
-- indices: A list of valid indices for results, in descending order.
-- We initialize these with 0, our base case.
local results = { [0] = { price = 0 } };
local indices = { 0 };
-- Determine the maximum number of items we want to consider buying.
-- This is the number requested plus the maximum stack size available
-- in the AH. We don't need to consider anything large than this,
-- because if we could fill the user's order for a larger count, then
-- we must be able to remove one listing to fill the user's order for
-- less money overall.
local maxCount = 0;
local listing;
for _, listing in ipairs(available) do
if listing.count > maxCount then
maxCount = listing.count;
end
end
maxCount = maxCount + requested;
-- Sort by count, which improves the speed of the algorithm below.
table.sort(available, function(a, b) return a.count > b.count end);
-- Save our start time.
local start = time();
-- Consider each listing in turn. At each step, the results table
-- will have the best results for the set of listings we've considered
-- so far.
local timeout = false;
local listing;
for _, listing in ipairs(available) do
-- If we've taken more than 1-2 seconds, bail.
if start + 1 < time() then
timeout = true;
break;
end
-- Iterate over all counts where we have results so far, in
-- descending order so that we can update in place.
local count;
for _, count in ipairs(indices) do
local info = results[count];
local newCount = count + listing.count;
assert(info ~= nil);
-- Add this listing to the best result at this stack size.
-- Do we care about the resulting set of items?
if newCount < maxCount then
-- If this is the first option at this new stack size, or if it's
-- cheaper than our previous best effort, update the data.
local newInfo = results[newCount];
local newPrice = info.price + listing.buyout;
if newInfo == nil or newPrice < newInfo.price then
-- The new data consists of the current result's listings plus
-- the listing we just added.
results[newCount] = {
price = newPrice,
listing = listing,
info = info,
};
end
-- If we added a new index to the results table, add it to our
-- list of indices too, and make sure it's still sorted.
if newInfo == nil then
table.insert(indices, newCount);
table.sort(indices, function(a, b) return a > b end);
end
end
end
end
if timeout then
-- We hit a timeout, so just do things the fast way.
-- First sort by per-item buyout.
local sort = function(a, b)
return a.buyout / a.count < b.buyout / b.count;
end
table.sort(available, sort);
-- Get the cheapest items until the order is filled.
local selected = {};
local count = 0;
local i = 1;
while i < table.getn(available) and count < requested do
local listing = available[i];
table.insert(selected, listing);
count = count + listing.count;
i = i + 1;
end
-- Go over the selected items in reverse order and remove any
-- that aren't actually necessary.
local i = table.getn(selected);
while i > 0 do
local listing = selected[i];
if count - listing.count >= requested then
table.remove(selected, i);
count = count - listing.count;
end
i = i - 1;
end
-- Update the selected items list as appropriate.
local listing;
for _, listing in ipairs(selected) do
SelectedItems[listing] = true;
end
else
-- We've computed our best options for all stack sizes.
-- Figure out the resale value for this item. We use the lesser
-- of the cheapest price and the historical price.
local resale = cheapest - 1;
local hist = self:GetHistoricalPrice(DetailLink);
if hist ~= nil and hist.price > 0 and hist.price < resale then
resale = hist.price;
end
-- Now find the cheapest way to buy the requested number of items.
local bestCount = nil;
local bestInfo = nil;
local count;
local info;
for count, info in pairs(results) do
if count >= requested then
-- If the user wants us to, adjust for resale price.
if self.db.profile.considerResale then
info.price = info.price - (resale * (count - requested));
end
-- Is this the best option we've seen?
if bestInfo == nil or info.price < bestInfo.price or
(info.price == bestInfo.price and count > bestCount) then
bestCount = count;
bestInfo = info;
end
end
end
-- Update SelectedItems with the final results. We should always
-- find a result here, but we'll be defensive anyway.
while bestInfo ~= nil and bestInfo.listing ~= nil do
SelectedItems[bestInfo.listing] = true;
bestInfo = bestInfo.info;
end
end
-- Sort by selectedness in order to pull selected items to the
-- top, and mark the list as unsorted so that they will be sorted
-- properly (and stably) on the next update.
local sort = function(a, b)
return SelectedItems[b] and not SelectedItems[a];
end
table.sort(DetailData, sort);
DetailSort.sorted = false;
end
-- Do we have any listings for this item?
if cheapestOwned ~= nil then
-- Find the worst price we're paying.
local worstBought = nil;
local listing;
for listing, _ in pairs(SelectedItems) do
local price = listing.buyout / listing.count;
if worstBought == nil or worstBought < price then
worstBought = price;
end
end
-- If it's above the price for our cheapest listing, warn the user.
if cheapestOwned < worstBought then
self:Print(L["|cffff0000[Warning]|r Skipping your own auctions. " ..
"You might want to cancel them instead."]);
end
end
-- Now create our buyout order.
self:CreateOrder(true, requested);
end
end
-- The query system needs us to approve purchases.
function AuctionLite:RequestApproval()
-- Just update the display!
-- TODO: Process shopping cart here.
self:AuctionFrameBuy_Update();
end
-- Notification that a purchase has completed.
function AuctionLite:PurchaseComplete()
-- Update our display according to the purchase.
if DetailLink ~= nil then
local summary = SummaryDataByLink[DetailLink];
local i = table.getn(DetailData);
while i > 0 do
local listing = DetailData[i];
listing.found = nil;
if listing.purchased then
if PurchaseOrder.isBuyout then
-- If we bought an item, remove it.
table.remove(DetailData, i);
summary.itemsAll = summary.itemsAll - listing.count;
summary.listingsAll = summary.listingsAll - 1;
-- The selected items map is going to get all screwed up, so
-- just nuke it. (TODO: Do a better job here!)
SelectedItems = {};
else
-- If we bid on an item, update the minimum bid.
local increment = math.floor(listing.bid / 100) * 5;
listing.purchased = false;
listing.bidder = 1;
listing.bid = listing.bid + increment;
if listing.bid > listing.buyout and listing.buyout > 0 then
listing.bid = listing.buyout;
end
end
end
i = i - 1;
end
if table.getn(DetailData) == 0 then
self:SetDetailLink(nil);
end
end
-- Clear our purchase order.
PurchaseOrder = nil;
-- Update the display.
self:AuctionFrameBuy_Update();
end
-- Update search progress display.
function AuctionLite:UpdateProgressSearch(pct, getAll, scan)
-- If we got a progress update, record it.
if pct ~= nil then
Progress = pct;
end
-- Record a start time if we don't have one already.
if StartTime == nil then
StartTime = math.floor(time());
end
-- Figure out whether we're actually doing getAll and/or scanning.
if GetAll == nil and getAll ~= nil then
GetAll = getAll;
end
if Scanning == nil and scan ~= nil then
Scanning = scan;
end
-- Update the display every second.
local currentTime = math.floor(time());
if LastTime == nil or currentTime > LastTime then
LastTime = currentTime;
-- If we have some data, compute the time remaining.
local elapsed = currentTime - StartTime;
if elapsed > 0 and Progress > 0 then
-- In order to reduce jitter in the estimate, do the following:
-- 1. Update once every two seconds at first.
-- 2. Average each estimate with the previous one.
-- 3. Ignore estimates that exceed the previous by less than 10%.
if Progress > 15 or (elapsed % 2) == 0 then
local remaining = math.floor((100 * elapsed / Progress) - elapsed);
if LastRemaining ~= nil then
remaining = math.floor((remaining + LastRemaining) / 2);
if remaining > LastRemaining and remaining < LastRemaining * 1.1 then
remaining = LastRemaining;
end
end
LastRemaining = remaining;
BuyRemainingData:SetText(self:PrintTime(remaining));
end
else
BuyRemainingData:SetText("---");
end
-- Update the percentage.
if GetAll then
BuyStatusText:SetText(L["Scanning..."]);
BuyStatusData:SetText("");
else
if Scanning then
BuyStatusText:SetText(L["Scanning:"]);
else
BuyStatusText:SetText(L["Searching:"]);
end
BuyStatusData:SetText(tostring(Progress) .. "%");
end
-- Update the elapsed time and show the whole pane.
BuyElapsedData:SetText(self:PrintTime(elapsed));
BuyStatus:Show();
end
end
-- Show our progress for the favorites scan. We assume each scan takes
-- roughly the same amount of time, and then split each segment accordingly.
function AuctionLite:UpdateProgressMulti(pct)
local numDone = 0;
for _, _ in pairs(MultiScanData) do
numDone = numDone + 1;
end
local numItems = 0;
for _, _ in pairs(MultiScanItems) do
numItems = numItems + 1;
end
local overall = math.floor(((100 * numDone) + pct) / numItems);
self:UpdateProgressSearch(overall);
end
-- Take the next step in a multi-item scan. If we need to scan for another
-- item, do it; if there are no items left, display our results.
function AuctionLite:MultiScan(items)
-- Are we starting a new multi-item scan?
local first = false;
if items ~= nil then
MultiScanOriginal = items;
MultiScanItems = {};
MultiScanData = {};
for item, _ in pairs(items) do
MultiScanItems[item] = true;
end
first = true;
end
-- Find an unscanned favorite.
MultiScanCurrent = nil;
local item;
for item, _ in pairs(MultiScanItems) do
if MultiScanItems[item] then
MultiScanCurrent = item;
break;
end
end
if MultiScanCurrent ~= nil then
-- Start the scan.
local query = {
wait = true,
update = function(pct) AuctionLite:UpdateProgressMulti(pct) end,
finish = function(data) AuctionLite:SetMultiScanData(data) end,
};
if self:IsLink(MultiScanCurrent) then
query.link = MultiScanCurrent;
else
query.name = MultiScanCurrent;
end
if self:StartQuery(query) and first then
DetailLinkPrev = nil;
self:ClearBuyFrame(true);
end
else
-- Show our results.
self:SetBuyData(MultiScanData);
MultiScanData = {};
end
end
-- Get the results for a multi-item scan.
function AuctionLite:SetMultiScanData(data)
-- Gather all relevant results returned by the search.
local found = false;
for link, results in pairs(data) do
local item = self:IsFavorite(link, MultiScanItems);
if item then
-- We were looking for this one. Save the data and mark it as found.
MultiScanItems[link] = false;
MultiScanItems[item] = false;
MultiScanData[link] = results;
-- We now have a link to use, so swap it in if we just had a name before.
if MultiScanOriginal[item] then
MultiScanOriginal[item] = nil;
MultiScanOriginal[link] = true;
end
end
-- Did we find the actual search term?
if MultiScanCurrent == link or MultiScanCurrent == item then
found = true;
end
end
-- We didn't find the item we were looking for, so add a fake entry to
-- the list so that it shows up with zero results.
if not found then
local link;
if self:IsLink(MultiScanCurrent) then
link = MultiScanCurrent;
else
link = "|cffffffff|Hitem:0:0:0:0:0:0:0:0|h[" ..
MultiScanCurrent ..
"]|h|r";
end
MultiScanItems[MultiScanCurrent] = false;
MultiScanData[link] = {
link = link,
data = {},
price = 0,
itemsAll = 0,
itemsMine = 0,
listingsAll = 0,
listingsMine = 0,
};
end
-- Continue the scan.
self:MultiScan();
end
-- Sort the detail view.
function AuctionLite:ApplyDetailSort()
local info = DetailSort;
local data = DetailData;
local cmp;
if info.sort == "Item" then
cmp = function(a, b) return a.count < b.count end;
elseif info.sort == "BidEach" then
cmp = function(a, b) return a.bid / a.count < b.bid / b.count end;
elseif info.sort == "BidAll" then
cmp = function(a, b) return a.bid < b.bid end;
elseif info.sort == "BuyoutEach" then
cmp = function(a, b) return a.buyout / a.count < b.buyout / b.count end;
elseif info.sort == "BuyoutAll" then
cmp = function(a, b) return a.buyout < b.buyout end;
else
assert(false);
end