Advertisement
Redxone

ComputerCraft - StoneLock

Jan 8th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.41 KB | None | 0 0
  1. oldpull = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3.  
  4.  
  5. local w,h = term.getSize()
  6. local rw = w/2
  7. local rh = h/2
  8. local inlock = true
  9. local name = ""
  10. local role = ""
  11. local pass = ""
  12. scp = term.setCursorPos
  13. sbc = term.setBackgroundColor
  14. stc = term.setTextColor
  15.  
  16. function readCredentials()
  17.     local f = fs.open(".usercomputerinfo","r")
  18.     local name = f.readLine()
  19.     local role = f.readLine()
  20.     local pass = f.readLine()
  21.     return name, role, pass
  22. end
  23.  
  24. function scaleup(sx,sy,ex,ey,color,speed)
  25.     local xd = ex - sx
  26.     local yd = ey - sy
  27.     local winaddx = 0
  28.     local winaddy = 0
  29.     for i = 1, xd+yd do
  30.         paintutils.drawFilledBox(sx,sy,sx+winaddx,sy+winaddy,color)
  31.         if(sx+winaddx < ex)then winaddx=winaddx+1 end
  32.         if(sy+winaddy < ey)then winaddy=winaddy+1 end
  33.         sleep(speed)
  34.     end
  35. end
  36.  
  37. function scaledown(sx,sy,ex,ey,color,speed)
  38.     local xd = ex - sx
  39.     local yd = ey - sy
  40.     local winaddx = 0
  41.     local winaddy = 0
  42.     for i = 1, xd+yd do
  43.         paintutils.drawFilledBox(ex,ey,ex+winaddx,ey+winaddy,color)
  44.         if(sy-winaddy < ey)then winaddy=winaddy-1 end
  45.         if(sx-winaddx < ex)then winaddx=winaddx-1 end
  46.         sleep(speed)
  47.     end
  48. end
  49.  
  50.  
  51.  
  52. function drawLogin()
  53.     paintutils.drawFilledBox(1,1,2,h,colors.blue)
  54.     for i = 1, w/2 do
  55.         term.current().setVisible(false)
  56.         paintutils.drawFilledBox(1,1,3+i,h,colors.gray)
  57.         paintutils.drawFilledBox(1,1,2+i,h,colors.lightGray)
  58.         paintutils.drawFilledBox(1,1,2+i,3,colors.blue)
  59.         if(i % 5 < 1)then sleep(0.01) end
  60.         term.current().setVisible(true)
  61.     end
  62.     scp(rw+3,rh+1)
  63.     sbc(colors.gray)
  64.     stc(colors.black)
  65.     write("<")
  66.     sbc(colors.lightBlue)
  67.     stc(colors.white)
  68.     local locking = "Verify credentials."
  69.     scp(((w/4)-#locking/2)+2,2)
  70.     write(locking)
  71. end
  72.  
  73. function pwrite(x,y,txt)
  74.     scp(x,y)
  75.     write(txt)
  76. end
  77.  
  78. function cwrite(txt,h)
  79.     scp(rw - #txt/2,h)
  80.     write(txt)
  81. end
  82.  
  83.  
  84. function undrawLogin()
  85.     term.current().setVisible(false)
  86.     paintutils.drawFilledBox(1,1,rw+3,h,colors.white)
  87.     term.current().setVisible(true)
  88.     for x = 1, rw+1 do
  89.         term.current().setVisible(false)
  90.         paintutils.drawFilledBox(1,1,(rw+3)-x,h,colors.gray)
  91.         paintutils.drawFilledBox(1,1,(rw+1)-x,h,colors.lightGray)
  92.         paintutils.drawFilledBox(1,1,(rw+1)-x,3,colors.blue)
  93.         paintutils.drawFilledBox((rw+3)-x,1,(rw+4)-x,h,colors.white)
  94.         term.current().setVisible(true)
  95.         if(x % 5 < 1)then sleep(0.01) end
  96.     end
  97.  
  98. end
  99.  
  100.  
  101.  
  102. function window_write(ww,wh,txt)
  103.     local x, y = term.getCursorPos()
  104.     scp(((rw+1) - ww/2)+x,((rh+2) - wh/2)+y)
  105.     write(txt)
  106. end
  107.  
  108. function window(dowait,title,text,ww,wh,bgcolor,topcolor,sdwcolor,ccol)
  109.     if(dowait == nil)then dowait = true end
  110.     paintutils.drawFilledBox(
  111.         (rw+1) - ww/2,
  112.         (rh+2) - wh/2,
  113.         (rw+1) + ww/2,
  114.         (rh+2) + wh/2,
  115.         sdwcolor
  116.     )
  117.     paintutils.drawFilledBox(
  118.         rw - ww/2,
  119.         (rh+1) - wh/2,
  120.         rw + ww/2,
  121.         (rh+1) + wh/2,
  122.         bgcolor
  123.     )
  124.     paintutils.drawLine(
  125.         rw - ww/2,
  126.         (rh+1) - wh/2,
  127.         rw + ww/2,
  128.         (rh+1) - wh/2,
  129.         topcolor
  130.     )
  131.     sbc(topcolor)
  132.     stc(2^ ( 15 - (math.log(sdwcolor) / math.log(2) ) ))
  133.     scp((rw-ww/2),(rh+1) - wh/2)
  134.     write(title)
  135.     sbc(bgcolor)
  136.     stc(sdwcolor)
  137.     local strlin = ( ( rh + 2 ) - wh/2) +1
  138.     scp((rw-ww/2), strlin)
  139.     local xx = 0
  140.     local online = 1
  141.     local ind = 0
  142.     for i = 1, #text do
  143.         xx = xx + 1
  144.         if(text:sub(ind,ind+1) == "/n")then
  145.             online = online + 1
  146.             xx = 1
  147.             ind = ind + 1
  148.         else
  149.             scp((rw-ww/2)+xx,strlin+online)
  150.             write(text:sub(ind,ind))
  151.         end
  152.         ind = ind + 1
  153.     end
  154.     if(dowait)then
  155.         os.pullEvent("key")
  156.         paintutils.drawFilledBox(
  157.             (rw) - ww/2,
  158.             (rh) - wh/2,
  159.             (rw+1) + ww/2,
  160.             (rh+2) + wh/2,
  161.             ccol
  162.         )
  163.     end
  164. end
  165.  
  166. function askyn()
  167.     local inyn = true
  168.     local isy = false
  169.     while(inyn)do
  170.         local e, k = os.pullEvent("key")
  171.         if(k == keys.y)then
  172.             isy = true
  173.             inyn = false
  174.             break
  175.         elseif(k == keys.n)then
  176.             inyn = false
  177.             break
  178.         end
  179.     end
  180.     return isy
  181. end
  182.  
  183. function defwindow()
  184.     return colors.white,colors.blue,colors.black,colors.lightGray --term.getBackgroundColor()
  185. end
  186.  
  187. function darkwindow()
  188.     return colors.lightGray,colors.blue,colors.black,colors.white --term.getBackgroundColor()
  189. end
  190.  
  191. function setupPass()
  192.     paintutils.drawFilledBox(1,1,w,h,colors.lightGray)
  193.     window(false,"Setup Password","Enter new password below.",38,8,defwindow())
  194.     scp(1,4)
  195.     sbc(colors.gray)
  196.     window_write(38,8,string.rep(" ",35))
  197.     scp(rw - 17, 11)
  198.     stc(colors.white)
  199.     local pass = read('*')
  200.     scp(-1,7)
  201.     window_write(38,8,string.rep(" ",39))
  202.     scp(16,7)
  203.     stc(colors.white)
  204.     window_write(38,8,"Y/n?")
  205.     if(not askyn())then return setupPass() end
  206.     sbc(colors.lightGray)
  207.     window(1,"Setup Password","Press any key to continue.",38,8,defwindow())
  208.  
  209.     if(not fs.exists("sha256"))then
  210.         window(1,"Setup Password","Required file missing: sha256. /n Press any key to download sha256.",38,8,defwindow())
  211.         local sha = http.get("http://pastebin.com/raw.php?i=U1fGPi4B")
  212.         if sha then
  213.             local f = fs.open("sha256","w")
  214.             f.write(sha.readAll())
  215.             f.close()
  216.             window(1,"Setup Password","Downloaded file: sha256. /n Successfully.",38,8,defwindow())
  217.             os.loadAPI("sha256")
  218.             local f = fs.open(".usercomputerinfo",'w')
  219.             f.writeLine(name)
  220.             f.writeLine(role)
  221.             f.writeLine(sha256.sha256(pass))
  222.             f.close()
  223.             window(1,"Setup Password","Setup completed! /n Press any key to reboot.",38,8,defwindow())
  224.             os.reboot()
  225.         else
  226.             window(false,"Setup Password","/n/n Sha256 download failed.",38,8,defwindow())
  227.             stc(colors.red)
  228.             scp(1,1)
  229.             window_write(38,8,"FATAL ERROR")
  230.             os.pullEvent("key")
  231.             window(1,"Fatal Error","Sha256 download failed /n press any key to exit.",38,8,defwindow())
  232.             sbc(colors.black)
  233.             stc(colors.white)
  234.             term.clear()
  235.             return false
  236.         end
  237.     end
  238.  
  239. end
  240.  
  241.  
  242. function drawCredentials()
  243.     local n, r, p = readCredentials()
  244.     paintutils.drawFilledBox(1,1,rw+3,h,colors.white)
  245.     stc(colors.lightGray)
  246.     cwrite("This Computer Is Secured With StoneLock",1)
  247.     cwrite("This computer belongs to ", 3)
  248.     pwrite(rw-10,6,"Name: " .. n)
  249.     pwrite(rw-10,7,"Role: " .. r)
  250. end
  251.  
  252. function refresh(inpass)
  253.     paintutils.drawFilledBox(1,1,w,h,colors.white)
  254.     if not sha256 then
  255.         os.loadAPI("sha256")
  256.     end
  257.     local n, r, p = readCredentials()
  258.     drawCredentials()
  259.  
  260.     if(inpass)then
  261.         paintutils.drawFilledBox(1,1,2,h,colors.lightGray)
  262.         paintutils.drawFilledBox(1,1,3+w/2,h,colors.gray)
  263.         paintutils.drawFilledBox(1,1,2+w/2,h,colors.lightGray)
  264.         paintutils.drawFilledBox(1,1,2+w/2,3,colors.blue)
  265.         scp(rw+3,rh+1)
  266.         sbc(colors.gray)
  267.         stc(colors.black)
  268.         write("<")
  269.         sbc(colors.blue)
  270.         stc(colors.white)
  271.         local locking = "Verify credentials."
  272.         scp(((w/4)-#locking/2)+2,2)
  273.         write(locking)
  274.         sbc(colors.lightGray)
  275.         stc(colors.white)
  276.         scp(2,5)
  277.         write("Enter the password for: ")
  278.         scp(2,7)
  279.         write("[" .. r .. "] " .. n)
  280.         scp(2,9)
  281.         sbc(colors.gray)
  282.         stc(colors.white)
  283.         write(string.rep(" ", rw-2))
  284.         scp(2,9)
  285.         local pss = read('*')
  286.         if(sha256.sha256(pss) == p)then
  287.             undrawLogin()
  288.             drawCredentials()
  289.             window(false,"Password","/n/n Welcome back, " .. n.. "/n Press any key.",28,8,darkwindow())
  290.             stc(colors.lime)
  291.             scp(2,1)
  292.             window_write(28,8,"Password Verified.")
  293.             os.pullEvent("key")
  294.             os.pullEvent = oldpull
  295.             inlock = false
  296.             sbc(colors.black)
  297.             stc(colors.white)
  298.             scp(1,1)
  299.             term.clear()
  300.             return true
  301.         else
  302.             undrawLogin()
  303.             drawCredentials()
  304.             window(false,"Password","/n/n Press any key.",28,8,darkwindow())
  305.             stc(colors.red)
  306.             scp(2,1)
  307.             window_write(28,8,"Password Invalid.")
  308.             os.pullEvent("key")
  309.             return false
  310.         end
  311.     else
  312.         paintutils.drawFilledBox(1,1,1,h,colors.gray)
  313.         scp(1,rh+1)
  314.         sbc(colors.gray)
  315.         stc(colors.black)
  316.         write(">")
  317.     end
  318. end
  319.  
  320.  
  321. function drawScreen()
  322.     paintutils.drawFilledBox(1,1,w,h,colors.white)
  323.     if(not fs.exists(".usercomputerinfo"))then
  324.         paintutils.drawFilledBox(1,1,w,h,colors.lightGray)
  325.         window(1,"Setup Description","  Welcome to StoneLock. /n/n A lock designed for all /n StoneFruit staff members.",28,8,defwindow())
  326.         window(1,"Setup Description","Role description required. /n/n Press any key to setup.",28,8,defwindow())
  327.         window(false,"Setup Description","Please enter your role at StoneFruit.",38,8,defwindow())
  328.         scp(1,4)
  329.         sbc(colors.gray)
  330.         window_write(38,8,string.rep(" ",35))
  331.         os.pullEvent("key")
  332.         scp(rw - 17, 11)
  333.         stc(colors.white)
  334.         role = read()
  335.         sbc(colors.lightGray)
  336.         window(1,"Setup Description","Press any key to continue.",38,8,defwindow())
  337.         window(false,"Setup Description","What is your name?.",28,8,defwindow())
  338.         scp(1,4)
  339.         sbc(colors.gray)
  340.         window_write(28,8,string.rep(" ",26))
  341.         os.pullEvent("key")
  342.         scp(rw - 12, 11)
  343.         stc(colors.white)
  344.         name = read()
  345.         window(false,"Setup Description","Is this correct? /n/n Name: " .. name .. "/n Role: " .. role,38,10,defwindow())
  346.         sbc(colors.gray)
  347.         scp(-1,9)
  348.         window_write(38,10,string.rep(" ",39))
  349.         scp(16,9)
  350.         stc(colors.white)
  351.         window_write(38,10,"Y/n?")
  352.         if(not askyn())then return drawScreen() end
  353.         sbc(colors.lightGray)
  354.         window(1,"Setup Password","Press any key to continue.",38,10,defwindow())
  355.         return setupPass()
  356.     else
  357.         return refresh(false)
  358.     end
  359. end
  360.  
  361. function update()
  362.     drawScreen()
  363.     while inlock do
  364.         -- Handle menu opening and function calling here
  365.          local e, b, x, y = os.pullEvent()
  366.          if(e == "mouse_click")then
  367.              if(x == 1)then
  368.                 drawLogin()
  369.                 if(not refresh(true))then
  370.                     refresh(false)
  371.                 end
  372.              end
  373.          elseif(e == "key")then
  374.             if(b == keys.right)then
  375.                 drawLogin()
  376.                 if(not refresh(true))then
  377.                     refresh(false)
  378.                 end
  379.              end
  380.          end
  381.     end
  382. end
  383.  
  384.  
  385.  
  386. update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement