CCCPaste

Config editor reactor server

local configPath = "config"

-- =========================
-- FUNZIONI DI SUPPORTO
-- =========================
local function loadConfig()
    if not fs.exists(configPath) then return nil end
    local f = fs.open(configPath, "r")
    local data = f.readAll()
    f.close()
    local func = loadstring(data)
    if func then return func() else error("Errore nel config") end
end

local function saveConfig(cfg)
    local f = fs.open(configPath, "w")
    f.write("return " .. textutils.serialize(cfg))
    f.close()
end

local function listReactors(cfg)
    print("\nLista reattori:")
    for i, r in ipairs(cfg.reactors) do
        print(string.format(" [%d] Nome: %s | ID: %d | Colore: %s", i, r.name, r.id, r.color))
    end
end

local function getValidColors()
    local t = {}
    for k, v in pairs(colors) do
        t[k] = v
    end
    return t
end

-- =========================
-- MAIN
-- =========================
term.setTextColor(colors.cyan)
print("=== Editor Configurazione Reattori ===")
term.setTextColor(colors.white)

local config = loadConfig()
if not config then
    print("Nessun file di configurazione trovato!")
    return
end

local validColors = getValidColors()

while true do
    print("\nCosa vuoi modificare?")
    print("[1] Intervallo aggiornamento (attuale: " .. tostring(config.update_interval) .. "s)")
    print("[2] Dati di un reattore")
    print("[3] Aggiungi nuovo reattore")
    print("[4] Elimina un reattore")
    print("[5] Esci")
    write("> ")
    local choice = read()

    if choice == "1" then
        write("Nuovo intervallo in secondi: ")
        local n = tonumber(read())
        if n and n > 0 then
            config.update_interval = n
            print("Aggiornato a " .. n .. " secondi.")
        else
            print("Valore non valido.")
        end

    elseif choice == "2" then
        if #config.reactors == 0 then
            print("Nessun reattore.")
        else
            listReactors(config)
            write("Seleziona indice reattore: ")
            local idx = tonumber(read())
            local r = config.reactors[idx]
            if r then
                print("\nCosa vuoi modificare?")
                print("[1] Nome (" .. r.name .. ")")
                print("[2] ID (" .. r.id .. ")")
                print("[3] Colore (" .. r.color .. ")")
                write("> ")
                local sub = read()
                if sub == "1" then
                    write("Nuovo nome: ")
                    r.name = read()
                elseif sub == "2" then
                    local id
                    repeat
                        write("Nuovo ID rednet (numero): ")
                        id = tonumber(read())
                        if not id then print("Inserisci un numero valido!") end
                    until id
                    r.id = id
                elseif sub == "3" then
                    local color
                    repeat
                        write("Nuovo colore (white, red, blue...): ")
                        color = read()
                        if not validColors[color] then print("Colore non valido!") end
                    until validColors[color]
                    r.color = color
                else
                    print("Scelta non valida.")
                end
            else
                print("Indice non valido.")
            end
        end

    elseif choice == "3" then
        if #config.reactors >= 8 then
            print("Numero massimo di reattori (8) raggiunto!")
        else
            local name, id, color
            write("Nome reattore: ") name = read()
            repeat
                write("ID rednet (numero): ") id = tonumber(read())
                if not id then print("Inserisci un numero valido!") end
            until id
            repeat
                write("Colore assegnato al reattore (white, blue...): ") color = read()
                if not validColors[color] then print("Colore non valido!") end
            until validColors[color]
            table.insert(config.reactors, { name = name, id = id, color = color })
            print("Reattore aggiunto.")
        end

    elseif choice == "4" then
        if #config.reactors == 0 then
            print("Nessun reattore da eliminare.")
        else
            listReactors(config)
            write("Indice reattore da eliminare: ")
            local idx = tonumber(read())
            if idx and config.reactors[idx] then
                table.remove(config.reactors, idx)
                print("Reattore rimosso.")
            else
                print("Indice non valido.")
            end
        end

    elseif choice == "5" then
        break
    else
        print("Scelta non valida.")
    end

    -- Salvataggio automatico a ogni modifica
    saveConfig(config)
    print("\nConfig salvata.")
end

term.setTextColor(colors.green)
print("Uscita dall’editor.")