Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require "component"
- local net = require "internet"
- local event = require "event"
- local fs = require "filesystem"
- local rd = component.os_rolldoorcontroller
- local magcard = component.os_magreader
- local lamp = component.colorful_lamp
- magcard.setEventName "magswipe"
- rd.setSpeed(3)
- rd.close()
- local function set_lamp(r, g, b)
- lamp.setLampColor(r * 1024 + g * 32 + b)
- end
- local function fetch(url)
- local txt = ""
- for chunk in net.request(url) do
- txt = txt .. chunk
- end
- return txt
- end
- local function read_file(file)
- return io.open(file):read "*a"
- end
- local function load_data(dat)
- local fn, err = load("return " .. dat, "=data", "t", {})
- if not fn then return false, err
- else
- local ok, res = pcall(fn)
- if not ok then return false, res
- else return true, res end
- end
- end
- local function open()
- rd.open()
- os.sleep(3)
- rd.close()
- end
- local ok, config = load_data(read_file "/home/conf.lua")
- if not ok then error(config) end
- if not config.door_name then error "Door name not specified." end
- if not config.data_URL then error "Data URL not specified." end
- local function authorized(player, card)
- print("Player:", player, "Card data:", card)
- local ok, sysdata = load_data(fetch(config.data_URL))
- if not ok then print("DOOR DATA FORMAT ERROR:", sysdata) return false end
- if player ~= card then return false end
- local player_clearance = sysdata.players[player]
- if not player_clearance then print("Player", player, "not found") return false end
- local door_clearance = sysdata.doors[config.door_name]
- return door_clearance <= player_clearance
- end
- while true do
- set_lamp(0, 0, 31)
- local _, _, player_name, card_data, player_UUID = event.pull "magswipe"
- if authorized(player_name, card_data) then
- set_lamp(0, 31, 0)
- open()
- else
- set_lamp(31, 0, 0)
- os.sleep(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement