milo performance

This commit is contained in:
kepler155c
2018-11-14 13:22:31 -05:00
parent 8e9ca385d9
commit 364ae04ceb
7 changed files with 134 additions and 133 deletions

View File

@@ -19,18 +19,6 @@ function Milo:getContext()
return self.context
end
function Milo:requestCrafting(item)
local key = Milo:uniqueKey(item)
if not self.context.craftingQueue[key] then
item.crafted = 0
item.pending = { }
item.key = key
self.context.craftingQueue[key] = item
os.queueEvent('milo_cycle')
end
end
function Milo:pauseCrafting()
self.craftingPaused = true
Milo:showError('Crafting Paused')
@@ -105,52 +93,33 @@ function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash)
end
end
function Milo:getItemWithQty(res, ignoreDamage, ignoreNbtHash)
local items = self:listItems()
local item = self:getItem(items, res, ignoreDamage, ignoreNbtHash)
local count = 0
if item and (ignoreDamage or ignoreNbtHash) then
for _,v in pairs(items) do
if item.name == v.name and
(ignoreDamage or item.damage == v.damage) and
(ignoreNbtHash or item.nbtHash == v.nbtHash) then
count = count + v.count
end
end
elseif item then
count = item.count
end
return item, count
end
function Milo:getMatches(items, item, ignoreDamage, ignoreNbtHash)
-- returns a list of items that matches along with a total count
function Milo:getMatches(item, flags)
local t = { }
local count = 0
local items = self:listItems()
if not ignoreDamage and not ignoreNbtHash then
if not flags.ignoreDamage and not flags.ignoreNbtHash then
local key = item.key or Milo:uniqueKey(item)
local e = items[key]
if e then
t[key] = Util.shallowCopy(e)
local v = items[key]
if v then
t[key] = Util.shallowCopy(v)
count = v.count
end
else
for k,v in pairs(items) do
for key,v in pairs(items) do
if item.name == v.name and
(ignoreDamage or item.damage == v.damage) and
(ignoreNbtHash or item.nbtHash == v.nbtHash) then
local e = t[k]
if not e then
t[k] = Util.shallowCopy(v)
else
e.count = e.count + item.count
end
(flags.ignoreDamage or item.damage == v.damage) and
(flags.ignoreNbtHash or item.nbtHash == v.nbtHash) then
t[key] = Util.shallowCopy(v)
count = count + v.count
end
end
end
return t
return t, count
end
function Milo:clearGrid()
@@ -172,6 +141,18 @@ function Milo:getTurtleInventory()
return list
end
function Milo:requestCrafting(item)
local key = Milo:uniqueKey(item)
if not self.context.craftingQueue[key] then
item.crafted = 0
item.pending = { }
item.key = key
self.context.craftingQueue[key] = item
os.queueEvent('milo_cycle')
end
end
-- queue up an action that reliees on the crafting grid
function Milo:queueRequest(request, callback)
if Util.empty(self.context.queue) then

View File

@@ -256,9 +256,9 @@ function Storage:export(target, slot, count, item)
local amount = adapter:provide(item, count, slot, target)
if amount > 0 then
_G._debug('EXT: %s(%d): %s -> %s%s',
item.displayName or item.name, amount, sn(adapter.name), sn(target),
slot and string.format('[%d]', slot) or '')
_G._debug('EXT: %s(%d): %s -> %s%s',
item.displayName or item.name, amount, sn(adapter.name), sn(target),
slot and string.format('[%d]', slot) or '[*]')
self:updateCache(adapter, key, -amount)
self:updateCache(self, key, -amount)
@@ -277,8 +277,17 @@ function Storage:export(target, slot, count, item)
end
end
_G._debug('STORAGE: MISS: %s - %d', key, count)
_G._debug('MISS: %s(%d): %s%s %s',
item.displayName or item.name, count, sn(target),
slot and string.format('[%d]', slot) or '[*]', key)
-- TODO: If there are misses when a slot is specified than something is wrong...
-- The caller should confirm the quantity beforehand
-- If no slot and full amount is not exported, then no need to check rest of adapters
-- ... so should not reach here
--[[
-- not found - scan all others
for _, adapter in self:onlineAdapters() do
if not adapter.cache or not adapter.cache[key] then
@@ -289,11 +298,15 @@ function Storage:export(target, slot, count, item)
end
end
end
--]]
return total
end
function Storage:import(source, slot, count, item)
if not source then error('Storage:import: source is required') end
if not slot then error('Storage:import: slot is required') end
local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
@@ -305,9 +318,9 @@ function Storage:import(source, slot, count, item)
local amount = adapter:insert(slot, count, nil, source)
if amount > 0 then
_G._debug('INS: %s(%d): %s[%d] -> %s',
item.displayName or item.name, amount,
sn(source), slot, sn(adapter.name))
_G._debug('INS: %s(%d): %s[%d] -> %s',
item.displayName or item.name, amount,
sn(source), slot, sn(adapter.name))
self:updateCache(adapter, key, amount)
self:updateCache(self, key, amount)
@@ -347,11 +360,15 @@ _G._debug('INS: %s(%d): %s[%d] -> %s',
end
if not itemDB:get(item) then
if not slot then
if item.displayName then
-- this item already has metadata
itemDB:add(item)
elseif not slot then
_G._debug("IMPORT: NO SLOT")
elseif not device[source] or not device[source].getItemMeta then
_G._debug("IMPORT: DEVICE? : " .. source)
else
-- get the metadata from the device and add to db
itemDB:add(device[source].getItemMeta(slot))
end
end
@@ -374,7 +391,7 @@ function Storage:trash(source, slot, count)
local trashcan = Util.find(self.nodes, 'mtype', 'trashcan')
if trashcan and trashcan.adapter and trashcan.adapter.online then
_G._debug('TRA: %s[%d] (%d)', sn(source), slot, count or 64)
_G._debug('TRA: %s[%d] (%d)', sn(source), slot, count or 64)
return trashcan.adapter.pullItems(source, slot, count)
end