cc/orders.lua
Ben e414f229db
Refactor, add listview to requests view
Update line by line and do full redraw only when necessary.
2024-06-02 00:52:58 +02:00

92 lines
2.8 KiB
Lua

local config = require "config"
local colony = peripheral.wrap(config.colony_interface_side)
local orders = {}
local frame = term.current()
local function setTerm(termlike)
frame = termlike
end
local function fetch(wait_for)
orders = {}
local colony_orders = colony.getWorkOrders()
if colony_orders ~= nil then
for i, wo in pairs(colony_orders) do
local resources = colony.getWorkOrderResources(wo.id)
local res_status = "?/?/0"
if resources ~= nil then
local unavailable_count = 0
local deliver_count = 0
for j, res in pairs(resources) do
if not res.available then
unavailable_count = unavailable_count + 1
end
if res.delivering then
deliver_count = deliver_count + 1
end
end
res_status = unavailable_count .. "/" .. deliver_count .. "/" .. #resources
end
table.insert(orders, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, res_status=res_status, claimed=wo.isClaimed})
wait_for(config.step_sleep_time)
end
end
end
local function displayTab(self)
frame.clear()
local tw,th = frame.getSize()
for i, val in pairs(orders) do
local line_num = (i-self.scrollPos)*2+1
if line_num > 0 and line_num < th-1 then
frame.setTextColor(colors.lightGray)
frame.setCursorPos(1,line_num)
frame.write(val.id)
frame.write(" ")
if val.pos then
frame.write(val.pos.x .. "," .. val.pos.y .. "," .. val.pos.z)
frame.write(" ")
end
frame.setCursorPos(2,line_num+1)
frame.setTextColor(colors.white)
if val.type then
frame.write(val.type)
frame.write(" ")
end
if val.target then
frame.write(val.target)
frame.write(" ")
end
if val.res_status then
frame.setCursorPos(tw-2-#val.res_status, line_num)
frame.write(val.res_status)
else
frame.setCursorPos(tw-2, line_num)
end
frame.write(" ")
if val.claimed then
frame.setTextColor(colors.yellow)
frame.write("C")
frame.setTextColor(colors.white)
else
frame.write(" ")
end
frame.write(" ")
end
end
end
local function onTouch(self, touch_x, touch_y)
return false
end
return {
name="orders",
setTerm=setTerm,
fetch=fetch,
displayTab=displayTab,
onTouch=onTouch,
}