Advertisement
eraendigo_pro

Untitled

Dec 8th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.loadAPI("System/API/windows")
  2.  
  3. --ПАРОЛЬ
  4. local password = "14360"
  5.  
  6. --СКОЛЬКО БУДЕТ ОТКРЫТА ДВЕРЬ
  7. local doorOpenTime = 3
  8.  
  9. --СКОЛЬКО БУДЕТ НАЖАТА КНОПКА
  10. local buttonPressTime = 0.2
  11.  
  12. --ПЕРЕМЕННАЯ ТАЙМЕРА
  13. local changePasswordTimer = nil
  14.  
  15. --РЕЖИМ РАБОТЫ ДИСПЛЕЯ
  16. local mode = "default"
  17.  
  18. --СКРЫВАТЬ ЛИ ВВОДИМЫЙ ПАРОЛЬ
  19. local hidePassword = "true"
  20.  
  21. --ОТСЮДА ИЗЛУЧАТЬ СИГНАЛ РЕДА, ЕСЛИ ПАРОЛЬ ВЕРЕН
  22. local redstoneSide = "left"
  23. --А ОТСЮДА, ЕСЛИ НЕ ВЕРЕН
  24. local redstoneSideOnWrongPassword = "right"
  25.  
  26. --КНОПОЧКИ
  27. local buttons = {
  28.     {"1","2","3"},
  29.     {"4","5","6"},
  30.     {"7","8","9"},
  31.     {"C","0","#"},
  32. }
  33.  
  34. --МАССИВ ОБЪЕКТОВ
  35. local Obj = {}
  36.  
  37. --МАССИВ СИМВОЛОВ, ВВЕДЕННЫХ С ПОМОЩЬЮ КНОПОК
  38. local inputCode = {}
  39.  
  40. --ПОЛУЧЕНИЕ РАЗМЕРА МОНИТОРА
  41. local xSize, ySize = term.getSize()
  42.  
  43. --ПОИСК ПЕРИФЕРИИ
  44. local function findPeripheral(whatToFind)
  45.   local PeriList = peripheral.getNames()
  46.   for i=1,#PeriList do
  47.     if peripheral.getType(PeriList[i]) == whatToFind then
  48.       return PeriList[i]
  49.     end
  50.   end
  51. end
  52.  
  53. --УНИВЕРСАЛЬНАЯ ФУНКЦИЯ ДЛЯ ОТОБРАЖЕНИЯ ТЕКСТА ПО ЦЕНТРУ ЭКРАНА
  54. local function centerText(how,coord,text,textColor,backColor)
  55.     term.setTextColor(textColor)
  56.     term.setBackgroundColor(backColor)
  57.     if how == "xy" then
  58.         term.setCursorPos(math.floor(xSize/2-#text/2),math.floor(ySize/2))
  59.     elseif how == "x" then
  60.         term.setCursorPos(math.floor(xSize/2-#text/2),coord)
  61.     elseif how == "y" then
  62.         term.setCursorPos(coord,math.floor(ySize/2))
  63.     end
  64.     term.write(text)
  65. end
  66.  
  67. --ЧТЕНИЕ КОНФИГА
  68. function configRead(pathToConfig,whatToRead)
  69.     if not fs.exists(pathToConfig) then error("No such file") end
  70.     local file = fs.open(pathToConfig,"r")
  71.     while true do
  72.         local line = file.readLine()
  73.         if line ~= nil then
  74.             local key, value = string.match(line,"(.*)=(.*)")
  75.             if value ~= nil and key == whatToRead then
  76.                 file.close()
  77.                 return value
  78.             end
  79.         else
  80.             file.close()
  81.             break
  82.         end
  83.     end
  84. end
  85.  
  86. --ЗАПИСЬ В КОНФИГ
  87. local function configWrite(pathToConfig,key,value)
  88.     if not fs.exists(pathToConfig) then
  89.         local file = fs.open(pathToConfig,"w")
  90.         file.close()
  91.     end
  92.  
  93.     local file = fs.open(pathToConfig,"r")
  94.     local Massiv = {}
  95.    
  96.     local lineCounter = 1
  97.     while true do
  98.         local line = file.readLine()
  99.         if line ~= nil then
  100.             Massiv[lineCounter] = line
  101.         else
  102.             file.close()
  103.             break
  104.         end
  105.         lineCounter = lineCounter + 1
  106.     end
  107.  
  108.     local success = false
  109.     for i=1,#Massiv do
  110.         local key1, value1 = string.match(Massiv[i],"(.*)=(.*)")
  111.         if value1 ~= nil and key1 == key then
  112.             Massiv[i] = key.."="..value
  113.             success = true
  114.         end
  115.     end
  116.  
  117.     if success == false then Massiv[#Massiv+1] = key.."="..value end
  118.  
  119.     local file = fs.open(pathToConfig,"w")
  120.     for i=1,#Massiv do
  121.         file.writeLine(Massiv[i])
  122.     end
  123.     file.close()
  124. end
  125.  
  126. --ОБЪЕКТЫ
  127. local function newObj(name,x,y)
  128.     Obj[name]={}
  129.     Obj[name]["x"]=x
  130.     Obj[name]["y"]=y
  131. end
  132.  
  133. --ПРОСТАЯ ЗАЛИВКА ЭКРАНА ЦВЕТОМ
  134. local function clearScreen(color)
  135.     term.setBackgroundColor(color)
  136.     term.clear()
  137. end
  138.  
  139. --ПРОСТОЙ ТЕКСТ
  140. local function usualText(x,y,text)
  141.     term.setCursorPos(x,y)
  142.     term.write(text)
  143. end
  144.  
  145. --ОТРИСОВКА ВЕРХНЕЙ ШТУЧКИ
  146. local function drawTab(textColor,backColor)
  147.     term.setBackgroundColor(backColor)
  148.     term.setTextColor(textColor)
  149.     term.setCursorPos(2,1)
  150.     term.clearLine()
  151.     term.write("-----")
  152.  
  153.     for i=1,#inputCode do
  154.         if hidePassword == "true" then
  155.             usualText(i+1,1,"*")
  156.         else
  157.             usualText(i+1,1,inputCode[i])
  158.         end
  159.     end
  160. end
  161.  
  162. --ОТРИСОВКА КОНКРЕТНОЙ КНОПКИ
  163. local function drawButton(name,textColor,backColor)
  164.     term.setBackgroundColor(backColor)
  165.     term.setTextColor(textColor)
  166.     usualText(Obj[name]["x"],Obj[name]["y"],name)
  167. end
  168.  
  169.  
  170. --ОТРИСОВКА ВСЕГО ИНТЕРФЕЙСА
  171. local function gui()
  172.     --ОЧИСТКА ЭКРАНА
  173.     term.setCursorBlink(false)
  174.     clearScreen(colors.white)
  175.     term.setTextColor(colors.black)
  176.  
  177.     --ОТРИСОВКА КНОПОЧЕК
  178.     for j=1,#buttons do
  179.         for i=1,#buttons[j] do
  180.             local xPos = i*2
  181.             local yPos = 1+j
  182.             usualText(xPos,yPos,buttons[j][i])
  183.             newObj(buttons[j][i],xPos,yPos)
  184.         end
  185.     end
  186.  
  187.     --ОТРИСОВКА ВЕРХНЕЙ ШНЯГИ
  188.     drawTab(colors.white,colors.black)
  189. end
  190.  
  191. ------------------------------CТАРТ ПРОГРАММЫ------------------------------------
  192.  
  193. if not term.isColor() then
  194.     error("This program requires an advanced computer.")
  195. end
  196.  
  197. --РИСУЕМ В КОМПЕ ХУЙНЮ
  198. clearScreen(colors.white)
  199. centerText("xy",0,"Sistems aktiwate.",colors.lightGray,colors.white)
  200.  
  201. --ПОДКЛЮЧЕНИЕ МОНИТОРА
  202. local m = findPeripheral("monitor")
  203. if m ~= nil then
  204.     m = peripheral.wrap(m)
  205.     if not m.isColor() then
  206.         windows.error("This program works only with advanced monitor.")
  207.         do return end
  208.     end
  209.     m.setTextScale(1)
  210.     term.redirect(m)
  211. else
  212.     windows.error("This program requires advanced external monitor.")
  213.     do return end
  214. end
  215.  
  216. --ЧТЕНИЕ КОНФИГА
  217. if fs.exists("System/CodeDoor.cfg") then
  218.     password = configRead("System/CodeDoor.cfg","password")
  219.     redstoneSide = configRead("System/CodeDoor.cfg","redstone side")
  220.     redstoneSideOnWrongPassword = configRead("System/CodeDoor.cfg","redstone side on wrong password")
  221.     doorOpenTime = configRead("System/CodeDoor.cfg","door open time")
  222.     hidePassword = configRead("System/CodeDoor.cfg","hide password")
  223. else
  224.     configWrite("System/CodeDoor.cfg","password",password)
  225.     configWrite("System/CodeDoor.cfg","redstone side",redstoneSide)
  226.     configWrite("System/CodeDoor.cfg","redstone side on wrong password",redstoneSideOnWrongPassword)
  227.     configWrite("System/CodeDoor.cfg","door open time",doorOpenTime)
  228.     configWrite("System/CodeDoor.cfg","hide password",hidePassword)
  229. end
  230.  
  231. --РИСУЕМ ВСЕ
  232. gui()
  233.  
  234. --АНАЛИЗ КАСАНИЙ ЭКРАНА
  235. while true do
  236.     --ЗАКРЫВАЕМ ДВЕРЬ НА ВСЯКИЙ ПОЖАРНЫЙ
  237.     rs.setOutput(redstoneSide,false)
  238.     rs.setOutput(redstoneSideOnWrongPassword,false)
  239.  
  240.     local event,side,x,y = os.pullEvent()
  241.     if event == "monitor_touch" then
  242.         --ПЕРЕБОР ВСЕХ ЭЛЕМЕНТОВ МАССИВА ОБЪЕКТОВ
  243.         for key,val in pairs(Obj) do
  244.             --ПРОВЕРКА СОВПАДЕНИЯ КООРДИНАТ КАСАНИЯ И КООРДИНАТ ОБЪЕКТОВ
  245.             if x==Obj[key]["x"] and y==Obj[key]["y"] then
  246.                 --РИСУЕМ НАЖАТУЮ КНОПОЧКУ
  247.                 drawButton(key,colors.white,colors.green)
  248.                 sleep(buttonPressTime)
  249.                 drawButton(key,colors.black,colors.white)
  250.  
  251.                 --ПРОВЕРКА, ЧТО ЗА КЛАВИША НАЖАТА - ЦИФРА ИЛИ СИСТЕМНАЯ КЛАВИША
  252.                 if key == "C" then
  253.                     inputCode = {}
  254.                     if mode == "edit" then
  255.                         drawTab(colors.white,colors.orange)
  256.                     else
  257.                         drawTab(colors.white,colors.black)
  258.                     end
  259.  
  260.                 elseif key == "#" then
  261.                     --СОВМЕЩЕНИЕ ВСЕХ ЭЛЕМЕНТОВ МАССИВА ВВОДА В ОДНУ СТРОКУ
  262.                     local inputPass = ""
  263.                     for i=1,#inputCode do
  264.                         inputPass = inputPass..inputCode[i]
  265.                     end
  266.  
  267.                     --ПРОВЕРКА РЕЖИМА
  268.                     if mode == "edit" then
  269.                         --СМЕНА ПАРОЛЯ
  270.                         password = inputPass
  271.                         configWrite("System/CodeDoor.cfg","password",password)
  272.                         inputCode = {}
  273.                         term.setCursorPos(3,1)
  274.                         term.setBackgroundColor(colors.orange)
  275.                         term.clearLine()
  276.                         term.setTextColor(colors.white)
  277.                         term.write("Ok!")
  278.  
  279.                         sleep(2)
  280.  
  281.                         drawTab(colors.white,colors.black)
  282.                         mode = "default"
  283.                     else
  284.                         --СРАВНЕНИЕ ВВЕДЕННОГО ГОВНА С ПЕРЕМЕННОЙ ПАРОЛЯ
  285.                         if inputPass == password then
  286.                             drawTab(colors.white,colors.green)
  287.                             rs.setOutput(redstoneSide,true)
  288.                             rs.setOutput(redstoneSideOnWrongPassword,false)
  289.  
  290.                             --СТАРТУЕМ ТАЙМЕР НА УКАЗАННОЕ ВРЕМЯ
  291.                             changePasswordTimer = os.startTimer(tonumber(doorOpenTime))
  292.                             while true do
  293.                                 local event2,side2,x2,y2 = os.pullEvent()
  294.                                 --ЕСЛИ НИЧЕГО НЕ НАЖАТО, ТО ВЫХОД ИЗ ЦИКЛА
  295.                                 if event2 == "timer" then
  296.                                     if side2 == changePasswordTimer then
  297.                                         break
  298.                                     end
  299.                                 --ЕСЛИ НАЖАТА КЛАВИША "С", ТО ЗАПУСТИТЬ РЕЖИМ СМЕНЫ ПАРОЛЯ
  300.                                 elseif event2 == "monitor_touch" then
  301.                                     if x2==Obj["C"]["x"] and y2==Obj["C"]["y"] then
  302.                                         drawButton("C",colors.white,colors.green)
  303.                                         sleep(buttonPressTime)
  304.                                         drawButton("C",colors.black,colors.white)
  305.                                         mode = "edit"
  306.                                         os.queueEvent("timer",changePasswordTimer)
  307.                                     end
  308.                                 end
  309.                             end
  310.  
  311.                             --ОЧИЩАЕМ ВВЕДЕННЫЙ ТЕКСТ И ЗАКРЫВАЕМ ДВЕРЬ
  312.                             inputCode = {}
  313.                             rs.setOutput(redstoneSide,false)
  314.  
  315.                             --РИСУЕМ ВЕРХНЮЮ ШНЯГУ РАЗНОГО ЦВЕТА В ЗАВИСИМОСТИ ОТ РЕЖИМА
  316.                             if mode == "edit" then
  317.                                 drawTab(colors.white,colors.orange)
  318.                             else
  319.                                 drawTab(colors.white,colors.black)
  320.                             end
  321.                         --ЕСЛИ ВВЕДЕН НЕВЕРНЫЙ ПАРОЛЬ
  322.                         else
  323.                             drawTab(colors.white,colors.red)
  324.                             rs.setOutput(redstoneSide,false)
  325.                             rs.setOutput(redstoneSideOnWrongPassword,true)
  326.                             sleep(3)
  327.                             rs.setOutput(redstoneSideOnWrongPassword,false)
  328.                             inputCode = {}
  329.                             drawTab(colors.white,colors.black)
  330.                         end
  331.                     end
  332.                 --ЕСЛИ НАЖАТА ЛЮБАЯ ЦИФРА
  333.                 else
  334.                     if #inputCode < 5 then
  335.                         inputCode[#inputCode+1] = key
  336.                         if mode == "edit" then
  337.                             drawTab(colors.white,colors.orange)
  338.                         else
  339.                             drawTab(colors.white,colors.black)
  340.                         end
  341.                     end
  342.                 end
  343.                 --ВЫХОД ИЗ ЦИКЛА ПЕРЕБОРА ОБЪЕКТОВ
  344.                 break
  345.             end
  346.         end
  347.  
  348.     --ЕСЛИ НАЖАТА КЛАВИША ENTER
  349.     elseif event == "key" and side == 28 then
  350.         break
  351.     end
  352. end
  353.  
  354. clearScreen(colors.black)
  355. term.setTextColor(colors.white)
  356.  
  357. term.redirect(term.native())
  358.  
  359. clearScreen(colors.black)
  360. term.setTextColor(colors.white)
  361. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement