cc/requests.lua

118 lines
3.6 KiB
Lua

local config = require "config"
local permset = require "permset"
local listview = require "listview"
local winhlp = require "winhlp"
-- list of allowed item names, will be requested
local allowed_items = permset.new(".allowed_items")
local main_me = {}
if config.has_me then
main_me = peripheral.wrap(config.main_system_ae_bridge_side)
end
local colony = peripheral.wrap(config.colony_interface_side)
local frame = term.current()
local function setTerm(termlike)
frame = termlike
listview.setTerm(frame)
end
local function fetch(wait_for)
local reqs = colony.getRequests()
local me_items = nil
if config.has_me then
me_items = main_me.listItems()
end
local currentIdx = 1
for i, req in pairs(reqs) do
local found = false
if me_items then
local left_amount = req.count
for j, it in pairs(req.items) do
for k, have_it in pairs(me_items) do
-- TODO: check NBT
if have_it.name == it.name then
found = true
listview.updateItemAt(currentIdx, {req=req, answer=have_it})
currentIdx = currentIdx + 1
if left_amount > 0 and permset.has(allowed_items, it.name) then
local export_amount = math.min(have_it.amount, left_amount)
left_amount = left_amount - export_amount
main_me.exportItem({name=it.name, count=export_amount}, config.main_me_inventory_side)
end
end
end
end
end
if found == false then
listview.updateItemAt(currentIdx, {req=req, answer=nil})
currentIdx = currentIdx + 1
end
wait_for(config.step_sleep_time)
end
listview.clearAfter(currentIdx)
end
local function drawLine(termlike, obj)
termlike.setBackgroundColor(colors.black)
termlike.setTextColor(colors.white)
termlike.write(obj.req.name)
termlike.write(" ")
if obj.answer == nil then
termlike.setTextColor(colors.orange)
local padding = winhlp.alignRight(termlike, 5)
termlike.write(string.format("%" .. padding+5 .. "s", " N/A "))
else
if permset.has(allowed_items, obj.answer.name) then
termlike.setTextColor(colors.green)
else
termlike.setTextColor(colors.yellow)
end
termlike.write(obj.answer.name)
termlike.setTextColor(colors.white)
local padding = winhlp.alignRight(termlike, 5)
termlike.write(string.format("%" .. padding+2 .. "s", " ["))
if permset.has(allowed_items, obj.answer.name) then
termlike.setTextColor(colors.green)
termlike.write("X")
termlike.setTextColor(colors.white)
else
termlike.write(" ")
end
termlike.write("] ")
end
end
listview.setDrawLineFunc(drawLine)
local function displayTab(self)
listview.updatePage(self.scrollPos)
end
local function onTouch(self, touch_x, touch_y)
local tw, th = self.window.getSize()
if touch_x > tw-5 then
local obj = listview.itemAt(touch_y)
if obj ~= nil and obj.answer ~= nil then
if permset.has(allowed_items, obj.answer.name) then
permset.remove(allowed_items, obj.answer.name)
else
permset.add(allowed_items, obj.answer.name)
end
return true
end
end
return false
end
return {
name="requests",
setTerm=setTerm,
fetch=fetch,
displayTab=displayTab,
onTouch=onTouch,
}