1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-17 22:24:50 +00:00
opus/apps/refinedManager.lua

536 lines
13 KiB
Lua
Raw Normal View History

2017-04-01 23:21:49 +00:00
local injector = requireInjector or load(http.get('http://pastebin.com/raw/c0TWsScv').readAll())()
require = injector(getfenv(1))
2017-04-17 07:13:00 +00:00
2017-04-01 23:21:49 +00:00
local UI = require('ui')
2017-04-17 07:13:00 +00:00
local RefinedProvider = require('refinedProvider')
local Terminal = require('terminal')
2017-04-01 23:21:49 +00:00
local Peripheral = require('peripheral')
2017-04-17 07:13:00 +00:00
local controller = RefinedProvider()
if not controller:isValid() then
2017-04-01 23:21:49 +00:00
error('Refined storage controller not found')
end
multishell.setTitle(multishell.getCurrent(), 'Storage Manager')
function getItem(items, inItem, ignoreDamage)
for _,item in pairs(items) do
if item.name == inItem.name then
2017-04-22 04:51:27 +00:00
if ignoreDamage then
2017-04-01 23:21:49 +00:00
return item
elseif item.damage == inItem.damage and item.nbtHash == inItem.nbtHash then
return item
end
end
end
end
local function uniqueKey(item)
2017-04-23 14:06:35 +00:00
return table.concat({ item.name, item.damage, item.nbtHash }, ':')
2017-04-01 23:21:49 +00:00
end
function mergeResources(t)
2017-04-23 14:06:35 +00:00
local resources = Util.readTable('resource.limits') or { }
2017-04-01 23:21:49 +00:00
for _,v in pairs(resources) do
2017-04-25 16:29:43 +00:00
v.low = tonumber(v.low) -- backwards compatibility
2017-04-01 23:21:49 +00:00
local item = getItem(t, v)
if item then
2017-04-23 14:06:35 +00:00
item.low = v.low
2017-04-01 23:21:49 +00:00
item.auto = v.auto
item.ignoreDamage = v.ignoreDamage
2017-04-17 07:13:00 +00:00
item.rsControl = v.rsControl
item.rsDevice = v.rsDevice
item.rsSide = v.rsSide
2017-04-01 23:21:49 +00:00
else
v.count = 0
table.insert(t, v)
end
2017-04-23 14:06:35 +00:00
end
for _,v in pairs(t) do
2017-04-23 05:05:53 +00:00
v.lname = v.displayName:lower()
2017-04-01 23:21:49 +00:00
end
end
function filterItems(t, filter)
if filter then
2017-04-23 14:06:35 +00:00
local r = {}
2017-04-01 23:21:49 +00:00
filter = filter:lower()
for k,v in pairs(t) do
2017-04-23 14:06:35 +00:00
if string.find(v.lname, filter) then
2017-04-01 23:21:49 +00:00
table.insert(r, v)
end
end
2017-04-23 14:06:35 +00:00
return r
2017-04-01 23:21:49 +00:00
end
2017-04-23 14:06:35 +00:00
return t
2017-04-01 23:21:49 +00:00
end
function craftItems(itemList, allItems)
for _,item in pairs(itemList) do
local cItem = getItem(allItems, item)
2017-04-17 07:13:00 +00:00
if controller:isCrafting(item) then
2017-04-01 23:21:49 +00:00
item.status = '(crafting)'
2017-04-17 07:13:00 +00:00
elseif item.rsControl then
item.status = 'Activated'
2017-04-01 23:21:49 +00:00
elseif not cItem then
item.status = '(no recipe)'
else
local count = item.count
while count >= 1 do -- try to request smaller quantities until successful
local s, m = pcall(function()
item.status = '(no recipe)'
2017-04-23 14:06:35 +00:00
if not controller:craft(cItem, count) then
2017-04-01 23:21:49 +00:00
item.status = '(missing ingredients)'
error('failed')
end
item.status = '(crafting)'
end)
if s then
break -- successfully requested crafting
end
count = math.floor(count / 2)
end
end
end
end
2017-04-23 14:06:35 +00:00
function getAutocraftItems()
2017-04-01 23:21:49 +00:00
local t = Util.readTable('resource.limits') or { }
local itemList = { }
for _,res in pairs(t) do
2017-04-22 04:51:27 +00:00
if res.auto then
2017-04-01 23:21:49 +00:00
res.count = 4 -- this could be higher to increase autocrafting speed
table.insert(itemList, res)
end
end
return itemList
end
local function getItemWithQty(items, res, ignoreDamage)
local item = getItem(items, res, ignoreDamage)
if item then
2017-04-22 04:51:27 +00:00
if ignoreDamage then
2017-04-01 23:21:49 +00:00
local count = 0
for _,v in pairs(items) do
if item.name == v.name and item.nbtHash == v.nbtHash then
if item.maxDamage > 0 or item.damage == v.damage then
count = count + v.count
end
end
end
item.count = count
end
end
return item
end
function watchResources(items)
local itemList = { }
local t = Util.readTable('resource.limits') or { }
for k, res in pairs(t) do
2017-04-25 16:29:43 +00:00
res.low = tonumber(res.low) -- backwards compatibility
2017-04-01 23:21:49 +00:00
local item = getItemWithQty(items, res, res.ignoreDamage)
if not item then
item = {
damage = res.damage,
nbtHash = res.nbtHash,
name = res.name,
displayName = res.displayName,
count = 0
}
end
if res.low and item.count < res.low then
2017-04-22 04:51:27 +00:00
if res.ignoreDamage then
2017-04-01 23:21:49 +00:00
item.damage = 0
end
table.insert(itemList, {
damage = item.damage,
nbtHash = item.nbtHash,
count = res.low - item.count,
name = item.name,
displayName = item.displayName,
2017-04-17 07:13:00 +00:00
status = '',
rsControl = res.rsControl,
2017-04-01 23:21:49 +00:00
})
end
2017-04-17 07:13:00 +00:00
if res.rsControl and res.rsDevice and res.rsSide then
2017-04-23 14:06:35 +00:00
pcall(function()
device[res.rsDevice].setOutput(res.rsSide, item.count < res.low)
end)
2017-04-17 07:13:00 +00:00
end
2017-04-01 23:21:49 +00:00
end
return itemList
end
itemPage = UI.Page {
backgroundColor = colors.lightGray,
titleBar = UI.TitleBar {
title = 'Limit Resource',
previousPage = true,
event = 'form_cancel',
backgroundColor = colors.green
},
displayName = UI.Window {
x = 5, y = 3, width = UI.term.width - 10, height = 3,
},
form = UI.Form {
2017-04-22 04:51:27 +00:00
x = 4, y = 6, height = 10, rex = -4,
2017-04-01 23:21:49 +00:00
[1] = UI.TextEntry {
width = 7,
backgroundColor = colors.gray,
backgroundFocusColor = colors.gray,
formLabel = 'Min', formKey = 'low', help = 'Craft if below min'
},
[2] = UI.Chooser {
width = 7,
formLabel = 'Autocraft', formKey = 'auto',
nochoice = 'No',
choices = {
2017-04-22 04:51:27 +00:00
{ name = 'Yes', value = true },
{ name = 'No', value = false },
2017-04-01 23:21:49 +00:00
},
help = 'Craft until out of ingredients'
},
[3] = UI.Chooser {
width = 7,
formLabel = 'Ignore Dmg', formKey = 'ignoreDamage',
nochoice = 'No',
choices = {
2017-04-22 04:51:27 +00:00
{ name = 'Yes', value = true },
{ name = 'No', value = false },
2017-04-01 23:21:49 +00:00
},
help = 'Ignore damage of item'
},
2017-04-17 07:13:00 +00:00
[4] = UI.Chooser {
width = 7,
formLabel = 'RS Control', formKey = 'rsControl',
nochoice = 'No',
choices = {
{ name = 'Yes', value = true },
{ name = 'No', value = false },
},
help = 'Control via redstone'
},
[5] = UI.Chooser {
width = 25,
formLabel = 'RS Device', formKey = 'rsDevice',
--choices = devices,
help = 'Redstone Device'
},
[6] = UI.Chooser {
width = 10,
formLabel = 'RS Side', formKey = 'rsSide',
--nochoice = 'No',
choices = {
{ name = 'up', value = 'up' },
{ name = 'down', value = 'down' },
{ name = 'east', value = 'east' },
{ name = 'north', value = 'north' },
{ name = 'west', value = 'west' },
{ name = 'south', value = 'south' },
},
help = 'Output side'
},
2017-04-01 23:21:49 +00:00
},
statusBar = UI.StatusBar { }
}
function itemPage.displayName:draw()
local item = self.parent.item
local str = string.format('Name: %s\nDamage: %d', item.displayName, item.damage)
if item.nbtHash then
str = str .. string.format('\nNBT: %s\n', item.nbtHash)
end
self:setCursorPos(1, 1)
self:print(str)
end
function itemPage:enable(item)
self.item = item
self.form:setValues(item)
self.titleBar.title = item.name
2017-04-17 07:13:00 +00:00
local devices = self.form[5].choices
Util.clear(devices)
for _,device in pairs(device) do
if device.setOutput then
table.insert(devices, { name = device.name, value = device.name })
end
end
2017-04-22 04:51:27 +00:00
if Util.size(devices) == 0 then
table.insert(devices, { name = 'None found', values = '' })
end
2017-04-01 23:21:49 +00:00
UI.Page.enable(self)
self:focusFirst()
end
function itemPage:eventHandler(event)
if event.type == 'form_cancel' then
UI:setPreviousPage()
elseif event.type == 'focus_change' then
self.statusBar:setStatus(event.focused.help)
self.statusBar:draw()
elseif event.type == 'form_complete' then
local values = self.form.values
local t = Util.readTable('resource.limits') or { }
2017-04-17 07:13:00 +00:00
local keys = { 'name', 'displayName', 'auto', 'low', 'damage',
2017-04-23 14:06:35 +00:00
'maxDamage', 'nbtHash', 'ignoreDamage',
2017-04-17 07:13:00 +00:00
'rsControl', 'rsDevice', 'rsSide', }
2017-04-23 14:06:35 +00:00
2017-04-01 23:21:49 +00:00
local filtered = { }
for _,key in pairs(keys) do
filtered[key] = values[key]
end
2017-04-22 04:51:27 +00:00
filtered.low = tonumber(filtered.low)
filtered.ignoreDamage = filtered.ignoreDamage == true
filtered.auto = filtered.auto == true
filtered.rsControl = filtered.rsControl == true
2017-04-01 23:21:49 +00:00
2017-04-22 04:51:27 +00:00
if filtered.ignoreDamage then
2017-04-01 23:21:49 +00:00
filtered.damage = 0
end
t[uniqueKey(filtered)] = filtered
Util.writeTable('resource.limits', t)
2017-04-23 14:06:35 +00:00
UI:setPreviousPage()
2017-04-01 23:21:49 +00:00
else
return UI.Page.eventHandler(self, event)
end
return true
end
listingPage = UI.Page {
menuBar = UI.MenuBar {
buttons = {
{ text = 'Forget', event = 'forget' },
},
},
grid = UI.Grid {
y = 2, height = UI.term.height - 2,
columns = {
{ heading = 'Name', key = 'displayName', width = UI.term.width - 14 },
{ heading = 'Qty', key = 'count', width = 5 },
{ heading = 'Min', key = 'low', width = 4 },
},
sortColumn = 'lname',
},
statusBar = UI.StatusBar {
backgroundColor = colors.gray,
width = UI.term.width,
filterText = UI.Text {
x = 2, width = 6,
value = 'Filter',
},
filter = UI.TextEntry {
x = 9, rex = -12,
limit = 50,
},
refresh = UI.Button {
rx = -9, width = 8,
text = 'Refresh',
event = 'refresh',
},
},
accelerators = {
r = 'refresh',
q = 'quit',
}
}
function listingPage.grid:getRowTextColor(row, selected)
if row.is_craftable then -- not implemented
return colors.yellow
end
return UI.Grid:getRowTextColor(row, selected)
end
function listingPage.grid:getDisplayValues(row)
row = Util.shallowCopy(row)
row.count = Util.toBytes(row.count)
if row.low then
row.low = Util.toBytes(row.low)
end
return row
end
function listingPage.statusBar:draw()
return UI.Window.draw(self)
end
function listingPage.statusBar.filter:eventHandler(event)
if event.type == 'mouse_rightclick' then
self.value = ''
self:draw()
local page = UI:getCurrentPage()
page.filter = nil
page:applyFilter()
page.grid:draw()
page:setFocus(self)
end
return UI.TextEntry.eventHandler(self, event)
end
function listingPage:eventHandler(event)
if event.type == 'quit' then
2017-04-17 07:13:00 +00:00
UI:exitPullEvents()
2017-04-01 23:21:49 +00:00
elseif event.type == 'grid_select' then
local selected = event.selected
UI:setPage('item', selected)
elseif event.type == 'refresh' then
self:refresh()
self.grid:draw()
2017-04-23 14:06:35 +00:00
self.statusBar.filter:focus()
2017-04-01 23:21:49 +00:00
elseif event.type == 'forget' then
local item = self.grid:getSelected()
if item then
local resources = Util.readTable('resource.limits') or { }
resources[uniqueKey(item)] = nil
Util.writeTable('resource.limits', resources)
self.statusBar:timedStatus('Forgot: ' .. item.name, 3)
self:refresh()
self.grid:draw()
end
elseif event.type == 'text_change' 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()
self:refresh()
self:setFocus(self.statusBar.filter)
UI.Page.enable(self)
end
function listingPage:refresh()
2017-04-17 07:13:00 +00:00
self.allItems = controller:listItems()
2017-04-01 23:21:49 +00:00
mergeResources(self.allItems)
self:applyFilter()
end
function listingPage:applyFilter()
local t = filterItems(self.allItems, self.filter)
self.grid:setValues(t)
end
local function jobMonitor(jobList)
local mon = Peripheral.getByType('monitor')
if mon then
mon = UI.Device({
2017-04-25 17:00:47 +00:00
device = device.monitor,
2017-04-01 23:21:49 +00:00
textScale = .5,
})
else
mon = UI.Device({
2017-04-17 07:13:00 +00:00
device = Terminal.getNullTerm(term.current())
2017-04-01 23:21:49 +00:00
})
end
jobListGrid = UI.Grid {
parent = mon,
sortColumn = 'displayName',
columns = {
{ heading = 'Qty', key = 'count', width = 6 },
{ heading = 'Crafting', key = 'displayName', width = mon.width / 2 - 10 },
{ heading = 'Status', key = 'status', width = mon.width - 10 },
},
}
return jobListGrid
end
UI:setPages({
listing = listingPage,
item = itemPage,
})
UI:setPage(listingPage)
listingPage:setFocus(listingPage.statusBar.filter)
local jobListGrid = jobMonitor()
jobListGrid:draw()
jobListGrid:sync()
function craftingThread()
while true do
os.sleep(5)
2017-04-17 07:13:00 +00:00
--pcall(function()
2017-04-01 23:21:49 +00:00
2017-04-17 07:13:00 +00:00
local items = controller:listItems()
2017-04-01 23:21:49 +00:00
2017-04-17 07:13:00 +00:00
if not controller:isOnline() then
2017-04-01 23:21:49 +00:00
jobListGrid.parent:clear()
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'Power failure')
jobListGrid:sync()
elseif Util.size(items) == 0 then
jobListGrid.parent:clear()
jobListGrid.parent:centeredWrite(math.ceil(jobListGrid.parent.height/2), 'No items in system')
jobListGrid:sync()
else
local itemList = watchResources(items)
jobListGrid:setValues(itemList)
jobListGrid:draw()
jobListGrid:sync()
craftItems(itemList, items)
2017-04-23 14:06:35 +00:00
--jobListGrid:update()
2017-04-01 23:21:49 +00:00
jobListGrid:draw()
jobListGrid:sync()
2017-04-23 14:06:35 +00:00
itemList = getAutocraftItems() -- autocrafted items don't show on job monitor
2017-04-01 23:21:49 +00:00
craftItems(itemList, items)
end
2017-04-17 07:13:00 +00:00
--end)
2017-04-01 23:21:49 +00:00
end
end
2017-04-17 07:13:00 +00:00
UI:pullEvents(craftingThread)
2017-04-01 23:21:49 +00:00
UI.term:reset()
jobListGrid.parent:reset()