From fa43186d52b140a5145e4b5bcacda3cf6d167194 Mon Sep 17 00:00:00 2001 From: kepler155c Date: Fri, 23 Nov 2018 11:39:39 -0500 Subject: [PATCH] milo: cleanup --- core/etc/apps/opus-apps.db | 2 +- milo/apis/{turtle/craft.lua => craft2.lua} | 2 +- milo/apis/milo.lua | 2 +- milo/apis/storage.lua | 16 +- milo/core/listing.lua | 301 +++++++++++++++++++++ milo/core/machines.lua | 2 - milo/plugins/craftTask.lua | 8 +- milo/plugins/demandCraft.lua | 2 +- milo/plugins/item.lua | 31 +-- milo/plugins/jobMonitor.lua | 2 +- milo/plugins/listing.lua | 301 +-------------------- milo/plugins/potionImportTask.lua | 2 +- milo/plugins/turtleLearn.lua | 2 +- 13 files changed, 326 insertions(+), 347 deletions(-) rename milo/apis/{turtle/craft.lua => craft2.lua} (99%) create mode 100644 milo/core/listing.lua diff --git a/core/etc/apps/opus-apps.db b/core/etc/apps/opus-apps.db index 952d632..450301f 100644 --- a/core/etc/apps/opus-apps.db +++ b/core/etc/apps/opus-apps.db @@ -46,6 +46,7 @@ Needs work \030 \031 \030f\0317\130\030 \031 \030f\0317\130", run = "Appstore.lua", }, +--[[ [ "131260cbfbb0c821f8eae5e7c3c296c7aa4d50b9" ] = { title = "Music", category = "Apps", @@ -56,7 +57,6 @@ Needs work run = "usr/apps/Music.lua", requires = 'turtle', }, ---[[ [ "89307d419a2fe4fbb69af92b3d3af27b6ec14d3e" ] = { title = "Telnet", category = "Apps", diff --git a/milo/apis/turtle/craft.lua b/milo/apis/craft2.lua similarity index 99% rename from milo/apis/turtle/craft.lua rename to milo/apis/craft2.lua index 94dfdd3..be3cf84 100644 --- a/milo/apis/turtle/craft.lua +++ b/milo/apis/craft2.lua @@ -205,7 +205,7 @@ function Craft.craftRecipeInternal(recipe, count, storage, origItem) _G._debug({'eval', recipe.result, count }) - local maxCount = recipe.maxCount or math.floor(64 / recipe.count) + local maxCount = math.floor((recipe.maxCount or 64) / recipe.count) for key,icount in pairs(Craft.sumIngredients(recipe)) do local itemCount = Craft.getItemCount(origItem.ingredients, key) diff --git a/milo/apis/milo.lua b/milo/apis/milo.lua index b372501..20ccba0 100644 --- a/milo/apis/milo.lua +++ b/milo/apis/milo.lua @@ -1,5 +1,5 @@ local Config = require('config') -local Craft = require('turtle.craft') +local Craft = require('craft2') local itemDB = require('itemDB') local Util = require('util') diff --git a/milo/apis/storage.lua b/milo/apis/storage.lua index aa3c1f4..f0e8492 100644 --- a/milo/apis/storage.lua +++ b/milo/apis/storage.lua @@ -287,20 +287,6 @@ function Storage:export(target, slot, count, item) -- If no slot and full amount is not exported, then no need to check rest of adapters -- ... so should not reach here - ---[[ - -- not found - scan all others - for _, adapter in self:onlineAdapters() do - if not adapter.cache or not adapter.cache[key] then - provide(adapter) - if count <= 0 then - _G._debug('STORAGE: FOUND: %s - %d', key, count) - break - end - end - end ---]] - return total end @@ -368,7 +354,7 @@ function Storage:import(source, slot, count, item) if count <= 0 then return total end - if adapter.cache and adapter.cache[key] and not node.lock then + if not node.lock and adapter.cache and adapter.cache[key] then insert(adapter) end end diff --git a/milo/core/listing.lua b/milo/core/listing.lua new file mode 100644 index 0000000..4bdd4f4 --- /dev/null +++ b/milo/core/listing.lua @@ -0,0 +1,301 @@ +local Craft = require('craft2') +local itemDB = require('itemDB') +local Event = require('event') +local Milo = require('milo') +local UI = require('ui') +local Util = require('util') + +local colors = _G.colors +local context = Milo:getContext() +local displayMode = Milo:getState('displayMode') or 0 +local string = _G.string + +local displayModes = { + [0] = { text = 'A', help = 'Showing all items' }, + [1] = { text = 'I', help = 'Showing inventory items' }, + [2] = { text = 'C', help = 'Showing craftable items' }, +} + +local function filterItems(t, filter) + if filter or displayMode > 0 then + local r = { } + if filter then + filter = filter:lower() + end + for _,v in pairs(t) do + if not filter or string.find(v.lname, filter, 1, true) then + if not displayMode or + displayMode == 0 or + displayMode == 1 and v.count > 0 or + displayMode == 2 and v.has_recipe then + table.insert(r, v) + end + end + end + return r + end + return t +end + +local listingPage = UI.Page { + menuBar = UI.MenuBar { + buttons = { + { text = 'Learn', event = 'learn' }, + --{ text = 'Forget', event = 'forget' }, + { text = 'Craft', event = 'craft' }, + { text = 'Edit', event = 'details' }, + { text = 'Refresh', event = 'refresh', x = -12 }, + { + text = '\206', + x = -3, + dropdown = { + { text = 'Setup', event = 'network' }, + UI.MenuBar.spacer, + { + text = 'Rescan storage', + event = 'rescan', + help = 'Rescan all inventories' + }, + }, + }, + }, + }, + grid = UI.Grid { + y = 2, ey = -2, + columns = { + { heading = ' Qty', key = 'count' , width = 4, justify = 'right' }, + { heading = 'Name', key = 'displayName' }, + { heading = 'Min', key = 'low' , width = 4 }, + { heading = 'Max', key = 'limit' , width = 4 }, + }, + sortColumn = 'displayName', + }, + statusBar = UI.StatusBar { + filter = UI.TextEntry { + x = 1, ex = -17, + limit = 50, + shadowText = 'filter', + shadowTextColor = colors.gray, + backgroundColor = colors.cyan, + backgroundFocusColor = colors.cyan, + accelerators = { + [ 'enter' ] = 'eject', + }, + }, + storageStatus = UI.Text { + x = -16, ex = -9, + textColor = colors.lime, + backgroundColor = colors.cyan, + value = '', + }, + amount = UI.TextEntry { + x = -8, ex = -4, + limit = 3, + shadowText = '1', + shadowTextColor = colors.gray, + backgroundColor = colors.black, + backgroundFocusColor = colors.black, + accelerators = { + [ 'enter' ] = 'eject_specified', + }, + help = 'Specify an amount to send', + }, + display = UI.Button { + x = -3, + event = 'toggle_display', + value = 0, + text = displayModes[displayMode].text, + help = displayModes[displayMode].help, + }, + }, + notification = UI.Notification(), + throttle = UI.Throttle { + textColor = colors.yellow, + borderColor = colors.gray, + }, + accelerators = { + r = 'refresh', + [ 'control-r' ] = 'refresh', + + [ 'control-e' ] = 'eject', + [ 'control-s' ] = 'eject_stack', + [ 'control-a' ] = 'eject_all', + + [ 'control-m' ] = 'network', + + q = 'quit', + }, +} + +function listingPage.statusBar:draw() + return UI.Window.draw(self) +end + +function listingPage.grid:getRowTextColor(row, selected) + if row.is_craftable then + return colors.yellow + end + if row.has_recipe then + return colors.cyan + end + return UI.Grid:getRowTextColor(row, selected) +end + +function listingPage.grid:getDisplayValues(row) + row = Util.shallowCopy(row) + row.count = row.count > 0 and Util.toBytes(row.count) + if row.low then + row.low = Util.toBytes(row.low) + end + if row.limit then + row.limit = Util.toBytes(row.limit) + end + return row +end + +function listingPage:eventHandler(event) + if event.type == 'quit' then + UI:exitPullEvents() + + elseif event.type == 'eject' or event.type == 'grid_select' then + local item = self.grid:getSelected() + if item then + item.count = Milo:craftAndEject(item, 1) + self.grid:draw() + end + + elseif event.type == 'eject_stack' then + local item = self.grid:getSelected() + if item then + item.count = Milo:craftAndEject(item, itemDB:getMaxCount(item)) + self.grid:draw() + 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 + Milo:craftAndEject(item, updated.count) + end + end + + elseif event.type == 'eject_specified' then + local item = self.grid:getSelected() + local count = tonumber(self.statusBar.amount.value) + if item and count then + self.statusBar.amount:reset() + self:setFocus(self.statusBar.filter) + Milo:craftAndEject(item, count) + end + + elseif event.type == 'network' then + UI:setPage('network') + + elseif event.type == 'details' or event.type == 'grid_select_right' then + local item = self.grid:getSelected() + if item then + UI:setPage('item', item) + end + + elseif event.type == 'refresh' then + self:refresh() + self.grid:draw() + self:setFocus(self.statusBar.filter) + + elseif event.type == 'rescan' then + self:refresh(true) + self.grid:draw() + self:setFocus(self.statusBar.filter) + + elseif event.type == 'toggle_display' then + displayMode = (displayMode + 1) % 3 + Util.merge(event.button, displayModes[displayMode]) + event.button:draw() + self:applyFilter() + self.grid:draw() + Milo:setState('displayMode', displayMode) + + elseif event.type == 'learn' then + UI:setPage('learn') + + elseif event.type == 'craft' then + local item = self.grid:getSelected() + if item then + if Craft.findRecipe(item) then -- or item.is_craftable then + UI:setPage('craft', self.grid:getSelected()) + else + self.notification:error('No recipe defined') + end + end + + elseif event.type == 'text_change' and event.element == self.statusBar.filter then + self.filter = event.text + if #self.filter == 0 then + self.filter = nil + end + self:applyFilter() + self.grid:draw() + self.statusBar.filter:focus() + + else + UI.Page.eventHandler(self, event) + end + return true +end + +function listingPage:enable() + Event.onTimeout(0, function() + self:refresh() + self:draw() + self:sync() + + self.timer = Event.onInterval(3, function() + for _,v in pairs(self.allItems) do + local c = context.storage.cache[v.key] + v.count = c and c.count or 0 + end + self.grid:draw() + self:sync() + end) + + local function updateStatus() + self.statusBar.storageStatus.value = + context.storage:isOnline() and '' or 'offline' + self.statusBar.storageStatus.textColor = + context.storage:isOnline() and colors.lime or colors.red + end + updateStatus() + + self.handler = Event.on({ 'storage_offline', 'storage_online' }, function() + updateStatus() + self.statusBar.storageStatus:draw() + self:sync() + end) + end) + + self:setFocus(self.statusBar.filter) + UI.Page.enable(self) +end + +function listingPage:disable() + Event.off(self.timer) + Event.off(self.handler) + UI.Page.disable(self) +end + +function listingPage:refresh(force) + local throttle = function() self.throttle:update() end + + self.throttle:enable() + self.allItems = Milo:mergeResources(Milo:listItems(force, throttle)) + self:applyFilter() + self.throttle:disable() +end + +function listingPage:applyFilter() + local t = filterItems(self.allItems, self.filter) + self.grid:setValues(t) +end + +UI:addPage('listing', listingPage) diff --git a/milo/core/machines.lua b/milo/core/machines.lua index 261317b..68ce4f7 100644 --- a/milo/core/machines.lua +++ b/milo/core/machines.lua @@ -442,8 +442,6 @@ function nodeWizard:enable(node) self.node.adapter = adapter node.adapter = adapter -_G._p3 = self.node -- TODO: remove - debugging - local choices = { { name = 'Ignore', value = 'ignore', '' }, { name = 'Hidden', value = 'hidden', help = 'Do not show in list' }, diff --git a/milo/plugins/craftTask.lua b/milo/plugins/craftTask.lua index 2ccb73d..a00b606 100644 --- a/milo/plugins/craftTask.lua +++ b/milo/plugins/craftTask.lua @@ -1,4 +1,4 @@ -local Craft = require('turtle.craft') +local Craft = require('craft2') local Milo = require('milo') local sync = require('sync').sync local Util = require('util') @@ -36,6 +36,7 @@ function craftTask:craft(recipe, item) item.ingredients[recipe.result].total = item.count item.ingredients[recipe.result].crafted = item.crafted +--[[ _G._p2 = item if not item.history then item.history = { } @@ -46,13 +47,14 @@ for k,v in pairs(item.ingredients) do t.history.input[k] = Util.shallowCopy(v) end table.insert(item.history, t) - +]] Craft.craftRecipe(recipe, item.requested - item.crafted, context.storage, item) +--[[ for k,v in pairs(item.ingredients) do t.history.output[k] = Util.shallowCopy(v) end - +]] end function craftTask:cycle() diff --git a/milo/plugins/demandCraft.lua b/milo/plugins/demandCraft.lua index be09056..882944d 100644 --- a/milo/plugins/demandCraft.lua +++ b/milo/plugins/demandCraft.lua @@ -1,4 +1,4 @@ -local Craft = require('turtle.craft') +local Craft = require('craft2') local itemDB = require('itemDB') local Milo = require('milo') local UI = require('ui') diff --git a/milo/plugins/item.lua b/milo/plugins/item.lua index 24430b5..77c11dd 100644 --- a/milo/plugins/item.lua +++ b/milo/plugins/item.lua @@ -1,16 +1,14 @@ -local Ansi = require('ansi') -local Craft = require('turtle.craft') -local Milo = require('milo') -local UI = require('ui') -local Util = require('util') +local Ansi = require('ansi') +local Craft = require('craft2') +local Milo = require('milo') +local UI = require('ui') +local Util = require('util') local colors = _G.colors local device = _G.device local context = Milo:getContext() --- TODO: allow change of machine - local itemPage = UI.Page { titleBar = UI.TitleBar { title = 'Limit Resource', @@ -29,18 +27,6 @@ local itemPage = UI.Page { formLabel = 'Max', formKey = 'limit', help = 'Send to trash if above max', validate = 'numeric', }, ---[[ - [3] = UI.Chooser { - width = 7, - formLabel = 'Autocraft', formKey = 'auto', - nochoice = 'No', - choices = { - { name = 'Yes', value = true }, - { name = 'No', value = false }, - }, - help = 'Craft until out of ingredients' - }, -]] [4] = UI.Checkbox { formLabel = 'Ignore Dmg', formKey = 'ignoreDamage', help = 'Ignore damage of item', @@ -163,7 +149,12 @@ function itemPage:enable(item) end function itemPage.machines.grid:isRowValid(_, value) - return value.mtype == 'machine' + local ignores = Util.transpose({ 'storage', 'ignore', 'hidden' }) + if not ignores[value.mtype] then + local node = context.storage.nodes[value.name] +_debug(node) + return node and node.adapter and node.adapter.online and node.adapter.pushItems + end end function itemPage.machines.grid:getDisplayValues(row) diff --git a/milo/plugins/jobMonitor.lua b/milo/plugins/jobMonitor.lua index eb750d0..e555754 100644 --- a/milo/plugins/jobMonitor.lua +++ b/milo/plugins/jobMonitor.lua @@ -1,4 +1,4 @@ -local Craft = require('turtle.craft') +local Craft = require('craft2') local Event = require('event') local itemDB = require('itemDB') local Milo = require('milo') diff --git a/milo/plugins/listing.lua b/milo/plugins/listing.lua index f28d2b2..8a44d2f 100644 --- a/milo/plugins/listing.lua +++ b/milo/plugins/listing.lua @@ -1,300 +1 @@ -local Craft = require('turtle.craft') -local itemDB = require('itemDB') -local Event = require('event') -local Milo = require('milo') -local UI = require('ui') -local Util = require('util') - -local colors = _G.colors -local context = Milo:getContext() -local displayMode = Milo:getState('displayMode') or 0 - -local displayModes = { - [0] = { text = 'A', help = 'Showing all items' }, - [1] = { text = 'I', help = 'Showing inventory items' }, - [2] = { text = 'C', help = 'Showing craftable items' }, -} - -local function filterItems(t, filter) - if filter or displayMode > 0 then - local r = { } - if filter then - filter = filter:lower() - end - for _,v in pairs(t) do - if not filter or string.find(v.lname, filter, 1, true) then - if not displayMode or - displayMode == 0 or - displayMode == 1 and v.count > 0 or - displayMode == 2 and v.has_recipe then - table.insert(r, v) - end - end - end - return r - end - return t -end - -local listingPage = UI.Page { - menuBar = UI.MenuBar { - buttons = { - { text = 'Learn', event = 'learn' }, - --{ text = 'Forget', event = 'forget' }, - { text = 'Craft', event = 'craft' }, - { text = 'Edit', event = 'details' }, - { text = 'Refresh', event = 'refresh', x = -12 }, - { - text = '\206', - x = -3, - dropdown = { - { text = 'Setup', event = 'network' }, - UI.MenuBar.spacer, - { - text = 'Rescan storage', - event = 'rescan', - help = 'Rescan all inventories' - }, - }, - }, - }, - }, - grid = UI.Grid { - y = 2, ey = -2, - columns = { - { heading = ' Qty', key = 'count' , width = 4, justify = 'right' }, - { heading = 'Name', key = 'displayName' }, - { heading = 'Min', key = 'low' , width = 4 }, - { heading = 'Max', key = 'limit' , width = 4 }, - }, - sortColumn = 'displayName', - }, - statusBar = UI.StatusBar { - filter = UI.TextEntry { - x = 1, ex = -17, - limit = 50, - shadowText = 'filter', - shadowTextColor = colors.gray, - backgroundColor = colors.cyan, - backgroundFocusColor = colors.cyan, - accelerators = { - [ 'enter' ] = 'eject', - }, - }, - storageStatus = UI.Text { - x = -16, ex = -9, - textColor = colors.lime, - backgroundColor = colors.cyan, - value = '', - }, - amount = UI.TextEntry { - x = -8, ex = -4, - limit = 3, - shadowText = '1', - shadowTextColor = colors.gray, - backgroundColor = colors.black, - backgroundFocusColor = colors.black, - accelerators = { - [ 'enter' ] = 'eject_specified', - }, - help = 'Specify an amount to send', - }, - display = UI.Button { - x = -3, - event = 'toggle_display', - value = 0, - text = displayModes[displayMode].text, - help = displayModes[displayMode].help, - }, - }, - notification = UI.Notification(), - throttle = UI.Throttle { - textColor = colors.yellow, - borderColor = colors.gray, - }, - accelerators = { - r = 'refresh', - [ 'control-r' ] = 'refresh', - - [ 'control-e' ] = 'eject', - [ 'control-s' ] = 'eject_stack', - [ 'control-a' ] = 'eject_all', - - [ 'control-m' ] = 'network', - - q = 'quit', - }, -} - -function listingPage.statusBar:draw() - return UI.Window.draw(self) -end - -function listingPage.grid:getRowTextColor(row, selected) - if row.is_craftable then - return colors.yellow - end - if row.has_recipe then - return colors.cyan - end - return UI.Grid:getRowTextColor(row, selected) -end - -function listingPage.grid:getDisplayValues(row) - row = Util.shallowCopy(row) - row.count = row.count > 0 and Util.toBytes(row.count) - if row.low then - row.low = Util.toBytes(row.low) - end - if row.limit then - row.limit = Util.toBytes(row.limit) - end - return row -end - -function listingPage:eventHandler(event) - if event.type == 'quit' then - UI:exitPullEvents() - - elseif event.type == 'eject' or event.type == 'grid_select' then - local item = self.grid:getSelected() - if item then - item.count = Milo:craftAndEject(item, 1) - self.grid:draw() - end - - elseif event.type == 'eject_stack' then - local item = self.grid:getSelected() - if item then - item.count = Milo:craftAndEject(item, itemDB:getMaxCount(item)) - self.grid:draw() - 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 - Milo:craftAndEject(item, updated.count) - end - end - - elseif event.type == 'eject_specified' then - local item = self.grid:getSelected() - local count = tonumber(self.statusBar.amount.value) - if item and count then - self.statusBar.amount:reset() - self:setFocus(self.statusBar.filter) - Milo:craftAndEject(item, count) - end - - elseif event.type == 'network' then - UI:setPage('network') - - elseif event.type == 'details' or event.type == 'grid_select_right' then - local item = self.grid:getSelected() - if item then - UI:setPage('item', item) - end - - elseif event.type == 'refresh' then - self:refresh() - self.grid:draw() - self:setFocus(self.statusBar.filter) - - elseif event.type == 'rescan' then - self:refresh(true) - self.grid:draw() - self:setFocus(self.statusBar.filter) - - elseif event.type == 'toggle_display' then - displayMode = (displayMode + 1) % 3 - Util.merge(event.button, displayModes[displayMode]) - event.button:draw() - self:applyFilter() - self.grid:draw() - Milo:setState('displayMode', displayMode) - - elseif event.type == 'learn' then - UI:setPage('learn') - - elseif event.type == 'craft' then - local item = self.grid:getSelected() - if item then - if Craft.findRecipe(item) then -- or item.is_craftable then - UI:setPage('craft', self.grid:getSelected()) - else - self.notification:error('No recipe defined') - end - end - - elseif event.type == 'text_change' and event.element == self.statusBar.filter then - self.filter = event.text - if #self.filter == 0 then - self.filter = nil - end - self:applyFilter() - self.grid:draw() - self.statusBar.filter:focus() - - else - UI.Page.eventHandler(self, event) - end - return true -end - -function listingPage:enable() - Event.onTimeout(0, function() - self:refresh() - self:draw() - self:sync() - - self.timer = Event.onInterval(3, function() - for _,v in pairs(self.allItems) do - local c = context.storage.cache[v.key] - v.count = c and c.count or 0 - end - self.grid:draw() - self:sync() - end) - - local function updateStatus() - self.statusBar.storageStatus.value = - context.storage:isOnline() and '' or 'offline' - self.statusBar.storageStatus.textColor = - context.storage:isOnline() and colors.lime or colors.red - end - updateStatus() - - self.handler = Event.on({ 'storage_offline', 'storage_online' }, function() - updateStatus() - self.statusBar.storageStatus:draw() - self:sync() - end) - end) - - self:setFocus(self.statusBar.filter) - UI.Page.enable(self) -end - -function listingPage:disable() - Event.off(self.timer) - Event.off(self.handler) - UI.Page.disable(self) -end - -function listingPage:refresh(force) - local throttle = function() self.throttle:update() end - - self.throttle:enable() - self.allItems = Milo:mergeResources(Milo:listItems(force, throttle)) - self:applyFilter() - self.throttle:disable() -end - -function listingPage:applyFilter() - local t = filterItems(self.allItems, self.filter) - self.grid:setValues(t) -end - -UI:addPage('listing', listingPage) +--moved file diff --git a/milo/plugins/potionImportTask.lua b/milo/plugins/potionImportTask.lua index 6f5eff4..af34f6b 100644 --- a/milo/plugins/potionImportTask.lua +++ b/milo/plugins/potionImportTask.lua @@ -1,4 +1,4 @@ -local Craft = require('turtle.craft') +local Craft = require('craft2') local itemDB = require('itemDB') local Milo = require('milo') diff --git a/milo/plugins/turtleLearn.lua b/milo/plugins/turtleLearn.lua index 3d49455..23d7743 100644 --- a/milo/plugins/turtleLearn.lua +++ b/milo/plugins/turtleLearn.lua @@ -1,4 +1,4 @@ -local Craft = require('turtle.craft') +local Craft = require('craft2') local itemDB = require('itemDB') local Milo = require('milo') local sync = require('sync')