enderpro100

RednetServer

Dec 3rd, 2020 (edited)
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.36 KB | Gaming | 0 0
  1. --- Program : Rednet server
  2. --- Author : LightKnight51
  3. --- Last modification : 18/04/2023
  4. --- Dependency for work : Rednet client (muA3ykGt)
  5. --- You can start any program (with arguments) with this remote control tool
  6.  
  7. --- Variables
  8.  
  9. local sSeparator = "|"
  10. local computerId = os.getComputerID()
  11. local libraryName = "MarquitoLuaUtils"
  12. local libraryCode = "mVTSwvw1"
  13.  
  14. local deviceType = "COMPUTER"
  15.  
  16. --- Functions
  17.  
  18. ---- Order functions
  19.  
  20. -- Send order to one to many turtles
  21. function SendOrder(bAll, receiverId, turtleType, typeOrder, messageContent)
  22.     local protocol = "TurtleProgram." .. turtleType
  23.    
  24.     local data
  25.     if messageContent ~= nil then
  26.         data = typeOrder .. sSeparator .. messageContent
  27.     else
  28.         data = typeOrder
  29.     end
  30.     if bAll then
  31.         MarquitoLuaUtils.BroadcastDataWithRednet(data, protocol)
  32.     else
  33.         if MarquitoLuaUtils.IsAnArray(receiverId) then
  34.             MarquitoLuaUtils.SendDataWithRednetForMultiplesDevices(receiverId, data, protocol)
  35.         else
  36.             MarquitoLuaUtils.SendDataWithRednetForOneDevice(receiverId, data, protocol)
  37.         end
  38.     end
  39. end
  40. -- Send exit order to turtle
  41. function SendExitOrder(bAll, receiverId, turtleType)
  42.     if bAll then
  43.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtles on this network must stop")
  44.     else
  45.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtle " .. textutils.serialise(receiverId) .. " must stop")
  46.     end
  47.  
  48.     SendOrder(bAll, receiverId, turtleType, "exit", nil)
  49. end
  50. -- Send program download order to turtle (in fact, it's download the programe here, and send the code to the turtle)
  51. function SendDownloadProgramOrder(bAll, receiverId, turtleType, programName, programContent)
  52.     if bAll then
  53.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtles on this network receive program " .. programName)
  54.     else
  55.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtle " .. textutils.serialise(receiverId) .. " receive program" .. programName)
  56.     end
  57.  
  58.     local data = programName .. sSeparator .. programContent
  59.     SendOrder(bAll, receiverId, turtleType, "download", data)
  60. end
  61. -- Send program execution order
  62. function SendExecuteProgramOrder(bAll, receiverId, turtleType, programName)
  63.     if bAll then
  64.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtles on this network must execute " .. programName)
  65.     else
  66.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtle " .. textutils.serialise(receiverId) .. " must execute " .. programName)
  67.     end
  68.     SendOrder(bAll, receiverId, turtleType, "execute", programName)
  69. end
  70. -- Send program delete order
  71. function SendDeleteProgramOrder(bAll, receiverId, turtleType, programName)
  72.     if bAll then
  73.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtles on this network must delete " .. programName)
  74.     else
  75.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Turtle " .. textutils.serialise(receiverId) .. " must delete " .. programName)
  76.     end
  77.     SendOrder(bAll, receiverId, turtleType, "delete", programName)
  78. end
  79. -- Send program coordinates order
  80. function SendCordinatesOrder(receiverId)
  81.     SendOrder(false, receiverId, "coordinates", nil)
  82. end
  83.  
  84. ---- Ask user input functions
  85.  
  86. -- Ask if all turtles
  87. function AskIfAllTurtles()
  88.     local nOption = nil
  89.     print("Do you want to perform this action on specific(s) turtle(s) or all ?")
  90.     print("0 -> One turtle")
  91.     print("1 -> A set of turtles (ids delimited with \",\")")
  92.     print("2 -> Every turtle in the network")
  93.     while true do
  94.         nOption = tonumber(read())
  95.         if nOption == 0 or nOption == 1 or nOption == 2 then
  96.             break
  97.         else
  98.             print("Incorrect code")
  99.         end
  100.     end
  101.     return nOption
  102. end
  103. -- Ask turtle type
  104. function AskTurtleType()
  105.     local turtleType = MarquitoLuaUtils.TurtleType.NONE
  106.  
  107.     local nOption = nil
  108.     print("Which turtle need to get this order ?")
  109.     print("0 -> Mining turtle")
  110.     print("1 -> Melee turtle")
  111.     print("2 -> Farming turtle")
  112.     print("3 -> Felling turtle")
  113.     print("4 -> Crafting turtle")
  114.     print("5 -> Diging turtle")
  115.     print("6 -> Noisy turtle")
  116.     while true do
  117.         nOption = tonumber(read())
  118.         if nOption == 0 then
  119.             turtleType = MarquitoLuaUtils.TurtleType.MINING
  120.             break
  121.         elseif nOption == 1 then
  122.             turtleType = MarquitoLuaUtils.TurtleType.MELEE
  123.             break
  124.         elseif nOption == 2 then
  125.             turtleType = MarquitoLuaUtils.TurtleType.FARM
  126.             break
  127.         elseif nOption == 3 then
  128.             turtleType = MarquitoLuaUtils.TurtleType.FELLING
  129.             break
  130.         elseif nOption == 4 then
  131.             turtleType = MarquitoLuaUtils.TurtleType.CRAFT
  132.             break
  133.         elseif nOption == 5 then
  134.             turtleType = MarquitoLuaUtils.TurtleType.DIG
  135.             break
  136.         elseif nOption == 6 then
  137.             turtleType = MarquitoLuaUtils.TurtleType.NOISY
  138.             break
  139.         else
  140.             print("Incorrect code")
  141.         end
  142.     end
  143.     return turtleType
  144. end
  145. -- Ask turtle id
  146. function AskTurtleId()
  147.     print("Turtle ID ?")
  148.     local turtleId = tonumber(read())
  149.     return turtleId
  150. end
  151. -- Ask turtle id
  152. function AskTurtleIds()
  153.     print("Turtle IDs (ids delimited with \",\") ?")
  154.     local turtleIds = tostring(read())
  155.  
  156.     return MarquitoLuaUtils.Split(turtleIds, ",")
  157. end
  158. -- Ask program name
  159. function AskProgramName()
  160.     print("Program's name ?")
  161.     program = tostring(read())
  162.     return program
  163. end
  164. -- Ask pastebin code to download a program
  165. function AskPastebinCode()
  166.     print("Pastebin program's code ?")
  167.     pastebinCd = tostring(read())
  168.     return pastebinCd
  169. end
  170. -- Ask if we need to download again and update the program
  171. function AsIfWeNeedUpdate()
  172.     print("Do you want to update this program ? - no(0) yes(1)")
  173.     local nOption = 0
  174.     while true do
  175.         nOption = tonumber(read())
  176.         if nOption == 0 or nOption == 1 then
  177.             break
  178.         else
  179.             print("Incorrect code")
  180.         end
  181.     end
  182.    
  183.     return nOption == 1
  184. end
  185.  
  186. -- Receive data from client
  187. function ReceiveData()
  188.     while true do
  189.         senderId, message, protocol = rednet.receive()
  190.         if protocol == "turtleSendCoords" then
  191.             x, y, z = returnCoords(message)
  192.             print("Cordonnées de la turtle n" .. tostring(senderId) .. " : " .. tostring(x) .. " " .. tostring(y) .. " " .. tostring(z))
  193.         end
  194.     end
  195. end
  196.  
  197. function returnCoords(message)
  198.     x, y, z = 0
  199.     i = 0
  200.     for value in string.gmatch(message, "%a+") do
  201.         if i == 0 then
  202.             x = tonumber(value)
  203.         elseif i == 1 then
  204.             y = tonumber(value)
  205.         elseif i == 2 then
  206.             z = tonumber(value)
  207.         end
  208.         i = i + 1
  209.     end
  210.     return x, y, z
  211. end
  212.  
  213. -- Download program
  214. function DownloadProgram(pastebinCode, programName)
  215.     if fs.exists(programName) then
  216.         fs.delete(programName)
  217.     end
  218.     shell.run("pastebin get " .. pastebinCode .. " " .. programName)
  219. end
  220.  
  221. -- Exit the program
  222. function ExitProgram()
  223.     print("End of program")
  224.     os.sleep(1)
  225.     MarquitoLuaUtils.EndLog()
  226.     os.shutdown()
  227. end
  228.  
  229. --sendOrder()
  230. function MainProgram()
  231.     term.clear()
  232.     if MarquitoLuaUtils.OpenWirelessRednetModem() then
  233.         print("Id of computer / tablet : " .. tostring(computerId))
  234.         print("Welcome to the program allow you to send order to turtles")
  235.         while true do
  236.             -- The turtle type
  237.             local turtleType = MarquitoLuaUtils.TurtleType.NONE
  238.  
  239.             local nOption = nil
  240.            
  241.             print("0 -> Exit this program")
  242.             print("1 -> Download program for the turtle")
  243.             print("2 -> Execute turtle program")
  244.             print("3 -> Delete turtle program")
  245.             print("4 -> Stop one or many turtle(s)")
  246.             print("What do you want to do ?")
  247.             while true do
  248.                 nOption = tonumber(read())
  249.                 if nOption == 0 or nOption == 1 or nOption == 2 or nOption == 3 or nOption == 4 or nOption == 5 then
  250.                     break
  251.                 else
  252.                     print("Incorrect code")
  253.                 end
  254.             end
  255.             -- Exit current program
  256.             if nOption == 0 then
  257.                 MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Exit RednetServer")
  258.                 break
  259.             -- Download a program
  260.             elseif nOption == 1 then
  261.                 -- Ask turtle type
  262.                 turtleType = AskTurtleType()
  263.                 -- Ask if one, a set of, or all turtle(s)
  264.                 turtleChoice = AskIfAllTurtles()
  265.                 turtleId = nil
  266.                 if turtleChoice == 0 then
  267.                     turtleId = AskTurtleId()
  268.                 elseif turtleChoice == 1 then
  269.                     turtleId = AskTurtleIds()
  270.                 end
  271.                 -- Ask the program name
  272.                 programName = AskProgramName()
  273.                 -- Check if the program exist, or if we want to update it before send to turtle(s)
  274.                 if not MarquitoLuaUtils.ProgramExistInLocal(programName) or AsIfWeNeedUpdate() then
  275.                     pastebinCode = AskPastebinCode()
  276.                     DownloadProgram(pastebinCode, programName)
  277.                     MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Download program : " .. programName .. " with pastebin code : " .. pastebinCode)
  278.                 end
  279.                 -- The program content
  280.                 programContent = MarquitoLuaUtils.GetProgramContent(programName)
  281.  
  282.                 SendDownloadProgramOrder(turtleChoice == 2, turtleId, turtleType, programName, programContent)
  283.             -- Execute a program
  284.             elseif nOption == 2 then
  285.                 -- Ask turtle type
  286.                 turtleType = AskTurtleType()
  287.                 -- Ask if one, a set of, or all turtle(s)
  288.                 turtleChoice = AskIfAllTurtles()
  289.                 turtleId = nil
  290.                 if turtleChoice == 0 then
  291.                     turtleId = AskTurtleId()
  292.                 elseif turtleChoice == 1 then
  293.                     turtleId = AskTurtleIds()
  294.                 end
  295.                 -- Ask the programe name
  296.                 programName = AskProgramName()
  297.  
  298.                 SendExecuteProgramOrder(turtleChoice == 2, turtleId, turtleType, programName)
  299.             -- Delete a program
  300.             elseif nOption == 3 then
  301.                 -- Ask turtle type
  302.                 turtleType = AskTurtleType()
  303.                 -- Ask if one, a set of, or all turtle(s)
  304.                 turtleChoice = AskIfAllTurtles()
  305.                 turtleId = nil
  306.                 if turtleChoice == 0 then
  307.                     turtleId = AskTurtleId()
  308.                 elseif turtleChoice == 1 then
  309.                     turtleId = AskTurtleIds()
  310.                 end
  311.                 -- Ask the programe name
  312.                 programName = AskProgramName()
  313.  
  314.                 SendDeleteProgramOrder(turtleChoice == 2, turtleId, turtleType, programName)
  315.             -- exit the rednetClient
  316.             elseif nOption == 4 then
  317.                 -- Ask turtle type
  318.                 turtleType = AskTurtleType()
  319.                 -- Ask if one, a set of, or all turtle(s)
  320.                 turtleChoice = AskIfAllTurtles()
  321.                 turtleId = nil
  322.                 if turtleChoice == 0 then
  323.                     turtleId = AskTurtleId()
  324.                 elseif turtleChoice == 1 then
  325.                     turtleId = AskTurtleIds()
  326.                 end
  327.  
  328.                 SendExitOrder(turtleChoice == 2, turtleId, turtleType)
  329.             end
  330.             nOption = nil
  331.             os.sleep(0.5)
  332.             term.clear()
  333.         end
  334.         MarquitoLuaUtils.CloseWirelessRednetModem()
  335.     end
  336. end
  337.  
  338. -- We need this library for other programs work
  339. DownloadProgram(libraryCode, libraryName)
  340. os.loadAPI("MarquitoLuaUtils")
  341. -- Run the rednet client
  342. parallel.waitForAny(MainProgram, ReceiveData)
  343. ExitProgram()
Advertisement
Add Comment
Please, Sign In to add comment