better wireless

This commit is contained in:
kepler155c@gmail.com
2018-12-02 18:42:49 -05:00
parent ef0cf88387
commit 93d2aa331c
5 changed files with 209 additions and 149 deletions

View File

@@ -177,9 +177,62 @@ function page:setStatus(status)
self:sync() self:sync()
end end
function page:sendRequest(data, statusMsg) function page:processMessages(s)
local response Event.addRoutine(function()
repeat
local response = s:read()
if not response then
break
end
if response.type == 'received' then
Sound.play('entity.item.pickup')
local ritem = self.items[response.key]
if ritem then
ritem.count = response.count
self.grid:draw()
self:sync()
end
elseif response.type == 'list' then
self.items = self:expandList(response.list)
self:applyFilter()
self.grid:draw()
self.grid:sync()
elseif response.type == 'transfer' then
if response.count > 0 then
Sound.play('entity.item.pickup')
local item = self.items[response.key]
if item then
item.count = response.current
self.grid:draw()
self:sync()
end
end
if response.craft then
if response.craft > 0 then
self:setStatus(response.craft .. ' crafting ...')
elseif response.craft + response.count < response.requested then
if response.craft + response.count == 0 then
Sound.play('entity.villager.no')
end
self:setStatus((response.craft + response.count) .. ' available ...')
end
end
end
if response.msg then
self:setStatus(response.msg)
end
until not s.connected
s:close()
s = nil
self:setStatus('disconnected ...')
Sound.play('entity.villager.no')
end)
end
function page:sendRequest(data, statusMsg)
if not config.server then if not config.server then
self:setStatus('Invalid configuration') self:setStatus('Invalid configuration')
return return
@@ -202,30 +255,23 @@ function page:sendRequest(data, statusMsg)
local r = socket:read(2) local r = socket:read(2)
if r and not r.msg then if r and not r.msg then
self:setStatus('connected ...') self:setStatus('connected ...')
self:processMessages(socket)
else else
msg = r and r.msg or 'Timed out' msg = r and r.msg or 'Timed out'
socket:close() socket:close()
socket = nil socket = nil
break
end end
end end
end end
if socket then if socket then
if statusMsg then if statusMsg then
self:setStatus(statusMsg) self:setStatus(statusMsg)
Event.onTimeout(2, function()
self:setStatus('')
end)
end end
if socket:write(data) then if socket:write(data) then
response = socket:read(2) return true
if response then
if response.msg then
self:setStatus(response.msg)
response = nil
end
Event.onTimeout(2, function()
self:setStatus('')
end)
return
end
end end
socket:close() socket:close()
socket = nil socket = nil
@@ -233,8 +279,6 @@ function page:sendRequest(data, statusMsg)
end end
self:setStatus(msg or 'Failed to connect') self:setStatus(msg or 'Failed to connect')
end) end)
return response
end end
function page.grid:getRowTextColor(row, selected) function page.grid:getRowTextColor(row, selected)
@@ -282,17 +326,8 @@ function page.grid:eventHandler(event)
end end
function page:transfer(item, count, msg) function page:transfer(item, count, msg)
Sound.play('ui.button.click', .3) --Sound.play('ui.button.click', .3)
local response = self:sendRequest({ request = 'transfer', item = item, count = count }, msg) self:sendRequest({ request = 'transfer', item = item, count = count }, msg)
if response then
item.count = response.current - response.count
self.grid:draw()
if response.craft > 0 then
self:setStatus(response.craft .. ' crafting ...')
elseif response.craft + response.count < response.requested then
self:setStatus((response.craft + response.count) .. ' available ...')
end
end
end end
function page.setup:eventHandler(event) function page.setup:eventHandler(event)
@@ -443,12 +478,7 @@ function page:expandList(list)
end end
function page:refresh(requestType) function page:refresh(requestType)
local items = self:sendRequest({ request = requestType }, 'refreshing...') self:sendRequest({ request = requestType }, 'refreshing...')
if items then
self.items = self:expandList(items)
self:applyFilter()
end
end end
function page:applyFilter() function page:applyFilter()
@@ -486,23 +516,13 @@ Event.addRoutine(function()
elseif config.server and (config.useShield or config.slot) then elseif config.server and (config.useShield or config.slot) then
local s, m = pcall(function() local s, m = pcall(function()
local method = neural[inv] local method = neural[inv]
local item = method and method().getItemMeta(config.useShield and SHIELD_SLOT or config.slot) local item = method and method().list()[config.useShield and SHIELD_SLOT or config.slot]
if item then if item then
local slotNo = config.useShield and 'shield' or config.slot if page:sendRequest({
local response = page:sendRequest({
request = 'deposit', request = 'deposit',
slot = slotNo, slot = config.useShield and 'shield' or config.slot,
count = item.count, count = item.count,
key = table.concat({ item.name, item.damage, item.nbtHash }, ':') }) then
})
if response then
Sound.play('entity.item.pickup')
local ritem = page.items[response.key]
if ritem then
ritem.count = response.current + item.count
end
page.grid:draw()
page:sync()
sleepTime = math.max(sleepTime - .25, .25) sleepTime = math.max(sleepTime - .25, .25)
end end
else else

View File

@@ -188,6 +188,7 @@ function Milo:makeRequest(item, count, callback)
count = 0, count = 0,
current = current.count, current = current.count,
item = item, item = item,
key = item.key or Milo:uniqueKey(item),
} }
end end
@@ -208,6 +209,7 @@ function Milo:makeRequest(item, count, callback)
count = math.min(count, current.count), count = math.min(count, current.count),
current = current.count, current = current.count,
item = item, item = item,
key = item.key or Milo:uniqueKey(item),
} }
if request.count > 0 then if request.count > 0 then

