From 55c53c6f935ff7ce1540664e5b5abd169b38f58d Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Sat, 10 Oct 2015 13:13:47 +0200 Subject: [PATCH] some refactorings --- buy.lua | 20 ++++++++-------- control.lua | 4 ++++ core.lua | 12 +++++----- scan.lua | 47 ++++++++++++++++--------------------- sell.lua | 45 ++++++++++++++++++----------------- stack.lua | 67 ++++++++++++++++++++++++++++------------------------- 6 files changed, 99 insertions(+), 96 deletions(-) diff --git a/buy.lua b/buy.lua index e139fc0..e94aa2b 100644 --- a/buy.lua +++ b/buy.lua @@ -84,12 +84,22 @@ function Aux.buy.SearchButton_onclick() end, on_complete = function() entries = entries or {} + if getn(entries) == 0 then + set_message("No auctions were found") + else + AuxBuyMessage:Hide() + end AuxBuyStopButton:Hide() AuxBuySearchButton:Show() refresh = true end, on_abort = function() entries = entries or {} + if getn(entries) == 0 then + set_message("No auctions were found") + else + AuxBuyMessage:Hide() + end AuxBuyStopButton:Hide() AuxBuySearchButton:Show() refresh = true @@ -303,16 +313,6 @@ end function Aux_Buy_ScrollbarUpdate() Aux.list.populate(AuxBuyList, entries or {}) - - -- if entries then - -- table.sort(entries, function(a,b) return a.buyout_price_per_unit < b.buyout_price_per_unit end) - -- end - - if entries and getn(entries) == 0 then - set_message("No auctions were found") - else - AuxBuyMessage:Hide() - end end ----------------------------------------- diff --git a/control.lua b/control.lua index b6a3bc5..d7f7ebb 100644 --- a/control.lua +++ b/control.lua @@ -135,6 +135,10 @@ function Aux.control.controller() } end + function self.reset() + state = nil + end + function self.cleanup() listener.stop() end diff --git a/core.lua b/core.lua index fd99222..3cf5b3e 100644 --- a/core.lua +++ b/core.lua @@ -206,7 +206,7 @@ function Aux_OnAuctionHouseClosed() AuxOptionsFrame:Hide() AuxAboutFrame:Hide() AuxSellPanel:Hide() - AuxBuyPanel:Hide() + AuxBuyPanel:Hide() end @@ -224,7 +224,7 @@ function Aux_AuctionFrameTab_OnClick(index) Aux.scan.abort() AuxSellPanel:Hide() - AuxBuyPanel:Hide() + AuxBuyPanel:Hide() if index == 2 then Aux_ShowElems(Aux.tabs.buy.hiddenElements) @@ -245,8 +245,8 @@ function Aux_AuctionFrameTab_OnClick(index) AuxSellPanel:Show() AuctionFrame:EnableMouse(false) - elseif index == Aux.tabs.buy.index then - AuctionFrameTab_OnClick(2) + elseif index == Aux.tabs.buy.index then + AuctionFrameTab_OnClick(2) PanelTemplates_SetTab(AuctionFrame, Aux.tabs.buy.index) @@ -256,8 +256,8 @@ function Aux_AuctionFrameTab_OnClick(index) AuctionFrame:EnableMouse(false) Aux_Buy_ScrollbarUpdate() - else - Aux.orig.AuctionFrameTab_OnClick(index) + else + Aux.orig.AuctionFrameTab_OnClick(index) lastItemPosted = nil end end diff --git a/scan.lua b/scan.lua index 609c55f..e608e32 100644 --- a/scan.lua +++ b/scan.lua @@ -12,38 +12,37 @@ end)() local state, abort, new_job --- forward declaration of local functions -local wait_for_callback, wait_for_results, wait_for_update -local scan, scan_auctions, scan_auctions_helper, submit_query - - +local scan, scan_auctions, scan_auctions_helper, submit_query, wait_for_callback, wait_for_results, abort function Aux.scan.start(job) - Aux.scan.abort(function() --; - wait_for_update(function() --; + Aux.control.on_next_update(function() + abort() state = { job = job, page = job.page } return scan() - end)end) + end) end function Aux.scan.abort(k) - wait_for_update(function() --; + Aux.control.on_next_update(function() + abort() + + if k then + return k() + end + end) +end + +function abort() + controller().reset() if state and state.job and state.job.on_abort then state.job.on_abort() end state = nil - - if k then - return k() - end - end) end - - function wait_for_results(k) local ok Aux.control.on_next_event('AUCTION_ITEM_LIST_UPDATE', function() @@ -53,10 +52,6 @@ function wait_for_results(k) return controller().wait(function() return ok end, k) end -function wait_for_update(k) - return controller().wait(function() return true end, k) -end - function wait_for_callback(f, args, k) local ok @@ -70,15 +65,13 @@ function wait_for_callback(f, args, k) return controller().wait(function() return ok end, k) end - - function scan() - submit_query(function() --; + submit_query(function() local count, total_count = GetNumAuctionItems("list") - scan_auctions(count, function() --; + scan_auctions(count, function() state.total_pages = math.ceil(total_count / AUCTIONS_PER_PAGE) @@ -101,7 +94,7 @@ function scan_auctions(count, k) end function scan_auctions_helper(i, n, k) - wait_for_callback(state.job.on_read_auction, {i}, function() --; + wait_for_callback(state.job.on_read_auction, {i}, function() if i >= n then return k() @@ -114,8 +107,8 @@ end function submit_query(k) if state.page then - wait_for_callback(state.job.on_start_page, {state.page, state.total_pages}, function() --; - controller().wait(CanSendAuctionQuery, function() --; + wait_for_callback(state.job.on_start_page, {state.page, state.total_pages}, function() + controller().wait(CanSendAuctionQuery, function() wait_for_results(k) QueryAuctionItems( state.job.query.name, diff --git a/sell.lua b/sell.lua index 71d769f..3fc9207 100644 --- a/sell.lua +++ b/sell.lua @@ -295,25 +295,27 @@ function Aux.sell.set_auction(bag, slot) if auction_sell_item then - Aux.scan.abort() + Aux.scan.abort(function() - current_auction = { - name = container_item.name, - texture = container_item.texture, - stackSize = container_item.charges or container_item.count, - stackCount = 1, - class = container_item.type, - subclass = container_item.subtype, - base_deposit = auction_sell_item.base_deposit, - has_charges = container_item.charges ~= nil, - } - - if not auxSellEntries[current_auction.name] then - refresh_entries() - end + current_auction = { + name = container_item.name, + texture = container_item.texture, + stackSize = container_item.charges or container_item.count, + stackCount = 1, + class = container_item.type, + subclass = container_item.subtype, + base_deposit = auction_sell_item.base_deposit, + has_charges = container_item.charges ~= nil, + } - select_entry() - update_recommendation() + if not auxSellEntries[current_auction.name] then + refresh_entries() + end + + select_entry() + update_recommendation() + + end) end end end @@ -462,10 +464,11 @@ end ----------------------------------------- function AuxSellRefreshButton_OnClick() - Aux.scan.abort() - refresh_entries() - select_entry() - update_recommendation() + Aux.scan.abort(function() + refresh_entries() + select_entry() + update_recommendation() + end) end ----------------------------------------- diff --git a/stack.lua b/stack.lua index 4086538..0f54c46 100644 --- a/stack.lua +++ b/stack.lua @@ -10,11 +10,7 @@ end)() local state -local inventory, item_slots, find_empty_slot, locked, same_slot, move_item, item_name, stack_size, wait_for_update - -function wait_for_update(k) - return controller().wait(function() return true end, k) -end +local inventory, item_slots, find_empty_slot, locked, same_slot, move_item, item_name, stack_size, stop function inventory() local inventory = {} @@ -143,7 +139,7 @@ function process() end end - return Aux.stack.stop() + return stop() end function max_stack(slot) @@ -151,8 +147,18 @@ function max_stack(slot) return item_stack_count end -function Aux.stack.stop() - wait_for_update(function() +function Aux.stack.stop(k) + Aux.control.on_next_update(function() + stop() + + if k then + return k() + end + end) +end + +function stop() + controller().reset() if state then local slot if state.target_slot and (stack_size(state.target_slot) == state.target_size or item_charges(state.target_slot) == state.target_size) then @@ -166,31 +172,28 @@ function Aux.stack.stop() callback(slot) end end - end) end function Aux.stack.start(name, size, callback) - wait_for_update(function() - - Aux.stack.stop() - - local slots = item_slots(name) - local target_slot = slots() - - state = { - target_size = size, - target_slot = target_slot, - other_slots = slots, - callback = callback, - } - - if not target_slot then - Aux.stack.stop() - elseif item_charges(target_slot) then - state.target_slot = find_charges_item_slot(name, size) - Aux.stack.stop() - end - - process() + Aux.control.on_next_update(function() + stop() + local slots = item_slots(name) + local target_slot = slots() + + state = { + target_size = size, + target_slot = target_slot, + other_slots = slots, + callback = callback, + } + + if not target_slot then + stop() + elseif item_charges(target_slot) then + state.target_slot = find_charges_item_slot(name, size) + stop() + end + + process() end) -end +end \ No newline at end of file