1
0
cc-resource-monitor/client.lua
2023-05-11 21:53:52 +03:00

135 lines
3.1 KiB
Lua

local PROTOCOL = "resource-monitor"
local REFRESH_RATE = 5 -- seconds between every request
local function pprint(...)
local pretty = require("cc.pretty")
pretty.pretty_print(...)
end
local function isWirelessModem(peripheral_name)
return peripheral.getType(peripheral_name) == "modem"
and peripheral.call(peripheral_name, "isWireless")
end
local function getWirelessModem()
for _, name in ipairs(peripheral.getNames()) do
if isWirelessModem(name) then
return name
end
end
end
local function getItems(servers)
local allItems = {}
for _, id in ipairs(servers) do
rednet.send(id, nil, PROTOCOL)
local _, payload = rednet.receive(PROTOCOL)
local items = textutils.unserialise(payload)
for name, count in pairs(items) do
allItems[name] = (allItems[name] or 0) + count
end
end
local orderedItems = {}
for name, count in pairs(allItems) do
table.insert(orderedItems, { name = name, count = count })
end
table.sort(orderedItems, function(a, b) return a.count > b.count end)
return orderedItems
end
local function getNumberLength(number)
return math.floor(1+math.log10(number))
end
local function countColumnWidths(items)
local multiplierWidth = 0
local remainderWidth = 0
for _, item in ipairs(items) do
local multiplier = math.floor(item.count/64)
if multiplier > 0 then
multiplierWidth = math.max(multiplierWidth, getNumberLength(multiplier))
end
local remainder = item.count%64
if remainder > 0 then
remainderWidth = math.max(remainderWidth, getNumberLength(remainder))
end
end
return multiplierWidth, remainderWidth
end
local function writeAlignedNumber(number, width)
local x, y = term.getCursorPos()
term.setCursorPos(x - math.floor(math.log10(number)+1) + width, y)
term.write(number)
end
local function displayItems(items)
local w, h = term.getSize()
local multiplierColumnWidth, remainderColumnWidth = countColumnWidths(items)
term.clear()
term.setCursorPos(1, 1)
print("==== Resource monitor ====")
for i = 1, math.min(#items, h-1) do
local item = items[i]
term.setCursorPos(1, 1+i)
term.write(item.name)
term.write(" ")
local multiplier = math.floor(item.count/64)
local remainder = item.count%64
local y = i+1
if multiplier > 0 then
local x = w-remainderColumnWidth-2
if remainderColumnWidth > 0 then
x = x - 1
end
term.setCursorPos(x, y)
term.write("x64")
term.setCursorPos(x-multiplierColumnWidth, y)
writeAlignedNumber(multiplier, multiplierColumnWidth)
end
if remainder > 0 then
term.setCursorPos(w-remainderColumnWidth+1, y)
writeAlignedNumber(remainder, remainderColumnWidth)
end
if remainder > 0 and multiplier > 0 then
term.setCursorPos(w-remainderColumnWidth, y)
term.write("+")
end
end
end
local function main()
local modem_name = getWirelessModem()
if not modem_name then
print("ERROR: please attach a wireless modem")
return
end
rednet.open(modem_name)
term.clear()
term.setCursorPos(1, 1)
print("Starting...")
local servers = {rednet.lookup(PROTOCOL)}
while true do
local items = getItems(servers)
displayItems(items)
os.sleep(REFRESH_RATE)
end
rednet.close(modem_name)
end
main()