Refactor and split code

This commit is contained in:
Ben 2024-06-01 04:39:41 +02:00
parent 76663b9371
commit 61d59846ac
Signed by: ben
GPG key ID: 1353F41CF1CFF2D3
5 changed files with 216 additions and 169 deletions

9
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,9 @@
{
"Lua.diagnostics.globals": [
"peripheral",
"term",
"colors",
"fs",
"shell"
]
}

76
orders.lua Normal file
View file

@ -0,0 +1,76 @@
local config = require "config"
local colony = peripheral.wrap(config.colony_interface_side)
local orders = {}
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 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)
end
end
end
local function display(page_start_idx)
local tw,th = term.getSize()
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)
else
term.setCursorPos(tw-2, line_num)
end
term.write(" ")
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 function on_touch(touch_x, touch_y)
return false
end
return {
name="orders",
fetch=fetch,
display=display,
on_touch=on_touch,
}

105
requests.lua Normal file
View file

@ -0,0 +1,105 @@
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
local function display(page_start_idx)
local tw,th = term.getSize()
display_list = {}
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
if permset.has(allowed_items, val.answer.name) then
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(" [")
if permset.has(allowed_items, val.answer.name) then
term.setTextColor(colors.green)
term.write("X")
term.setTextColor(colors.white)
else
term.write(" ")
end
term.write("] ")
end
end
end
end
local function on_touch(touch_x, touch_y)
local tw,th = term.getSize()
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,
display=display,
on_touch=on_touch,
}

176
test.lua
View file

@ -1,21 +1,7 @@
local config = require "config"
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)
end
local colony = peripheral.wrap(config.colony_interface_side)
local requests = {}
local orders = {}
-- index of current tab
local current_tab = config.initial_tab
-- 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
@ -23,99 +9,19 @@ local page_start_idx = 1
-- schedule immediate reload
local immediate_reload = false
function requestsDisplay()
local tw,th = term.getSize()
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
if permset.has(allowed_items, val.answer.name) then
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(" [")
if permset.has(allowed_items, val.answer.name) then
term.setTextColor(colors.green)
term.write("X")
term.setTextColor(colors.white)
else
term.write(" ")
end
term.write("] ")
end
end
end
end
function ordersDisplay()
local tw,th = term.getSize()
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)
else
term.setCursorPos(tw-2, line_num)
end
term.write(" ")
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},
[1]=require "requests",
-- to display colony work orders
[2]={name="orders", display_func=ordersDisplay},
[2]=require "orders",
}
function display()
local 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)
@ -131,7 +37,7 @@ function display()
term.write(" ^ v ")
term.setBackgroundColor(colors.black)
tabs[current_tab].display_func()
tabs[current_tab].display(page_start_idx)
if currently_working then
local cx,cy = term.getCursorPos()
@ -143,17 +49,17 @@ function display()
end
function page_down(th)
local function page_down(th)
page_start_idx = page_start_idx+th-3
end
function page_up(th)
local function page_up(th)
page_start_idx = math.max(1, page_start_idx-th+3)
end
function on_touch(touch_x, touch_y)
local 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
@ -163,6 +69,7 @@ function on_touch(touch_x, touch_y)
end
return true
elseif currently_working == false and touch_x > tw-9 and touch_x < tw-5 then
-- reload button
immediate_reload = true
return true
elseif touch_x > tw-6 and touch_x < tw-2 then
@ -172,20 +79,14 @@ function on_touch(touch_x, touch_y)
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]
if permset.has(allowed_items, item_name) then
permset.remove(allowed_items, item_name)
else
permset.add(allowed_items, item_name)
end
elseif tabs[current_tab].on_touch(touch_x, touch_y) then
return true
end
return false
end
function wait_for(time_secs)
local function wait_for(time_secs)
display()
local timer_id = os.startTimer(time_secs)
while true do
@ -195,6 +96,7 @@ function wait_for(time_secs)
display()
end
if immediate_reload == true then
os.cancelTimer(timer_id)
immediate_reload = false
break
end
@ -213,59 +115,11 @@ if peripheral.isPresent(config.monitor_side) then
term.redirect(monitor)
end
function get_requests()
requests = {}
local reqs = colony.getRequests()
local me_items = config.has_me
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
function get_orders()
orders = {}
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)
end
end
end
while true do
currently_working = true
get_requests()
get_orders()
for i, tab in pairs(tabs) do
tab.fetch(wait_for)
end
currently_working = false
wait_for(config.round_sleep_time)
end

View file

@ -19,15 +19,18 @@ function update_update()
return true
end
function download_file(filename)
if fs.exists(filename) then
fs.delete(filename)
end
shell.execute("wget", "https://gitea.rs485.network/ben/cc/raw/branch/main/" .. filename, filename)
end
if update_update() then
if fs.exists("permset.lua") then
fs.delete("permset.lua")
end
shell.execute("wget", "https://gitea.rs485.network/ben/cc/raw/branch/main/permset.lua", "permset.lua")
if fs.exists("test.lua") then
fs.delete("test.lua")
end
shell.execute("wget", "https://gitea.rs485.network/ben/cc/raw/branch/main/test.lua", "test.lua")
download_file("permset.lua")
download_file("requests.lua")
download_file("orders.lua")
download_file("test.lua")
shell.run("test")
end