Advertisement
Guest User

client

a guest
Jun 8th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.29 KB | None | 0 0
  1. serverID = 5
  2. currentUser = {}
  3. os.loadAPI("gui")
  4. modemSide = "top"
  5.  
  6. rednet.open(modemSide)
  7.  
  8. allChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  9.  
  10. function randomString(length)
  11.   e = ""
  12.   for i = 1, length do
  13.     r = math.random(#allChars)
  14.     e = e..string.sub(allChars,r,r)
  15.   end
  16.   return e
  17. end
  18.  
  19. function clear()
  20.   term.clear()
  21.   term.setCursorPos(1,1)
  22. end
  23.  
  24. term.setBackgroundColor(colors.lightBlue)
  25. clear()
  26.  
  27. term.setCursorPos(23,4)
  28. print("LOG IN")
  29. gui.newButton(17,6,18,3,"",colors.lightGray)
  30. usernameArea = gui.newButton(18,7,16,1,"usernameArea",colors.gray)
  31.  
  32. gui.newButton(17,10,18,3,"",colors.lightGray)
  33. passwordArea = gui.newButton(18,11,16,1,"passwordArea",colors.gray)
  34.  
  35. submit = gui.newButton(22,15,8,3,"submit",colors.red)
  36. term.setCursorPos(23,16)
  37. term.setBackgroundColor(colors.red)
  38. term.write("Submit")
  39.  
  40. username = ""
  41. password = ""
  42.  
  43. while true do
  44.   title = gui.waitForButtons(usernameArea,passwordArea,submit)
  45.   if title == "usernameArea" then
  46.     username = gui.inputField(18,7,username,16,colors.gray)
  47.   elseif title == "passwordArea" then
  48.     password = gui.inputField(18,11,password,16,colors.gray,"*")
  49.   elseif title == "submit" then
  50.     rednet.send(serverID, {{username," ",1,password},"LOG_IN"})
  51.     id,msg,d = rednet.receive(.3)
  52.     if id == serverID then
  53.       currentUser = msg
  54.      
  55.       while true do
  56.      
  57.         term.setBackgroundColor(colors.lightBlue)
  58.         clear()
  59.      
  60.         inhibitors = gui.newButton(2,2,12,3,"INHIBITORS ",colors.blue)
  61.         term.setBackgroundColor(colors.blue)
  62.         term.setCursorPos(3,3)
  63.         term.write("Inhibitors")
  64.        
  65.         switches = gui.newButton(2,6,10,3,"SWITCHES",colors.blue)
  66.         term.setCursorPos(3,7)
  67.         term.write("Switches")
  68.        
  69.         keyLocks = gui.newButton(2,10,11,3,"KEY_LOCKS ",colors.blue)
  70.         term.setCursorPos(3,11)
  71.         term.write("Key Locks")
  72.        
  73.         account = gui.newButton(2,14,9,3,"ACCOUNT",colors.blue)
  74.         term.setCursorPos(3,15)
  75.         term.write("Account")
  76.        
  77.         addAccount = gui.newButton(15,2,13,3,"ADD_ACCOUNT",colors.blue)
  78.         term.setCursorPos(16,3)
  79.         term.write("Add Account")
  80.        
  81.         admin = gui.newButton(15,6,7,3,"ADMIN",colors.blue)
  82.         term.setCursorPos(16,7)
  83.         term.write("Admin")
  84.        
  85.         back = gui.newButton(15,10,6,3,"BACK",colors.blue)
  86.         term.setCursorPos(16,11)
  87.         term.write("Back")
  88.        
  89.         choice = gui.waitForButtons(back,addAccount,account,inhibitors,switches,keyLocks,admin)
  90.          
  91.         if choice == "INHIBITORS " or choice == "SWITCHES" or choice == "KEY_LOCKS " then
  92.           while true do
  93.             term.setBackgroundColor(colors.lightBlue)
  94.             clear()
  95.             print(choice)
  96.             rednet.send(serverID, {currentUser,"GET_"..choice})
  97.             id,msg,d = rednet.receive(.5)
  98.             if id == serverID then
  99.               if type(msg) == "table" then
  100.                 drawArray = {}
  101.                 for i,v in ipairs(msg) do
  102.                   table.insert(drawArray,v[4].."("..v[3].."): "..v[2])
  103.                 end
  104.                 table.insert(drawArray,"<--")
  105.              
  106.                 selection = gui.drawArray(drawArray,3)
  107.                
  108.                 if selection ~= nil and selection ~= #drawArray then
  109.                   term.setBackgroundColor(colors.lightBlue)
  110.                   msgToServer = string.sub(choice,1,-3)
  111.                   rednet.send(serverID,{currentUser,"CHANGE_"..msgToServer,msg[selection][1]})
  112.                   sleep(.3)
  113.                 elseif selection == #drawArray then
  114.                   term.setBackgroundColor(colors.lightBlue)
  115.                   clear()
  116.                   break
  117.                 end
  118.               end
  119.             else
  120.               print("SERVER ERROR")
  121.               sleep(1)
  122.             end
  123.           end
  124.         elseif choice == "ADMIN" then
  125.           while true do
  126.             sleep(.3)
  127.             term.setBackgroundColor(colors.lightBlue)
  128.             clear()
  129.             print("Admin")
  130.            
  131.             term.setBackgroundColor(colors.blue)
  132.             changeKey = gui.newButton(2,2,22,3,"CHANGE_KEY",colors.blue)
  133.             term.setCursorPos(3,3)
  134.             term.write("Change Key Clearance")
  135.            
  136.             changeUserClearance = gui.newButton(2,6,23,3,"CHANGE_USER_CLEARANCE",colors.blue)
  137.             term.setCursorPos(3,7)
  138.             term.write("Change User Clearance")
  139.            
  140.             removeUser = gui.newButton(2,10,13,3,"REMOVE_USER",colors.blue)
  141.             term.setCursorPos(3,11)
  142.             term.write("Remove User")
  143.            
  144.             back = gui.newButton(2,14,6,3,"BACK",colors.blue)
  145.             term.setCursorPos(3,15)
  146.             term.write("Back")
  147.            
  148.             choice2 = gui.waitForButtons(changeKey,changeUserClearance,removeUser,back)
  149.            
  150.             if choice2 == "CHANGE_KEY" then
  151.               while true do
  152.                 sleep(.3)
  153.                 rednet.send(serverID,{currentUser,"GET_KEY_LOCKS "})
  154.                 id,msg,d = rednet.receive(.3)
  155.                 displayArray = {}
  156.                 if id == serverID then
  157.                   allLocks = msg
  158.                  
  159.                   for i,v in ipairs(allLocks) do
  160.                     table.insert(displayArray,v[4]..":"..v[5])
  161.                   end
  162.                
  163.                   table.insert(displayArray,"Back")
  164.                
  165.                   term.setBackgroundColor(colors.lightBlue)
  166.                   clear()
  167.                   selection = gui.drawArray(displayArray)
  168.                  
  169.                   if selection ~= nil and selection ~= #displayArray then
  170.                     newClearance = allLocks[selection][5]
  171.                     while true do
  172.                       term.setBackgroundColor(colors.lightBlue)
  173.                       clear()
  174.                       print(allLocks[selection][4]..":"..allLocks[selection][5])
  175.                      
  176.                       term.setBackgroundColor(colors.blue)
  177.                      
  178.                       up = gui.newButton(2,3,1,1,"UP",colors.blue)
  179.                       term.setCursorPos(2,3)
  180.                       term.write("^")
  181.                      
  182.                       down = gui.newButton(2,4,1,1,"DOWN",colors.blue)
  183.                       term.setCursorPos(2,4)
  184.                       term.write("V")
  185.                      
  186.                       term.setBackgroundColor(colors.red)
  187.                       submit = gui.newButton(2,6,8,3,"SUBMIT",colors.red)
  188.                       term.setCursorPos(3,7)
  189.                       term.write("Submit")
  190.                      
  191.                       term.setCursorPos(3,3)
  192.                       term.setBackgroundColor(colors.lightBlue)
  193.                       term.write(newClearance)
  194.                      
  195.                       choice3 = gui.waitForButtons(up,down,submit)
  196.                      
  197.                       if choice3 == "UP" then
  198.                         newClearance = newClearance +1
  199.                       elseif choice3 == "DOWN" then
  200.                         newClearance = newClearance - 1
  201.                       elseif choice3 == "SUBMIT" then
  202.                         rednet.send(serverID,{currentUser,"CHANGE_KEY_LOCK_CLEARANCE",allLocks[selection],newClearance})
  203.                         break
  204.                       end
  205.                     end
  206.                   elseif selection == #displayArray then
  207.                     break
  208.                   end
  209.                 else
  210.                   term.setBackgroundColor(colors.lightBlue)
  211.                   clear()
  212.                   print("TOO LOW CLEARANCE OR SERVER ERROR")
  213.                   sleep(1)
  214.                   break
  215.                 end
  216.               end
  217.             elseif choice2 == "CHANGE_USER_CLEARANCE" then
  218.               while true do
  219.                 sleep(.3)
  220.                 rednet.send(serverID,{currentUser,"GET_USERS"})
  221.                 id,msg,d = rednet.receive(.3)
  222.                
  223.                 if id == serverID then
  224.                   displayArray = {}
  225.                  
  226.                   for i,v in ipairs(msg) do
  227.                     table.insert(displayArray,v[1]..":"..v[3])
  228.                   end
  229.                  
  230.                   table.insert(displayArray,"Back")
  231.                
  232.                   term.setBackgroundColor(colors.lightBlue)
  233.                   clear()
  234.                   print("Users")
  235.                   selection = gui.drawArray(displayArray)
  236.                
  237.                   if selection ~= nil and selection ~= #displayArray then
  238.                     newClearance = msg[selection][3]
  239.                     while true do
  240.                       term.setBackgroundColor(colors.lightBlue)
  241.                       clear()
  242.                       print(displayArray[selection])
  243.                       up = gui.newButton(2,3,1,1,"UP",colors.blue)
  244.                       term.setBackgroundColor(colors.blue)
  245.                       term.setCursorPos(2,3)
  246.                       term.write("^")
  247.                      
  248.                       down = gui.newButton(2,4,1,1,"DOWN",colors.blue)
  249.                       term.setBackgroundColor(colors.blue)
  250.                       term.setCursorPos(2,4)
  251.                       term.write("V")
  252.                    
  253.                       term.setBackgroundColor(colors.lightBlue)
  254.                       term.setCursorPos(3,3)
  255.                       term.write(newClearance)
  256.                    
  257.                       term.setBackgroundColor(colors.red)
  258.                       submit = gui.newButton(2,7,8,3,"SUBMIT",colors.red)
  259.                       term.setCursorPos(3,8)
  260.                       term.write("Submit")
  261.                    
  262.                       choice3 = gui.waitForButtons(up,down,submit)
  263.                    
  264.                       if choice3 == "UP" then
  265.                         newClearance = newClearance + 1
  266.                       elseif choice3 == "DOWN" then
  267.                         newClearance = newClearance - 1
  268.                       elseif choice3 == "SUBMIT" then
  269.                         rednet.send(serverID, {currentUser,"CHANGE_USER_CLEARANCE",msg[selection],newClearance})
  270.                         break
  271.                       end
  272.                     end
  273.                   elseif selection == #displayArray then
  274.                     break
  275.                   end
  276.                 else
  277.                   term.setBackgroundColor(colors.lightBlue)
  278.                   clear()
  279.                   print("TOO LOW CLEARANCE LEVEL OR SERVER ERROR")
  280.                   os.sleep(1)
  281.                   break
  282.                 end
  283.               end
  284.             elseif choice2 == "REMOVE_USER" then
  285.               while true do
  286.                 sleep(.3)
  287.                 rednet.send(serverID,{currentUser,"GET_USERS"})
  288.                 id,msg,d = rednet.receive(.3)
  289.                 if id == serverID then
  290.                   displayArray = {}
  291.                  
  292.                   for i,v in ipairs(msg) do
  293.                     table.insert(displayArray,v[1]..":"..v[3])
  294.                   end
  295.                  
  296.                   term.setBackgroundColor(colors.lightBlue)
  297.                   clear()
  298.                  
  299.                   table.insert(displayArray,"Back")
  300.                  
  301.                   selection = gui.drawArray(displayArray)
  302.                  
  303.                   if selection ~= nil and selection ~= #displayArray then
  304.                     rednet.send(serverID,{currentUser,"REMOVE_USER",msg[selection]})
  305.                   elseif selection == #displayArray then
  306.                     break
  307.                   end
  308.                 else
  309.                   term.setBackgroundColor(colors.lightBlue)
  310.                   clear()
  311.                   print("TOO LOW CLEARANCE OR SERVER ERROR")
  312.                   sleep(1)
  313.                   break
  314.                 end
  315.               end
  316.             elseif choice2 == "BACK" then
  317.               break    
  318.             end
  319.           end
  320.         elseif choice == "BACK" then
  321.           term.setBackgroundColor(colors.black)
  322.           clear()
  323.           return exit
  324.         elseif choice == "ADD_ACCOUNT" then
  325.           newUsername = ""
  326.           term.setBackgroundColor(colors.lightBlue)
  327.           clear()
  328.            
  329.           print("Create Account")
  330.           print("A password will be randomly generated and can be changed later")
  331.            
  332.           gui.newButton(2,5,10,3," ",colors.lightGray)
  333.           term.setBackgroundColor(colors.lightGray)
  334.           term.setCursorPos(3,5)
  335.           term.write("USERNAME")
  336.           usernameBox = gui.newButton(3,6,8,1,"USERNAME_BOX",colors.gray)
  337.          
  338.           term.setBackgroundColor(colors.red)
  339.           submit = gui.newButton(2,9,8,3,"SUBMIT",colors.red)
  340.           term.setCursorPos(3,10)
  341.           term.write("Submit")
  342.          
  343.           term.setBackgroundColor(colors.blue)
  344.           back = gui.newButton(2,13,6,3,"BACK",colors.blue)
  345.           term.setCursorPos(3,14)
  346.           term.write("Back")
  347.            
  348.           while true do
  349.             choice2 = gui.waitForButtons(usernameBox,submit,back)
  350.            
  351.             if choice2 == "USERNAME_BOX" then
  352.               newUsername = gui.inputField(3,6,newUsername,8,colors.gray)
  353.             elseif choice2 == "SUBMIT" then
  354.               rednet.send(serverID,{currentUser,"ADD_ACCOUNT",newUsername})
  355.               id2,msg2,d2 = rednet.receive(.3)
  356.               if id2 == serverID then
  357.                 term.setBackgroundColor(colors.lightBlue)
  358.                 clear()
  359.                 print("SUBMITTED")
  360.                 print("PASSWORD: "..msg2)
  361.                 print()
  362.                 print("PRESS ENTER")
  363.                 read()
  364.               else
  365.                 term.setBackgroundColor(colors.lightBlue)
  366.                 clear()
  367.                 print("SUBMISSION FAILED")
  368.                 sleep(1)
  369.               end
  370.               break
  371.             elseif choice2 == "BACK" then
  372.               break
  373.             end
  374.           end
  375.         elseif choice == "ACCOUNT" then
  376.           while true do
  377.             term.setBackgroundColor(colors.lightBlue)
  378.             clear()
  379.             print("Welcome "..currentUser[1])
  380.             print("Clearance Level: "..currentUser[3])
  381.            
  382.             term.setBackgroundColor(colors.blue)
  383.             resetCard = gui.newButton(2,4,12,3,"RESET_CARD",colors.blue)
  384.             term.setCursorPos(3,5)
  385.             term.write("Reset Card")
  386.            
  387.             resetPassword = gui.newButton(2,8,16,3,"RESET_PASSWORD",colors.blue)
  388.             term.setCursorPos(3,9)
  389.             term.write("Reset Password")
  390.            
  391.             printCard = gui.newButton(2,12,12,3,"PRINT_CARD",colors.blue)
  392.             term.setCursorPos(3,13)
  393.             term.write("Print Card")
  394.            
  395.             back = gui.newButton(2,16,6,3,"BACK",colors.blue)
  396.             term.setCursorPos(3,17)
  397.             term.write("Back")
  398.            
  399.             choice2 = gui.waitForButtons(printCard,resetCard,resetPassword,back)
  400.            
  401.             if choice2 == "RESET_CARD" then
  402.               newCard = randomString(6)
  403.               rednet.send(serverID,{currentUser,"RESET_CARD",newCard})
  404.               currentUser[2] = newCard
  405.               found = false
  406.               for i,v in ipairs(peripheral.getNames()) do
  407.                 if peripheral.getType(v) == "drive" then
  408.                   found = true
  409.                   term.setBackgroundColor(colors.lightBlue)
  410.                   clear()
  411.                   print("ENTER DISK")
  412.                   while true do
  413.                     e = os.pullEvent()
  414.                     if e == "disk" then
  415.                       file = fs.open("disk/access", "w")
  416.                       file.write(newCard)
  417.                       file.close()
  418.                       break
  419.                     end
  420.                   end
  421.                 end
  422.               end
  423.               if not found then
  424.                 term.setBackgroundColor(colors.lightBlue)
  425.                 clear()
  426.                 print("ATTATCH A DISK DRIVE")
  427.                 sleep(1)
  428.               end
  429.             elseif choice2 == "PRINT_CARD" then
  430.              found = false
  431.              for i,v in ipairs(peripheral.getNames()) do
  432.                if peripheral.getType(v) == "drive" then
  433.                  found = true
  434.                  term.setBackgroundColor(colors.lightBlue)
  435.                  clear()
  436.                  print("ENTER CARD")
  437.                  while true do
  438.                    e = os.pullEvent()
  439.                    if e == "disk" then
  440.                      file = fs.open("disk/access", "w")
  441.                      file.write(currentUser[2])
  442.                      file.close()
  443.                      break
  444.                    end
  445.                  end
  446.                end
  447.              end
  448.              
  449.              if not found then
  450.                term.setBackgroundColor(colors.lightBlue)
  451.                clear()
  452.                print("NO DRIVE FOUND")
  453.                sleep(1)
  454.              end
  455.             elseif choice2 == "RESET_PASSWORD" then
  456.               term.setBackgroundColor(colors.lightBlue)
  457.               clear()
  458.               print("Reset Password")
  459.              
  460.               gui.newButton(2,3,12,3," ",colors.lightGray)
  461.               pField = gui.newButton(3,4,10,1,"pField",colors.gray)
  462.              
  463.               submit = gui.newButton(2,7,8,3,"submit",colors.red)
  464.               term.setBackgroundColor(colors.red)
  465.               term.setCursorPos(3,8)
  466.               term.write("Submit")
  467.              
  468.               newPassword = ""
  469.              
  470.               while true do
  471.                 choice3 = gui.waitForButtons(pField,submit)
  472.                 if choice3 == "pField" then
  473.                   newPassword = gui.inputField(3,4,newPassword,10,colors.gray)
  474.                 elseif choice3 == "submit" then
  475.                   if newPassword ~= nil then
  476.                     rednet.send(serverID,{currentUser,"RESET_PASSWORD",newPassword})
  477.                     break
  478.                   end
  479.                 end
  480.               end
  481.             elseif choice2 == "BACK" then
  482.               clear()
  483.               break
  484.             end
  485.          
  486.           end
  487.         end
  488.      
  489.       end
  490.     else
  491.       term.setBackgroundColor(colors.lightBlue)
  492.       clear()
  493.       print("SERVER ERROR")
  494.       sleep(1.5)
  495.       os.shutdown()
  496.     end
  497.   end
  498. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement