-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreateAuctions.lua
More file actions
279 lines (231 loc) · 8.05 KB
/
CreateAuctions.lua
File metadata and controls
279 lines (231 loc) · 8.05 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
-------------------------------------------------------------------------------
-- CreateAuctions.lua
--
-- Create a group of auctions based on input in the "Sell" tab.
-------------------------------------------------------------------------------
local L = LibStub("AceLocale-3.0"):GetLocale("AuctionLite", false)
-- Flag indicating whether we're currently posting auctions.
local Selling = false;
local MultisellCreated = 0;
local MultisellDone = false;
local MultisellError = false;
-- Current coroutine.
local Coro = nil;
-------------------------------------------------------------------------------
-- Helpers
-------------------------------------------------------------------------------
-- Count the number of items matching the link (ignoring uniqueId).
function AuctionLite:CountItems(targetLink)
local total = 0;
if targetLink ~= nil then
local i, j;
for i = 0, 4 do
local numItems = GetContainerNumSlots(i);
for j = 1, numItems do
local link = self:RemoveUniqueId(GetContainerItemLink(i, j));
if link == targetLink then
local _, count = GetContainerItemInfo(i, j);
total = total + count;
end
end
end
end
return total;
end
-- Find item in bags.
function AuctionLite:FindItem(targetLink)
local total = 0;
if targetLink ~= nil then
local i, j;
for i = 0, 4 do
local numItems = GetContainerNumSlots(i);
for j = 1, numItems do
local link = self:RemoveUniqueId(GetContainerItemLink(i, j));
if link == targetLink then
return i, j;
end
end
end
end
return nil;
end
-------------------------------------------------------------------------------
-- Auction creation
-------------------------------------------------------------------------------
-- Start the auctions and print a message to the console.
function AuctionLite:StartAuctions(bid, buyout, time, size, stacks, name, link)
-- Make sure the item is present.
if GetAuctionSellItemInfo() == nil then
local container, slot = self:FindItem(link);
if container ~= nil then
ClearCursor();
PickupContainerItem(container, slot);
ClickAuctionSellItemButton();
end
end
-- Check again, just to be sure.
if GetAuctionSellItemInfo() ~= nil then
-- Grab our initial state.
local count = self:CountItems(link);
MultisellCreated = 0;
MultisellDone = false;
MultisellError = false;
-- Start the new auction, ignoring console spam.
StartAuction(bid, buyout, time, size, stacks);
self:IgnoreMessage(ERR_AUCTION_STARTED, stacks);
-- If it's a multisell, wait for it to complete. Note that we might
-- sell fewer items than originally requested if the user cancels.
if stacks > 1 then
self:WaitForMultisell();
stacks = MultisellCreated;
end
-- Now wait for the inventory to be updated so that CountItems doesn't
-- get confused later.
self:WaitForQuantity(link, count - (size * stacks));
-- And tell the user what we did.
self:Print(L["Created %d |4auction:auctions; of %s x%d (%s total)."]:
format(stacks, name, size, self:PrintMoney(stacks * buyout)));
end
end
-- Create new auctions based on the fields in the "Sell" tab.
function AuctionLite:CreateAuctionsCore()
-- TODO: check stack size against max size
if not Selling then
Selling = true;
local name, _, count, _, _, _, link, sellContainer, sellSlot =
self:GetAuctionSellItemInfoAndLink();
local stacks = SellStacks:GetNumber();
local size = SellSize:GetNumber();
local bid = MoneyInputFrame_GetCopper(SellBidPrice);
local buyout = MoneyInputFrame_GetCopper(SellBuyoutPrice);
local time = self:GetDuration();
local numItems = self:CountItems(link);
local _, _, _, _, _, _, _, maxSize = GetItemInfo(link);
-- If we're pricing per item, then get the stack price.
if self.db.profile.method == 1 then
bid = bid * size;
buyout = buyout * size;
end
-- Now do some sanity checks.
if name == nil then
self:Print(L["Error locating item in bags. Please try again!"]);
elseif bid == 0 then
self:Print(L["Invalid starting bid."]);
elseif 0 < buyout and buyout < bid then
self:Print(L["Buyout cannot be less than starting bid."]);
elseif GetMoney() < self:CalculateDeposit() then
self:Print(L["Not enough cash for deposit."]);
elseif stacks > math.ceil(numItems / size) then
self:Print(L["Not enough items available."]);
elseif maxSize < size then
self:Print(L["Stack size too large."]);
elseif count ~= nil and stacks > 0 then
local created = 0;
-- Determine if we have an excess stack to sell.
local excessStacks = 0;
local excessSize = 0;
if size * stacks > numItems then
stacks = stacks - 1;
excessStacks = 1;
excessSize = numItems - (size * stacks);
assert(0 < excessSize);
assert(excessSize < size);
end
-- Disable the auction creation button.
SellCreateAuctionButton:Disable();
-- Sell the main batch of items.
self:StartAuctions(bid, buyout, time, size, stacks, name, link);
-- Sell the excess, if necessary.
if excessStacks > 0 and not MultisellError then
self:StartAuctions(bid * excessSize / size, buyout * excessSize / size,
time, excessSize, excessStacks, name, link);
end
-- We're done; clear the frame.
self:ClearSellFrame();
end
Selling = false;
else
self:Print(L["Auction creation is already in progress."]);
end
end
-- Start a coroutine to create auctions.
function AuctionLite:CreateAuctions()
self:StartCoroutine(function() AuctionLite:CreateAuctionsCore() end);
end
-------------------------------------------------------------------------------
-- Coroutine functions
-------------------------------------------------------------------------------
-- Yield until the number of items in the inventory drops below a threshold.
function AuctionLite:WaitForQuantity(link, qty)
self:WaitUntil(function()
local count = self:CountItems(link);
return (count <= qty);
end);
end
-- Yield until a multisell operation completes.
function AuctionLite:WaitForMultisell()
self:WaitUntil(function()
return MultisellDone;
end);
end
-- Yield until a condition is true.
function AuctionLite:WaitUntil(cond)
while not cond() do
coroutine.yield();
end
end
-- Start a coroutine to call the specified function.
function AuctionLite:StartCoroutine(fn)
if Coro == nil then
Coro = coroutine.create(fn);
AuctionLite:ResumeCoroutine();
end
end
-- Resume the stalled coroutine.
function AuctionLite:ResumeCoroutine()
if Coro ~= nil and coroutine.status(Coro) == "suspended" then
coroutine.resume(Coro)
if coroutine.status(Coro) == "dead" then
Coro = nil;
end
end
end
-------------------------------------------------------------------------------
-- Coroutine hooks
-------------------------------------------------------------------------------
function AuctionLite:BAG_UPDATE()
self:ResumeCoroutine();
end
function AuctionLite:AUCTION_MULTISELL_UPDATE(_, cur, total)
MultisellCreated = cur;
if cur == total then
MultisellDone = true;
self:ResumeCoroutine();
end
end
function AuctionLite:AUCTION_MULTISELL_FAILURE(_, cur, total)
MultisellDone = true;
MultisellError = true;
self:ResumeCoroutine();
end
-- Add the hooks needed for our coroutines.
function AuctionLite:HookCoroutines()
self:RegisterEvent("BAG_UPDATE");
self:RegisterEvent("AUCTION_MULTISELL_UPDATE");
self:RegisterEvent("AUCTION_MULTISELL_FAILURE");
end
-------------------------------------------------------------------------------
-- Miscellaneous
-------------------------------------------------------------------------------
-- Indicate whether we're creating auctions.
function AuctionLite:CreateInProgress()
return Selling;
end
-- Reset state. Useful for recovering from bugs.
function AuctionLite:ResetAuctionCreation()
Selling = false;
MultisellCreated = 0;
MultisellDone = false;
MultisellError = false;
Coro = nil;
end