Advertisement
dadragon84

Monitor1

Nov 8th, 2013 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. local monitor = nil
  3. -- Determine where monitor is.
  4. monitorSide = nil
  5. if peripheral.isPresent("left") and peripheral.getType("left")=="monitor" then
  6.   monitorSide = "left"
  7. elseif peripheral.isPresent("right") and peripheral.getType("right")=="monitor" then
  8.   monitorSide = "right"
  9. elseif peripheral.isPresent("top") and peripheral.getType("top")=="monitor" then
  10.   monitorSide = "top"
  11. elseif peripheral.isPresent("back") and peripheral.getType("back")=="monitor" then
  12.   monitorSide = "back"
  13. elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")=="monitor" then
  14.   monitorSide = "bottom"
  15. end
  16. if monitorSide ~= nil then
  17.   monitor = peripheral.wrap(monitorSide)
  18.   print("Monitor found on "..monitorSide)
  19.   monitor.setTextScale(.7)
  20. else
  21.   monitor = nil
  22. end
  23. local players = {}
  24. local stayInLoop = true
  25. local delay = 0
  26. local adminLogin = 0
  27.  
  28. function fileSave()
  29.   file = fs.open("players","w")
  30.   file.writeLine(textutils.serialize(players))
  31.   file.close()
  32. end
  33.  
  34. function player_new(name,job)
  35.   table.insert(players,{name,job})
  36.   fileSave()
  37.   return true
  38. end
  39.  
  40. function player_job(name,newJob)
  41.   for i=1,#players do
  42.     if players[i][1] == name then
  43.     players[i][2] = newJob
  44.     fileSave()
  45.     return true
  46.   end
  47.  end
  48.  return false
  49. end
  50.  
  51. function player_remove(name)
  52.   for i=1,#players do
  53.     if players[i][1] == name then
  54.       table.remove(players,i)
  55.       fileSave()
  56.       return true
  57.     end
  58.   end
  59.   return false
  60. end
  61.  
  62. function commands_help()
  63.   print("Available options are: ")
  64.   print("'new' - Adds a new player to the console.")
  65.   print("'job' - Changes the job for an existing player.")
  66.   print("'rename' - Renames an existing player")
  67.   print("'delete' - Deletes and existing players from the console.")
  68.   print("'list' - Shows a list of players in the system.")
  69.   print("'logout' - Logs out of this system.")
  70.   print("NOTE: Please logout after every command to prevent any unwanted changes!")  
  71. end
  72.  
  73. function after_command()
  74.   print("")
  75.   print("press 'Enter' to continue...")
  76.   read()
  77.   term.clear()
  78.   term.setCursorPos(1,1)
  79. end
  80.  
  81. print("Monitor Activated!")
  82. if (fs.exists("players") == true) then
  83.   print("Reading data...")
  84.   file = fs.open("players","r")
  85.   data = file.readLine()
  86.   file.close()
  87.   players = textutils.unserialize(data)
  88. end
  89.  
  90. while stayInLoop do
  91.     local cmdIssued = 0
  92.     sleep(0.1)
  93.     delay = delay + 0.1
  94.     --Auto reboot to prevent computer shutdown.
  95.     if (delay > 600) then
  96.         os.reboot()
  97.     end
  98.  
  99.     --Reset monitor.
  100.     monitor.clear()
  101.  
  102.     --Header.
  103.     monitor.setCursorPos(9,1)
  104.     monitor.write("Current Duties:")
  105.     monitor.setCursorPos(9,2)
  106.     monitor.write("===============")
  107.  
  108.     --Loop through all players.
  109.     local line = 3
  110.     for i=1,#players do
  111.         monitor.setCursorPos(1,line)
  112.         monitor.write(">>"..players[i][1]..":")
  113.         line = line + 1
  114.         monitor.setCursorPos(1,line)
  115.         monitor.write("Job:> "..players[i][2])
  116.         line = line + 2
  117.     end
  118.  
  119.     if adminLogin == 0 then
  120.         write("Enter admin password for commands: ")
  121.         local pass = read("0")
  122.         if pass == "master" then
  123.           adminLogin = 1
  124.         end
  125.     end
  126.  
  127.     if adminLogin == 1 then
  128.       --Wait for commands and execute them.
  129.       commands_help()
  130.       write("Command: ")
  131.       local cmd = read()
  132.      
  133.       if cmd == "new" then
  134.         print("New player adding. Write 'q' to quit.")
  135.         write("Name: ")
  136.         local name = read()
  137.         if not (name=="q") then
  138.           print("New Job: ")
  139.           local job= read()
  140.           if (player_new(name,job)==true) then
  141.             print("Player "..name.." added successfully")
  142.           end
  143.           after_command()
  144.         end
  145.       end
  146.  
  147.       if cmd == "job" then
  148.         print("Change job for player. Write 'q' to quit.")
  149.         write("Name: ")
  150.         local name = read()
  151.         if not (name=="q") then
  152.           print ("New Job: ")
  153.           local job = read()
  154.           if (player_job(name,job)==true) then
  155.             print("Player "..name.." edited successfully")
  156.           else
  157.             print("Player "..name.." not found!")
  158.           end
  159.           after_command()
  160.         end
  161.       end
  162.      
  163.       if cmd == "rename" then
  164.         print("Change name for and existing player. Write 'q' to quit")
  165.         write("Name: ")
  166.         local name = read()
  167.         if not (name=="q") then
  168.           write("New name: ")
  169.           local new = read()
  170.           if (player_rename(name,new)==true) then
  171.             print("Player "..name.." renamed to "..new)
  172.           else
  173.             print("Player "..name.." not found!")
  174.           end
  175.         end
  176.       end
  177.      
  178.       if cmd == "delete" then
  179.           print(" Delete a player by name. Write 'q' to quit")
  180.           write("Name: ")
  181.           local name = read()
  182.           if not (name=="q") then
  183.             if (player_remove(name)==true) then
  184.               print("Player "..name.." removed!")
  185.             else
  186.               print("Player "..name.." not found!")
  187.             end
  188.             after_command()
  189.           end
  190.        end
  191.      
  192.       if cmd == "list" then
  193.         for i=1,#players do
  194.             print(players[i][1])
  195.         end
  196.         after_command()
  197.       end
  198.    
  199.       if cmd == "admin" then
  200.         stayInLoop = false
  201.         term.clear()
  202.         term.setCursorPos(1,1)
  203.         write("Maintenance Mode!")
  204.       end
  205.      
  206.       if cmd == "logout" then
  207.         os.reboot()
  208.       end
  209.     end
  210.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement