Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local buttons = {
- { name = "1", x = 7, y = 4, width = 4, height = 3, color = colors.lightBlue },
- { name = "2", x = 13, y = 4, width = 4, height = 3, color = colors.lightBlue },
- { name = "3", x = 19, y = 4, width = 4, height = 3, color = colors.lightBlue },
- { name = "4", x = 7, y = 8, width = 4, height = 3, color = colors.lightBlue },
- { name = "5", x = 13, y = 8, width = 4, height = 3, color = colors.lightBlue },
- { name = "6", x = 19, y = 8, width = 4, height = 3, color = colors.lightBlue },
- { name = "7", x = 7, y = 12, width = 4, height = 3, color = colors.lightBlue },
- { name = "8", x = 13, y = 12, width = 4, height = 3, color = colors.lightBlue },
- { name = "9", x = 19, y = 12, width = 4, height = 3, color = colors.lightBlue },
- { name = "C", x = 7, y = 16, width = 4, height = 3, color = colors.red },
- { name = "0", x = 13, y = 16, width = 4, height = 3, color = colors.lightBlue },
- { name = "E", x = 19, y = 16, width = 4, height = 3, color = colors.green }
- }
- local pinFilePath = "/disk/security/PIN.config"
- local function readPINFromFile()
- local file = fs.open(pinFilePath, "r")
- local pin = file and file.readLine() or "0000"
- if file then
- file.close()
- end
- return pin
- end
- local correctPIN = readPINFromFile()
- local function drawPINScreen(enteredPIN)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clear()
- -- Display "Enter PIN" at the top center
- term.setCursorPos((term.getSize() - #"Enter PIN") / 2 + 1, 1)
- write("Enter PIN")
- -- Display entered PIN right under "Enter PIN" text
- term.setCursorPos((term.getSize() - #enteredPIN) / 2 + 1, 2)
- write(string.rep("*", #enteredPIN))
- for _, button in ipairs(buttons) do
- paintutils.drawBox(button.x, button.y, button.x + button.width - 1, button.y + button.height - 1, button.color)
- term.setCursorPos(button.x + 1, button.y + 1)
- write(button.name)
- end
- end
- local function handleButtonClick(button)
- if button.name == "C" then
- -- Clear button
- return "C"
- elseif button.name == "E" then
- -- Enter button
- return "E"
- else
- return tostring(button.name)
- end
- end
- local function enterPIN()
- local enteredPIN = ""
- local attempts = 3
- while attempts > 0 do
- drawPINScreen(enteredPIN)
- term.setCursorBlink(false)
- term.setCursorPos(1, 18)
- local event, button, x, y = os.pullEvent("mouse_click")
- if event == "mouse_click" then
- for _, btn in ipairs(buttons) do
- if x >= btn.x and x < btn.x + btn.width and y >= btn.y and y < btn.y + btn.height then
- local result = handleButtonClick(btn)
- if result == "E" then
- if enteredPIN == correctPIN then
- term.clear()
- term.setCursorPos(1, 1)
- print("Access Granted!")
- -- Perform actions upon successful entry
- return
- else
- term.clear()
- term.setCursorPos(1, 1)
- print("Incorrect PIN. Try again.")
- attempts = attempts - 1
- if attempts == 0 then
- print("Access Denied!")
- else
- print("Attempts remaining: " .. attempts)
- end
- sleep(2)
- enteredPIN = ""
- end
- elseif result == "C" then
- enteredPIN = ""
- else
- enteredPIN = enteredPIN .. result
- end
- break
- end
- end
- end
- end
- end
- enterPIN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement