better wireless
This commit is contained in:
@@ -12,70 +12,76 @@ end
|
||||
|
||||
function ExportTask:cycle(context)
|
||||
for node in context.storage:filterActive('machine', filter) do
|
||||
for _, entry in pairs(node.exports) do
|
||||
local s, m = pcall(function()
|
||||
for _, entry in pairs(node.exports) do
|
||||
|
||||
if not entry.filter then
|
||||
-- exports must have a filter
|
||||
-- TODO: validate in exportView
|
||||
break
|
||||
end
|
||||
|
||||
local function exportSingleSlot()
|
||||
local slot = node.adapter.getItemMeta(entry.slot)
|
||||
|
||||
if slot and slot.count == slot.maxCount then
|
||||
return
|
||||
if not entry.filter then
|
||||
-- exports must have a filter
|
||||
-- TODO: validate in exportView
|
||||
break
|
||||
end
|
||||
|
||||
if slot then
|
||||
-- something is in the slot, find what we can export
|
||||
for key in pairs(entry.filter) do
|
||||
local filterItem = Milo:splitKey(key)
|
||||
if (slot.name == filterItem.name and
|
||||
entry.ignoreDamage or slot.damage == filterItem.damage and
|
||||
entry.ignoreNbtHash or slot.nbtHash == filterItem.nbtHash) then
|
||||
local function exportSingleSlot()
|
||||
local slot = node.adapter.getItemMeta(entry.slot)
|
||||
|
||||
local items = Milo:getMatches(filterItem, entry)
|
||||
local _, item = next(items)
|
||||
if item then
|
||||
local count = math.min(item.count, slot.maxCount - slot.count)
|
||||
context.storage:export(node, entry.slot, count, item)
|
||||
if slot and slot.count == slot.maxCount then
|
||||
return
|
||||
end
|
||||
|
||||
if slot then
|
||||
-- something is in the slot, find what we can export
|
||||
for key in pairs(entry.filter) do
|
||||
local filterItem = Milo:splitKey(key)
|
||||
if (slot.name == filterItem.name and
|
||||
entry.ignoreDamage or slot.damage == filterItem.damage and
|
||||
entry.ignoreNbtHash or slot.nbtHash == filterItem.nbtHash) then
|
||||
|
||||
local items = Milo:getMatches(filterItem, entry)
|
||||
local _, item = next(items)
|
||||
if item then
|
||||
local count = math.min(item.count, slot.maxCount - slot.count)
|
||||
context.storage:export(node, entry.slot, count, item)
|
||||
end
|
||||
break
|
||||
end
|
||||
break
|
||||
end
|
||||
return
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
-- slot is empty - export first matching item we have in storage
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(Milo:splitKey(key), entry)
|
||||
local _, item = next(items)
|
||||
if item then
|
||||
local count = math.min(item.count, itemDB:getMaxCount(item))
|
||||
context.storage:export(node, entry.slot, count, item)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function exportItems()
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(itemDB:splitKey(key), entry)
|
||||
for _,item in pairs(items) do
|
||||
if context.storage:export(node, nil, item.count, item) == 0 then
|
||||
-- TODO: really shouldn't break here as there may be room in other slots
|
||||
-- leaving for now for performance reasons
|
||||
-- slot is empty - export first matching item we have in storage
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(Milo:splitKey(key), entry)
|
||||
local _, item = next(items)
|
||||
if item then
|
||||
local count = math.min(item.count, itemDB:getMaxCount(item))
|
||||
context.storage:export(node, entry.slot, count, item)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function exportItems()
|
||||
for key in pairs(entry.filter) do
|
||||
local items = Milo:getMatches(itemDB:splitKey(key), entry)
|
||||
for _,item in pairs(items) do
|
||||
if context.storage:export(node, nil, item.count, item) == 0 then
|
||||
-- TODO: really shouldn't break here as there may be room in other slots
|
||||
-- leaving for now for performance reasons
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if type(entry.slot) == 'number' then
|
||||
exportSingleSlot()
|
||||
else
|
||||
exportItems()
|
||||
end
|
||||
end
|
||||
if type(entry.slot) == 'number' then
|
||||
exportSingleSlot()
|
||||
else
|
||||
exportItems()
|
||||
end
|
||||
end)
|
||||
if not s and m then
|
||||
_G._debug('Importer error')
|
||||
_G._debug(m)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,50 +11,56 @@ end
|
||||
|
||||
function ImportTask:cycle(context)
|
||||
for node in context.storage:filterActive('machine', filter) do
|
||||
for _, entry in pairs(node.imports) do
|
||||
local s, m = pcall(function()
|
||||
for _, entry in pairs(node.imports) do
|
||||
|
||||
local function itemMatchesFilter(item)
|
||||
if not entry.ignoreDamage and not entry.ignoreNbtHash then
|
||||
local key = Milo:uniqueKey(item)
|
||||
return entry.filter[key]
|
||||
local function itemMatchesFilter(item)
|
||||
if not entry.ignoreDamage and not entry.ignoreNbtHash then
|
||||
local key = Milo:uniqueKey(item)
|
||||
return entry.filter[key]
|
||||
end
|
||||
|
||||
for key in pairs(entry.filter) do
|
||||
local v = Milo:splitKey(key)
|
||||
if item.name == v.name and
|
||||
(entry.ignoreDamage or item.damage == v.damage) and
|
||||
(entry.ignoreNbtHash or item.nbtHash == v.nbtHash) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for key in pairs(entry.filter) do
|
||||
local v = Milo:splitKey(key)
|
||||
if item.name == v.name and
|
||||
(entry.ignoreDamage or item.damage == v.damage) and
|
||||
(entry.ignoreNbtHash or item.nbtHash == v.nbtHash) then
|
||||
local function matchesFilter(item)
|
||||
if not entry.filter then
|
||||
return true
|
||||
end
|
||||
|
||||
if entry.blacklist then
|
||||
return not itemMatchesFilter(item)
|
||||
end
|
||||
|
||||
return itemMatchesFilter(item)
|
||||
end
|
||||
|
||||
local function importSlot(slotNo)
|
||||
local item = node.adapter.getItemMeta(slotNo)
|
||||
if item and matchesFilter(item) then
|
||||
context.storage:import(node, slotNo, item.count, item)
|
||||
end
|
||||
end
|
||||
|
||||
if type(entry.slot) == 'number' then
|
||||
importSlot(entry.slot)
|
||||
else
|
||||
for i in pairs(node.adapter.list()) do
|
||||
importSlot(i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function matchesFilter(item)
|
||||
if not entry.filter then
|
||||
return true
|
||||
end
|
||||
|
||||
if entry.blacklist then
|
||||
return not itemMatchesFilter(item)
|
||||
end
|
||||
|
||||
return itemMatchesFilter(item)
|
||||
end
|
||||
|
||||
local function importSlot(slotNo)
|
||||
local item = node.adapter.getItemMeta(slotNo)
|
||||
if item and matchesFilter(item) then
|
||||
context.storage:import(node, slotNo, item.count, item)
|
||||
end
|
||||
end
|
||||
|
||||
if type(entry.slot) == 'number' then
|
||||
importSlot(entry.slot)
|
||||
else
|
||||
for i in pairs(node.adapter.list()) do
|
||||
importSlot(i)
|
||||
end
|
||||
end
|
||||
end)
|
||||
if not s and m then
|
||||
_G._debug('Importer error')
|
||||
_G._debug(m)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -75,14 +75,22 @@ local function client(socket)
|
||||
if not data then
|
||||
break
|
||||
end
|
||||
--_G._debug(data)
|
||||
socket.co = coroutine.running()
|
||||
|
||||
if data.request == 'scan' then -- full scan of all inventories
|
||||
local items = Milo:mergeResources(Milo:listItems(true))
|
||||
socket:write(compactList(items))
|
||||
socket:write({
|
||||
type = 'list',
|
||||
list = compactList(items),
|
||||
})
|
||||
|
||||
elseif data.request == 'list' then
|
||||
local items = Milo:mergeResources(Milo:listItems())
|
||||
socket:write(compactList(items))
|
||||
socket:write({
|
||||
type = 'list',
|
||||
list = compactList(items),
|
||||
})
|
||||
|
||||
elseif data.request == 'deposit' then
|
||||
local function deposit()
|
||||
@@ -97,19 +105,20 @@ local function client(socket)
|
||||
if node then
|
||||
local slot = node.adapter.getItemMeta(slotNo)
|
||||
if slot then
|
||||
context.storage:import(node, slotNo, slot.count, slot)
|
||||
if context.storage:import(node, slotNo, slot.count, slot) then
|
||||
local item = Milo:getItem(Milo:listItems(), slot)
|
||||
if item then
|
||||
socket:write({
|
||||
type = 'received',
|
||||
key = item.key,
|
||||
count = item.count,
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local list = Milo:listItems()
|
||||
local current = list[data.key] and list[data.key].count or 0
|
||||
|
||||
socket:write({
|
||||
key = data.key,
|
||||
current = current,
|
||||
})
|
||||
|
||||
Milo:queueRequest({ }, deposit)
|
||||
|
||||
elseif data.request == 'transfer' then
|
||||
@@ -125,17 +134,34 @@ local function client(socket)
|
||||
local function transfer(request)
|
||||
local target = makeNode('inventory')
|
||||
if target then
|
||||
context.storage:export(
|
||||
local amount = context.storage:export(
|
||||
target,
|
||||
nil,
|
||||
request.requested,
|
||||
data.item)
|
||||
local item = Milo:listItems()[request.key]
|
||||
socket:write({
|
||||
type = 'transfer',
|
||||
key = request.key,
|
||||
requested = request.requested,
|
||||
current = item and item.count or 0,
|
||||
count = amount,
|
||||
craft = request.craft,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
local request = Milo:makeRequest(data.item, count, transfer)
|
||||
|
||||
socket:write(request)
|
||||
if (request.craft + request.count == 0) or
|
||||
(request.craft > 0 and request.count == 0) then
|
||||
socket:write({
|
||||
type = 'transfer',
|
||||
key = request.key,
|
||||
requested = request.requested,
|
||||
count = request.current,
|
||||
craft = request.craft,
|
||||
})
|
||||
end
|
||||
end
|
||||
until not socket.connected
|
||||
|
||||
|
||||
Reference in New Issue
Block a user