milo perf + cleanup

This commit is contained in:
kepler155c@gmail.com
2019-01-26 00:28:46 -05:00
parent 779d58cd7e
commit bae28521db
7 changed files with 69 additions and 61 deletions

View File

@@ -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()

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)