Advertisement
DOGGYWOOF

PIN lock for Doggy OS pocket

Mar 10th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. -- File path for storing the PIN
  2. local pinFilePath = "/disk/security/PIN.config"
  3.  
  4. -- Function to read the PIN from file or use default if not available
  5. local function readPINFromFile()
  6. local file = fs.open(pinFilePath, "r")
  7. local pin = file and file.readLine() or "1234"
  8. if file then
  9. file.close()
  10. end
  11. return pin
  12. end
  13.  
  14. -- Define your PIN
  15. local correctPIN = readPINFromFile()
  16.  
  17. -- GUI Functions
  18. local function drawScreen()
  19. term.clear()
  20. term.setCursorPos(1, 1)
  21. print("+-----+-----+-----+")
  22. print("| | | |")
  23. print("| 1 | 2 | 3 |")
  24. print("| | | |")
  25. print("+-----+-----+-----+")
  26. print("| | | |")
  27. print("| 4 | 5 | 6 |")
  28. print("| | | |")
  29. print("+-----+-----+-----+")
  30. print("| | | |")
  31. print("| 7 | 8 | 9 |")
  32. print("| | | |")
  33. print("+-----+-----+-----+")
  34. print("| | | |")
  35. print("| C | 0 | E |")
  36. print("| | | |")
  37. print("+-----+-----+-----+")
  38. term.setCursorPos(1, 18)
  39. write("Enter PIN:")
  40. end
  41.  
  42. local function handleMouseClick(x, y)
  43. if x >= 2 and x <= 5 and y >= 2 and y <= 4 then
  44. return 1
  45. elseif x >= 8 and x <= 11 and y >= 2 and y <= 4 then
  46. return 2
  47. elseif x >= 14 and x <= 17 and y >= 2 and y <= 4 then
  48. return 3
  49. elseif x >= 2 and x <= 5 and y >= 6 and y <= 8 then
  50. return 4
  51. elseif x >= 8 and x <= 11 and y >= 6 and y <= 8 then
  52. return 5
  53. elseif x >= 14 and x <= 17 and y >= 6 and y <= 8 then
  54. return 6
  55. elseif x >= 2 and x <= 5 and y >= 10 and y <= 12 then
  56. return 7
  57. elseif x >= 8 and x <= 11 and y >= 10 and y <= 12 then
  58. return 8
  59. elseif x >= 14 and x <= 17 and y >= 10 and y <= 12 then
  60. return 9
  61. elseif x >= 2 and x <= 5 and y >= 14 and y <= 16 then
  62. return "C" -- Clear
  63. elseif x >= 8 and x <= 11 and y >= 14 and y <= 16 then
  64. return 0
  65. elseif x >= 14 and x <= 17 and y >= 14 and y <= 16 then
  66. return "E" -- Enter
  67. else
  68. return nil
  69. end
  70. end
  71.  
  72. -- Main PIN Entry Function
  73. local function enterPIN()
  74. local enteredPIN = ""
  75. local attempts = 3
  76.  
  77. while attempts > 0 do
  78. drawScreen()
  79. term.setCursorBlink(false)
  80. term.setCursorPos(12, 18)
  81. write(string.rep("*", #enteredPIN))
  82. local event, _, x, y = os.pullEvent("mouse_click")
  83.  
  84. if event == "mouse_click" then
  85. local button = handleMouseClick(x, y)
  86. if button then
  87. if button == "C" then
  88. -- Clear button
  89. enteredPIN = ""
  90. elseif button == "E" then
  91. -- Enter button
  92. if enteredPIN == correctPIN then
  93. term.clear()
  94. term.setCursorPos(1, 1)
  95. print("Access Granted!")
  96. -- Perform actions upon successful entry
  97. break
  98. else
  99. term.clear()
  100. term.setCursorPos(1, 1)
  101. print("Incorrect PIN. Try again.")
  102. attempts = attempts - 1
  103. if attempts == 0 then
  104. print("Access Denied!")
  105. else
  106. print("Attempts remaining: " .. attempts)
  107. end
  108. sleep(2)
  109. enteredPIN = ""
  110. end
  111. else
  112. enteredPIN = enteredPIN .. tostring(button)
  113. end
  114. end
  115. end
  116. end
  117. end
  118.  
  119. -- Start PIN Entry
  120. enterPIN()
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement