exploder2013

ComputerCraft Door Security

Jan 17th, 2013
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.97 KB | None | 0 0
  1. -- [[ CENTER TEXT ]]
  2. function printcenter(sText)
  3.     local w, h = term.getSize()
  4.     local x, y = term.getCursorPos()
  5.     x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
  6.     term.setCursorPos(x, y)
  7.     print(sText)
  8. end
  9.  
  10. -- [[ Login Func ]]
  11. function login()
  12.   term.clear()
  13.   term.setCursorPos(1,1)
  14.   printcenter("Please enter your details")
  15.   while true do
  16.     term.setCursorPos(1,3)
  17.     term.clearLine()
  18.     write("Username: ")
  19.     user = read()
  20.     if user == "" then
  21.  
  22.     elseif user == keyw then
  23.       term.setBackgroundColor(colors.black)
  24.       term.clear()
  25.       term.setCursorPos(1,1)
  26.       error()
  27.  
  28.     elseif user == username then break
  29.  
  30.   end
  31. end
  32.  
  33.   while true do
  34.     term.setCursorPos(1,5)
  35.     term.clearLine()
  36.     write("Password: ")
  37.     pass = read("*")
  38.     if pass == "" then
  39.  
  40.     elseif pass == keyw then
  41.  
  42.       term.setBackgroundColor(colors.black)
  43.       term.clear()
  44.       term.setCursorPos(1,1)
  45.       error()
  46.     elseif pass ~= keyw or password then break
  47.     elseif pass == password then break
  48.   end
  49. end
  50. -- [[ Accepting Password, Username ]]
  51.   if user == username and pass == password then
  52.     term.setCursorPos(1,7)
  53.     term.setTextColor(colors.orange)
  54.     printcenter("Access graned.")
  55.     rs.setOutput(side,true)
  56.     sleep(3.5)
  57.     rs.setOutput(side,false)
  58.     os.reboot()
  59.   else
  60.     term.setCursorPos(1,7)
  61.     term.setTextColor(colors.red)
  62.     printcenter("Access denided.")
  63.     printcenter("Please try again.")
  64.     sleep(1.5)
  65.     os.reboot()
  66.   end
  67. end
  68.  
  69.  
  70.  
  71. -- [[ Saving Func ]]
  72. function save(name,input)
  73.   local file = fs.open("security/door/"..name,"w")
  74.   file.writeLine(input)
  75.   file.close()
  76. end
  77.  
  78. -- [[ Load Func ]]
  79. function load(fileName)
  80.   file = fs.open("security/door/"..fileName,"r")
  81.   local line = file.readLine()
  82.   file.close()
  83.   return line or "none"
  84. end
  85.  
  86. -- [[ Process func ]]
  87. function process()
  88.   term.setCursorPos(1,13)
  89.   printcenter("Processing your info, please wait.")
  90.   sleep(1.5)
  91.   term.setCursorPos(1,15)
  92.   printcenter("Processing your info, please wait..")
  93.   sleep(1.5)
  94.   term.setCursorPos(1,17)
  95.   printcenter("Done!")
  96.   sleep(1)
  97.   term.clear()
  98.   term.setCursorPos(1,4)
  99. -- [[ Display input ]]
  100.   print("              :------------------------:")
  101.   print("              : Username: "..usern)
  102.   print("              :                        ")
  103.   print("              : Password: "..passw)
  104.   print("              :                        ")
  105.   if keyw == "" then print("              : Code: NONE")
  106.   else print("              : Code: "..keyw)
  107.   end
  108.   print("              :                        ")
  109.   print("              : Side: "..side)
  110.   print("              :------------------------:")
  111.   print("")
  112.   print("              :---------:   :----------:")
  113.   print("              :Try again:   : Continue :")
  114.   print("              :---------:   :----------:")
  115.  
  116. -- [[ Buttons ]]
  117.   while true do
  118.     event, param, x,y = os.pullEvent("mouse_click")
  119.     if event == "mouse_click" and (x >= 16 and x <= 24) and y == 15 then os.reboot() -- Button 1.
  120.     elseif event == "mouse_click" and (x >= 30 and x <= 39) and y == 15 then -- Button 2.
  121.     fs.makeDir("security/door/")
  122.     save("info",usern) --Username
  123.     save("password",passw) --Password
  124.     save("side",side) --Door Side
  125.     if keyw == "" then
  126.       os.reboot()
  127.     else
  128.       save("keyw",keyw) --OS access code
  129.       os.reboot()
  130.   end
  131. end
  132. end
  133. end
  134.  
  135. -- [[ Checking for first run ]]
  136. if not term.isColor() then
  137.   print("Program requires an advanced computer.")
  138.   return
  139. end
  140.  
  141. term.setCursorPos(1,1)
  142. term.clear()
  143. term.setBackgroundColor(colors.blue)
  144. term.setTextColor(colors.white)
  145. term.clear()
  146. if fs.exists("security/door/info") then
  147. else
  148.   term.setCursorPos(1,1)
  149.   printcenter("This is your first run")
  150.   term.setCursorPos(1,2)
  151.   printcenter(" Please make a new account.")
  152.   while true do
  153.     term.setCursorPos(1,4)
  154.     term.clearLine()
  155.     write("Enter your username: ")
  156.     usern = read()
  157.     if usern == "" then
  158.  
  159.     else
  160.       while true do
  161.         term.setCursorPos(1,6)
  162.         write("Enter your password: ")
  163.         passw = read("*")
  164.         if passw == "" then
  165.  
  166.         elseif passw == usern then
  167.  
  168.           term.setCursorPos(1,6)
  169.           print("Password can't be same as username.")
  170.           sleep(1.5)
  171.           term.setCursorPos(1,6)
  172.           term.clearLine()
  173.         else
  174.           term.setCursorPos(1,8)
  175.           write("Debug Password(blank for none): ")
  176.           keyw = read()
  177.           while true do
  178.             term.setCursorPos(1,10)
  179.             print("Please enter door side(right,left,bottom,top)")
  180.             term.setCursorPos(1,11)
  181.             term.clearLine()
  182.             write("Side: ")
  183.             side = read()
  184.             if side == "" then  
  185.  
  186.             elseif side == "right" then process()
  187.             elseif side == "left" then process()
  188.             elseif side == "bottom" then process()
  189.             elseif side == "top" then process()
  190.  
  191.             end
  192.           end
  193.         end
  194.       end
  195.     end
  196.   end
  197. end
  198.  
  199. -- [[ Run ]]
  200. os.pullEvent = os.pullEventRaw
  201. if fs.exists("security/door/keyw") then
  202.   keyw = load("keyw")
  203.   username = load("info")
  204.   password = load("password")
  205.   side = load("side")
  206.   login()
  207. else
  208.  
  209.   username = load("info")
  210.   password = load("password")
  211.   side = load("side")
  212.   login()
  213.  
  214. end
Advertisement
Add Comment
Please, Sign In to add comment