This commit is contained in:
kepler155c
2018-10-31 19:38:54 -04:00
parent 4d3a896d6b
commit 865d642e5c
16 changed files with 276 additions and 234 deletions

View File

@@ -51,18 +51,9 @@ end
local introspectionModule = Peripheral.get('plethora:introspection') or local introspectionModule = Peripheral.get('plethora:introspection') or
error('Introspection module not found') error('Introspection module not found')
local function loadResources()
local resources = Util.readTable(Milo.RESOURCE_FILE) or { }
for k,v in pairs(resources) do
Util.merge(v, itemDB:splitKey(k))
end
return resources
end
local context = { local context = {
config = config, config = config,
resources = loadResources(), resources = Util.readTable(Milo.RESOURCE_FILE) or { },
craftingQueue = { }, craftingQueue = { },

View File

@@ -2,12 +2,13 @@ _G.requireInjector(_ENV)
local Event = require('event') local Event = require('event')
local Socket = require('socket') local Socket = require('socket')
local sync = require('sync') local sync = require('sync').sync
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
local colors = _G.colors local colors = _G.colors
local device = _G.device local device = _G.device
local os = _G.os
local socket local socket
local SHIELD_SLOT = 2 local SHIELD_SLOT = 2
@@ -139,7 +140,6 @@ function page:sendRequest(data)
local response local response
sync(self, function() sync(self, function()
self:sync()
local msg local msg
for _ = 1, 2 do for _ = 1, 2 do
if not socket or not socket.connected then if not socket or not socket.connected then
@@ -154,6 +154,10 @@ function page:sendRequest(data)
if socket:write(data) then if socket:write(data) then
response = socket:read(2) response = socket:read(2)
if response then if response then
if response.msg then
self:setStatus(response.msg)
response = nil
end
Event.onTimeout(2, function() Event.onTimeout(2, function()
self:setStatus('') self:setStatus('')
end) end)
@@ -188,6 +192,20 @@ function page.grid:getDisplayValues(row)
return row return row
end end
function page:transfer(item, count)
local response = self:sendRequest({ request = 'transfer', item = item, count = count })
debug(response)
if response then
item.count = response.current - response.transferred
self.grid:draw()
if response.craft > 0 then
self:setStatus(response.craft .. ' crafting ...')
elseif response.craft + response.available < response.requested then
self:setStatus((response.craft + response.available) .. ' available ...')
end
end
end
function page:eventHandler(event) function page:eventHandler(event)
if event.type == 'quit' then if event.type == 'quit' then
UI:exitPullEvents() UI:exitPullEvents()
@@ -199,33 +217,21 @@ function page:eventHandler(event)
local item = self.grid:getSelected() local item = self.grid:getSelected()
if item then if item then
self:setStatus('requesting 1 ...') self:setStatus('requesting 1 ...')
local response = self:sendRequest({ request = 'transfer', item = item, count = 1 }) self:transfer(item, 1)
if response then
item.count = response.count
self.grid:draw()
end
end end
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
self:setStatus('requesting stack ...') self:setStatus('requesting stack ...')
local response = self:sendRequest({ request = 'transfer', item = item, count = 'stack' }) self:transfer(item, 'stack')
if response then
item.count = response.count
self.grid:draw()
end
end end
elseif event.type == 'eject_all' then elseif event.type == 'eject_all' then
local item = self.grid:getSelected() local item = self.grid:getSelected()
if item then if item then
self:setStatus('requesting all ...') self:setStatus('requesting all ...')
local response = self:sendRequest({ request = 'transfer', item = item, count = 'all' }) self:transfer(item, 'all')
if response then
item.count = response.count
self.grid:draw()
end
end end
elseif event.type == 'eject_specified' then elseif event.type == 'eject_specified' then
@@ -235,9 +241,7 @@ function page:eventHandler(event)
self.statusBar.amount:reset() self.statusBar.amount:reset()
self:setFocus(self.statusBar.filter) self:setFocus(self.statusBar.filter)
self:setStatus('requesting ' .. count .. ' ...') self:setStatus('requesting ' .. count .. ' ...')
local response = self:sendRequest({ request = 'transfer', item = item, count = count }) self:transfer(item, count)
item.count = response.count
self.grid:draw()
else else
self:setStatus('nope ...') self:setStatus('nope ...')
end end
@@ -299,50 +303,43 @@ function page:applyFilter()
self.grid:setValues(t) self.grid:setValues(t)
end end
_G.p4 = Event if options.slot.value or options.shield.value then
local inv = 'getInventory'
local slotNo = options.slot.value
local slotValue = options.slot.value
if options.shield.value then
slotNo = SHIELD_SLOT
slotValue = 'shield'
inv = 'getEquipment'
end
_debug(options.slot)
if options.slot.value then
_debug('Transfer items initialized')
Event.addRoutine(function() Event.addRoutine(function()
while true do while true do
_debug('sleeping')
os.sleep(1.5) os.sleep(1.5)
local neural = device.neuralInterface local neural = device.neuralInterface
_debug(neural) if not neural or not neural[inv] then
if neural and neural.getInventory then
local item = neural.getInventory().getItem(options.slot.value)
if item then
_debug('depositing')
page:sendRequest({ request = 'deposit', slot = options.slot.value })
-- local item =
-- TODO: update count for this one item
-- page.grid:draw() page:sync()
else
_debug('empty')
end
else
_debug('missing Introspection module') _debug('missing Introspection module')
end end
end
end)
end
if options.shield.value then local method = neural and neural[inv]
_debug('Transfer items initialized') local item = method and method().getItemMeta(slotNo)
Event.onInterval(2, function()
local neural = device.neuralInterface
if neural and neural.getEquipment then
local item = neural.getEquipment().getItem(SHIELD_SLOT)
if item then if item then
_debug('depositing') _debug('depositing')
page:sendRequest({ request = 'deposit', slot = 'shield' }) local response = page:sendRequest({
-- local item = request = 'deposit',
-- TODO: update count for this one item slot = slotValue,
-- page.grid:draw() page:sync() key = table.concat({ item.name, item.damage, item.nbtHash }, ':')
})
if response then
local ritem = page.items[response.key]
if ritem then
ritem.count = response.current
end
page.grid:draw()
page:sync()
end
end end
else
_debug('missing Introspection module')
end end
end) end)
end end

View File

@@ -64,6 +64,10 @@ function Milo:uniqueKey(item)
return table.concat({ item.name, item.damage, item.nbtHash }, ':') return table.concat({ item.name, item.damage, item.nbtHash }, ':')
end end
function Milo:splitKey(key)
return itemDB:splitKey(key)
end
function Milo:resetCraftingStatus() function Milo:resetCraftingStatus()
self.context.storage.activity = { } self.context.storage.activity = { }
@@ -84,6 +88,10 @@ function Milo:showError(msg)
end end
function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash) function Milo:getItem(items, inItem, ignoreDamage, ignoreNbtHash)
if not ignoreDamage and not ignoreNbtHash then
return items[inItem.key or self:uniqueKey(inItem)]
end
for _,item in pairs(items) do for _,item in pairs(items) do
if item.name == inItem.name and if item.name == inItem.name and
(ignoreDamage or item.damage == inItem.damage) and (ignoreDamage or item.damage == inItem.damage) and
@@ -96,10 +104,9 @@ end
function Milo:getItemWithQty(res, ignoreDamage, ignoreNbtHash) function Milo:getItemWithQty(res, ignoreDamage, ignoreNbtHash)
local items = self:listItems() local items = self:listItems()
local item = self:getItem(items, res, ignoreDamage, ignoreNbtHash) local item = self:getItem(items, res, ignoreDamage, ignoreNbtHash)
local count = 0
if item and (ignoreDamage or ignoreNbtHash) then if item and (ignoreDamage or ignoreNbtHash) then
local count = 0
for _,v in pairs(items) do for _,v in pairs(items) do
if item.name == v.name and if item.name == v.name and
(ignoreDamage or item.damage == v.damage) and (ignoreDamage or item.damage == v.damage) and
@@ -107,10 +114,11 @@ function Milo:getItemWithQty(res, ignoreDamage, ignoreNbtHash)
count = count + v.count count = count + v.count
end end
end end
item.count = count elseif item then
count = item.count
end end
return item return item, count
end end
function Milo:getMatches(items, item, ignoreDamage, ignoreNbtHash) function Milo:getMatches(items, item, ignoreDamage, ignoreNbtHash)
@@ -170,21 +178,29 @@ function Milo:getTurtleInventory()
return list return list
end end
function Milo:xxx(item, count) function Milo:craftAndEject(item, count)
return self:provideItem(item, count, function(providable, currentCount) local provided = self:provideItem(item, count, function(amount)
-- return the current amount in the system -- eject rest when finished crafted
return currentCount - self:eject(item, providable) return self:eject(item, amount)
end) end)
-- eject what we currently have
return item.count - self:eject(item, provided.available)
end end
function Milo:provideItem(item, count, callback) function Milo:provideItem(item, count, callback)
local current = Milo:getItem(Milo:listItems(), item) or { count = 0 }
if count <= 0 then if count <= 0 then
return 0 return {
requested = 0,
craft = 0,
available = 0,
current = current.count,
}
end end
local current = Milo:getItem(Milo:listItems(), item) or { count = 0 }
local toCraft = count - math.min(current.count, count) local toCraft = count - math.min(current.count, count)
if toCraft > 0 then if toCraft > 0 then
local recipe = Craft.findRecipe(self:uniqueKey(item)) local recipe = Craft.findRecipe(self:uniqueKey(item))
if not recipe then if not recipe then
@@ -195,18 +211,20 @@ function Milo:provideItem(item, count, callback)
end end
end end
if toCraft == 0 then if toCraft > 0 then
return callback(math.min(count, current.count), current.count) item = Util.shallowCopy(item)
-- return current.count - self:eject(item, math.min(count, current.count)) item.count = toCraft
item.eject = callback
self:requestCrafting(item)
item.crafted = 0
end end
item = Util.shallowCopy(item) return {
item.count = current.count + toCraft requested = count,
item.eject = callback craft = toCraft,
self:requestCrafting(item) available = math.min(count, current.count),
item.crafted = current.count current = current.count,
}
return current.count
end end
function Milo:eject(item, count) function Milo:eject(item, count)
@@ -230,36 +248,43 @@ function Milo:saveMachineRecipe(recipe, result, machine)
end end
function Milo:mergeResources(t) function Milo:mergeResources(t)
for _,v in pairs(self.context.resources) do t = Util.shallowCopy(t)
local item = self:getItem(t, v)
for k,v in pairs(self.context.resources) do
local key = itemDB:splitKey(k)
local item = self:getItem(t, key)
if item then if item then
Util.merge(item, v) item = Util.shallowCopy(item)
else else
item = Util.shallowCopy(v) item = key
item.count = 0 item.count = 0
item.key = self:uniqueKey(v) item.key = k
-- table.insert(t, item)
t[item.key] = item
end end
Util.merge(item, v)
item.resource = v
t[item.key] = item
end end
for k in pairs(Craft.recipes) do for k in pairs(Craft.recipes) do
local v = itemDB:splitKey(k) local v = itemDB:splitKey(k)
local item = self:getItem(t, v) local item = self:getItem(t, v)
if not item then if not item then
item = Util.shallowCopy(v) item = v
item.count = 0 item.count = 0
item.key = self:uniqueKey(v) item.key = k
t[item.key] = item else
-- table.insert(t, item) item = Util.shallowCopy(item)
end end
t[item.key] = item
item.has_recipe = true item.has_recipe = true
end end
for key in pairs(Craft.machineLookup) do for key in pairs(Craft.machineLookup) do
local item = t[key] local item = t[key]
if item then if item then
item = Util.shallowCopy(item)
item.is_craftable = true item.is_craftable = true
t[item.key] = item
end end
end end
@@ -269,38 +294,17 @@ function Milo:mergeResources(t)
end end
v.lname = v.displayName:lower() v.lname = v.displayName:lower()
end end
return t
end end
function Milo:saveResources() function Milo:saveResources()
local t = { } Util.writeTable(self.RESOURCE_FILE, self.context.resources)
for k,v in pairs(self.context.resources) do
v = Util.shallowCopy(v)
local keys = Util.transpose({ 'auto', 'low', 'limit',
'ignoreDamage', 'ignoreNbtHash',
'rsControl', 'rsDevice', 'rsSide' })
for _,key in pairs(Util.keys(v)) do
if not keys[key] then
v[key] = nil
end
end
if not Util.empty(v) then
t[k] = v
end
end
Util.writeTable(self.RESOURCE_FILE, t)
end end
-- Return a list of everything in the system -- Return a list of everything in the system
function Milo:listItems() function Milo:listItems(forceRefresh)
return self.context.storage:listItems() return forceRefresh and self.context.storage:refresh() or self.context.storage:listItems()
end
-- force a full rescan of chests
function Milo:refreshItems()
return self.context.storage:refresh()
end end
return Milo return Milo

View File

@@ -18,6 +18,7 @@ listCount = 0,
storageOnline = true, storageOnline = true,
hits = 0, hits = 0,
misses = 0, misses = 0,
lastRefresh = os.clock(),
} }
Util.merge(self, defaults) Util.merge(self, defaults)
Util.merge(self, args) Util.merge(self, args)
@@ -136,6 +137,7 @@ end
function Storage:refresh(throttle) function Storage:refresh(throttle)
self.dirty = true self.dirty = true
self.lastRefresh = os.clock()
_debug('STORAGE: Forcing full refresh') _debug('STORAGE: Forcing full refresh')
for _, adapter in self:onlineAdapters() do for _, adapter in self:onlineAdapters() do
adapter.dirty = true adapter.dirty = true
@@ -266,7 +268,6 @@ function Storage:insert(slot, qty, toSlot, item, source)
local function insert(adapter) local function insert(adapter)
local amount = adapter:insert(slot, qty, toSlot, source or self.localName) local amount = adapter:insert(slot, qty, toSlot, source or self.localName)
if amount > 0 then if amount > 0 then
-- TODO: change debug to _debug
_debug('INS: %s(%d): %s[%d] -> %s', _debug('INS: %s(%d): %s[%d] -> %s',
item.name, amount, item.name, amount,
source or self.localName, slot, adapter.name) source or self.localName, slot, adapter.name)

View File

@@ -2,6 +2,7 @@ local Config = require('config')
local Event = require('event') local Event = require('event')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Milo = require('milo') local Milo = require('milo')
local sync = require('sync')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
@@ -11,8 +12,6 @@ local turtle = _G.turtle
local context = Milo:getContext() local context = Milo:getContext()
-- TODO: no blacklist for export
local function saveConfig() local function saveConfig()
local t = { } local t = { }
for k,v in pairs(context.config.remoteDefaults) do for k,v in pairs(context.config.remoteDefaults) do
@@ -272,11 +271,13 @@ function machineWizard.filter:show(entry, callback, whitelistOnly)
self:setFocus(self.form.scan) self:setFocus(self.form.scan)
Milo:pauseCrafting() Milo:pauseCrafting()
sync.lock(turtle)
end end
function machineWizard.filter:hide() function machineWizard.filter:hide()
UI.SlideOut.hide(self) UI.SlideOut.hide(self)
Milo:resumeCrafting() Milo:resumeCrafting()
sync.release(turtle)
end end
function machineWizard.filter:resetGrid() function machineWizard.filter:resetGrid()

View File

@@ -1,9 +1,11 @@
local Craft = require('turtle.craft') local Craft = require('turtle.craft')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Milo = require('milo') local Milo = require('milo')
local sync = require('sync').sync
local Util = require('util') local Util = require('util')
local context = Milo:getContext() local context = Milo:getContext()
local turtle = _G.turtle
local craftTask = { local craftTask = {
name = 'crafting', name = 'crafting',
@@ -101,12 +103,14 @@ function craftTask:cycle()
if item.count - item.crafted > 0 then if item.count - item.crafted > 0 then
local recipe = Craft.findRecipe(key) local recipe = Craft.findRecipe(key)
if recipe then if recipe then
self:craft(recipe, item) sync(turtle, function()
self:craft(recipe, item)
end)
if item.eject and item.crafted >= item.count then if item.eject and item.crafted >= item.count then
if type(item.eject) == 'boolean' then if type(item.eject) == 'boolean' then
Milo:eject(item, item.count) Milo:eject(item, item.count)
else else
item.eject(item.count, 0) -- unknown amount in system item.eject(item.count) -- invoke callback
end end
end end
elseif not context.controllerAdapter then elseif not context.controllerAdapter then

View File

@@ -20,11 +20,13 @@ local itemPage = UI.Page {
x = 1, y = 2, height = 10, ex = -1, x = 1, y = 2, height = 10, ex = -1,
[1] = UI.TextEntry { [1] = UI.TextEntry {
width = 7, width = 7,
formLabel = 'Min', formKey = 'low', help = 'Craft if below min' formLabel = 'Min', formKey = 'low', help = 'Craft if below min',
validate = 'numeric',
}, },
[2] = UI.TextEntry { [2] = UI.TextEntry {
width = 7, width = 7,
formLabel = 'Max', formKey = 'limit', help = 'Eject if above max' formLabel = 'Max', formKey = 'limit', help = 'Send to trash if above max',
validate = 'numeric',
}, },
--[[ --[[
[3] = UI.Chooser { [3] = UI.Chooser {
@@ -40,11 +42,11 @@ local itemPage = UI.Page {
]] ]]
[4] = UI.Checkbox { [4] = UI.Checkbox {
formLabel = 'Ignore Dmg', formKey = 'ignoreDamage', formLabel = 'Ignore Dmg', formKey = 'ignoreDamage',
help = 'Ignore damage of item' help = 'Ignore damage of item',
}, },
[5] = UI.Checkbox { [5] = UI.Checkbox {
formLabel = 'Ignore NBT', formKey = 'ignoreNbtHash', formLabel = 'Ignore NBT', formKey = 'ignoreNbtHash',
help = 'Ignore NBT of item' help = 'Ignore NBT of item',
}, },
[6] = UI.Button { [6] = UI.Button {
x = 2, y = -2, width = 10, x = 2, y = -2, width = 10,
@@ -135,13 +137,15 @@ local itemPage = UI.Page {
event = 'hide_info', event = 'hide_info',
}, },
}, },
statusBar = UI.StatusBar { } statusBar = UI.StatusBar { },
notification = UI.Notification { },
} }
function itemPage:enable(item) function itemPage:enable(item)
self.item = Util.shallowCopy(item) self.item = Util.shallowCopy(item)
self.res = item.resource or { }
self.form:setValues(self.item) self.form:setValues(self.res)
self.titleBar.title = item.displayName or item.name self.titleBar.title = item.displayName or item.name
UI.Page.enable(self) UI.Page.enable(self)
@@ -232,47 +236,46 @@ function itemPage:eventHandler(event)
elseif event.type == 'hide_info' then elseif event.type == 'hide_info' then
self.info:hide() self.info:hide()
elseif event.type == 'form_invalid' then
self.notification:error(event.message)
elseif event.type == 'focus_change' then elseif event.type == 'focus_change' then
self.statusBar:setStatus(event.focused.help) self.statusBar:setStatus(event.focused.help)
self.statusBar:draw() self.statusBar:draw()
elseif event.type == 'form_complete' then elseif event.type == 'form_complete' then
local values = self.form.values local item = self.item
local originalKey = Milo:uniqueKey(self.item)
local filtered = Util.shallowCopy(values) if self.form:save() then
filtered.low = tonumber(filtered.low) Util.prune(self.res, function(v)
filtered.limit = tonumber(filtered.limit) if type(v) == 'boolean' then
return v
elseif type(v) == 'string' then
return #v > 0
end
return true
end)
if filtered.auto ~= true then local newKey = {
filtered.auto = nil name = item.name,
damage = self.res.ignoreDamage and 0 or item.damage,
nbtHash = not self.res.ignoreNbtHash and item.nbtHash or nil,
}
for k,v in pairs(context.resources) do
if v == self.res then
context.resources[k] = nil
break
end
end
if not Util.empty(self.res) then
context.resources[Milo:uniqueKey(newKey)] = self.res
end
Milo:saveResources()
UI:setPreviousPage()
end end
if filtered.rsControl ~= true then
filtered.rsControl = nil
filtered.rsSide = nil
filtered.rsDevice = nil
end
if filtered.ignoreDamage == true then
filtered.damage = 0
else
filtered.ignoreDamage = nil
end
if filtered.ignoreNbtHash == true then
filtered.nbtHash = nil
else
filtered.ignoreNbtHash = nil
end
context.resources[originalKey] = nil
context.resources[Milo:uniqueKey(filtered)] = filtered
filtered.count = nil
Milo:saveResources()
-- TODO: min not updated upon save until refresh
UI:setPreviousPage()
else else
return UI.Page.eventHandler(self, event) return UI.Page.eventHandler(self, event)
end end

View File

@@ -11,6 +11,8 @@ local context = Milo:getContext()
local mon = Peripheral.lookup(context.config.monitor) or local mon = Peripheral.lookup(context.config.monitor) or
error('Monitor is not attached') error('Monitor is not attached')
-- TODO: some way to cancel a job
local jobMonitor = UI.Page { local jobMonitor = UI.Page {
parent = UI.Device { parent = UI.Device {
device = mon, device = mon,

View File

@@ -1,7 +1,9 @@
local Milo = require('milo') local Milo = require('milo')
local sync = require('sync')
local UI = require('ui') local UI = require('ui')
local context = Milo:getContext() local context = Milo:getContext()
local turtle = _G.turtle
local learnPage = UI.Dialog { local learnPage = UI.Dialog {
height = 6, width = UI.term.width - 6, height = 6, width = UI.term.width - 6,
@@ -34,6 +36,9 @@ function learnPage:enable()
Milo:getState('learnType') or Milo:getState('learnType') or
self.chooser.choices[1].value self.chooser.choices[1].value
Milo:pauseCrafting()
sync.lock(turtle)
self:focusFirst() self:focusFirst()
UI.Dialog.enable(self) UI.Dialog.enable(self)
end end
@@ -44,6 +49,8 @@ end
function learnPage:eventHandler(event) function learnPage:eventHandler(event)
if event.type == 'cancel' then if event.type == 'cancel' then
sync.release(turtle)
Milo:resumeCrafting()
UI:setPreviousPage() UI:setPreviousPage()
elseif event.type == 'accept' then elseif event.type == 'accept' then

View File

@@ -9,14 +9,15 @@ function LimitTask:cycle(context)
local trashcan = context.storage:filterActive('trashcan')() local trashcan = context.storage:filterActive('trashcan')()
if trashcan then if trashcan then
for _,res in pairs(context.resources) do for k,res in pairs(context.resources) do
if res.limit then if res.limit then
local item = Milo:getItemWithQty(res, res.ignoreDamage, res.ignoreNbtHash) -- TODO: change to export method of finding items (maybe)
if item and item.count > res.limit then local item, count = Milo:getItemWithQty(Milo:splitKey(k), res.ignoreDamage, res.ignoreNbtHash)
if item and count > res.limit then
context.storage:export( context.storage:export(
trashcan.name, trashcan.name,
nil, nil,
item.count - res.limit, count - res.limit,
item) item)
end end
end end

View File

@@ -118,7 +118,7 @@ end
function listingPage.grid:getDisplayValues(row) function listingPage.grid:getDisplayValues(row)
row = Util.shallowCopy(row) row = Util.shallowCopy(row)
row.count = row.count > 0 and Util.toBytes(row.count) or '' row.count = row.count > 0 and Util.toBytes(row.count)
if row.low then if row.low then
row.low = Util.toBytes(row.low) row.low = Util.toBytes(row.low)
end end
@@ -139,7 +139,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()
item.count = Milo:xxx(item, 1) item.count = Milo:craftAndEject(item, 1)
self.grid:draw() self.grid:draw()
end) end)
end end
@@ -148,7 +148,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()
item.count = Milo:xxx(item, itemDB:getMaxCount(item)) item.count = Milo:craftAndEject(item, itemDB:getMaxCount(item))
self.grid:draw() self.grid:draw()
end) end)
end end
@@ -257,12 +257,7 @@ function listingPage:disable()
end end
function listingPage:refresh(force) function listingPage:refresh(force)
if force then self.allItems = Milo:mergeResources(Milo:listItems(force))
self.allItems = Milo:refreshItems()
else
self.allItems = Milo:listItems()
end
Milo:mergeResources(self.allItems)
self:applyFilter() self:applyFilter()
end end

View File

@@ -1,5 +1,6 @@
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Milo = require('milo') local Milo = require('milo')
local sync = require('sync')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
@@ -133,13 +134,9 @@ function pages.confirmation:validate()
return true return true
end end
function machineLearnWizard:enable()
Milo:pauseCrafting()
UI.Page.enable(self)
end
function machineLearnWizard:disable() function machineLearnWizard:disable()
Milo:resumeCrafting() Milo:resumeCrafting()
sync.release(turtle)
UI.Page.disable(self) UI.Page.disable(self)
end end

View File

@@ -0,0 +1,16 @@
local Milo = require('milo')
-- Do a full scan of inventories every minute
local RefreshTask = {
name = 'refresher',
priority = 0,
}
function RefreshTask:cycle(context)
if os.clock() - context.storage.lastRefresh > 60 then
context.storage:refresh()
end
end
Milo:registerTask(RefreshTask)

View File

@@ -2,6 +2,7 @@ local Event = require('event')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Milo = require('milo') local Milo = require('milo')
local Socket = require('socket') local Socket = require('socket')
local Sync = require('sync')
local device = _G.device local device = _G.device
local turtle = _G.turtle local turtle = _G.turtle
@@ -19,7 +20,7 @@ local function getManipulatorForUser(user)
end end
local function client(socket) local function client(socket)
_debug('connection from ' .. socket.dhost) _G._debug('connection from ' .. socket.dhost)
local user = socket:read(2) local user = socket:read(2)
if not user then if not user then
@@ -36,28 +37,41 @@ local function client(socket)
if not data then if not data then
break break
end end
_debug('remote: ' .. data.request)
if data.request == 'list' then if data.request == 'list' then
local items = Milo:listItems() local items = Milo:mergeResources(Milo:listItems())
Milo:mergeResources(items)
socket:write(items) socket:write(items)
elseif data.request == 'deposit' then elseif data.request == 'deposit' then
local count if Sync.isLocked(turtle) then
socket:write({ msg = '' })
if data.slot == 'shield' then
count = manipulator.getEquipment().pushItems(
context.localName,
SHIELD_SLOT,
64)
else else
count = manipulator.getInventory().pushItems( local count
context.localName,
data.slot, Sync.sync(turtle, function()
64) if data.slot == 'shield' then
count = manipulator.getEquipment().pushItems(
context.localName,
SHIELD_SLOT,
64)
else
count = manipulator.getInventory().pushItems(
context.localName,
data.slot,
64)
end
Milo:clearGrid()
end)
local list = Milo:listItems()
local current = list[data.key] and list[data.key].count or 0
socket:write({
key = data.key,
count = count,
current = current + count,
})
end end
socket:write({ count = count })
Milo:clearGrid()
elseif data.request == 'transfer' then elseif data.request == 'transfer' then
local count = data.count local count = data.count
@@ -69,32 +83,42 @@ _debug('remote: ' .. data.request)
count = item and item.count or 0 count = item and item.count or 0
end end
local provided = Milo:provideItem(data.item, count, function(amount, currentCount) local function transfer(amount)
amount = context.storage:export( Sync.sync(turtle, function()
context.localName, amount = context.storage:export(
nil,
amount,
data.item)
turtle.eachFilledSlot(function(slot)
manipulator.getInventory().pullItems(
context.localName, context.localName,
slot.index, nil,
slot.count) amount,
end) data.item)
return currentCount - amount
end)
socket:write({ count = provided }) turtle.eachFilledSlot(function(slot)
manipulator.getInventory().pullItems(
context.localName,
slot.index,
slot.count)
end)
end)
return amount
end
if Sync.isLocked(turtle) then
socket:write({ msg = 'Turtle in use. please wait...' })
end
local provided = Milo:provideItem(data.item, count, transfer)
provided.transferred = provided.available > 0 and transfer(provided.available) or 0
socket:write(provided)
end end
until not socket.connected until not socket.connected
_debug('disconnected from ' .. socket.dhost) _G._debug('disconnected from ' .. socket.dhost)
end end
if device.wireless_modem then if device.wireless_modem then
Event.addRoutine(function() Event.addRoutine(function()
_debug('Milo: listening on port 4242') _G._debug('Milo: listening on port 4242')
while true do while true do
local socket = Socket.server(4242) local socket = Socket.server(4242)
Event.addRoutine(function() Event.addRoutine(function()

View File

@@ -7,27 +7,29 @@ local ReplenishTask = {
} }
function ReplenishTask:cycle(context) function ReplenishTask:cycle(context)
for _,res in pairs(context.resources) do for k,res in pairs(context.resources) do
if res.low then if res.low then
local item = Milo:getItemWithQty(res, res.ignoreDamage, res.ignoreNbtHash) local key = Milo:splitKey(k)
local item, count = Milo:getItemWithQty(key, res.ignoreDamage, res.ignoreNbtHash)
if not item then if not item then
item = { item = {
damage = res.damage, damage = key.damage,
nbtHash = res.nbtHash, nbtHash = key.nbtHash,
name = res.name, name = key.name,
displayName = itemDB:getName(res), displayName = itemDB:getName(key),
count = 0 count = 0
} }
end end
if item.count < res.low then if count < res.low then
if res.ignoreDamage then local nbtHash = item.nbtHash
item.damage = 0 if res.ignoreNbtHash then
nbtHash = nil
end end
Milo:requestCrafting({ Milo:requestCrafting({
damage = item.damage, damage = res.ignoreDamage and 0 or item.damage,
nbtHash = item.nbtHash, nbtHash = nbtHash,
count = res.low - item.count, count = res.low - count,
name = item.name, name = item.name,
displayName = item.displayName, displayName = item.displayName,
replenish = true, replenish = true,

View File

@@ -1,6 +1,7 @@
local Craft = require('turtle.craft') local Craft = require('turtle.craft')
local itemDB = require('itemDB') local itemDB = require('itemDB')
local Milo = require('milo') local Milo = require('milo')
local sync = require('sync')
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
@@ -122,13 +123,9 @@ local turtleLearnWizard = UI.Page {
notification = UI.Notification { }, notification = UI.Notification { },
} }
function turtleLearnWizard:enable()
Milo:pauseCrafting()
UI.Page.enable(self)
end
function turtleLearnWizard:disable() function turtleLearnWizard:disable()
Milo:resumeCrafting() Milo:resumeCrafting()
sync.release(turtle)
UI.Page.disable(self) UI.Page.disable(self)
end end