cc/test.lua

272 lines
8.1 KiB
Lua
Raw Normal View History

local config = require "config"
2024-05-31 23:12:12 +00:00
local pretty = require "cc.pretty"
local permset = require "permset"
local main_me = {}
if config.has_me then
main_me = peripheral.wrap(config.main_system_ae_bridge_side)
2024-05-31 23:12:12 +00:00
end
local colony = peripheral.wrap(config.colony_interface_side)
2024-05-31 23:12:12 +00:00
local requests = {}
local orders = {}
local current_tab = config.initial_tab
2024-05-31 23:12:12 +00:00
-- 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")
-- display working state
local currently_working = true
-- current start index to view requests
local page_start_idx = 1
-- schedule immediate reload
local immediate_reload = false
function requestsDisplay()
2024-05-31 23:42:26 +00:00
local tw,th = term.getSize()
2024-05-31 23:12:12 +00:00
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
2024-06-01 00:03:04 +00:00
if permset.has(allowed_items, val.answer.name) then
2024-05-31 23:12:12 +00:00
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(" [")
2024-06-01 00:03:04 +00:00
if permset.has(allowed_items, val.answer.name) then
2024-05-31 23:12:12 +00:00
term.setTextColor(colors.green)
term.write("X")
term.setTextColor(colors.white)
else
term.write(" ")
end
term.write("] ")
end
end
end
end
function ordersDisplay()
2024-05-31 23:42:26 +00:00
local tw,th = term.getSize()
2024-05-31 23:12:12 +00:00
for i, val in pairs(orders) do
local line_num = (i-page_start_idx)*2+3
if line_num > 2 and line_num < th-1 then
term.setTextColor(colors.lightGray)
term.setCursorPos(1,line_num)
term.write(val.id)
term.write(" ")
if val.pos then
term.write(val.pos.x .. "," .. val.pos.y .. "," .. val.pos.z)
term.write(" ")
end
term.setCursorPos(2,line_num+1)
term.setTextColor(colors.white)
if val.type then
term.write(val.type)
term.write(" ")
end
if val.target then
term.write(val.target)
term.write(" ")
end
if val.res_count then
term.setCursorPos(tw-2-#val.res_count, line_num)
term.write(val.res_count)
2024-05-31 23:12:12 +00:00
else
term.setCursorPos(tw-2, line_num)
2024-05-31 23:12:12 +00:00
end
term.write(" ")
2024-05-31 23:12:12 +00:00
if val.claimed then
term.setTextColor(colors.yellow)
term.write("C")
term.setTextColor(colors.white)
else
term.write(" ")
end
term.write(" ")
end
end
end
local tabs = {
-- to display requests and answer with an ME interface
[1]={name="requests", display_func=requestsDisplay},
-- to display colony work orders
[2]={name="orders", display_func=ordersDisplay},
}
function display()
local tw,th = term.getSize()
term.setCursorBlink(false)
term.setBackgroundColor(colors.black)
term.clear()
display_list = {}
term.setCursorPos(1,1)
term.setBackgroundColor(colors.yellow)
term.setTextColor(colors.black)
term.clearLine()
term.write(" " .. string.upper(tabs[current_tab].name) .. " ")
if currently_working == false then
term.setCursorPos(tw-8,1)
term.write(" R ")
end
term.setCursorPos(tw-5,1)
term.write(" ^ v ")
term.setBackgroundColor(colors.black)
tabs[current_tab].display_func()
if currently_working then
local cx,cy = term.getCursorPos()
term.setCursorPos(1,cy+2)
term.setCursorBlink(true)
term.setTextColor(colors.gray)
term.write("Working... ")
end
end
function page_down(th)
page_start_idx = page_start_idx+th-3
end
function page_up(th)
page_start_idx = math.max(1, page_start_idx-th+3)
end
function on_touch(touch_x, touch_y)
local tw,th = term.getSize()
if touch_y == 1 then
if touch_x < (#tabs[current_tab].name + 2) then
current_tab = current_tab + 1
if current_tab > #tabs then
current_tab = 1
end
return true
elseif currently_working == false and touch_x > tw-9 and touch_x < tw-5 then
immediate_reload = true
return true
elseif touch_x > tw-6 and touch_x < tw-2 then
page_up(th)
return true
elseif touch_x > tw-3 and touch_x <= tw then
page_down(th)
return true
end
elseif current_tab == 1 and touch_x > tw-5 and display_list[touch_y] ~= nil then
local item_name = display_list[touch_y]
2024-06-01 00:03:04 +00:00
if permset.has(allowed_items, item_name) then
permset.remove(allowed_items, item_name)
2024-05-31 23:12:12 +00:00
else
2024-06-01 00:03:04 +00:00
permset.add(allowed_items, item_name)
2024-05-31 23:12:12 +00:00
end
return true
end
return false
end
function wait_for(time_secs)
display()
local timer_id = os.startTimer(time_secs)
while true do
local event_data = {os.pullEvent()}
if event_data[1] == "monitor_touch" or (event_data[1] == "mouse_click" and event_data[2] == 1) then
if on_touch(event_data[3], event_data[4]) then
display()
end
if immediate_reload == true then
immediate_reload = false
break
end
elseif event_data[1] == "timer" then
if event_data[2] == timer_id then
break
end
end
end
end
if peripheral.isPresent(config.monitor_side) then
monitor = peripheral.wrap(config.monitor_side)
monitor.setTextScale(config.monitor_scale)
2024-05-31 23:12:12 +00:00
term.redirect(monitor)
end
function get_requests()
requests = {}
local reqs = colony.getRequests()
local me_items = config.has_me
if config.has_me then
2024-05-31 23:12:12 +00:00
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})
2024-06-01 00:03:04 +00:00
if left_amount > 0 and permset.has(allowed_items, it.name) then
2024-05-31 23:12:12 +00:00
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)
2024-05-31 23:12:12 +00:00
end
end
end
end
end
if found == false then
table.insert(requests, {req=req, answer=nil})
end
wait_for(config.step_sleep_time)
2024-05-31 23:12:12 +00:00
end
end
function get_orders()
orders = {}
2024-05-31 23:48:24 +00:00
local colony_orders = colony.getWorkOrders()
if colony_orders ~= nil then
for i, wo in pairs(colony_orders) do
local res = colony.getWorkOrderResources(wo.id)
if res == nil then
res = "0"
else
res = "" .. #res
end
table.insert(orders, {id=wo.id, type=wo.workOrderType, target=wo.type, pos=wo.builder, res_count=res, claimed=wo.isClaimed})
wait_for(config.step_sleep_time)
2024-05-31 23:12:12 +00:00
end
end
end
while true do
currently_working = true
get_requests()
get_orders()
currently_working = false
wait_for(config.round_sleep_time)
2024-05-31 23:12:12 +00:00
end