Advertisement
Pirnogion

CodeDoorForOC

Mar 25th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local password = "12345"
  2.  
  3. local buttons = {
  4.     {"1","2","3"},
  5.     {"4","5","6"},
  6.     {"7","8","9"},
  7.     {"C","0","#"},
  8. }
  9.  
  10. local Obj = {}
  11.  
  12. local inputCode = {}
  13.  
  14. local xSize,ySize = term.getSize()
  15. local centerX = math.floor(xSize/2)
  16. local centerX = math.floor(ySize/2)
  17.  
  18. --ПОИСК ПЕРИФЕРИИ
  19. local function findPeripheral(whatToFind)
  20.   local PeriList = peripheral.getNames()
  21.   for i=1,#PeriList do
  22.     if peripheral.getType(PeriList[i]) == whatToFind then
  23.       return PeriList[i]
  24.     end
  25.   end
  26. end
  27.  
  28. --ЧТЕНИЕ КОНФИГА
  29. function configRead(pathToConfig,whatToRead)
  30.     if not fs.exists(pathToConfig) then error("No such file") end
  31.     local file = fs.open(pathToConfig,"r")
  32.     while true do
  33.         local line = file.readLine()
  34.         if line ~= nil then
  35.             local key, value = string.match(line,"(.*)=(.*)")
  36.             if value ~= nil and key == whatToRead then
  37.                 file.close()
  38.                 return value
  39.             end
  40.         else
  41.             file.close()
  42.             break
  43.         end
  44.     end
  45. end
  46.  
  47. --ЗАПИСЬ В КОНФИГ
  48. local function configWrite(pathToConfig,key,value)
  49.     if not fs.exists(pathToConfig) then
  50.         local file = fs.open(pathToConfig,"w")
  51.         file.close()
  52.     end
  53.  
  54.     local file = fs.open(pathToConfig,"r")
  55.     local Massiv = {}
  56.    
  57.     local lineCounter = 1
  58.     while true do
  59.         local line = file.readLine()
  60.         if line ~= nil then
  61.             Massiv[lineCounter] = line
  62.         else
  63.             file.close()
  64.             break
  65.         end
  66.         lineCounter = lineCounter + 1
  67.     end
  68.  
  69.     local success = false
  70.     for i=1,#Massiv do
  71.         local key1, value1 = string.match(Massiv[i],"(.*)=(.*)")
  72.         if value1 ~= nil and key1 == key then
  73.             Massiv[i] = key.."="..value
  74.             success = true
  75.         end
  76.     end
  77.  
  78.     if success == false then Massiv[#Massiv+1] = key.."="..value end
  79.  
  80.     local file = fs.open(pathToConfig,"w")
  81.     for i=1,#Massiv do
  82.         file.writeLine(Massiv[i])
  83.     end
  84.     file.close()
  85. end
  86.  
  87. --ОБЪЕКТЫ
  88. local function newObj(name,x,y)
  89.     Obj[name]={}
  90.     Obj[name]["x"]=x
  91.     Obj[name]["y"]=y
  92. end
  93.  
  94. --ПРОСТАЯ ЗАЛИВКА ЭКРАНА ЦВЕТОМ
  95. local function clearScreen(color)
  96.     term.setBackgroundColor(color)
  97.     term.clear()
  98. end
  99.  
  100. --ПРОСТОЙ ТЕКСТ
  101. local function usualText(x,y,text)
  102.     term.setCursorPos(x,y)
  103.     term.write(text)
  104. end
  105.  
  106. --ОТРИСОВКА ВЕРХНЕЙ ШТУЧКИ
  107. local function drawTab(textColor,backColor)
  108.     term.setBackgroundColor(backColor)
  109.     term.setTextColor(textColor)
  110.     term.setCursorPos(2,1)
  111.     term.clearLine()
  112.     term.write("-----")
  113.  
  114.     for i=1,#inputCode do
  115.         usualText(1+i,1,inputCode[i])
  116.     end
  117. end
  118.  
  119. ----------------------CТАРТ ПРОГРАММЫ------------------------------------
  120.  
  121. --ПОДКЛЮЧЕНИЕ МОНИТОРА
  122. local m = findPeripheral("monitor")
  123. if m ~= nil then
  124.     m = peripheral.wrap(m)
  125.     if not m.isColor() then error("This program works ONLY with advanced monitor.") end
  126.     m.setTextScale(1)
  127.     term.redirect(m)
  128. else
  129.     error("This program requires advanced external monitor.")
  130. end
  131.  
  132. --ЧТЕНИЕ КОНФИГА
  133. if fs.exists("System/CodeDoor/password.cfg") then
  134.     password = configRead("System/CodeDoor/password.cfg","password")
  135. else
  136.     configWrite("System/CodeDoor/password.cfg","password","12345")
  137. end
  138.  
  139. term.setCursorBlink(false)
  140. clearScreen(colors.white)
  141. term.setTextColor(colors.black)
  142.  
  143. for j=1,#buttons do
  144.     for i=1,#buttons[j] do
  145.         local xPos = i*2
  146.         local yPos = 1+j
  147.         usualText(xPos,yPos,buttons[j][i])
  148.         newObj(buttons[j][i],xPos,yPos)
  149.     end
  150. end
  151.  
  152. drawTab(colors.white,colors.black)
  153.  
  154. while true do
  155.     local event,side,x,y = os.pullEvent()
  156.     if event == "monitor_touch" then
  157.         for key,val in pairs(Obj) do
  158.             if x==Obj[key]["x"] and y==Obj[key]["y"] then
  159.                 term.setBackgroundColor(colors.green)
  160.                 term.setTextColor(colors.white)
  161.                 usualText(Obj[key]["x"],Obj[key]["y"],key)
  162.                 sleep(0.2)
  163.                 term.setBackgroundColor(colors.white)
  164.                 term.setTextColor(colors.black)
  165.                 usualText(Obj[key]["x"],Obj[key]["y"],key)
  166.  
  167.                 if key ~= "C" and key ~= "#" then
  168.                     if #inputCode < 5 then
  169.                         inputCode[#inputCode+1] = key
  170.                         drawTab(colors.white,colors.black)
  171.                     end
  172.  
  173.                 elseif key == "C" then
  174.                     inputCode = {}
  175.                     drawTab(colors.white,colors.black)
  176.  
  177.                 elseif key == "#" then
  178.                     local inputPass = ""
  179.                     for i=1,#inputCode do
  180.                         inputPass = inputPass..inputCode[i]
  181.                     end
  182.  
  183.                     if inputPass == password then
  184.                         drawTab(colors.white,colors.green)
  185.                         sleep(5)
  186.                         inputCode = {}
  187.                         drawTab(colors.white,colors.black)
  188.                     else
  189.                         drawTab(colors.white,colors.red)
  190.                         sleep(1)
  191.                         drawTab(colors.white,colors.black)
  192.                     end
  193.                 end
  194.  
  195.                 break
  196.             end
  197.         end
  198.     end
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement