diff --git a/components/cache.lua b/components/cache.lua index 1e7f75a..8c7dc29 100644 --- a/components/cache.lua +++ b/components/cache.lua @@ -185,9 +185,7 @@ function private.scan_wdb() if item_id <= MAX_ITEM_ID then local t0 = GetTime() - Aux.control.as_soon_as(function() return GetTime() - t0 > 0.1 end, function() - return helper(item_id) - end) + Aux.control.as_soon_as(function() return GetTime() - t0 > 0.1 end, Aux.f(helper, item_id)) else sort(aux_auctionable_items, function(a, b) return strlen(a) < strlen(b) or (strlen(a) == strlen(b) and a < b) end) end @@ -209,9 +207,7 @@ function public.populate_wdb() AuxTooltip:SetHyperlink('item:'..item_id) end - Aux.control.on_next_update(function() - return helper(item_id + 1) - end) + Aux.control.on_next_update(Aux.f(helper, item_id + 1)) end helper(MIN_ITEM_ID) diff --git a/components/post.lua b/components/post.lua index 5ff047d..eeabfcf 100644 --- a/components/post.lua +++ b/components/post.lua @@ -5,24 +5,17 @@ private.state = nil function private.process() if m.state.posted < m.state.count then - local stacking_complete, target_slot + local stacking_complete - Aux.stack.start( - m.state.item_key, - m.state.stack_size, - function(slot) - stacking_complete = true - target_slot = slot - end - ) - - return Aux.control.wait_until(function() return stacking_complete end, function() - if target_slot then - return m.post_auction(target_slot, m.process) + local c = Aux.control.wait_for(function(slot) + if slot then + return m.post_auction(slot, m.process) else return m.stop() end end) + + return Aux.stack.start(m.state.item_key, m.state.stack_size, c) end return m.stop() @@ -41,20 +34,21 @@ function private.post_auction(slot, k) StartAuction(max(1, Aux.round(m.state.unit_start_price * item_info.aux_quantity)), Aux.round(m.state.unit_buyout_price * item_info.aux_quantity), m.state.duration) - local posted - local listener = Aux.control.event_listener('CHAT_MSG_SYSTEM') - listener:set_action(function() - if arg1 == ERR_AUCTION_STARTED then - posted = true - listener:stop() - end - end) - listener:start() - Aux.control.wait_until(function() return posted end, function() + local c = Aux.control.wait_for(function() m.state.posted = m.state.posted + 1 return k() end) + local posted + local listener = Aux.control.event_listener('CHAT_MSG_SYSTEM') + listener:set_action(function() + if arg1 == ERR_AUCTION_STARTED then + listener:stop() + c() + end + end) + listener:start() + else return m.stop() end diff --git a/components/scan.lua b/components/scan.lua index 5e47275..86dbc8e 100644 --- a/components/scan.lua +++ b/components/scan.lua @@ -31,9 +31,8 @@ function public.start(params) m.abort(m.threads[params.type].id) end - local thread_id = Aux.control.new_thread(function() - return m.wait_for_callback(m.current_thread().params.on_scan_start, m.scan) - end) + local thread_id = Aux.control.new_thread(Aux.f(m.wait_for_callback, params.on_scan_start, m.scan)) + m.threads[params.type] = { id = thread_id, params = params, @@ -69,16 +68,14 @@ function private.wait_for_results(k) end function private.wait_for_owner_results(k) - local updated - if m.current_thread().page == Aux.current_owner_page then - updated = true - else - Aux.control.on_next_event('AUCTION_OWNED_LIST_UPDATE', function() - updated = true - end) - end - Aux.control.wait_until(function() return updated end, k) + local c = Aux.control.wait_for(k) + + if m.current_thread().page == Aux.current_owner_page then + return c() + else + return Aux.control.on_next_event('AUCTION_OWNED_LIST_UPDATE', c) + end end function private.wait_for_list_results(k) @@ -201,7 +198,9 @@ function private.scan_auctions_helper(i, k) Aux.history.process_auction(auction_info) if m.current_thread().params.auto_buy_validator and m.current_thread().params.auto_buy_validator(auction_info) then - return Aux.place_bid(auction_info.query_type, auction_info.index, auction_info.buyout_price, function() return recurse(true) end) + local c = Aux.control.wait_for(recurse) + Aux.place_bid(auction_info.query_type, auction_info.index, auction_info.buyout_price, Aux.f(c, true)) + Aux.control.new_thread(Aux.control.sleep, 10, Aux.f(c, false)) elseif not m.current_query().validator or m.current_query().validator(auction_info) then return m.wait_for_callback(m.current_thread().params.on_auction, auction_info, function(removed) if removed then diff --git a/control.lua b/control.lua index cbf5523..262e94b 100644 --- a/control.lua +++ b/control.lua @@ -11,7 +11,7 @@ function public.LOAD() end function private.on_event() - for listener, _ in m.event_listeners do + for _, listener in m.event_listeners do if event == listener.event and not listener.deleted then listener.action() end @@ -19,7 +19,7 @@ function private.on_event() end function private.on_update() - m.event_listeners = Aux.util.set_filter(m.event_listeners, function(l) return not l.deleted end) + m.event_listeners = Aux.util.filter(m.event_listeners, function(l) return not l.deleted end) local threads = {} for thread_id, thread in m.threads do if not thread.killed then @@ -52,14 +52,14 @@ function public.event_listener(event, action) end function self:start() - Aux.util.set_add(m.event_listeners, listener) + tinsert(m.event_listeners, listener) m.event_frame:RegisterEvent(event) return self end function self:stop() listener.deleted = true - if not Aux.util.any(Aux.util.set_to_array(m.event_listeners), function(l) return l.event == event end) then + if not Aux.util.any(m.event_listeners, function(l) return l.event == event end) then m.event_frame:UnregisterEvent(event) end return self @@ -84,19 +84,13 @@ function public.on_next_update(callback) end function public.as_soon_as(p, callback) - return m.new_thread(function() - return m.wait_until(p, callback) - end) + return m.new_thread(m.wait_until, p, callback) end -do - local next_thread_id = 1 - function public.new_thread(k) - local thread_id = next_thread_id - next_thread_id = next_thread_id + 1 - m.threads[thread_id] = { k = k } - return thread_id - end +function public.new_thread(k, ...) + local thread_id = Aux.unique() + m.threads[thread_id] = { k = Aux.f(k, unpack(arg)) } + return thread_id end function public.kill_thread(thread_id) @@ -105,21 +99,31 @@ function public.kill_thread(thread_id) end end -function public.wait(...) - if type(arg[1]) == 'number' then - local count = tremove(arg, 1) - m.wait_until(function() count = count - 1 return count <= 0 end, unpack(arg)) - else - local k = tremove(arg, 1) - m.threads[m.thread_id].k = function() return k(unpack(arg)) end +function public.wait_for(k) + local ret + m.wait_until(function() return ret end, function() return k(unpack(ret)) end) + return function(...) + ret = arg end end -function public.wait_until(p, ...) - local k = tremove(arg, 1) +function public.sleep(dt, ...) + local t0 = GetTime() + return m.wait_until(function() return GetTime() - t0 >= dt end, unpack(arg)) +end + +function public.wait(k, ...) + if type(k) == 'number' then + m.wait_until(function() k = k - 1 return k <= 0 end, unpack(arg)) + else + m.threads[m.thread_id].k = Aux.f(k, unpack(arg)) + end +end + +function public.wait_until(p, k, ...) if p() then return k(unpack(arg)) else - return m.wait(m.wait_until, p, function() return k(unpack(arg)) end) + return m.wait(m.wait_until, p, Aux.f(k, unpack(arg))) end end diff --git a/core.lua b/core.lua index dba53dc..96bf94c 100644 --- a/core.lua +++ b/core.lua @@ -71,9 +71,7 @@ function public.on_load() btn:SetWidth(65) btn:SetHeight(24) btn:SetText('Close') - btn:SetScript('OnClick',function() - HideUIPanel(this:GetParent()) - end) + btn:SetScript('OnClick', Aux.f(HideUIPanel, AuxFrame)) public.close_button = btn end @@ -198,6 +196,25 @@ function private.on_addon_loaded() end end +function public.f(func, ...) + return function() + return func(unpack(arg)) + end +end + +function public.m(object, method, ...) + return m.f(object[method], object, unpack(arg)) +end + +do + local x = 0 + + function public.unique() + x = x + 1 + return x + end +end + do local locked @@ -274,9 +291,7 @@ function private.setup_hooks() m.hook('AuctionFrame_OnShow', function(...) if not m.blizzard_ui_shown then - m.control.as_soon_as(function() return AuctionFrame:GetScript('OnHide') == blizzard_ui_on_hide end, function() - HideUIPanel(AuctionFrame) - end) + m.control.as_soon_as(function() return AuctionFrame:GetScript('OnHide') == blizzard_ui_on_hide end, Aux.f(HideUIPanel, AuctionFrame)) end return m.orig.AuctionFrame_OnShow(unpack(arg)) end) diff --git a/gui.lua b/gui.lua index eb44730..870eb4f 100644 --- a/gui.lua +++ b/gui.lua @@ -125,7 +125,7 @@ do id = id, frame = parent, tabs = {}, - on_select = function() end, + on_select = Aux.util.pass, } function self:create_tab(text) @@ -230,20 +230,14 @@ function public.editbox(parent) editbox:SetShadowColor(0, 0, 0, 0) m.set_content_style(editbox) - editbox:SetScript('OnEditFocusLost', function() - this:HighlightText(0, 0) - end) + editbox:SetScript('OnEditFocusLost', Aux.m(editbox, 'HighlightText', 0, 0)) - editbox:SetScript('OnEscapePressed', function() - this:ClearFocus() - end) + editbox:SetScript('OnEscapePressed', Aux.m(editbox, 'ClearFocus')) do local last_time, last_x, last_y - editbox:SetScript('OnEditFocusGained', function() - this:HighlightText() - end) + editbox:SetScript('OnEditFocusGained', Aux.m(editbox, 'HighlightText')) editbox:SetScript('OnMouseUp', function() local x, y = GetCursorPosition() diff --git a/tabs/search/core.lua b/tabs/search/core.lua index 2f91a31..765943a 100644 --- a/tabs/search/core.lua +++ b/tabs/search/core.lua @@ -284,7 +284,8 @@ function private.start_real_time_scan(query, search, continuation) if not ignore_page then if search.auto_buy then ctrl.suspend() - Aux.place_bid('list', auction_record.index, auction_record.buyout_price, function() ctrl.resume(true) end) + Aux.place_bid('list', auction_record.index, auction_record.buyout_price, Aux.f(ctrl.resume, true)) + Aux.control.new_thread(Aux.control.sleep, 10, Aux.f(ctrl.resume, false)) else tinsert(new_records, auction_record) end @@ -376,7 +377,8 @@ function private.start_search(queries, continuation) on_auction = function(auction_record, ctrl) if search.auto_buy then ctrl.suspend() - Aux.place_bid('list', auction_record.index, auction_record.buyout_price, function() ctrl.resume(true) end) + Aux.place_bid('list', auction_record.index, auction_record.buyout_price, Aux.f(ctrl.resume, true)) + Aux.control.new_thread(Aux.control.sleep, 10, Aux.f(ctrl.resume, false)) elseif getn(search.records) < 1000 then tinsert(search.records, auction_record) if getn(search.records) == 1000 then diff --git a/util/completion.lua b/util/completion.lua index 207f450..6681fbf 100644 --- a/util/completion.lua +++ b/util/completion.lua @@ -8,7 +8,7 @@ function public:complete_filter() local filter_string = this:GetText() local completed_filter_string = ({strfind(filter_string, '([^;]*)/[^/;]*$')})[3] - local _, suggestions = Aux.scan_util.filter_from_string(completed_filter_string or '') + local _, suggestions = Aux.scan_util.query(completed_filter_string or '') local start_index, _, current_modifier = strfind(filter_string, '([^/;]*)$') current_modifier = current_modifier or '' diff --git a/util/scan_util.lua b/util/scan_util.lua index 52a7923..f1cc16e 100644 --- a/util/scan_util.lua +++ b/util/scan_util.lua @@ -402,7 +402,7 @@ function public.item_query(item_id, first_page, last_page) local item_info = Aux.info.item(item_id) if item_info then - local filter = m.filter_from_string(item_info.name..'/exact') + local filter = m.query(item_info.name..'/exact') filter.blizzard_query.first_page = first_page filter.blizzard_query.last_page = last_page return { @@ -419,7 +419,7 @@ function public.parse_filter_string(filter_string) for _, str in ipairs(parts) do str = Aux.util.trim(str) - local filter, _, error = m.filter_from_string(str) + local filter, _, error = m.query(str) if not filter then Aux.log('Invalid filter: '..error) @@ -435,16 +435,16 @@ function public.parse_filter_string(filter_string) return filters end -function public.filter_from_string(filter_term) +function public.query(filter_term) local parts = Aux.util.map(Aux.util.split(filter_term, '/'), function(part) return strlower(Aux.util.trim(part)) end) - local blizzard_filter = {} - local post_filter = {} + local blizzard_filters = {} + local post_filters = {} local prettified = m.filter_builder() local polish_notation_counter = 0 local i = 1 - local function non_blizzard_modifier(str) + local function post_filter(str) local filter = m.filters[str] if filter then prettified:append('|cffffff00'..str..'|r') @@ -465,7 +465,7 @@ function public.filter_from_string(filter_term) local test, suggestions, error = filter.test(unpack(args)) if test then - tinsert(post_filter, test) + tinsert(post_filters, test) else return error, i > getn(parts) and suggestions or {} end @@ -475,18 +475,30 @@ function public.filter_from_string(filter_term) local str = parts[i] i = i + 1 - if polish_notation_counter > 0 or str == 'and' or str == 'or' or str == 'not' then + if str == 'or*' then + if polish_notation_counter > 1 then + break + end + polish_notation_counter = 0 + tinsert(post_filters, str) + prettified:append('|cffffff00'..str..'|r') + elseif polish_notation_counter > 0 or strfind(str, '^and[2-9]?$') or strfind(str, '^or[2-9]?$') or str == 'not' then polish_notation_counter = polish_notation_counter == 0 and polish_notation_counter + 1 or polish_notation_counter - if str == 'and' or str == 'or' then - polish_notation_counter = polish_notation_counter + 1 - tinsert(post_filter, str) + if strfind(str, '^and[2-9]?$') or strfind(str, '^or[2-9]?$') then + local and_match, or_match = {strfind(str, '^(and)([2-9]?)$')}, {strfind(str, '^(or)([2-9]?)$') } + local op = and_match[3] or or_match[3] + local count = (tonumber(and_match[4]) or tonumber(or_match[4]) or 2) - 1 + polish_notation_counter = polish_notation_counter + count + for _=1,count do + tinsert(post_filters, op) + end prettified:append('|cffffff00'..str..'|r') elseif str == 'not' then - tinsert(post_filter, str) + tinsert(post_filters, str) prettified:append('|cffffff00'..str..'|r') elseif str ~= '' then polish_notation_counter = polish_notation_counter - 1 - local error, suggestions = non_blizzard_modifier(str) + local error, suggestions = post_filter(str) if error then return false, suggestions, error end @@ -495,60 +507,60 @@ function public.filter_from_string(filter_term) if tonumber(str) < 1 or tonumber(str) > 60 then return false, {}, 'Erroneous level range modifier' end - if not blizzard_filter.min_level then - blizzard_filter.min_level = tonumber(str) + if not blizzard_filters.min_level then + blizzard_filters.min_level = tonumber(str) prettified:append(Aux.gui.inline_color({216, 225, 211, 1})..str..'|r') - elseif not blizzard_filter.max_level and tonumber(str) >= blizzard_filter.min_level then - blizzard_filter.max_level = tonumber(str) + elseif not blizzard_filters.max_level and tonumber(str) >= blizzard_filters.min_level then + blizzard_filters.max_level = tonumber(str) prettified:append(Aux.gui.inline_color({216, 225, 211, 1})..str..'|r') else return false, {}, 'Erroneous level range modifier' end - elseif Aux.item_class_index(str) and not (blizzard_filter.class and not blizzard_filter.subclass and str == strlower(({ GetAuctionItemClasses() })[10])) then - if not blizzard_filter.class then - blizzard_filter.class = Aux.item_class_index(str) + elseif Aux.item_class_index(str) and not (blizzard_filters.class and not blizzard_filters.subclass and str == strlower(({ GetAuctionItemClasses() })[10])) then + if not blizzard_filters.class then + blizzard_filters.class = Aux.item_class_index(str) prettified:append(Aux.gui.inline_color({216, 225, 211, 1})..str..'|r') else return false, {}, 'Erroneous item class modifier' end - elseif blizzard_filter.class and Aux.item_subclass_index(blizzard_filter.class, str) then - if not blizzard_filter.subclass then - blizzard_filter.subclass = Aux.item_subclass_index(blizzard_filter.class, str) + elseif blizzard_filters.class and Aux.item_subclass_index(blizzard_filters.class, str) then + if not blizzard_filters.subclass then + blizzard_filters.subclass = Aux.item_subclass_index(blizzard_filters.class, str) prettified:append(Aux.gui.inline_color({216, 225, 211, 1})..str..'|r') else return false, {}, 'Erroneous item subclass modifier' end - elseif blizzard_filter.subclass and Aux.item_slot_index(blizzard_filter.class, blizzard_filter.subclass, str) then - if not blizzard_filter.slot then - blizzard_filter.slot = Aux.item_slot_index(blizzard_filter.class, blizzard_filter.subclass, str) + elseif blizzard_filters.subclass and Aux.item_slot_index(blizzard_filters.class, blizzard_filters.subclass, str) then + if not blizzard_filters.slot then + blizzard_filters.slot = Aux.item_slot_index(blizzard_filters.class, blizzard_filters.subclass, str) prettified:append(Aux.gui.inline_color({216, 225, 211, 1})..str..'|r') else return false, {}, 'Erroneous item slot modifier' end elseif Aux.item_quality_index(str) then - if not blizzard_filter.quality then - blizzard_filter.quality = Aux.item_quality_index(str) + if not blizzard_filters.quality then + blizzard_filters.quality = Aux.item_quality_index(str) prettified:append(Aux.gui.inline_color({216, 225, 211, 1})..str..'|r') else return false, {}, 'Erroneous rarity modifier' end elseif str == 'usable' then - if not blizzard_filter.usable then - blizzard_filter.usable = true + if not blizzard_filters.usable then + blizzard_filters.usable = true prettified:append(Aux.gui.inline_color({216, 225, 211, 1})..str..'|r') else return false, {}, 'Erroneous usable only modifier' end elseif str == 'exact' then - if not blizzard_filter.exact then - blizzard_filter.exact = true + if not blizzard_filters.exact then + blizzard_filters.exact = true else return false, {}, 'Erroneous exact only modifier' end elseif i == 2 and not m.filters[str] then - blizzard_filter.name = str + blizzard_filters.name = str elseif str ~= '' then - local error, suggestions = non_blizzard_modifier(str) + local error, suggestions = post_filter(str) if error then return false, suggestions, error end @@ -557,44 +569,44 @@ function public.filter_from_string(filter_term) end end - if polish_notation_counter ~= 0 then + if polish_notation_counter > 0 then local suggestions = {} - for filter, _ in pairs(m.filters) do + for filter, _ in m.filters do tinsert(suggestions, strlower(filter)) - tinsert(suggestions, 'and') - tinsert(suggestions, 'or') - tinsert(suggestions, 'not') end + tinsert(suggestions, 'and') + tinsert(suggestions, 'or') + tinsert(suggestions, 'not') return false, i > getn(parts) and suggestions, 'Malformed expression' end - if blizzard_filter.exact then - if blizzard_filter.min_level - or blizzard_filter.max_level - or blizzard_filter.class - or blizzard_filter.subclass - or blizzard_filter.slot - or blizzard_filter.quality - or blizzard_filter.usable - or not blizzard_filter.name + if blizzard_filters.exact then + if blizzard_filters.min_level + or blizzard_filters.max_level + or blizzard_filters.class + or blizzard_filters.subclass + or blizzard_filters.slot + or blizzard_filters.quality + or blizzard_filters.usable + or not blizzard_filters.name then return false, {}, 'Erroneous exact only modifier' else - prettified:prepend(Aux.info.display_name(Aux.cache.item_id(blizzard_filter.name)) or Aux.gui.inline_color({216, 225, 211, 1})..'['..blizzard_filter.name..']|r') + prettified:prepend(Aux.info.display_name(Aux.cache.item_id(blizzard_filters.name)) or Aux.gui.inline_color({216, 225, 211, 1})..'['..blizzard_filters.name..']|r') end - elseif blizzard_filter.name then - if blizzard_filter.name == '' then + elseif blizzard_filters.name then + if blizzard_filters.name == '' then prettified:prepend('|cffff0000'..'No Filter'..'|r') else - prettified:prepend(Aux.gui.inline_color({216, 225, 211, 1})..blizzard_filter.name..'|r') + prettified:prepend(Aux.gui.inline_color({216, 225, 211, 1})..blizzard_filters.name..'|r') end end return { - blizzard_query = m.blizzard_query(blizzard_filter), - validator = m.validator(blizzard_filter, post_filter), + blizzard_query = m.blizzard_query(blizzard_filters), + validator = m.validator(blizzard_filters, post_filters), prettified = prettified:get(), - }, m.suggestions(blizzard_filter, getn(parts)) + }, m.suggestions(blizzard_filters, getn(parts)) end function public.suggestions(blizzard_filter, num_parts) @@ -716,6 +728,8 @@ function public.validator(blizzard_filter, post_filter) tinsert(stack, a or b) elseif op == 'not' then tinsert(stack, not tremove(stack)) + elseif op == 'or*' then + stack = {Aux.util.any(stack, Aux.util.id)} else tinsert(stack, op(record) and true or false) end