Advertisement
Guest User

f

a guest
Aug 2nd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.26 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.   pass_field=0,
  26.   enter_pass=0x898989,
  27.   user_name=123123,
  28.   user_frame=0x505050
  29. }
  30.  
  31. local buttonsUsers={
  32. {72,11,9,7}, -- Right
  33. {39,11,9,7}, -- Left
  34. {45,27,30,3} -- Password Field
  35. }
  36.  
  37. local function saveTbl(tbl, fl)
  38.   file = io.open(fl, 'w')
  39.   file:write(serialization.serialize(tbl))
  40.   file:close()
  41. end
  42. local function loadTbl(fl)
  43.   file = io.open(fl, 'r')
  44.   if not file then
  45.     file = io.open(fl, 'w')
  46.     file:write('{}')
  47.     return {} else
  48.     return serialization.unserialize(file:read('*a'))
  49.   end
  50.   file:close()
  51. end
  52.  
  53. function getRawEvent(arr)
  54.   local N = nil
  55.   local key,_,x,y,clickMouse,name=event.pull()
  56.   if key == 'touch' then
  57.     for i=1,#arr do
  58.       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
  59.         N=i
  60.         break
  61.       end
  62.     end
  63.   end
  64.   if N == nil then
  65.     N = 0
  66.   end
  67.   return key,N,clickMouse,name  
  68. end
  69.  
  70. function enterPassword(userName)
  71.   draw.field(46,28,28,1,' ',color.pass_field,0x505050)
  72.   term.setCursor(47,28)
  73.   local password = users[userName][2]
  74.   local function getKey(s)
  75.     local _, _, _, code, _ = event.pull("key_up")
  76.     return code
  77.   end
  78.   local function enterPass()
  79.   try = ""
  80.   ex=true
  81.   while ex do
  82.     local err, f = pcall(getKey)
  83.     if not err then
  84.       return err, try
  85.     end
  86.     if keyboard.keys[f] == 'grave' then ex=false end
  87.     if f < 60 and f > 0 then
  88.       if keyboard.keys[f] == 'enter' then
  89.         return err, try
  90.       end
  91.       if keyboard.keys[f] == 'back' then
  92.         if unicode.len(try) > 0 then
  93.           try=unicode.sub(try,0,unicode.len(try)-1)
  94.           draw.field(46,28,28,1,' ',color.pass_field,0x505050)
  95.           draw.field(46,28,unicode.len(try),1,'*',color.pass_field,0x505050)
  96.         end
  97.       end
  98.       if unicode.len(keyboard.keys[f]) == 1 and unicode.len(try) < 28 then
  99.         try = try..keyboard.keys[f]
  100.         draw.field(46,28,28,1,' ',color.pass_field,0x505050)
  101.         if try ~= nil then
  102.           try2='' for l=1,unicode.len(try) do try2=try2..'*' end
  103.         end
  104.         draw.text(46,28,28,try2,color.pass_field,color.enter_pass)
  105.       end
  106.     end
  107.   end
  108. end
  109. while true do
  110.   err, try = enterPass()
  111.   if not err then
  112.     draw.field(46,28,28,1,' ',color.pass_field,0x505050)
  113.     draw.text(46,28,28,"Самый умный? =)",color.pass_field,color.enter_pass)
  114.   elseif try == password then -- success
  115.    
  116.     note.play(83, 0.3)
  117.     note.play(90, 0.2)
  118.     draw.field(46,28,28,1,' ',color.pass_field,color.enter_pass)
  119.     draw.text(46,28,28,'Доступ разрешен!',color.pass_field,0x00AA00)
  120.     os.sleep(2.5)
  121.     gpu.setBackground(oldBack)
  122.     gpu.setForeground(oldFore)
  123.     gpu.setResolution(A,B)
  124.     term.clear()
  125.     os.exit()
  126.   elseif try == "power" then -- admin pass
  127.     os.exit()
  128.   elseif try == "cls" then
  129.     --  term.clear()
  130.   else
  131.     draw.field(46,28,28,1,' ',color.pass_field,color.enter_pass)
  132.     draw.text(46,28,28,"В доступе отказано!",color.pass_field,0xAA0000)
  133.     note.play(70, 0.2)
  134.   end
  135. end
  136. end
  137.  
  138. function drawUsersList()
  139.   selectUser=1
  140.  
  141. local function drawUser(selectUser)
  142. -- <user>
  143.   draw.field(51,10,18,9,' ',color.user_frame,0x505050)
  144.   draw.clound('┌','┐','└','┘','│','─',51,10,18,9,color.user_frame,0xFFFFFF)
  145.   draw.text(52,14,16,users[selectUser][1],color.user_frame,color.user_name)
  146. -- </user>
  147. end
  148.   drawUser(selectUser)
  149. -- <Right>
  150.   draw.field(72,11,4,7,' ',0xFFA400,0x505050)
  151.   draw.field(76,12,2,5,' ',0xFFA400,0x505050)
  152.   draw.field(78,13,2,3,' ',0xFFA400,0x505050)
  153.   draw.field(80,14,1,1,' ',0xFFA400,0x505050)
  154. -- </Right>
  155.  
  156. -- <Left>
  157.   draw.field(44,11,4,7,' ',0xFFA400,0x505050)
  158.   draw.field(42,12,2,5,' ',0xFFA400,0x505050)
  159.   draw.field(40,13,2,3,' ',0xFFA400,0x505050)
  160.   draw.field(39,14,1,1,' ',0xFFA400,0x505050)
  161. -- </Left>
  162.  
  163. -- <line>
  164.   draw.field(20,21,80,1,'─',color.back,color.line)
  165.   draw.field(20,8,80,1,'─',color.back,color.line)
  166. -- </line>
  167.  
  168. -- <Password_Field>
  169.   draw.field(45,27,30,3,' ',color.pass_field,0x505050)
  170.   draw.clound('┌','┐','└','┘','│','─',45,27,30,3,color.pass_field,0xFFFFFF)
  171.   draw.text(47,28,17,'Введите пароль...',color.pass_field,color.enter_pass)
  172. -- </Password_Field>
  173.   while true do
  174.     key,N,clickMouse,name=getRawEvent(buttonsUsers)
  175.    
  176.   if N == 1 then
  177.     selectUser = selectUser+1
  178.     if selectUser > #users then
  179.       selectUser = #users
  180.     end
  181.     drawUser(selectUser)
  182.     elseif N == 2 then
  183.     selectUser = selectUser-1
  184.     if selectUser < 1 then
  185.       selectUser = 1
  186.     end
  187.     drawUser(selectUser)
  188.   elseif N == 3 then
  189.     enterPassword(selectUser)
  190.   end
  191.   end
  192. end
  193.  
  194. if filesystem.exists(usersFile) == true then  
  195.   term.clear()
  196.   draw.err('Добро пожаловать!',1,2)
  197.   term.clear()
  198.   users=loadTbl(usersFile)
  199.   drawUsersList()
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement