Advertisement
serafim7

сенсорный кодовый замок [OpenComputers]

Jun 26th, 2020 (edited)
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. --[[ opencomputers сенсорный кодовый замок by serafim
  2.      Fork from https://youtu.be/r2XaMOI_N9E?t=158
  3.  
  4. внешний вид:
  5. https://i.imgur.com/yHuS42j.gif
  6.  
  7. требования:
  8. монитор и видеокарта 2-го уровня,
  9. контроллер красного камня
  10.  
  11. использование:
  12. после запуска проги нужно снять клавиатуру с монитора,
  13. открытие двери по белому списку, нажать * или #
  14. открытие двери при правильном вводе пароля.
  15. выдаёт сигнал редстоуна сверху при правильном вводе пароля,
  16. выдаёт сигнал редстоуна снизу при неправильном вводе пароля.
  17. пишет логи кто нажимал на экран и что вводил, файл logs
  18. ]]--
  19.  
  20. local password = "123"
  21. local nicknames = {"serafim","zombi","creper"}
  22.  
  23. local com = require("component")
  24. local sides = require("sides")
  25. local gpu = com.gpu
  26. local buttons = {}
  27. local input,nick
  28. local keyPad = {
  29.   {"1", "2", "3"},
  30.   {"4", "5", "6"},
  31.   {"7", "8", "9"},
  32.   {"*", "0", "#"},
  33. }
  34.  
  35. if not com.isAvailable("redstone") then
  36.   print("нет контроллера красного камня")
  37.   os.exit()
  38. end
  39. local rs = com.redstone
  40.  
  41. local function drawKeyPad(x, y)
  42.   local xPos, yPos = x, y
  43.   buttons = {}
  44.   for j = 1, #keyPad do
  45.     xPos = x
  46.     for i = 1, #keyPad[j] do
  47.       gpu.set(xPos + 1, yPos + 1, keyPad[j][i])
  48.       buttons[keyPad[j][i]] = {xPos, yPos, xPos + 4, yPos + 2}
  49.       xPos = xPos + 6
  50.     end
  51.     yPos = yPos + 3
  52.   end
  53. end
  54.  
  55. local function pressButton(x, y, name)
  56.   gpu.setBackground(0xFFFF00)
  57.   gpu.fill(x, y, 3, 3," " )
  58.   gpu.set(x + 1, y + 1, name)
  59.   os.sleep(0.2)
  60.   gpu.setBackground(0x000000)
  61.   gpu.fill(x, y, 3, 3," " )
  62.   drawKeyPad(3,4)
  63. end
  64.  
  65. local function clickedAtArea(x,y,sx,sy,ex,ey)
  66.   if (x >= sx) and (x <= ex) and (y >= sy) and (y <= ey) then return true end
  67.   return false
  68. end
  69.  
  70. local function checkNickname(name)
  71.   for i = 1, #nicknames do
  72.     if name == nicknames[i] then
  73.       return true
  74.     end
  75.   end
  76.   return false
  77. end
  78.  
  79. local function redstone(state)
  80.   if state then
  81.     gpu.setForeground(0x00FF00)
  82.     gpu.set(2,2," доступ разрешён ")
  83.     rs.setOutput(sides.top, 15)
  84.     os.sleep(2)
  85.     rs.setOutput(sides.top, 0)
  86.   else
  87.     gpu.set(2,2," доступ запрещён ")
  88.     rs.setOutput(sides.bottom, 15)
  89.     os.sleep(1)
  90.     rs.setOutput(sides.bottom, 0)
  91.   end
  92.   gpu.setForeground(0xFF0000)
  93. end
  94.  
  95. gpu.setResolution(19,15)
  96. gpu.setBackground(0x000000)
  97. gpu.fill(1,1,19,15," ")
  98. gpu.setForeground(0xFF0000)
  99. drawKeyPad(3,4)
  100. gpu.set(2,2," введите  пароль ")
  101.  
  102. while true do
  103.   local e = {require("event").pull(1,"touch")}
  104.   if e[1] == "touch" then
  105.     nick = e[6]
  106.     for key in pairs(buttons) do
  107.       if clickedAtArea(e[3], e[4], buttons[key][1], buttons[key][2], buttons[key][3], buttons[key][4]) then
  108.         pressButton(buttons[key][1], buttons[key][2], key)
  109.         if key == "*" or key == "#" then
  110.           if checkNickname(nick) then
  111.             redstone(true)
  112.           else
  113.             redstone(false)
  114.           end
  115.         else
  116.           input = (input or "") .. key
  117.           gpu.fill(1, 2, 19, 1, " ")
  118.           gpu.set(11-string.len(input)/2,2,input)
  119.         end
  120.       end
  121.     end
  122.   else
  123.     if input then
  124.       if input == password then
  125.         redstone(true)
  126.       else
  127.         redstone(false)
  128.       end
  129.       file = io.open("logs", "a")
  130.       file:write("Nick: "..nick.." | Key: "..input.."\n")
  131.       file:close()
  132.       input = nil
  133.     end
  134.     gpu.fill(1, 2, 19, 1, " ")
  135.     gpu.set(2,2," введите  пароль ")
  136.   end
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement