ByteZz

Passcode

Dec 4th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1.  
  2. -- Variables -----------------------------------------------
  3. local password = "123"
  4. local keypadtable = {{3,2,"1"}, {4,2,"2"}, {5,2,"3"}, {3,3,"4"}, {4,3,"5"}, {5,3,"6"}, {3,4,"7"}, {4,4,"8"}, {5,4,"9"}}
  5. local dooropendelay = 4
  6. local errordelay = 3
  7. local monitorlocation = 'back'
  8. local redstonesignalside = 'left'
  9.  
  10. ------------------------------------------------------------
  11.  
  12. local monitor = peripheral.wrap(monitorlocation)
  13. local maxx, maxy = monitor.getSize()
  14.  
  15. local function idleScreen ()
  16.   monitor.clear()
  17.   monitor.setCursorPos(2,2)
  18.   monitor.write("Enter")
  19.   monitor.setCursorPos(3,3)
  20.   monitor.write("PIN")
  21. end
  22.  
  23. local function loginOK ()
  24.   monitor.clear()
  25.   monitor.setCursorPos(1,3)
  26.   monitor.write("Welcome")
  27. end
  28.  
  29. local function loginFail ()
  30.   monitor.clear()
  31.   monitor.setCursorPos(2,3)
  32.   monitor.write("Error")
  33. end
  34.  
  35. local function printKeyPad ()
  36.   monitor.clear()
  37.   for i=1,#keypadtable do
  38.         monitor.setCursorPos(keypadtable[i][1],keypadtable[i][2])
  39.         monitor.write(keypadtable[i][3])
  40.   end
  41. end
  42.  
  43. local function getKeyFromPad (xclick, yclick)
  44.   for i=1,#keypadtable do
  45.         if ((keypadtable[i][1] == xclick) and (keypadtable[i][2] == yclick)) then
  46.           return (keypadtable[i][3])
  47.         end
  48.   end
  49.   return ('')
  50. end
  51.  
  52. local function openDoor ()
  53.   redstone.setOutput (redstonesignalside, true)
  54. end
  55.  
  56. local function closeDoor ()
  57.   redstone.setOutput (redstonesignalside, false)
  58. end
  59.  
  60. -- MAIN ------------------------------------------------------------
  61.  
  62. while true do
  63.   idleScreen()
  64.   local pwentered = ''
  65.  
  66.   -- Get first keypress and show pinpad
  67.   local event, monside, xpos, ypos = os.pullEvent()
  68.   if event == 'monitor_touch' then
  69.         print ('* Starting keypad entry')
  70.         printKeyPad()
  71.   end
  72.  
  73.   while (tonumber(string.len(pwentered)) < tonumber(string.len(password))) do
  74.         local pressed = ''
  75.         local event, monside, xpos, ypos = os.pullEvent()
  76.  
  77.         if event == 'monitor_touch' then
  78.          local pressed = tostring(getKeyFromPad (xpos, ypos))
  79.          if pressed ~= "" then
  80.           pwentered = pwentered .. pressed
  81.           print ("* Pressed: " .. pressed)
  82.          end
  83.         end
  84.   end
  85.  
  86.   if (pwentered == password) then
  87.         print ('* Login SUCCESS')
  88.         loginOK ()
  89.         openDoor ()
  90.         sleep (dooropendelay)
  91.         closeDoor ()
  92.   else
  93.         print ('* Login FAIL')
  94.         loginFail ()
  95.         sleep (errordelay)
  96.   end
  97. end
Add Comment
Please, Sign In to add comment