Advertisement
Flawedspirit

Password API (Complex)

Mar 8th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. --[[
  2. This is a program for ComputerCraft
  3.  
  4. Instructions:
  5. 1. Login to your computer's prompt
  6.    -Your computer is usually there by default
  7. 2. Download with "pastebin get 5dNPx84w startup"
  8. 3. IMPORTANT! Make sure to view or change the default
  9.     password by typing "edit startup" and changing line 19
  10.    -The password can also be changed in-program
  11.    -However, it will RESET to the default if the computer restarts!
  12. 4. Make sure to change line 18 to reflect either where
  13.    the door is, or a redstone wire connection
  14. 5. Run by typing "startup"
  15.    or by rebooting your computer (CTRL-R)
  16. ]]
  17.  
  18. local side ="left" --Side the door is on relation to the computer
  19. local password = "password" --Make sure to change this!
  20. local openTime = 3 --Time in seconds the door will stay open
  21. local input = ""
  22. local status = "Door status: CLOSED" --Default status flag DO NOT EDIT
  23.  
  24. function getInput(prompt)
  25.   print(prompt)
  26.   term.write(">")
  27.   input = read("*")
  28. end
  29.  
  30. function validate(input)
  31.   if input == password then
  32.       return true
  33.   else
  34.       return false
  35.   end
  36. end
  37.  
  38. function openDoor(time)
  39.   status = "Door status: OPEN"
  40.   setFooter(status)
  41.   rs.setOutput(side, true)
  42.   sleep(time)
  43.   status = "Door status: CLOSED"
  44.   setFooter(status)
  45.   rs.setOutput(side, false)
  46. end
  47.  
  48. function setFooter(text)
  49.   term.setCursorPos(1, 19)
  50.   term.clearLine()
  51.   term.write(text)
  52. end
  53.  
  54. function callMainMenu()
  55.   term.clear()
  56.   setFooter(status)
  57.   term.setCursorPos(1, 1)
  58.   print("Flawedspirit Imperial Dynamics Inc.")
  59.   print("Access Mediator v1.1")
  60.   term.setCursorPos(1, 4)
  61.   print("1. Relock accessway")
  62.   print("2. Change password")
  63.   print("3. Override door")
  64.   print("4. Open access for " .. openTime .. " seconds")
  65.   print("5. Change activation time")
  66.   term.write(">")
  67.   local event, input = os.pullEvent("char")
  68.  
  69.   if input == "1" then
  70.     print("Relocking...")
  71.     sleep(1)
  72.     start()
  73.   elseif input == "2" then
  74.     changePass()
  75.   elseif input == "3" then
  76.     overrideDoor(input)
  77.   elseif input == "4" then
  78.       openDoor(openTime)
  79.       callMainMenu()
  80.   elseif input == "5" then
  81.       changePulse()
  82.   else
  83.     print("**Invalid option**")
  84.     sleep(2)
  85.     callMainMenu()
  86.   end
  87. end
  88.  
  89. function changePass()
  90.   term.clear()
  91.   setFooter(status)
  92.   term.setCursorPos(1, 1)
  93.   print("Change Password:")
  94.   term.setCursorPos(1, 3)
  95.  
  96.   getInput("Enter current access code:")
  97.  
  98.   if validate(input) == true then
  99.     getInput("Enter new access code:")
  100.    
  101.     --New password must be entered twice
  102.     local newCode1 = input
  103.     getInput("Enter new access code again:")
  104.     local newCode2 = input
  105.  
  106.     --Make sure new password is correct
  107.     if newCode1 == newCode2 then
  108.       password = newCode2
  109.       print("Password changed.")
  110.       sleep(2)
  111.       start()
  112.     else
  113.       print("**The new passwords did not match**")
  114.       sleep(2)
  115.       changePass()
  116.     end
  117.   else
  118.     print("**Incorrect password**")
  119.     sleep(2)
  120.     start()
  121.   end
  122. end
  123.  
  124. function overrideDoor(input)
  125.   if input == "3" and not redstone.getOutput(side) then
  126.     status = "Door status: FORCE OPEN"
  127.     setFooter(status)
  128.     redstone.setOutput(side, true)
  129.   elseif input == "3" and redstone.getOutput(side) then
  130.     status = "Door status: CLOSED"
  131.     setFooter(status)
  132.     redstone.setOutput(side, false)
  133.   end
  134.   sleep(2)
  135.   callMainMenu()
  136. end
  137.  
  138. function changePulse()
  139.   term.clear()
  140.   setFooter(status)
  141.   term.setCursorPos(1, 1)
  142.   print("Change Activation Time:")
  143.   term.setCursorPos(1, 3)
  144.   print("1. Short      (3 sec)")
  145.   print("2. Long       (5 sec)")
  146.   print("3. Extra Long (10 sec)")
  147.   print("4. Back")
  148.   term.write(">")
  149.   local event, input = os.pullEvent("char")
  150.  
  151.   if input == "1" then
  152.     openTime = 3
  153.   elseif input == "2" then
  154.     openTime = 5
  155.   elseif input == "3" then
  156.     openTime = 10
  157.   elseif input == "4" then
  158.     callMainMenu()
  159.   else
  160.     print("**Invalid option**")
  161.     sleep(2)
  162.     changePulse()
  163.   end
  164.  
  165.   print("Activation time changed.")
  166.   sleep(2)
  167.   callMainMenu()
  168. end
  169.  
  170. function start()
  171.   --Prevent user from terminating program early
  172.   local pullEvent = os.pullEvent
  173.   os.pullEvent = os.pullEventRaw
  174.  
  175.   term.clear()
  176.   setFooter(status)
  177.   term.setCursorPos(1, 1)
  178.   getInput("Enter access code:")
  179.  
  180.   if validate(input) == true then
  181.     openDoor(openTime)
  182.     callMainMenu()
  183.   else
  184.     print("**Incorrect password**")
  185.     sleep(2)
  186.     start()
  187.   end
  188. end
  189.  
  190. --Run the thing!
  191. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement