cc/requests.lua

121 lines
3.9 KiB
Lua
Raw Permalink Normal View History

2024-06-01 02:39:41 +00:00
local config = require "config"
local permset = require "permset"
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)
2024-06-03 21:18:16 +00:00
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.alignRightPadding(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.alignRightPadding(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
local listview = (require "listview"):new{drawEntry=drawLine}
local function setTerm(termlike)
2024-06-03 21:18:16 +00:00
listview.frame = termlike
end
2024-06-01 02:39:41 +00:00
local function fetch(wait_for)
local me_items = nil
if config.has_me then
me_items = main_me.listItems()
end
local currentIdx = 1
if colony.isInColony() then
for i, req in pairs(colony.getRequests()) 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
2024-06-03 21:18:16 +00:00
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
2024-06-01 02:39:41 +00:00
end
end
end
end
if found == false then
2024-06-03 21:18:16 +00:00
listview:updateItemAt(currentIdx, {req=req, answer=nil})
currentIdx = currentIdx + 1
end
wait_for(config.step_sleep_time)
2024-06-01 02:39:41 +00:00
end
end
2024-06-03 21:18:16 +00:00
listview:clearFrom(currentIdx)
2024-06-01 02:39:41 +00:00
end
local function displayTab(self)
2024-06-03 21:18:16 +00:00
listview:updatePage(self.scrollPos)
end
2024-06-01 02:39:41 +00:00
2024-06-02 15:51:21 +00:00
local function estimatedHeight()
2024-06-03 21:18:16 +00:00
return listview.length
2024-06-02 15:51:21 +00:00
end
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
2024-06-03 21:18:16 +00:00
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-03 21:18:16 +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,
2024-06-02 15:51:21 +00:00
estimatedHeight=estimatedHeight,
2024-06-01 14:20:00 +00:00
onTouch=onTouch,
2024-06-01 02:39:41 +00:00
}