Advertisement
eniallator

mControl - Scanner Functionality

Aug 30th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.79 KB | None | 0 0
  1. mProt = "masterRC"
  2. rednet.open("right")
  3. -- mProt is the protocol the computer will be using. You can also configure what side the wireless modem is on
  4.  
  5. status = {scan = 1,upload = 1,download = 1, fileList = 1,redstone = 1}
  6. -- Easily configure which programs you want running and which ones you don't by simply setting 1 for those you want running
  7.  
  8. helpTable = {scan = "scan + log players nearby",upload = "upload a file to connected computer", download = "download a file from connected computer", fileList = "list files on connected computer", redstone = "control sides of connected computer to emit redstone on"}
  9. -- Default help messages for each program
  10.  
  11. scannerDir = "baseChecker/"
  12.  
  13. myFunction = "scanner computer!"
  14. -- Called when the remote computer lists available computers it can connect with
  15.  
  16. help = ""
  17.  
  18. for program, progHelp in pairs(helpTable) do
  19.   if status[program] == 1 then
  20.     help = help .. program .. " - " .. progHelp .. "\n"
  21.   end
  22. end
  23.  
  24. help = help:sub(1,string.len(help) - 1)
  25. -- Creates the help variable from the programs that are active
  26.  
  27. while true do
  28.   sendID, msg = rednet.receive(mProt)
  29.  
  30.   if msg == "list" then
  31.     rednet.send(sendID, myFunction, mProt)
  32.     -- Will respond to computers that are running list unless running connect
  33.    
  34.   elseif msg == "connect" then
  35.     rednet.send(sendID, {"Connected!",status}, mProt)
  36.     -- Making the link from the mRemote program to the mControl program
  37.    
  38.     while true do
  39.       connectID, msg = rednet.receive(mProt)
  40.      
  41.       if connectID == sendID then
  42.         if msg == "exit" then
  43.           rednet.send(connectID, "Successfully exited the program", mProt)
  44.           break
  45.           -- Exits the connect cycle so that it will respond to list and for other computers to connect to it
  46.          
  47.         elseif msg[1] == "help" then
  48.           rednet.send(connectID, help, mProt)
  49.           -- Sends the help variable through
  50.          
  51.         elseif msg[1] == "upload" then
  52.           if fs.exists(msg[3]) == false then
  53.            
  54.             rednet.send(connectID, "send", mProt)
  55.             uselessID, downloadedFile = rednet.receive(mProt)
  56.             openFile = fs.open(msg[3], 'w')
  57.             openFile.write(downloadedFile)
  58.             openFile.close()
  59.           else
  60.             rednet.send(connectID, "File already exists on connected computer.", mProt)
  61.           end
  62.           -- Handles the upload function
  63.          
  64.         elseif msg[1] == "download" then
  65.           if fs.exists(msg[2]) == true and fs.isDir(msg[2]) == false then
  66.            
  67.             fileOpen = fs.open(msg[2], 'r')
  68.             stringSend = fileOpen.readAll()
  69.             fileOpen.close()
  70.             tableSend = {stringSend, "Successfully downloaded file."}
  71.             rednet.send(connectID, tableSend, mProt)
  72.           else
  73.             rednet.send(connectID, "File does not exist on connected computer.", mProt)
  74.           end
  75.           -- Handles the download function
  76.          
  77.         elseif msg[1] == "fileList" then
  78.          
  79.           programList = fs.list("")
  80.           files = ""
  81.          
  82.           for i=1,#programList do
  83.             if fs.isDir(programList[i]) == false then
  84.               if files == "" then
  85.                
  86.                 files = programList[i]
  87.               else
  88.                
  89.                 files = files .. ", " .. programList[i]
  90.               end
  91.             end
  92.           end
  93.           -- Handles the fileList function by sending a list of files (not directories) through
  94.          
  95.           rednet.send(connectID, files, mProt)
  96.        
  97.         elseif msg[1] == "redstone" and status.redstone == 1 then
  98.           if msg[2] == "up" or msg[2] == "down" or msg[2] == "back" or msg[2] == "front" or msg[2] == "left" or msg[2] == "right" then
  99.             if msg[3] == "on" then
  100.               -- First checks to see if msg[2] is a valid side then checks if msg[3] is a valid state
  101.              
  102.               redstone.setOutput(msg[2], true)
  103.               rednet.send(connectID, "Set output " .. msg[2] .. " to " .. msg[3], mProt)
  104.               -- Turns the side in msg[2] on
  105.             elseif msg[3] == "off" then
  106.              
  107.               redstone.setOutput(msg[2], false)
  108.               rednet.send(connectID, "Set the redstone on the " .. msg[2] .. " to " .. msg[3], mProt)
  109.               -- Turns the side in msg[2] off
  110.             else
  111.              
  112.               rednet.send(connectID, 'Invalid syntax. Use "redstone [side] [on/off]"', mProt)
  113.             end
  114.           else
  115.            
  116.             rednet.send(connectID, 'Invalid side. Available sides are: up, down, back, front, left, right', mProt)
  117.           end
  118.           -- Error messages to catch every combination
  119.        
  120.         elseif msg[1] == "scan" then
  121.           function toSRC(item)
  122.            
  123.             openSRC = fs.open(scannerDir .. "src", "w")
  124.             openSRC.write(item)
  125.             openSRC.close()
  126.           end
  127.          
  128.           if msg[2] == "start" then
  129.            
  130.             toSRC("[0/0/0]")
  131.             shell.run("bg " .. scannerDir .. "scanner")
  132.             rednet.send(connectID, "Starting the scanner program.", mProt)
  133.           elseif msg[2] == "stop" then
  134.            
  135.             readFile = fs.open(scannerDir .. "src","r")
  136.             contents = readfile.readAll()
  137.             readfile.close()
  138.             toSRC("[0/0/0] stop")
  139.             rednet.send(connectID, "Stopping the scanner program at " .. contents,mProt)
  140.           else
  141.            
  142.             rednet.send(connectID, 'Invalid syntax. Use "scan start" or "scan stop"')
  143.           end
  144.          
  145.         else
  146.           rednet.send(connectID, 'Invalid command. Type "help" for options.', mProt)
  147.           -- Default path if msg[1] doesn't get caught by any of the above functions
  148.         end
  149.       end
  150.     end
  151.   end
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement