CCCPaste

reactorInfoServer

-- Reactor Info Server
-- Reads reactor statistics and broadcasts them via rednet every 0.5 seconds

-- Configuration
local BROADCAST_INTERVAL = 0.5
local PROTOCOL = "reactor_stats"

-- Initialize peripherals
local r = peripheral.wrap("bottom")
if not r then
    print("Error: No reactor found on bottom!")
    return
end

-- Open rednet on any available modem
local modem = nil
for _, side in ipairs(peripheral.getNames()) do
    if peripheral.getType(side) == "modem" then
        modem = peripheral.wrap(side)
        if modem.isWireless() then
            rednet.open(side)
            print("Rednet opened on wireless modem: " .. side)
            break
        end
    end
end

if not modem then
    print("Error: No wireless modem found!")
    return
end

print("Reactor Info Server Started")
print("Broadcasting on protocol: " .. PROTOCOL)
print("Interval: " .. BROADCAST_INTERVAL .. " seconds")
print("Press Ctrl+T to stop")
print("")

while true do
    -- Get reactor information
    local success, info = pcall(function() return r.getReactorInfo() end)
    
    if success and info then
        -- Print statistics to local terminal
        term.clear()
        term.setCursorPos(1, 1)
        print("=== REACTOR SERVER STATUS ===")
        print("Status: " .. tostring(info.status))
        print("Temperature: " .. string.format("%.2f C", info.temperature))
        print("Generation: " .. string.format("%.2f kRF/t", info.generationRate / 1000))
        print("Field Drain: " .. string.format("%.2f kRF/t", info.fieldDrainRate / 1000))
        print("Net Energy: " .. string.format("%.2f kRF/t", (info.generationRate - info.fieldDrainRate) / 1000))
        print("Field Strength: " .. string.format("%.2f%%", (info.fieldStrength / info.maxFieldStrength) * 100))
        print("Energy Saturation: " .. string.format("%.2f%%", (info.energySaturation / info.maxEnergySaturation) * 100))
        
        -- Add fuel information if available
        if info.fuelConversion and info.maxFuelConversion then
            print("Fuel Progress: " .. string.format("%.2f%%", (info.fuelConversion / info.maxFuelConversion) * 100))
        end
        if info.fuelConversionRate then
            print("Fuel Rate: " .. string.format("%.3f nb/t", info.fuelConversionRate / 1000))
        end
        
        print("")
        print("Broadcasting to clients...")
        
        -- Broadcast the reactor info to all clients
        rednet.broadcast(info, PROTOCOL)
        
    else
        term.clear()
        term.setCursorPos(1, 1)
        print("=== REACTOR SERVER ERROR ===")
        print("Failed to read reactor data!")
        print("Error: " .. tostring(info))
        print("")
        print("Retrying in " .. BROADCAST_INTERVAL .. " seconds...")
    end
    
    sleep(BROADCAST_INTERVAL)
end