View File

@@ -12,70 +12,76 @@ end
function ExportTask:cycle(context) function ExportTask:cycle(context)
for node in context.storage:filterActive('machine', filter) do 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 if not entry.filter then
-- exports must have a filter -- exports must have a filter
-- TODO: validate in exportView -- TODO: validate in exportView
break break
end
local function exportSingleSlot()
local slot = node.adapter.getItemMeta(entry.slot)
if slot and slot.count == slot.maxCount then
return
end end
if slot then local function exportSingleSlot()
-- something is in the slot, find what we can export local slot = node.adapter.getItemMeta(entry.slot)
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) if slot and slot.count == slot.maxCount then
local _, item = next(items) return
if item then end
local count = math.min(item.count, slot.maxCount - slot.count)
context.storage:export(node, entry.slot, count, item) 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 end
break
end end
return
end end
return
end
-- slot is empty - export first matching item we have in storage -- slot is empty - export first matching item we have in storage
for key in pairs(entry.filter) do for key in pairs(entry.filter) do
local items = Milo:getMatches(Milo:splitKey(key), entry) local items = Milo:getMatches(Milo:splitKey(key), entry)
local _, item = next(items) local _, item = next(items)
if item then if item then
local count = math.min(item.count, itemDB:getMaxCount(item)) local count = math.min(item.count, itemDB:getMaxCount(item))
context.storage:export(node, entry.slot, count, 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 break
end end
end 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 end
if type(entry.slot) == 'number' then end)
exportSingleSlot() if not s and m then
else _G._debug('Importer error')
exportItems() _G._debug(m)
end
end end
end end
end end

View File

@@ -11,50 +11,56 @@ end
function ImportTask:cycle(context) function ImportTask:cycle(context)
for node in context.storage:filterActive('machine', filter) do 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) local function itemMatchesFilter(item)
if not entry.ignoreDamage and not entry.ignoreNbtHash then if not entry.ignoreDamage and not entry.ignoreNbtHash then
local key = Milo:uniqueKey(item) local key = Milo:uniqueKey(item)
return entry.filter[key] 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 end
for key in pairs(entry.filter) do local function matchesFilter(item)
local v = Milo:splitKey(key) if not entry.filter then
if item.name == v.name and
(entry.ignoreDamage or item.damage == v.damage) and
(entry.ignoreNbtHash or item.nbtHash == v.nbtHash) then
return true return true
end 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
end end
end)
local function matchesFilter(item) if not s and m then
if not entry.filter then _G._debug('Importer error')
return true _G._debug(m)
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 end
end end
end end

View File

@@ -75,14 +75,22 @@ local function client(socket)
if not data then if not data then
break break
end end
--_G._debug(data)
socket.co = coroutine.running()
if data.request == 'scan' then -- full scan of all inventories if data.request == 'scan' then -- full scan of all inventories
local items = Milo:mergeResources(Milo:listItems(true)) local items = Milo:mergeResources(Milo:listItems(true))
socket:write(compactList(items)) socket:write({
type = 'list',
list = compactList(items),
})
elseif data.request == 'list' then elseif data.request == 'list' then
local items = Milo:mergeResources(Milo:listItems()) local items = Milo:mergeResources(Milo:listItems())
socket:write(compactList(items)) socket:write({
type = 'list',
list = compactList(items),
})
elseif data.request == 'deposit' then elseif data.request == 'deposit' then
local function deposit() local function deposit()
@@ -97,19 +105,20 @@ local function client(socket)
if node then if node then
local slot = node.adapter.getItemMeta(slotNo) local slot = node.adapter.getItemMeta(slotNo)
if slot then 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 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) Milo:queueRequest({ }, deposit)
elseif data.request == 'transfer' then elseif data.request == 'transfer' then
@@ -125,17 +134,34 @@ local function client(socket)
local function transfer(request) local function transfer(request)
local target = makeNode('inventory') local target = makeNode('inventory')
if target then if target then
context.storage:export( local amount = context.storage:export(
target, target,
nil, nil,
request.requested, request.requested,
data.item) 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
end end
local request = Milo:makeRequest(data.item, count, transfer) local request = Milo:makeRequest(data.item, count, transfer)
if (request.craft + request.count == 0) or
socket:write(request) (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 end
until not socket.connected until not socket.connected