local allowedFile = "allowed_users"
-- Colori RS Latch
local COLOR_APERTO = colors.green
local COLOR_CHIUSO = colors.red
-- Funzione XOR di criptazione
local function xorCrypt(str, key)
local res = {}
for i = 1, #str do
local k = string.byte(key, ((i - 1) % #key) + 1)
local c = string.byte(str, i)
table.insert(res, string.char(bit.bxor(c, k)))
end
return table.concat(res)
end
-- Carica utenti autorizzati
local function loadAllowed()
if not fs.exists(allowedFile) then return {} end
local f = fs.open(allowedFile, "r")
local enc = f.readAll()
f.close()
local dec = xorCrypt(enc, "passwordchiave")
local t = textutils.unserialize(dec)
return t or {}
end
-- Legge stato dal latch
local function leggiStato()
local bund = redstone.getBundledInput("bottom")
if bit.band(bund, COLOR_APERTO) ~= 0 then
return true
elseif bit.band(bund, COLOR_CHIUSO) ~= 0 then
return false
else
return true
end
end
-- Imposta latch
local function impostaStato(nuovoStato)
if nuovoStato then
redstone.setBundledOutput("bottom", COLOR_APERTO)
else
redstone.setBundledOutput("bottom", COLOR_CHIUSO)
end
sleep(0.2)
redstone.setBundledOutput("bottom", 0) -- reset segnali
end
-- Animazione apertura/chiusura
local function movimento(stato)
if stato then
-- CHIUDO
redstone.setBundledOutput("bottom", colors.magenta)
sleep(0.2)
for i = 0, 2 do
redstone.setBundledOutput("top", colors.white)
sleep(1)
redstone.setBundledOutput("top", 0)
print("abbasso")
sleep(0.1)
end
redstone.setBundledOutput("bottom", 0)
else
-- APRO
for i = 0, 2 do
redstone.setBundledOutput("top", colors.lightBlue)
sleep(1)
redstone.setBundledOutput("top", 0)
print("alzo")
sleep(0.2)
end
redstone.setBundledOutput("bottom", colors.blue)
sleep(0.2)
redstone.setBundledOutput("bottom", 0)
end
end
-- MAIN LOOP
local allowed = loadAllowed()
local stato = leggiStato()
print("Sistema avviato. Stato iniziale: ", stato and "APERTO" or "CHIUSO")
while true do
local event, user = os.pullEvent("player")
for _, name in ipairs(allowed) do
if user == name then
stato = not stato
movimento(stato)
impostaStato(stato)
print("Nuovo stato: ", stato and "APERTO" or "CHIUSO")
end
end
end