diff --git a/milo/Milo.lua b/milo/Milo.lua index ce0a8e2..40cfe1c 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -118,9 +118,9 @@ table.sort(context.tasks, function(a, b) return a.priority < b.priority end) -_debug('Tasks\n-----') +_G._debug('Tasks\n-----') for _, task in ipairs(context.tasks) do - _debug('%d: %s', task.priority, task.name) + _G._debug('%d: %s', task.priority, task.name) end Milo:clearGrid() @@ -128,11 +128,18 @@ Milo:clearGrid() UI:setPage(UI:getPage('listing')) Sound.play('ui.toast.challenge_complete') -local processing +Event.on({ 'milo_cycle', 'milo_queue' }, function(e) + if context.storage:isOnline() then + if #context.queue > 0 then + local queue = context.queue + context.queue = { } + for _, entry in pairs(queue) do + entry.callback(entry.request) + end + end + end -Event.on('milo_cycle', function() - if not processing and not Milo:isCraftingPaused() then - processing = true + if e == 'milo_cycle' and not Milo:isCraftingPaused() then Milo:resetCraftingStatus() for _, task in ipairs(context.tasks) do @@ -142,37 +149,17 @@ Event.on('milo_cycle', function() _G._debug(m) end end - processing = false - if not Util.empty(context.queue) then - os.queueEvent('milo_queue') - end - end -end) - -Event.on('milo_queue', function() - if not processing and context.storage:isOnline() then - processing = true - - for _, key in pairs(Util.keys(context.queue)) do - local entry = context.queue[key] - entry.callback(entry.request) - context.queue[key] = nil - end - - processing = false end end) Event.on('turtle_inventory', function() - if not processing and not Milo:isCraftingPaused() then + Milo:queueRequest({ }, function() Milo:clearGrid() - end + end) end) Event.onInterval(5, function() - if not Milo:isCraftingPaused() then - os.queueEvent('milo_cycle') - end + Event.trigger('milo_cycle') end) Event.on({ 'storage_offline', 'storage_online' }, function() diff --git a/milo/MiloRemote.lua b/milo/MiloRemote.lua index ef1dc40..e0e8ed0 100644 --- a/milo/MiloRemote.lua +++ b/milo/MiloRemote.lua @@ -11,7 +11,6 @@ local colors = _G.colors local device = _G.device local fs = _G.fs local shell = _ENV.shell -local string = _G.string local context = { state = Config.load('miloRemote', { displayMode = 0, deposit = true }), @@ -311,7 +310,7 @@ function page:applyFilter() for _,v in pairs(t) do v.score = fuzzy(v.lname, filter) - if v.score > 0 then + if v.score then if v.count > 0 then v.score = v.score + 1 end diff --git a/milo/apis/fuzzyMatch.lua b/milo/apis/fuzzyMatch.lua index bca2dad..5fb7bd2 100644 --- a/milo/apis/fuzzyMatch.lua +++ b/milo/apis/fuzzyMatch.lua @@ -3,20 +3,17 @@ -- -- not very fuzzy anymore -local SCORE_WEIGHT = 1000 -local LEADING_LETTER_PENALTY = -3 +local SCORE_WEIGHT = 1000 +local LEADING_LETTER_PENALTY = -3 local LEADING_LETTER_PENALTY_MAX = -9 local _find = string.find +local _max = math.max -return function(str_lower, ptrn_lower) - local score = 0 - - local start = _find(str_lower, ptrn_lower, 1, true) +return function(str, pattern) + local start = _find(str, pattern, 1, true) if start then -- All letters before the current one are considered leading, so add them to our penalty - score = SCORE_WEIGHT + math.max(LEADING_LETTER_PENALTY * (start - 1), LEADING_LETTER_PENALTY_MAX) + return SCORE_WEIGHT + _max(LEADING_LETTER_PENALTY * (start - 1), LEADING_LETTER_PENALTY_MAX) end - - return score end diff --git a/milo/apis/milo.lua b/milo/apis/milo.lua index 724ce29..2b8a572 100644 --- a/milo/apis/milo.lua +++ b/milo/apis/milo.lua @@ -149,9 +149,7 @@ end -- queue up an action that relies on the crafting grid function Milo:queueRequest(request, callback) - if Util.empty(self.context.queue) then - os.queueEvent('milo_queue') - end + os.queueEvent('milo_queue') table.insert(self.context.queue, { request = request, callback = callback diff --git a/milo/apis/storage.lua b/milo/apis/storage.lua index 1657864..ef016fd 100644 --- a/milo/apis/storage.lua +++ b/milo/apis/storage.lua @@ -122,6 +122,12 @@ function Storage:initStorage() v.adapter = device[k] v.adapter.online = true end + + if v.adapter then + -- force a new getTransferLocations() as the list may have changed + v.adapter.transferLocations = nil + end + if v.mtype == 'storage' then online = online and not not (v.adapter and v.adapter.online) end @@ -334,7 +340,13 @@ function Storage:_sn(name) end local function isValidTransfer(adapter, target) - for _,v in pairs(adapter.getTransferLocations()) do + -- lazily cache transfer locations + local transferLocations = adapter.transferLocations + if not transferLocations then + transferLocations = adapter.getTransferLocations() + adapter.transferLocations = transferLocations + end + for _,v in pairs(transferLocations) do if v == target then return true end @@ -500,7 +512,6 @@ function Storage:import(source, slot, count, item) total = total + self:trash(source, slot, count) return total end - --return total end if count <= 0 then return total diff --git a/milo/core/listing.lua b/milo/core/listing.lua index 8b1e326..705dd07 100644 --- a/milo/core/listing.lua +++ b/milo/core/listing.lua @@ -1,5 +1,6 @@ local Craft = require('craft2') local Event = require('event') +local fuzzy = require('fuzzyMatch') local Milo = require('milo') local Sound = require('sound') local UI = require('ui') @@ -8,7 +9,6 @@ local Util = require('util') local colors = _G.colors local context = Milo:getContext() local displayMode = Milo:getState('displayMode') or 0 -local string = _G.string local displayModes = { [0] = { text = 'A', help = 'Showing all items' }, @@ -331,21 +331,37 @@ end function page:applyFilter() local function filterItems(t, filter) - if filter or displayMode > 0 then + self.grid.sortColumn = Milo:getState('sortColumn') or 'count' + self.grid.inverseSort = Milo:getState('inverseSort') + + if filter then local r = { } - if filter then - filter = filter:lower() - end + filter = filter:lower() + self.grid.sortColumn = 'score' + self.grid.inverseSort = true + for _,v in pairs(t) do - if not filter or string.find(v.lname, filter, 1, true) then - if filter or --displayMode == 0 or - displayMode == 1 and v.count > 0 then - table.insert(r, v) + v.score = fuzzy(v.lname, filter) + if v.score then + if v.count > 0 then + v.score = v.score + 1 end + table.insert(r, v) + end + end + return r + + elseif displayMode > 0 then + local r = { } + + for _,v in pairs(t) do + if v.count > 0 then + table.insert(r, v) end end return r end + return t end diff --git a/milo/plugins/item/machinesTab.lua b/milo/plugins/item/machinesTab.lua index 5246c26..fe940f3 100644 --- a/milo/plugins/item/machinesTab.lua +++ b/milo/plugins/item/machinesTab.lua @@ -24,16 +24,16 @@ local machinesTab = UI.Window { function machinesTab:setItem(item) self.item = item local machine = Craft.machineLookup[self.item.key] + local t = Util.filter(context.storage.nodes, function(node) + if node.category == 'machine' or node.category == 'custom' then -- TODO: - need a setting instead (ie. canCraft) + return node.adapter and node.adapter.online and node.adapter.pushItems + end + end) + self.grid:setValues(t) if machine then - local t = Util.filter(context.storage.nodes, function(node) - if node.category == 'machine' or node.category == 'custom' then -- TODO: - need a setting instead (ie. canCraft) - return node.adapter and node.adapter.online and node.adapter.pushItems - end - end) - self.grid:setValues(t) self.grid:setSelected('name', machine) end - self.parent:setActive(self, machine) + self.parent:setActive(self, item.has_recipe) end function machinesTab.grid:getDisplayValues(row)