cc/requests.lua

119 lines
3.8 KiB
Lua
Raw Normal View History

2024-06-01 02:39:41 +00:00
local config = require "config"
local permset = require "permset"
local listview = require "listview"
local winhlp = require "winhlp"
2024-06-01 02:39:41 +00:00
-- 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
2024-06-01 02:39:41 +00:00
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
2024-06-01 02:39:41 +00:00
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
2024-06-01 02:39:41 +00:00
if have_it.name == it.name then
found = true
listview.updateItemAt(currentIdx, {req=req, answer=have_it})
currentIdx = currentIdx + 1
2024-06-01 02:39:41 +00:00
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
2024-06-01 02:39:41 +00:00
end
wait_for(config.step_sleep_time)
end
2024-06-02 01:25:04 +00:00
listview.clearFrom(currentIdx)
2024-06-01 02:39:41 +00:00
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)
2024-06-01 02:39:41 +00:00
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("] ")
2024-06-01 02:39:41 +00:00
end
end
listview.setDrawLineFunc(drawLine)
local function displayTab(self)
listview.updatePage(self.scrollPos)
end
2024-06-01 02:39:41 +00:00
2024-06-01 14:20:00 +00:00
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+self.scrollPos-1)
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
2024-06-02 02:22:07 +00:00
listview.updatePartial(function (item) return item.answer ~= nil and item.answer.name == obj.answer.name end)
return true
2024-06-01 02:39:41 +00:00
end
end
return false
end
2024-06-01 02:39:41 +00:00
return {
name="requests",
setTerm=setTerm,
2024-06-01 02:39:41 +00:00
fetch=fetch,
2024-06-01 14:20:00 +00:00
displayTab=displayTab,
onTouch=onTouch,
2024-06-01 02:39:41 +00:00
}