Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local sides = require("sides")
- local colors = require("colors")
- local term = require("term")
- local serialization = require("serialization")
- local computer = require("computer")
- local modem = component.modem
- local rs = component.redstone
- if not modem then error("Dieses Programm benoetigt ein Wireless-Modem!") end
- modem.open(225)
- if modem.isWireless() then modem.setStrength(400) end
- local CONFIG_FILE = "schleuse_config.cfg"
- local config = {}
- local status = "BEREIT"
- local innen_entsperrt = false
- local REDSTONE_SEITE = sides.back
- local CABLE_UNGESICHERT = colors.black
- local CABLE_GESICHERT = colors.gray
- local CABLE_CYCLE = colors.yellow
- local letzte_eintritts_seite = CABLE_UNGESICHERT
- local gpu_grau, gpu_schwarz, gpu_innen
- local alle_gpus = {}
- for addr in component.list("gpu") do table.insert(alle_gpus, component.proxy(addr)) end
- if #alle_gpus < 3 then
- error("Fehler: Es werden alle 3 Grafikkarten im Server/Rack benoetigt!")
- end
- local function printZentriert(gpu, maxW, y, text, fore, back, offsetX)
- offsetX = offsetX or 0
- if fore then gpu.setForeground(fore) end
- if back then gpu.setBackground(back) end
- local gekuerzt = string.sub(text, 1, maxW)
- local x = math.max(1, math.floor((maxW - #gekuerzt) / 2) + 1) + offsetX
- gpu.set(x, y, gekuerzt)
- end
- local function testeBildschirmMitIgendeinerGpu(screen_addr, text)
- for _, g in ipairs(alle_gpus) do
- if g.bind(screen_addr) then
- g.setResolution(50, 16) -- Erzwingt großes Layout beim Setup
- local sw, sh = g.getResolution()
- g.setBackground(0x00FF00)
- g.fill(1, 1, sw, sh, " ")
- printZentriert(g, sw, math.floor(sh/2), text, 0x000000, 0x00FF00)
- return g
- end
- end
- return nil
- end
- local function ladeOderErstelleConfig()
- local f = io.open(CONFIG_FILE, "r")
- if f then
- config = serialization.unserialize(f:read("*a")) or {}
- f:close()
- if config.grau and config.schwarz and config.innen and config.lock_id and config.lock_name then
- return
- end
- end
- term.clear()
- print("=== SCHLEUSEN SETUP ===")
- io.write("Anzeigename der Schleuse (z.B. Haupttor): ")
- local lname = io.read()
- if lname == "" then lname = "Unbenannt" end
- io.write("ID der Schleuse zur Verwaltung (z.B. gate_01): ")
- local lid = io.read()
- if lid == "" then lid = "lock_default" end
- print("\nMonitore leuchten nacheinander kurz GRÜN.")
- print("Drücke ENTER zum Starten...")
- io.read()
- config = { lock_name = lname, lock_id = lid }
- local alle_monitore = {}
- for addr in component.list("screen") do table.insert(alle_monitore, addr) end
- for i, addr in ipairs(alle_monitore) do
- local verwendete_gpu = testeBildschirmMitIgendeinerGpu(addr, "MONITOR " .. i)
- term.clear()
- print("Ein Monitor leuchtet jetzt GRÜN.")
- print("Welcher Monitor ist das?")
- print("1: Monitor AUSSEN (Ungesichert)")
- print("2: Monitor BASIS (Gesichert)")
- print("3: Monitor INNENRAUM (Karte)")
- print("4: Ignorieren")
- local auswahl = ""
- while auswahl ~= "1" and auswahl ~= "2" and auswahl ~= "3" and auswahl ~= "4" do
- io.write("Auswahl (1-4): ")
- auswahl = io.read()
- end
- if auswahl == "1" then config.grau = addr
- elseif auswahl == "2" then config.schwarz = addr
- elseif auswahl == "3" then config.innen = addr
- end
- if verwendete_gpu then
- local sw, sh = verwendete_gpu.getResolution()
- verwendete_gpu.setBackground(0x000000)
- verwendete_gpu.fill(1, 1, sw, sh, " ")
- end
- end
- f = io.open(CONFIG_FILE, "w")
- f:write(serialization.serialize(config))
- f:close()
- end
- local function frageServerAn(req_type, detail)
- local alter_status = status
- status = "PRUEFE..."
- if gpu_grau then drawAussenInterface(gpu_grau, "AUSSEN") end
- if gpu_schwarz then drawAussenInterface(gpu_schwarz, "BASIS") end
- if gpu_innen then
- local iw, ih = gpu_innen.getResolution()
- gpu_innen.setBackground(0x000000)
- gpu_innen.fill(1, 1, iw, ih, " ")
- printZentriert(gpu_innen, iw, math.floor(ih/2), "VERBINDE SERVER...", 0xFFFF00, 0x000000)
- end
- modem.broadcast(225, req_type, config.lock_id, detail)
- local start = computer.uptime()
- while computer.uptime() - start < 2 do
- local e = { event.pull(0.2, "modem_message") }
- if e[1] == "modem_message" then
- local _, _, _, _, _, res_type, server_lock_id, allowed, info = table.unpack(e)
- if server_lock_id == config.lock_id then
- if (req_type == "card_auth_req" and res_type == "card_auth_res") or
- (req_type == "button_auth_req" and res_type == "button_auth_res") then
- status = alter_status
- return allowed, info
- end
- end
- end
- end
- status = alter_status
- return false, "SERVER TIMEOUT"
- end
- function drawAussenInterface(gpu, titel)
- local w, h = gpu.getResolution()
- gpu.setBackground(0x000000)
- gpu.fill(1, 1, w, h, " ")
- printZentriert(gpu, w, math.floor(h*0.2), "== " .. config.lock_name .. " ==", 0xFFFFFF, 0x000000)
- if status == "BEREIT" then
- printZentriert(gpu, w, math.floor(h*0.4), "STATUS: BEREIT", 0x00FF00, 0x000000)
- gpu.setBackground(0x333333)
- local btn_y = math.floor(h*0.6)
- gpu.fill(1, btn_y, w, h - btn_y + 1, " ")
- printZentriert(gpu, w, btn_y + math.floor((h-btn_y)/2), "[ OEFFNEN ]", 0xFFFFFF, 0x333333)
- else
- printZentriert(gpu, w, math.floor(h*0.4), "STATUS:", 0xFF0000, 0x000000)
- printZentriert(gpu, w, math.floor(h*0.6), status, 0xFF0000, 0x000000)
- end
- end
- local function drawInnenInterface(gpu)
- local w, h = gpu.getResolution()
- gpu.setBackground(0x000000)
- gpu.fill(1, 1, w, h, " ")
- printZentriert(gpu, w, 1, "=== INNENRAUM ===", 0xFFFFFF, 0x000000)
- if status ~= "BEREIT" then
- printZentriert(gpu, w, math.floor(h/2), status, 0xFF0000, 0x000000)
- return
- end
- local trenn_y = math.floor(h * 0.5)
- if innen_entsperrt then
- local halbe_breite = math.floor(w / 2)
- gpu.setBackground(0x555500)
- gpu.fill(1, 2, halbe_breite, trenn_y - 1, " ")
- printZentriert(gpu, halbe_breite, math.floor(trenn_y/2)+1, "[BASIS]", 0xFFFFFF, 0x555500, 0)
- gpu.setBackground(0x005500)
- gpu.fill(halbe_breite + 1, 2, w - halbe_breite, trenn_y - 1, " ")
- printZentriert(gpu, w - halbe_breite, math.floor(trenn_y/2)+1, "[AUSSEN]", 0xFFFFFF, 0x005500, halbe_breite)
- else
- printZentriert(gpu, w, math.floor(trenn_y/2), "BITTE KARTE", 0xFFFF00, 0x000000)
- printZentriert(gpu, w, math.floor(trenn_y/2)+1, "SCANNEN", 0xFFFF00, 0x000000)
- end
- gpu.setBackground(0x222222)
- gpu.fill(1, trenn_y + 1, w, h - trenn_y, " ")
- local weg_text = (letzte_eintritts_seite == CABLE_UNGESICHERT) and "<- ZURUECK (RAUS)" or "<- ZURUECK (REIN)"
- printZentriert(gpu, w, trenn_y + math.floor((h-trenn_y)/2), weg_text, 0xFFFFFF, 0x222222)
- end
- local function initialisiereFesteGpuZuweisung()
- local pool = {}
- for _, g in ipairs(alle_gpus) do table.insert(pool, g) end
- local function weiseZu(screen_addr)
- for i, g in ipairs(pool) do
- if g.bind(screen_addr) then
- -- HIER KORRIGIERT: Erzwingt ein sauberes, klobiges 50x16 Breitbild-Verhältnis
- g.setResolution(50, 16)
- table.remove(pool, i)
- return g
- end
- end
- return nil
- end
- gpu_grau = weiseZu(config.grau)
- gpu_schwarz = weiseZu(config.schwarz)
- gpu_innen = weiseZu(config.innen)
- if not gpu_grau or not gpu_schwarz or not gpu_innen then
- error("Konnte den Monitoren keine Grafikkarte zuweisen!")
- end
- end
- local function updateAlleMonitore()
- if not gpu_grau or not gpu_schwarz or not gpu_innen then return end
- drawAussenInterface(gpu_grau, "AUSSEN")
- drawAussenInterface(gpu_schwarz, "BASIS")
- drawInnenInterface(gpu_innen)
- end
- local function oeffneTuer(farbe, mitCycle)
- if mitCycle then
- status = "CYCLE..."
- updateAlleMonitore()
- rs.setBundledOutput(REDSTONE_SEITE, CABLE_CYCLE, 255)
- os.sleep(5)
- rs.setBundledOutput(REDSTONE_SEITE, CABLE_CYCLE, 0)
- end
- status = "OEFFNE..."
- updateAlleMonitore()
- rs.setBundledOutput(REDSTONE_SEITE, farbe, 255)
- os.sleep(12)
- status = "OFFEN"
- updateAlleMonitore()
- os.sleep(5)
- status = "SCHLIESSEN"
- updateAlleMonitore()
- rs.setBundledOutput(REDSTONE_SEITE, farbe, 0)
- os.sleep(2)
- status = "ZULOCKEN..."
- updateAlleMonitore()
- os.sleep(8)
- status = "BEREIT"
- innen_entsperrt = false
- updateAlleMonitore()
- end
- local function zeigeFehler(text)
- if not gpu_innen then return end
- local iw, ih = gpu_innen.getResolution()
- gpu_innen.setBackground(0x550000)
- gpu_innen.fill(1, 1, iw, ih, " ")
- printZentriert(gpu_innen, iw, math.floor(ih/2), text, 0xFFFFFF, 0x550000)
- os.sleep(2)
- updateAlleMonitore()
- end
- -------------------------------------------------------------------------------
- -- MAIN RUNTIME START
- -------------------------------------------------------------------------------
- ladeOderErstelleConfig()
- initialisiereFesteGpuZuweisung()
- term.clear()
- print("[ONLINE] '" .. config.lock_name .. "' (" .. config.lock_id .. ") aktiv.")
- updateAlleMonitore()
- while true do
- local eventDaten = { event.pull() }
- local id = eventDaten[1]
- if id == "touch" and status == "BEREIT" then
- local screen_addr, x, y = eventDaten[2], eventDaten[3], eventDaten[4]
- -- AUSSEN
- if screen_addr == config.grau then
- local _, h = gpu_grau.getResolution()
- if y >= math.floor(h*0.6) then
- local erlaubt, fehler = frageServerAn("button_auth_req", "aussen")
- if erlaubt then
- letzte_eintritts_seite = CABLE_UNGESICHERT
- oeffneTuer(CABLE_UNGESICHERT, false)
- else zeigeFehler(fehler) end
- end
- -- BASIS
- elseif screen_addr == config.schwarz then
- local _, h = gpu_schwarz.getResolution()
- if y >= math.floor(h*0.6) then
- local erlaubt, fehler = frageServerAn("button_auth_req", "basis")
- if erlaubt then
- letzte_eintritts_seite = CABLE_GESICHERT
- oeffneTuer(CABLE_GESICHERT, false)
- else zeigeFehler(fehler) end
- end
- -- INNENRAUM
- elseif screen_addr == config.innen then
- local w, h = gpu_innen.getResolution()
- local trenn_y = math.floor(h * 0.5)
- if y >= 2 and y <= trenn_y and innen_entsperrt then
- if x <= math.floor(w / 2) then
- oeffneTuer(CABLE_GESICHERT, true)
- else
- oeffneTuer(CABLE_UNGESICHERT, true)
- end
- elseif y > trenn_y then
- local erlaubt, fehler = frageServerAn("button_auth_req", "rueckweg")
- if erlaubt then
- oeffneTuer(letzte_eintritts_seite, true)
- else zeigeFehler(fehler) end
- end
- end
- elseif id == "magData" and status == "BEREIT" then
- local card_id = eventDaten[4]
- local erlaubt, fehler = frageServerAn("card_auth_req", card_id)
- if erlaubt then
- innen_entsperrt = true
- updateAlleMonitore()
- else
- zeigeFehler(fehler)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment