This commit is contained in:
kepler155c
2018-10-28 20:36:40 -04:00
parent 1c70085450
commit ca22b49aaf
7 changed files with 121 additions and 37 deletions

View File

@@ -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,7 +261,9 @@ 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()
while true do
os.sleep(1.5)
local neural = device.neuralInterface local neural = device.neuralInterface
if neural and neural.getInventory then if neural and neural.getInventory then
local item = neural.getInventory().getItem(options.slot.value) local item = neural.getInventory().getItem(options.slot.value)
@@ -265,6 +277,7 @@ if options.slot.value then
else else
debug('missing Introspection module') debug('missing Introspection module')
end end
end
end) end)
end end

View File

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

View File

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

View File

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

View File

@@ -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
if type(item.eject) == 'boolean' then
Milo:eject(item, item.count) 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)'

View File

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

View File

@@ -59,10 +59,11 @@ 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)
amount = context.storage:export(
context.localName, context.localName,
nil, nil,
data.count, amount,
data.item) data.item)
turtle.eachFilledSlot(function(slot) turtle.eachFilledSlot(function(slot)
@@ -71,6 +72,8 @@ debug('remote: ' .. data.request)
slot.index, slot.index,
slot.count) slot.count)
end) end)
return currentCount - amount
end)
socket:write({ count = count }) socket:write({ count = count })
end end