Advertisement
serafim7

codedoor ECS [OpenComputers]

Jul 26th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.85 KB | None | 0 0
  1. --https://youtu.be/r2XaMOI_N9E?t=158
  2. --для работы нужна библиотека  liteECSapi  http://pastebin.com/jrN8dLPb
  3.  
  4. local rs
  5. local component = require("component")
  6. local gpu = component.gpu
  7. local unicode = require("unicode")
  8. local ecs = require("liteECSapi")
  9. local sides = require("sides")
  10. local event = require("event")
  11. local fs = require("filesystem")
  12. local serialization = require("serialization")
  13.  
  14. if not component.isAvailable("redstone") then
  15.     ecs.error("This program requires Redstone I/O block or Redstone Card to work.")
  16.     return
  17. else
  18.     rs = component.redstone
  19. end
  20.  
  21. ------------------------------------------------------------------------------------------------------------
  22.  
  23. local colors = {
  24.     background = 0x202020,
  25.     borders = 0xFFDD00,
  26. }
  27.  
  28. ------------------------------------------------------------------------------------------------------------
  29.  
  30. local keyPad = {
  31.     {"1", "2", "3"},
  32.     {"4", "5", "6"},
  33.     {"7", "8", "9"},
  34.     {"*", "0", "#"},
  35. }
  36.  
  37. local xSize, ySize
  38. local buttons = {}
  39. local biometry = {}
  40. local input
  41.  
  42. local password = "12345"
  43.  
  44. local showPassword = true
  45. local showKeyPresses = true
  46.  
  47. local nicknames = {
  48.     "serafim"
  49. }
  50.  
  51. local pathToConfig = "System/CodeDoor/Config.cfg"
  52. local function saveConfig()
  53.     local file = io.open(pathToConfig, "w")
  54.     local massiv = {["password"] = password, ["nicknames"] = nicknames, ["showPassword"] = showPassword, ["showKeyPresses"] = showKeyPresses}
  55.     file:write(serialization.serialize(massiv))
  56.     file:close()
  57. end
  58.  
  59. local function loadConfig()
  60.     if fs.exists(pathToConfig) then
  61.         local massiv = {}
  62.         local file = io.open(pathToConfig, "r")
  63.         local stroka = file:read("*a")
  64.         massiv = serialization.unserialize(stroka)
  65.         file:close()
  66.         nicknames = massiv.nicknames
  67.         password = massiv.password
  68.         showPassword = massiv.showPassword
  69.         showKeyPresses = massiv.showKeyPresses
  70.     else
  71.         fs.makeDirectory(fs.path(pathToConfig))
  72.         local data = ecs.universalWindow("auto", "auto", 30, 0xEEEEEE, true, {"EmptyLine"}, {"CenterText", 0x880000, "Добро пожаловать в программу"}, {"CenterText", 0x880000, "конфигурации кодовой двери!"},  {"EmptyLine"},  {"CenterText", 0x262626, "Введите ваш пароль:"}, {"Input", 0x262626, 0x880000, "12345"}, {"EmptyLine"}, {"Switch", 0xF2B233, 0xffffff, 0x262626, "Показывать вводимый пароль", true}, {"EmptyLine"}, {"Switch", 0x3366CC, 0xffffff, 0x262626, "Показывать нажатие клавиш", true}, {"EmptyLine"}, {"Button", {0xbbbbbb, 0xffffff, "OK"}})
  73.         if data[1] == "" or tonumber(data[1]) == nil then ecs.error("Указан неверный пароль. По умолчанию он будет 12345."); password = "12345" else password = data[1] end
  74.         showPassword = data[2]
  75.         showKeyPresses = data[3]
  76.         saveConfig()
  77.     end
  78. end
  79.  
  80. local function drawKeyPad(x, y)
  81.     local xPos, yPos = x, y
  82.     buttons = {}
  83.     for j = 1, #keyPad do
  84.         xPos = x
  85.         for i = 1, #keyPad[j] do
  86.             ecs.drawFramedButton(xPos, yPos, 5, 3, keyPad[j][i], colors.borders)
  87.             buttons[keyPad[j][i]] = {xPos, yPos, xPos + 4, yPos + 2}
  88.             xPos = xPos + 6
  89.         end
  90.         yPos = yPos + 3
  91.     end
  92. end
  93.  
  94. local function visualScan(x, y, timing)
  95.     local yPos = y
  96.     gpu.setBackground(colors.background)
  97.     gpu.setForeground(colors.borders)
  98.  
  99.     gpu.set(x, yPos, "╞══════════╡")
  100.     yPos = yPos - 1
  101.     os.sleep(timing)
  102.  
  103.     for i = 1, 3 do
  104.         gpu.set(x, yPos, "╞══════════╡")
  105.         gpu.set(x, yPos + 1, "│          │")
  106.         yPos = yPos - 1
  107.         os.sleep(timing)
  108.     end
  109.  
  110.     yPos = yPos + 2
  111.  
  112.     for i = 1, 3 do
  113.         gpu.set(x, yPos, "╞══════════╡")
  114.         gpu.set(x, yPos - 1, "│          │")
  115.         yPos = yPos + 1
  116.         os.sleep(timing)
  117.     end
  118.     gpu.set(x, yPos - 1, "│          │")
  119. end
  120.  
  121. local function infoPanel(info, background, foreground, hideData)
  122.     ecs.square(1, 1, xSize, 3, background)
  123.     local text  if hideData then
  124.         text = ecs.stringLimit("start", string.rep("*", unicode.len(info)), xSize - 4)
  125.     else
  126.         text = ecs.stringLimit("start", info, xSize - 4)
  127.     end
  128.     ecs.colorText(math.ceil(xSize / 2 - unicode.len(text) / 2) + 1 , 2, foreground, text)
  129. end
  130.  
  131. local function drawAll()
  132.     local xPos, yPos = 3, 5
  133.    
  134.     --Как прописывать знаки типа © § ® ™
  135.    
  136.     --кейпад
  137.     gpu.setBackground(colors.background)
  138.     drawKeyPad(xPos, yPos)
  139.  
  140.     --Био
  141.     xPos = xPos + 18
  142.     ecs.border(xPos, yPos, 12, 6, colors.background, colors.borders)
  143.     ecs.square(xPos + 5, yPos + 2, 2, 2, colors.borders)
  144.     gpu.setBackground(colors.background)
  145.     biometry = {xPos, yPos, xPos + 11, yPos + 5}
  146.  
  147.     --Био текст
  148.     yPos = yPos + 7
  149.     xPos = xPos + 1
  150.     gpu.set(xPos + 3, yPos, "ECS®")
  151.     gpu.set(xPos + 1, yPos + 1, "Security")
  152.     gpu.set(xPos + 1, yPos + 2, "Systems™")
  153.  
  154. end
  155.  
  156. local function checkNickname(name)
  157.     for i = 1, #nicknames do
  158.         if name == nicknames[i] then
  159.             return true
  160.         end
  161.     end
  162.     return false
  163. end
  164.  
  165. local function pressButton(x, y, name)
  166.     ecs.square(x, y, 5, 3, colors.borders)
  167.     gpu.setForeground(colors.background)
  168.     gpu.set(x + 2, y + 1, name)
  169.     os.sleep(0.2)
  170. end
  171.  
  172. local function waitForExit()
  173.     local e2 = {event.pull(3, "touch")}
  174.     if #e2 > 0 then
  175.         if ecs.clickedAtArea(e2[3], e2[4], buttons["*"][1], buttons["*"][2], buttons["*"][3], buttons["*"][4]) then
  176.             pressButton(buttons["*"][1], buttons["*"][2], "*")
  177.             return true
  178.         end
  179.     end
  180.     return false
  181. end
  182.  
  183. local function redstone(go)
  184.     if go then
  185.         rs.setOutput(sides.top, 15)
  186.         local goexit = waitForExit()
  187.         rs.setOutput(sides.top, 0)
  188.         rs.setOutput(sides.bottom, 0)
  189.         return goexit
  190.     else
  191.         rs.setOutput(sides.bottom, 15)
  192.         os.sleep(2)
  193.         rs.setOutput(sides.top, 0)
  194.         rs.setOutput(sides.bottom, 0)
  195.     end
  196.     return false
  197. end
  198.  
  199. ------------------------------------------------------------------------------------------------------------
  200.  
  201. ecs.prepareToExit(colors.background)
  202. loadConfig()
  203.  
  204. local oldWidth, oldHeight = gpu.getResolution()
  205. gpu.setResolution(34, 17)
  206. xSize, ySize = 34, 17
  207.  
  208. drawAll()
  209. infoPanel("Введите пароль", colors.borders, colors.background)
  210.  
  211. while true do
  212.     local e = {event.pull()}
  213.     if e[1] == "touch" then
  214.         for key in pairs(buttons) do
  215.             if ecs.clickedAtArea(e[3], e[4], buttons[key][1], buttons[key][2], buttons[key][3], buttons[key][4]) then
  216.                
  217.                 if showKeyPresses then
  218.                     pressButton(buttons[key][1], buttons[key][2], key)
  219.                 end
  220.  
  221.                 if key == "*" then
  222.                     input = nil
  223.                     infoPanel("Поле ввода очищено", colors.borders, colors.background)
  224.                 elseif key == "#" then
  225.                     drawAll()
  226.                     if input == password then
  227.                         infoPanel("Доступ разрешён!", ecs.colors.green, 0xFFFFFF)
  228.                         local goexit = redstone(true)
  229.                         for i = 1, #nicknames do
  230.                             if nicknames[i] == e[6] then nicknames[i] = nil end
  231.                         end
  232.                         table.insert(nicknames, e[6])
  233.                         saveConfig()
  234.  
  235.                         if goexit then
  236.                             ecs.prepareToExit()
  237.                             gpu.setResolution(oldWidth, oldHeight)
  238.                             ecs.prepareToExit()
  239.                             return
  240.                         end
  241.                     else
  242.                         infoPanel("Доступ запрещён!", ecs.colors.red, 0xFFFFFF)
  243.                         redstone(false)    
  244.                     end
  245.                     infoPanel("Введите пароль", colors.borders, colors.background)
  246.                     input = nil
  247.                 else
  248.                     input = (input or "") .. key
  249.                    
  250.                     infoPanel(input, colors.borders, colors.background, not showPassword)
  251.                 end
  252.                 drawAll()
  253.  
  254.                 break
  255.             end
  256.         end
  257.  
  258.         if ecs.clickedAtArea(e[3], e[4], biometry[1], biometry[2], biometry[3], biometry[4]) then
  259.             visualScan(biometry[1], biometry[2] + 4, 0.08)
  260.             if checkNickname(e[6]) then
  261.                 infoPanel("Привет, " .. e[6], ecs.colors.green, 0xFFFFFF)
  262.                 redstone(true)
  263.             else
  264.                 infoPanel("В доступе отказано!", ecs.colors.red, 0xFFFFFF)
  265.                 redstone(false)    
  266.             end
  267.             infoPanel("Введите пароль", colors.borders, colors.background)
  268.             drawAll()
  269.         end
  270.     end
  271. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement