cc/requests.lua

107 lines
3.5 KiB
Lua

local config = require "config"
local permset = require "permset"
-- to match click positions back to item names
local display_list = {}
-- 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 requests = {}
local function fetch(wait_for)
requests = {}
local reqs = colony.getRequests()
local me_items = nil
if config.has_me then
me_items = main_me.listItems()
end
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
if have_it.name == it.name then
found = true
table.insert(requests, {req=req, answer=have_it})
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
table.insert(requests, {req=req, answer=nil})
end
wait_for(config.step_sleep_time)
end
end
local function displayTab(self)
self.window.clear()
local tw,th = self.window.getSize()
display_list = {}
for i, val in pairs(requests) do
local line_num = i-self.scrollPos+1
if line_num > 0 and line_num < th then
self.window.setTextColor(colors.white)
self.window.setCursorPos(1,line_num)
self.window.write(val.req.name)
self.window.write(" ")
if val.answer == nil then
self.window.setTextColor(colors.orange)
self.window.write("N/A")
else
if permset.has(allowed_items, val.answer.name) then
self.window.setTextColor(colors.green)
else
self.window.setTextColor(colors.yellow)
end
self.window.write(val.answer.name)
display_list[line_num] = val.answer.name
self.window.setTextColor(colors.white)
self.window.setCursorPos(tw-4,line_num)
self.window.write(" [")
if permset.has(allowed_items, val.answer.name) then
self.window.setTextColor(colors.green)
self.window.write("X")
self.window.setTextColor(colors.white)
else
self.window.write(" ")
end
self.window.write("] ")
end
end
end
end
local function onTouch(self, touch_x, touch_y)
local tw, th = self.window.getSize()
if touch_x > tw-5 and display_list[touch_y] ~= nil then
local item_name = display_list[touch_y]
if permset.has(allowed_items, item_name) then
permset.remove(allowed_items, item_name)
else
permset.add(allowed_items, item_name)
end
return true
end
return false
end
return {
name="requests",
fetch=fetch,
displayTab=displayTab,
onTouch=onTouch,
}