Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.pullEvent=os.pullEventRaw
- width, height = term.getSize()
- -- Start of persistent variables
- --Copyright (C) 2012 Mads Clausen, Tomoyo Apps http://www.tomoyo.uphero.com
- function exists(name)
- if fs.exists('.vars/'..name..'.txt') then
- return true
- end
- end
- function getType(name)
- if exists(name) then
- local file = io.open('.vars/'..name..'.txt', 'r')
- local line = file:read('*l')
- file:close()
- return line
- else
- print('var.getType(): Variable does not exist')
- error()
- end
- end
- function getValue(name)
- if exists(name) then
- if getType(name) ~= 'table' then
- local file = io.open('.vars/'..name..'.txt', 'r')
- local line = file:read('*l')
- line = file:read('*l')
- file:close()
- if getType(name) == 'number' then
- return tonumber(line)
- elseif getType(name) == 'string' then
- return line
- elseif getType(name) == 'boolean' then
- if line == 'true' then
- return true
- elseif line == 'false' then
- return false
- end
- elseif getType(name) == 'nil' then
- return nil
- end
- else
- local file = io.open('.vars/'..name..'.txt', 'r')
- local line = file:read('*l')
- local lines = {}
- local returnTable = {}
- local i = 1
- line = file:read('*l')
- while line ~= nil do
- lines[i] = line
- line = file:read('*l')
- i = i + 1
- end
- for _i = 1, #lines do
- local pos = string.find(lines[_i], '_')
- local _type = string.sub(lines[_i], 1, pos - 1)
- local value = string.sub(lines[_i], pos + 1)
- if _type == 'number' then
- returnTable[_i] = tonumber(value)
- elseif _type == 'string' then
- returnTable[_i] = value
- elseif _type == 'boolean' then
- if value == 'true' then
- returnTable[_i] = true
- elseif value == 'false' then
- returnTable[_i] = false
- end
- elseif _type == 'nil' then
- returnTable[_i] = nil
- elseif _type == 'table' then
- print('var.getValue(): You cannot have a table inside a table')
- end
- end
- return returnTable
- end
- else
- print('var.getValue(): Variable does not exist')
- error()
- end
- end
- function setType(name, _type)
- if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
- print('var.setType(): Unsupported type, or the variable already exists')
- error()
- else
- local file = io.open('.vars/'..name..'.txt', 'w')
- file:write(_type)
- file:close()
- end
- end
- function setValue(name, value)
- if exists(name) then
- if tostring(type(value)) ~= getType(name) then
- print('var.setValue(): Wrong type: '..type(value))
- error()
- end
- if getType(name) ~= 'table' then
- local file = io.open('.vars/'..name..'.txt', 'r')
- local firstLine = file:read('*l')
- file:close()
- local file = io.open('.vars/'..name..'.txt', 'w')
- file:write(firstLine..'\n'..tostring(value))
- file:close()
- else
- local file = io.open('.vars/'..name..'.txt', 'r')
- local firstLine = file:read('*l')
- file:close()
- local file = io.open('.vars/'..name..'.txt', 'w')
- file:write(firstLine..'\n')
- for i = 1, #value do
- file:write(type(value[i])..'_'..tostring(value[i])..' \n')
- end
- file:close()
- end
- else
- print('var.setValue(): Variable does not exist')
- error()
- end
- end
- function create(name, _type)
- if not fs.exists('.vars') then fs.makeDir('.vars') end
- if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
- print('var.create(): Unsupported type, or the variable already exists')
- error()
- else
- local file = io.open('.vars/'..name..'.txt', 'w')
- file:write(_type)
- file:close()
- end
- end
- function delete(name)
- if exists(name) then
- fs.delete('.vars/'..name..'.txt')
- else
- print('var.delete(): Variable does not exist')
- error()
- end
- end
- -- End of persistant variables
- local int = 1
- function newi(inp)
- if inp == "reset" then
- int = 1
- elseif inp == "more" then
- int = int + 1
- if int > 5 then
- int = 1
- end
- elseif inp == "less" then
- int = int - 1
- if int < 1 then
- int = 5
- end
- end
- draw()
- end
- function fc()
- shell.run("clear")
- print("Where is the Redstone-Output for the PCs located?")
- write("PCs: ")
- pcside = read()
- print("Where is the Redstone-Output for the Lamps located?")
- write("Lamps: ")
- lside = read()
- print("On logout?")
- print("[1]Turn off lamps and disable PCs")
- print("[2]PCs and lamps stay the way they are")
- write("On logout: ")
- onlogout = read()
- if onlogout == "1" then onlogout = 1
- elseif onlogout == "2" then onlogout = 2
- else onlogout = 1 end
- print("Set a terminal Login")
- write("User: ")
- user = read()
- write("Password: ")
- pass1 = read("*")
- write("Confirm: ")
- pass2 = read("*")
- if pass1 == pass2 then
- shell.run("clear")
- print("Are these options correct?")
- print("")
- print("PC-Side: "..pcside)
- print("Lamp-Side: "..lside)
- write("On Logout: ")
- if onlogout == 1 then
- print("Turn off everything")
- elseif onlogout == 2 then
- print("Keep state of devices")
- end
- print("Username: "..user)
- print("")
- write("y/n: ")
- confirm = read()
- if confirm == "Y" or confirm == "y" then
- if exists ("pass") then delete("pass") end
- if exists ("fconf") then delete("fconf") end
- if exists ("pcside") then delete("pcside") end
- if exists ("user") then delete("user") end
- if exists ("lside") then delete("lside") end
- if exists ("logout") then delete("logout") end
- print("Saving config")
- create("logout", "number")
- create("pass", "string")
- create("fconf", "string")
- create("pcside", "string")
- create("user", "string")
- create("lside", "string")
- setValue("logout", onlogout)
- setValue("pcside", pcside)
- setValue("lside", lside)
- setValue("user", user)
- setValue("pass", pass1)
- setValue("fconf", "true")
- os.reboot()
- else
- os.reboot()
- end
- else
- print("Passwords do not match")
- sleep(2)
- os.reboot()
- end
- end
- function draw()
- shell.run("clear")
- print("What do you want to do?")
- print("")
- if int == 1 then
- print("<Unlock computers>")
- print(" Change lights")
- print(" Reconfigure")
- print(" Show Current State")
- print(" Logout")
- elseif int == 2 then
- print(" Unlock computers")
- print("<Change lights>")
- print(" Reconfigure")
- print(" Show Current State")
- print(" Logout")
- elseif int == 3 then
- print(" Unlock computers")
- print(" Change lights")
- print("<Reconfigure>")
- print(" Show Current State")
- print(" Logout")
- elseif int == 4 then
- print(" Unlock computers")
- print(" Change lights")
- print(" Reconfigure")
- print("<Show Current State>")
- print(" Logout")
- elseif int == 5 then
- print(" Unlock computers")
- print(" Change lights")
- print(" Reconfigure")
- print(" Show Current State")
- print("<Logout>")
- end
- print("")
- print("")
- print("[UP][DOWN] - Navigate")
- print("[ENTER] - Select")
- print("[S] - Shutdown the terminal")
- listen()
- end
- function listen()
- while true do
- event, key = os.pullEventRaw('key')
- if key == 200 then
- newi('less')
- elseif key == 208 then
- newi('more')
- elseif key == 31 then
- os.shutdown()
- elseif key == 28 then
- domenuentry()
- end
- end
- end
- pc = false
- function pct()
- shell.run("clear")
- if pc == false then
- print("Activate Computers...")
- rs.setOutput(getValue("pcside"), true)
- pc = true
- print("Booting PCs enabled")
- elseif pc == true then
- print("Deactivate Computers...")
- rs.setOutput(getValue("pcside"), false)
- pc = false
- print("Booting PCs disabled")
- end
- sleep(2)
- newi("reset")
- end
- light = false
- function tl()
- shell.run("clear")
- if light == false then
- print("Turning on the lights...")
- rs.setOutput(getValue("lside"), true)
- light = true
- elseif light == true then
- print("Turning off the Lights...")
- rs.setOutput(getValue("lside"), false)
- light = false
- end
- print("Light toggled")
- sleep(2)
- newi("reset")
- end
- function show()
- shell.run("clear")
- print("PC-Side: "..getValue("pcside"))
- print("Lamp-Side: "..getValue("lside"))
- print("Lamps on: "..tostring(light))
- print("PCs active: "..tostring(pc))
- write("On logout: ")
- if getValue("logout") == 1 then
- print("Turn everything off")
- elseif getValue("logout") == 2 then
- print("Keep State")
- end
- print("Username: "..getValue("user"))
- shell.run("time")
- print("Press any key to continiue...")
- while true do
- event, key = os.pullEventRaw('key')
- if key == 1 then
- else
- newi("reset")
- end
- end
- end
- function logout()
- if getValue("logout") == 1 then
- os.reboot()
- elseif getValue("logout") == 2 then
- login()
- end
- end
- function domenuentry()
- if int == 1 then pct()
- elseif int == 2 then tl()
- elseif int == 3 then fc()
- elseif int == 4 then show()
- elseif int == 5 then logout()
- end
- end
- function login()
- shell.run("clear")
- if exists("fconf") then
- term.setCursorPos(width/2 - 12, height/2 - 2)
- print("+----------------------+")
- term.setCursorPos(width/2 - 12, height/2 - 1)
- print("|Username: |")
- term.setCursorPos(width/2 - 12, height/2)
- print("|Password: |")
- term.setCursorPos(width/2 - 12, height/2 + 1)
- print("+----------------------+")
- term.setCursorPos(width/2 - 1, height/2 - 1)
- user = read()
- term.setCursorPos(width/2 + 11, height/2 - 1)
- write("|")
- term.setCursorPos(width/2 - 1, height/2)
- pass2 = read("*")
- term.setCursorPos(width/2 + 11, height/2)
- write("|")
- if user == getValue("user") then
- if pass2 == getValue("pass") then
- newi("reset")
- else
- print("Wrong password")
- sleep(2)
- os.reboot()
- end
- else
- print("Wrong username")
- sleep(2)
- os.reboot()
- end
- else
- fc()
- end
- end
- login()
Advertisement
Add Comment
Please, Sign In to add comment