cc/requests.lua

106 lines
3.4 KiB
Lua
Raw Normal View History

2024-06-01 02:39:41 +00:00
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 display(page_start_idx)
local tw,th = term.getSize()
display_list = {}
for i, val in pairs(requests) do
local line_num = i-page_start_idx+3
if line_num > 2 and line_num < th then
term.setTextColor(colors.white)
term.setCursorPos(1,line_num)
term.write(val.req.name)
term.write(" ")
if val.answer == nil then
term.setTextColor(colors.orange)
term.write("N/A")
else
if permset.has(allowed_items, val.answer.name) then
term.setTextColor(colors.green)
else
term.setTextColor(colors.yellow)
end
term.write(val.answer.name)
display_list[line_num] = val.answer.name
term.setTextColor(colors.white)
term.setCursorPos(tw-4,line_num)
term.write(" [")
if permset.has(allowed_items, val.answer.name) then
term.setTextColor(colors.green)
term.write("X")
term.setTextColor(colors.white)
else
term.write(" ")
end
term.write("] ")
end
end
end
end
local function on_touch(touch_x, touch_y)
local tw,th = term.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,
display=display,
on_touch=on_touch,
}