cc/requests.lua

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