Advertisement
Guest User

autorize.lua

a guest
Dec 4th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.35 KB | None | 0 0
  1. local cmp = require('component')
  2. local event = require('event')
  3. local serialization = require('serialization')
  4. local gpu=cmp.gpu
  5. local filesystem=require('filesystem')
  6. local term =require('term')
  7. local computer = require('computer')
  8. local unicode = require('unicode')
  9. local draw = require('draw')
  10. local sides = require("sides")
  11. local note = require("note")
  12. local keyboard = require("keyboard")
  13. ------------
  14. local A,B=gpu.getResolution()
  15. local oldBack = gpu.getBackground()
  16. local oldFore = gpu.getForeground()
  17. local usersFile = 'users.list'
  18. ------------
  19. --gpu.setResolution(120,40)
  20. ------------
  21.  
  22. local color={
  23.   back=0xDCDCDC,
  24.   line=0x505050,
  25.   line_back=0,
  26.   pass_field=0,
  27.   enter_pass=0x898989,
  28.   user_name=123123,
  29.   user_frame=0x505050,
  30.   right=0xFFA400,
  31.   left=0xFFA400,
  32.   color_ball=0xFFFFFF,
  33.   back_ball=0
  34. }
  35.  
  36. local buttonsUsers={
  37. {54,11,8,7}, -- Right
  38. {20,11,8,7}, -- Left
  39. {27,21,30,3} -- Password Field
  40. }
  41.  
  42.  
  43. local function saveTbl(tbl, fl)
  44.   file = io.open(fl, 'w')
  45.   file:write(serialization.serialize(tbl))
  46.   file:close()
  47. end
  48. local function loadTbl(fl)
  49.   file = io.open(fl, 'r')
  50.   if not file then
  51.     file = io.open(fl, 'w')
  52.     file:write('{}')
  53.     return {} else
  54.     return serialization.unserialize(file:read('*a'))
  55.   end
  56.   file:close()
  57. end
  58.  
  59. function getRawEvent(arr)
  60.   local N = nil
  61.   local key,_,x,y,clickMouse,name=event.pull()
  62.   if key == 'touch' then
  63.     for i=1,#arr do
  64.       if x > arr[i][1]-1 and x < arr[i][1]+arr[i][3] and y > arr[i][2]-1 and y < arr[i][2]+arr[i][4] then
  65.         N=i
  66.         break
  67.       end
  68.     end
  69.   end
  70.   if N == nil then
  71.     N = 0
  72.   end
  73.   return key,N,clickMouse,name  
  74. end
  75. -- ? y=20
  76. function drawBalls(selectUser)
  77.   balls=''
  78.   ball=' ● '
  79.   ball2=' • '
  80.   for i=1,#users do
  81.     if i == selectUser then
  82.       balls=balls..ball
  83.   else
  84.     balls=balls..ball2
  85.   end
  86.   end
  87.   draw.text(16,18,50,balls,color.back_ball,color.color_ball)
  88. end
  89.  
  90. function enter(x,y,w,h,color_back,color_fore,userName)
  91.   local password = users[userName][2]
  92.   got=nil
  93.   function drawTextInPass(x,y,w,h,txt,col)
  94.   if unicode.len(txt) > w-3 then
  95.       txt=unicode.sub(txt,unicode.len(txt)-(w-3),unicode.len(txt))
  96.     end
  97.   draw.field(x+1,y+1,w-2,1,' ',color.pass_field,color.enter_pass)
  98.     draw.text(x+1,y+1,w-2,txt,color.pass_field,col)
  99.   end
  100.   draw.field(x,y,w,h,' ',color_back,color_fore)
  101.   draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,0xAA0000)
  102.   local function getKey(s)
  103.     local key, _, x1 , code, _ = event.pull()
  104.   if key == 'key_up' or key == 'touch' then
  105.     return code , x1 , key
  106.   end
  107.   end
  108.   local function enterPass(x,y,w,h)
  109.   try = ""
  110.   ex=true
  111.   while ex do
  112.     local err, f , x1 , key  = pcall(getKey)
  113.     if not err then
  114.       return err, try
  115.     end
  116.   if f ~= nil and key == 'key_up' then --
  117.     if keyboard.keys[f] == 'grave' then ex=false end
  118.     if f < 60 and f > 0 then
  119.       if keyboard.keys[f] == 'enter' then
  120.             draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  121.             return err, try , 's'
  122.     end
  123.       if keyboard.keys[f] == 'back' then
  124.         if unicode.len(try) > 0 then
  125.           try=unicode.sub(try,0,unicode.len(try)-1)
  126.           if try ~= nil then
  127.             try2='' for l=1,unicode.len(try) do try2=try2..'*' end
  128.       end
  129.           drawTextInPass(x,y,w,h,try2,color_fore)
  130.       else
  131.         try = 'cancel'
  132.         draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  133.         return err , try , 'f'
  134.         end
  135.       end
  136.       if unicode.len(keyboard.keys[f]) == 1 then
  137.         try = try..keyboard.keys[f]
  138.       if try ~= nil then
  139.             try2='' for l=1,unicode.len(try) do try2=try2..'*' end
  140.       end
  141.           drawTextInPass(x,y,w,h,try2,color_fore)
  142.     elseif keyboard.keys[f] == 'space' then
  143.           if try ~= nil then
  144.             try2='' for l=1,unicode.len(try) do try2=try2..'*' end
  145.       end
  146.           drawTextInPass(x,y,w,h,try2,color_fore)
  147.     elseif keyboard.keys[f] == 'minus' then
  148.           if try ~= nil then
  149.             try2='' for l=1,unicode.len(try) do try2=try2..'*' end
  150.       end
  151.           drawTextInPass(x,y,w,h,try2,color_fore)
  152.     elseif keyboard.keys[f] == 'period' then
  153.           if try ~= nil then
  154.             try2='' for l=1,unicode.len(try) do try2=try2..'*' end
  155.       end
  156.           drawTextInPass(x,y,w,h,try2,color_fore)
  157.   elseif keyboard.keys[f] == 'comma' then
  158.           if try ~= nil then
  159.             try2='' for l=1,unicode.len(try) do try2=try2..'*' end
  160.       end
  161.           drawTextInPass(x,y,w,h,try2,color_fore)
  162.   elseif keyboard.keys[f] == 'slash' then
  163.           if try ~= nil then
  164.             try2='' for l=1,unicode.len(try) do try2=try2..'*' end
  165.       end
  166.           drawTextInPass(x,y,w,h,try2,color_fore)
  167.       end
  168.   end
  169.   elseif key == 'touch' and f ~= nil then
  170.       if x1 > x-1 and x1 < x+w and f > y-1 and f < y+h then
  171.       --
  172.       else
  173.           draw.clound('┌','┐','└','┘','│','─',x,y,w,h,color_back,color_fore)
  174.       try = nil
  175.           return err, try , 's'
  176.       end
  177.     end
  178.     end
  179.   end --
  180.   while true do
  181.     err, try , got = enterPass(x,y,w,h)
  182.     if got == 's' and try == password then -- success
  183.         note.play(83, 0.3)
  184.         note.play(90, 0.2)
  185.     drawTextInPass(x,y,w,h,'Доступ разрешен!',0x00FF00)
  186.         os.sleep(2.5)
  187.         gpu.setBackground(oldBack)
  188.         gpu.setForeground(oldFore)
  189.         gpu.setResolution(A,B)
  190.         term.clear()
  191.         os.exit()
  192.     elseif got == 's' and try ~= password then
  193.     drawTextInPass(x,y,w,h,'',0xFFFFFF)
  194.     try = nil
  195.     break
  196.   elseif got == 'f' then
  197.     drawTextInPass(x,y,w,h,'',0xFFFFFF)
  198.     try = nil
  199.     break
  200.     end
  201.   end
  202.   return try
  203. end
  204.  
  205. function drawUsersList()
  206.   selectUser=1
  207.   drawBalls(selectUser)
  208. local function drawUser(selectUser)
  209. -- <user>
  210.   draw.field(32,8,18,9,' ',color.user_frame,0x505050)
  211.   draw.clound('┌','┐','└','┘','│','─',32,8,18,9,color.user_frame,0xFFFFFF)
  212.   draw.text(33,12,16,users[selectUser][1],color.user_frame,color.user_name)
  213. -- </user>
  214. end
  215.   drawUser(selectUser)
  216. -- <Right>
  217.   draw.field(54,9,4,7,' ',color.right,0x505050)
  218.   draw.field(58,10,2,5,' ',color.right,0x505050)
  219.   draw.field(60,11,2,3,' ',color.right,0x505050)
  220.   draw.field(62,12,1,1,' ',color.right,0x505050)
  221. -- </Right>
  222.  
  223. -- <Left>
  224.   draw.field(24,9,4,7,' ',color.left,0x505050)
  225.   draw.field(22,10,2,5,' ',color.left,0x505050)
  226.   draw.field(20,11,2,3,' ',color.left,0x505050)
  227.   draw.field(19,12,1,1,' ',color.left,0x505050)
  228. -- </Left>
  229.  
  230. -- <line>
  231.   draw.field(15,19,50,1,'─',color.line_back,color.line)
  232.   draw.field(15,6,50,1,'─',color.line_back,color.line)
  233. -- </line>
  234.  
  235. -- <Password_Field>
  236.   draw.field(26,21,30,3,' ',color.pass_field,0x505050)
  237.   draw.clound('┌','┐','└','┘','│','─',26,21,30,3,color.pass_field,0xFFFFFF)
  238.   draw.text(27,22,17,'Введите пароль...',color.pass_field,color.enter_pass)
  239. -- </Password_Field>
  240.   while true do
  241.     key,N,clickMouse,name=getRawEvent(buttonsUsers)
  242.    
  243.   if N == 1 then
  244.     selectUser = selectUser+1
  245.     if selectUser > #users then
  246.       selectUser = #users
  247.     end
  248.     drawUser(selectUser)
  249.   drawBalls(selectUser)
  250.     elseif N == 2 then
  251.     selectUser = selectUser-1
  252.     if selectUser < 1 then
  253.       selectUser = 1
  254.     end
  255.     drawUser(selectUser)
  256.   drawBalls(selectUser)
  257.   elseif N == 3 then
  258.     enter(26,21,30,3,0,0xffffff,selectUser)
  259.   end
  260.   end
  261. end
  262.  
  263. if filesystem.exists(usersFile) == true then  
  264.   term.clear()
  265.   draw.err('Добро пожаловать!',1,2)
  266.   term.clear()
  267.   users=loadTbl(usersFile)
  268.   drawUsersList()
  269. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement