milo wip
This commit is contained in:
@@ -83,6 +83,7 @@ local page = UI.Page {
|
|||||||
[ 'control-e' ] = 'eject',
|
[ 'control-e' ] = 'eject',
|
||||||
[ 'control-r' ] = 'refresh',
|
[ 'control-r' ] = 'refresh',
|
||||||
[ 'control-s' ] = 'eject_stack',
|
[ 'control-s' ] = 'eject_stack',
|
||||||
|
[ 'control-a' ] = 'eject_all',
|
||||||
[ 'control-1' ] = 'eject_1',
|
[ 'control-1' ] = 'eject_1',
|
||||||
[ 'control-2' ] = 'eject_1',
|
[ 'control-2' ] = 'eject_1',
|
||||||
[ 'control-3' ] = 'eject_1',
|
[ 'control-3' ] = 'eject_1',
|
||||||
@@ -123,6 +124,7 @@ end
|
|||||||
function page:sendRequest(data)
|
function page:sendRequest(data)
|
||||||
local response
|
local response
|
||||||
|
|
||||||
|
debug(data)
|
||||||
sync(self, function()
|
sync(self, function()
|
||||||
local msg
|
local msg
|
||||||
for _ = 1, 2 do
|
for _ = 1, 2 do
|
||||||
@@ -144,7 +146,7 @@ function page:sendRequest(data)
|
|||||||
end
|
end
|
||||||
self.notification:error(msg or 'Failed to connect')
|
self.notification:error(msg or 'Failed to connect')
|
||||||
end)
|
end)
|
||||||
|
debug('got response')
|
||||||
return response
|
return response
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -182,7 +184,7 @@ function page:eventHandler(event)
|
|||||||
local item = self.grid:getSelected()
|
local item = self.grid:getSelected()
|
||||||
if item then
|
if item then
|
||||||
local response = self:sendRequest({ request = 'transfer', item = item, count = 1 })
|
local response = self:sendRequest({ request = 'transfer', item = item, count = 1 })
|
||||||
item.count = item.count - response.count
|
item.count = response.count
|
||||||
self.grid:draw()
|
self.grid:draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -190,7 +192,15 @@ function page:eventHandler(event)
|
|||||||
local item = self.grid:getSelected()
|
local item = self.grid:getSelected()
|
||||||
if item then
|
if item then
|
||||||
local response = self:sendRequest({ request = 'transfer', item = item, count = 64 })
|
local response = self:sendRequest({ request = 'transfer', item = item, count = 64 })
|
||||||
item.count = item.count - response.count
|
item.count = response.count
|
||||||
|
self.grid:draw()
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif event.type == 'eject_all' then
|
||||||
|
local item = self.grid:getSelected()
|
||||||
|
if item then
|
||||||
|
local response = self:sendRequest({ request = 'transfer', item = item, count = item.count })
|
||||||
|
item.count = response.count
|
||||||
self.grid:draw()
|
self.grid:draw()
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -251,19 +261,22 @@ end
|
|||||||
debug(options.slot)
|
debug(options.slot)
|
||||||
if options.slot.value then
|
if options.slot.value then
|
||||||
debug('Transfer items initialized')
|
debug('Transfer items initialized')
|
||||||
Event.onInterval(2, function()
|
Event.addRoutine(function()
|
||||||
local neural = device.neuralInterface
|
while true do
|
||||||
if neural and neural.getInventory then
|
os.sleep(1.5)
|
||||||
local item = neural.getInventory().getItem(options.slot.value)
|
local neural = device.neuralInterface
|
||||||
if item then
|
if neural and neural.getInventory then
|
||||||
debug('depositing')
|
local item = neural.getInventory().getItem(options.slot.value)
|
||||||
page:sendRequest({ request = 'deposit', slot = options.slot.value })
|
if item then
|
||||||
-- local item =
|
debug('depositing')
|
||||||
-- TODO: update count for this one item
|
page:sendRequest({ request = 'deposit', slot = options.slot.value })
|
||||||
-- page.grid:draw() page:sync()
|
-- local item =
|
||||||
|
-- TODO: update count for this one item
|
||||||
|
-- page.grid:draw() page:sync()
|
||||||
|
end
|
||||||
|
else
|
||||||
|
debug('missing Introspection module')
|
||||||
end
|
end
|
||||||
else
|
|
||||||
debug('missing Introspection module')
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -143,14 +143,45 @@ function Milo:getTurtleInventory()
|
|||||||
return list
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
function Milo:eject(item, qty)
|
function Milo:xxx(item, count)
|
||||||
local s, m = pcall(function()
|
return self:provideItem(item, count, function(providable, currentCount)
|
||||||
self.context.storage:provide(item, qty)
|
-- return the current amount in the system
|
||||||
turtle.emptyInventory()
|
return currentCount - self:eject(item, providable)
|
||||||
end)
|
end)
|
||||||
if not s and m then
|
end
|
||||||
debug(m)
|
|
||||||
|
function Milo:provideItem(item, count, callback)
|
||||||
|
local current = Milo:getItem(Milo:listItems(), item) or { count = 0 }
|
||||||
|
local toCraft = count - math.min(current.count, count)
|
||||||
|
|
||||||
|
if toCraft > 0 then
|
||||||
|
local recipe = Craft.findRecipe(self:uniqueKey(item))
|
||||||
|
if not recipe then
|
||||||
|
toCraft = 0
|
||||||
|
else
|
||||||
|
-- if you ask for 1 stick, getCraftableAmount will return 4 (obviously)
|
||||||
|
toCraft = math.min(toCraft, Craft.getCraftableAmount(recipe, toCraft, Milo:listItems(), { }))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if toCraft == 0 then
|
||||||
|
return callback(math.min(count, current.count), current.count)
|
||||||
|
-- return current.count - self:eject(item, math.min(count, current.count))
|
||||||
|
end
|
||||||
|
|
||||||
|
item = Util.shallowCopy(item)
|
||||||
|
item.count = current.count + toCraft
|
||||||
|
item.eject = callback
|
||||||
|
self:requestCrafting(item)
|
||||||
|
item.crafted = current.count
|
||||||
|
|
||||||
|
return current.count
|
||||||
|
end
|
||||||
|
|
||||||
|
function Milo:eject(item, count)
|
||||||
|
count = self.context.storage:provide(item, count)
|
||||||
|
turtle.emptyInventory()
|
||||||
|
return count
|
||||||
end
|
end
|
||||||
|
|
||||||
function Milo:saveMachineRecipe(recipe, result, machine)
|
function Milo:saveMachineRecipe(recipe, result, machine)
|
||||||
@@ -175,6 +206,7 @@ function Milo:mergeResources(t)
|
|||||||
else
|
else
|
||||||
item = Util.shallowCopy(v)
|
item = Util.shallowCopy(v)
|
||||||
item.count = 0
|
item.count = 0
|
||||||
|
item.key = self:uniqueKey(v)
|
||||||
table.insert(t, item)
|
table.insert(t, item)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -185,6 +217,7 @@ function Milo:mergeResources(t)
|
|||||||
if not item then
|
if not item then
|
||||||
item = Util.shallowCopy(v)
|
item = Util.shallowCopy(v)
|
||||||
item.count = 0
|
item.count = 0
|
||||||
|
item.key = self:uniqueKey(v)
|
||||||
table.insert(t, item)
|
table.insert(t, item)
|
||||||
end
|
end
|
||||||
item.has_recipe = true
|
item.has_recipe = true
|
||||||
|
|||||||
@@ -160,6 +160,7 @@ self.listCount = self.listCount + 1
|
|||||||
if not entry then
|
if not entry then
|
||||||
entry = Util.shallowCopy(v)
|
entry = Util.shallowCopy(v)
|
||||||
entry.count = v.count
|
entry.count = v.count
|
||||||
|
entry.key = key
|
||||||
cache[key] = entry
|
cache[key] = entry
|
||||||
table.insert(items, entry)
|
table.insert(items, entry)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -175,6 +175,10 @@ function Craft.craftRecipe(recipe, count, inventoryAdapter, origItem)
|
|||||||
--end
|
--end
|
||||||
|
|
||||||
for _, request in pairs(origItem.ingredients) do
|
for _, request in pairs(origItem.ingredients) do
|
||||||
|
if request.pending then
|
||||||
|
debug('??')
|
||||||
|
debug(request)
|
||||||
|
end
|
||||||
if request.crafted >= request.count then
|
if request.crafted >= request.count then
|
||||||
request.status = nil
|
request.status = nil
|
||||||
request.statusCode = Craft.STATUS_SUCCESS
|
request.statusCode = Craft.STATUS_SUCCESS
|
||||||
|
|||||||
@@ -99,11 +99,15 @@ function craftTask:cycle()
|
|||||||
for _,key in pairs(Util.keys(context.craftingQueue)) do
|
for _,key in pairs(Util.keys(context.craftingQueue)) do
|
||||||
local item = context.craftingQueue[key]
|
local item = context.craftingQueue[key]
|
||||||
if item.count - item.crafted > 0 then
|
if item.count - item.crafted > 0 then
|
||||||
local recipe = Craft.recipes[key]
|
local recipe = Craft.findRecipe(key)
|
||||||
if recipe then
|
if recipe then
|
||||||
self:craft(recipe, item)
|
self:craft(recipe, item)
|
||||||
if item.eject and item.crafted >= item.count then
|
if item.eject and item.crafted >= item.count then
|
||||||
Milo:eject(item, item.count)
|
if type(item.eject) == 'boolean' then
|
||||||
|
Milo:eject(item, item.count)
|
||||||
|
else
|
||||||
|
item.eject(item.count, 0) -- unknown amount in system
|
||||||
|
end
|
||||||
end
|
end
|
||||||
elseif not context.controllerAdapter then
|
elseif not context.controllerAdapter then
|
||||||
item.status = '(no recipe)'
|
item.status = '(no recipe)'
|
||||||
|
|||||||
@@ -91,6 +91,8 @@ local listingPage = UI.Page {
|
|||||||
q = 'quit',
|
q = 'quit',
|
||||||
[ 'control-e' ] = 'eject',
|
[ 'control-e' ] = 'eject',
|
||||||
[ 'control-s' ] = 'eject_stack',
|
[ 'control-s' ] = 'eject_stack',
|
||||||
|
[ 'control-a' ] = 'eject_all',
|
||||||
|
|
||||||
[ 'control-1' ] = 'eject_1',
|
[ 'control-1' ] = 'eject_1',
|
||||||
[ 'control-2' ] = 'eject_1',
|
[ 'control-2' ] = 'eject_1',
|
||||||
[ 'control-3' ] = 'eject_1',
|
[ 'control-3' ] = 'eject_1',
|
||||||
@@ -144,9 +146,7 @@ function listingPage:eventHandler(event)
|
|||||||
local item = self.grid:getSelected()
|
local item = self.grid:getSelected()
|
||||||
if item then
|
if item then
|
||||||
queue(function()
|
queue(function()
|
||||||
Milo:eject(item, 1)
|
item.count = Milo:xxx(item, 1)
|
||||||
local updated = Milo:getItem(Milo:listItems(), item)
|
|
||||||
item.count = updated and updated.count or 0
|
|
||||||
self.grid:draw()
|
self.grid:draw()
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -154,7 +154,19 @@ function listingPage:eventHandler(event)
|
|||||||
elseif event.type == 'eject_stack' then
|
elseif event.type == 'eject_stack' then
|
||||||
local item = self.grid:getSelected()
|
local item = self.grid:getSelected()
|
||||||
if item then
|
if item then
|
||||||
queue(function() Milo:eject(item, itemDB:getMaxCount(item)) end)
|
queue(function()
|
||||||
|
item.count = Milo:xxx(item, itemDB:getMaxCount(item))
|
||||||
|
self.grid:draw()
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif event.type == 'eject_all' then
|
||||||
|
local item = self.grid:getSelected()
|
||||||
|
if item then
|
||||||
|
local updated = Milo:getItem(Milo:listItems(), item)
|
||||||
|
if updated then
|
||||||
|
queue(function() Milo:eject(item, updated.count) end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif event.type == 'machines' then
|
elseif event.type == 'machines' then
|
||||||
@@ -235,9 +247,23 @@ end
|
|||||||
function listingPage:enable()
|
function listingPage:enable()
|
||||||
self:refresh()
|
self:refresh()
|
||||||
self:setFocus(self.statusBar.filter)
|
self:setFocus(self.statusBar.filter)
|
||||||
|
self.timer = Event.onInterval(5, function()
|
||||||
|
for _,v in pairs(self.allItems) do
|
||||||
|
if not v.key then debug(v) error('') end
|
||||||
|
local c = context.storage.cache[v.key]
|
||||||
|
v.count = c and c.count or 0
|
||||||
|
end
|
||||||
|
self.grid:draw()
|
||||||
|
self:sync()
|
||||||
|
end)
|
||||||
UI.Page.enable(self)
|
UI.Page.enable(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function listingPage:disable()
|
||||||
|
Event.off(self.timer)
|
||||||
|
UI.Page.disable(self)
|
||||||
|
end
|
||||||
|
|
||||||
function listingPage:refresh()
|
function listingPage:refresh()
|
||||||
self.allItems = Milo:refreshItems()
|
self.allItems = Milo:refreshItems()
|
||||||
Milo:mergeResources(self.allItems)
|
Milo:mergeResources(self.allItems)
|
||||||
|
|||||||
@@ -59,17 +59,20 @@ debug('remote: ' .. data.request)
|
|||||||
Milo:clearGrid()
|
Milo:clearGrid()
|
||||||
|
|
||||||
elseif data.request == 'transfer' then
|
elseif data.request == 'transfer' then
|
||||||
local count = context.storage:export(
|
local count = Milo:provideItem(data.item, data.count, function(amount, currentCount)
|
||||||
context.localName,
|
amount = context.storage:export(
|
||||||
nil,
|
|
||||||
data.count,
|
|
||||||
data.item)
|
|
||||||
|
|
||||||
turtle.eachFilledSlot(function(slot)
|
|
||||||
manipulator.getInventory().pullItems(
|
|
||||||
context.localName,
|
context.localName,
|
||||||
slot.index,
|
nil,
|
||||||
slot.count)
|
amount,
|
||||||
|
data.item)
|
||||||
|
|
||||||
|
turtle.eachFilledSlot(function(slot)
|
||||||
|
manipulator.getInventory().pullItems(
|
||||||
|
context.localName,
|
||||||
|
slot.index,
|
||||||
|
slot.count)
|
||||||
|
end)
|
||||||
|
return currentCount - amount
|
||||||
end)
|
end)
|
||||||
|
|
||||||
socket:write({ count = count })
|
socket:write({ count = count })
|
||||||
|
|||||||
Reference in New Issue
Block a user