Advertisement
DOGGYWOOF

Keycard

Jan 8th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. -- Keycard system with monitor display and specific passkeys for override and owner in ComputerCraft
  2.  
  3. -- Keycard IDs
  4. local ownerKeycardPath = "/disk/ownerKeycard.txt"
  5. local overrideKeycardPath = "/disk/overrideKeycard.txt"
  6.  
  7. -- Function to check if the keycard is valid
  8. local function isValidKeycard(keycardID, passkey)
  9. local file = nil
  10.  
  11. if keycardID == "owner" then
  12. file = fs.open(ownerKeycardPath, "r")
  13. elseif keycardID == "override" then
  14. file = fs.open(overrideKeycardPath, "r")
  15. end
  16.  
  17. if file then
  18. local storedPasskey = file.readAll()
  19. file.close()
  20. return passkey == storedPasskey
  21. end
  22.  
  23. return false
  24. end
  25.  
  26. -- Function to grant access if the keycard is valid
  27. local function grantAccess()
  28. print("Access granted!")
  29. -- Perform actions for granting access (e.g., opening a door, enabling a system)
  30. -- For monitor display
  31. term.redirect(peripheral.wrap("right")) -- Redirect output to the monitor on the right
  32. print("Access granted!") -- Display on the monitor
  33. term.restore() -- Restore output to the terminal
  34. end
  35.  
  36. -- Function to deny access if the keycard is invalid
  37. local function denyAccess()
  38. print("Access denied!")
  39. -- Perform actions for denying access (e.g., display a message, close a door)
  40. -- For monitor display
  41. term.redirect(peripheral.wrap("right")) -- Redirect output to the monitor on the right
  42. print("Access denied!") -- Display on the monitor
  43. term.restore() -- Restore output to the terminal
  44. end
  45.  
  46. -- Main program
  47. while true do
  48. -- Read input from the user
  49. print("Please swipe your keycard:")
  50. local keycardID = read()
  51. local passkey = ""
  52.  
  53. if keycardID == "owner" or keycardID == "override" then
  54. print("Enter passkey:")
  55. passkey = read("*")
  56. end
  57.  
  58. -- Check if the keycard is valid
  59. if isValidKeycard(keycardID, passkey) then
  60. grantAccess()
  61. else
  62. denyAccess()
  63. end
  64. end
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement