Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. -----PREFERENCES/VARIABLES-----
  2. local pin = 5948
  3. local maxtries = 4
  4. local locktime = 60 --seconds
  5. local waittotry = 2 --seconds
  6. local redstoneSide = "right"
  7. local monitorSide = "back"
  8.  
  9. local tries = 0
  10. local input = ""
  11. local keypad = {
  12. --{num,xpos,ypos}
  13.   {1,2,4},
  14.   {2,7,4},
  15.   {3,12,4},
  16.   {4,2,6},
  17.   {5,7,6},
  18.   {6,12,6},
  19.   {7,2,8},
  20.   {8,7,8},
  21.   {9,12,8},
  22.   {0,7,10}
  23. }
  24.  
  25. -----FUNCTIONS-----
  26. function checkClick(t,x,y)
  27.   for i,v in pairs(t) do
  28.     num,cx,cy = unpack(v)
  29.     if (x >= cx) and (x <= cx + 2) and (y == cy) then
  30.       return true,num
  31.     end
  32.   end
  33.   return false
  34. end
  35.  
  36. function numClick(num)
  37.   monitor.setCursorPos((5 + (string.len(input) * 2)),2)
  38.   input = input .. num
  39.   monitor.write("*")
  40.   if (string.len(input) == 4) then
  41.     if (tostring(pin) == input) then
  42.       monAction("Access Granted")
  43.       doorOpen()
  44.       monReset(true)
  45.       input = ""
  46.       tries = 0
  47.     else
  48.       input = ""
  49.       tries = tries + 1
  50.       if (tries >= maxtries) then
  51.         monReset()
  52.         monitor.setBackgroundColor(colors.red)
  53.         monAction("TERMINAL LOCKED")
  54.         sleep(locktime)
  55.         os.reboot()
  56.       end
  57.       monAction("Access Denied")
  58.       sleep(waittotry)
  59.       monReset(true)
  60.     end
  61.   end
  62. end
  63.  
  64. function monAction(mess)
  65.   monitor.setCursorPos(1,1)
  66.   monitor.write(mess)
  67. end
  68.  
  69. function monReset(isClear)
  70.   if (isClear) then monitor.clearLine() end
  71.   monitor.setCursorPos(5,2)
  72.   monitor.write("_ _ _ _")
  73. end
  74.  
  75. function doorOpen()
  76.   redstone.setOutput(redstoneSide, true)
  77.   sleep(7)
  78.   redstone.setOutput(redstoneSide, false)
  79. end
  80.  
  81. -----BEGIN MAIN-----
  82. monitor = peripheral.wrap(monitorSide)
  83. monitor.setTextScale(0.5)
  84. w,h = monitor.getSize()
  85. if (w > 15) or (h > 10) then
  86.   error("Monitor size is to big.")
  87.   exit()
  88. end
  89. monReset()
  90. monitor.setBackgroundColor(colors.red)
  91. monitor.setTextColor(colors.black)
  92.  
  93. for i,v in pairs(keypad) do
  94.   num,x,y = unpack(v)
  95.   monitor.setCursorPos(x,y)
  96.   monitor.write(" " .. num .. " ")
  97. end
  98.  
  99. monitor.setBackgroundColor(colors.black)
  100. monitor.setTextColor(colors.white)
  101. while true do
  102.   event,button,x,y = os.pullEvent("monitor_touch")
  103.   validClick,num = checkClick(keypad,x,y)
  104.   if (validClick) then
  105.     numClick(num)
  106.   end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement