CCCPaste

miner_monitor

-- === Miner Monitor (v2) ===
-- Auto-detects peripherals, monitors the miner named "chunkminer"
-- Modem: top
-- Monitor: left

local PROTOCOL = "miner_update"

-- === Peripheral Detection ===
local modemSide, monitorSide

for _, side in ipairs(peripheral.getNames()) do
    local t = peripheral.getType(side)
    if t == "modem" then modemSide = side end
    if t == "monitor" then monitorSide = side end
end

if not modemSide then error("No modem found!") end
if not monitorSide then error("No monitor found!") end

-- === Initialize Devices ===
rednet.open(modemSide)
local mon = peripheral.wrap(monitorSide)
mon.setTextScale(0.5)
mon.clear()

print("Modem opened on "..modemSide..", monitor detected on "..monitorSide..".")
print("Listening for miner updates...")

-- === Drawing ===
local function drawHeader(mon)
    mon.setCursorPos(1,1)
    mon.setTextColor(colors.cyan)
    mon.write("=== ChunkMiner Remote Monitor ===")
    mon.setTextColor(colors.white)
end

local function drawStatus(data)
    mon.clear()
    drawHeader(mon)
    mon.setCursorPos(1,3)
    mon.write("Miner: " .. (data.label or "Unnamed"))
    mon.setCursorPos(1,4)
    mon.write("Status: " .. (data.msg or "Waiting..."))
    mon.setCursorPos(1,5)
    mon.write("Layer: " .. tostring(data.layer or "?"))

    if data.maxDepth then
        local remaining = (data.maxDepth - (data.layer or 0))
        local percent = math.floor(((data.layer or 0) / data.maxDepth) * 100)
        mon.setCursorPos(1,6)
        mon.write(string.format("Remaining: %d layers (%d%%)", remaining, percent))

        -- Draw progress bar
        local barWidth = 30
        local filled = math.floor((percent / 100) * barWidth)
        mon.setCursorPos(1,8)
        mon.write("[")
        for i = 1, barWidth do
            if i <= filled then
                mon.setBackgroundColor(colors.lime)
                mon.write(" ")
                mon.setBackgroundColor(colors.black)
            else
                mon.write(" ")
            end
        end
        mon.write("]")
    end

    mon.setCursorPos(1,10)
    mon.write("Last update: " .. os.date("%H:%M:%S"))
end

-- === Main Loop ===
while true do
    local id, raw, protocol = rednet.receive(PROTOCOL)
    if protocol == PROTOCOL then
        local ok, data = pcall(textutils.unserialize, raw)
        if ok and type(data) == "table" and data.label == "chunkminer" then
            drawStatus(data)
        end
    end
end