EdgeLordKirito

Server Code

May 27th, 2023 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. -- Valid commands
  2. -- line startHeight length
  3. --
  4.  
  5. function Main()
  6. rednet.open("back")
  7. os.setComputerLabel("Server")
  8. print("Please Enter the Ids of the Slaves seperated by a Space")
  9. SLAVES = NewGetSlaves()
  10. local chestX,chestY,chestZ,chestString = GetChestCoords()
  11. local fuelX,fuelY,fuelZ,fuelString = GetFuelCoords()
  12. local fueltype = GetFuelType()
  13. --Sending
  14. MessageAllSlaves(chestString)
  15. PrintSysMessage("Chest Coords Send now Sleeping for 1 to let the Slaves Process")
  16. sleep(1)
  17. MessageAllSlaves(fuelString)
  18. PrintSysMessage("Fuel Coords Send now Sleeping for 1 to let the Slaves Process")
  19. sleep(1)
  20. MessageAllSlaves(fueltype)
  21. PrintSysMessage("Fueltype Send now Sleeping for 3 to let the Slaves Process")
  22. sleep(3)
  23. local command = GetCommand()
  24. MessageAllSlaves(command)
  25. sleep(3)
  26. --Sending closed
  27. PrintSysMessage("Programm has terminated with Code 0 " .. tostring(os.epoch("utc")))
  28.  
  29. end
  30.  
  31.  
  32. function MySplit (inputstr, sep)
  33. if sep == nil then
  34. sep = "%s"
  35. end
  36. local t={}
  37. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  38. table.insert(t, str)
  39. end
  40. return t
  41. end
  42.  
  43. function GetFuelType()
  44. print("Enter the Type of Fuel available")
  45. local result = io.read()
  46. return result
  47. end
  48.  
  49. function EvaluateFuelType(input)
  50. if input == "minecraft:coal" or input == "minecraft:charcoal" then
  51. return 1
  52. else if input == "minecraft:coal_block" or input == "thermal:charcoal_block" then
  53. return 2
  54. end
  55. end
  56. end
  57.  
  58. function GetChestCoords()
  59. print("Please Enter the Coordinates of the Chest")
  60. return GetCoords()
  61. end
  62.  
  63. function GetFuelCoords()
  64. print("Please Enter the Coordinates of the Fuelhub")
  65. return GetCoords()
  66. end
  67.  
  68. function GetCommand()
  69. print("Please enter your Command")
  70. local command = io.read()
  71. return command
  72. end
  73.  
  74. function PrintAllSlaveIds()
  75. for i = 1, table.getn(SLAVES) do
  76. print(SLAVES[i])
  77. end
  78. end
  79.  
  80. function ReceiveReturnMessage()
  81. local id, message = rednet.receive()
  82. PrintSysMessage(("Computer %d sent message %s"):format(id, message))
  83. EvaluateResponse(message)
  84. end
  85.  
  86. function EvaluateResponse(input)
  87. local message = string.upper(input)
  88. if message == "FUEL" then
  89. HandleRefuelingRetreat()
  90. return
  91. end
  92.  
  93. if message == "SLOTS" then
  94. HandleSlotRetreat()
  95. return
  96. end
  97. end
  98.  
  99. function HandleRefuelingRetreat()
  100. MessageAllSlaves(SLAVES,"Retreat")
  101. end
  102.  
  103. function HandleSlotRetreat()
  104. MessageAllSlaves(SLAVES,"Retreat")
  105. end
  106.  
  107. function GetCoords()
  108. local coords = io.read()
  109. local coordTable = MySplit(coords)
  110. local x,y,z = nil,nil,nil
  111. local resultstring = coordTable[1] .. " " .. coordTable[2] .. " " .. coordTable[3]
  112. x = tonumber(coordTable[1])
  113. y = tonumber(coordTable[2])
  114. z = tonumber(coordTable[3])
  115. return x,y,z, resultstring
  116. end
  117.  
  118. function GetSlaves()
  119. local listOfSlaves = {}
  120. local counter = 1
  121. local input = ""
  122. print("Please enter the Slave ComputerIDs: " .. tostring(os.epoch("utc")))
  123. while (not BreakOff(input)) do
  124. input = io.read()
  125. if BreakOff(input)
  126. then
  127. break
  128. end
  129. listOfSlaves[counter] = input
  130. counter = counter + 1
  131. end
  132. return listOfSlaves
  133. end
  134.  
  135. function NewGetSlaves()
  136. local listOfSlaves = {}
  137. local input = io.read()
  138. local inputList = MySplit(input)
  139. for i = 1, table.getn(inputList) do
  140. listOfSlaves[i] = (tonumber(inputList[i]))
  141. end
  142. return listOfSlaves
  143. end
  144.  
  145. function MessageAllSlaves( message)
  146. for i = 1, table.getn(SLAVES) do
  147. rednet.send(SLAVES[i],message)
  148. end
  149. end
  150.  
  151. function BreakOff(input)
  152. input = string.upper(input)
  153. if (input == "STOP")
  154. then
  155. return true
  156. else
  157. return false
  158. end
  159. end
  160.  
  161. function TakeUserInput()
  162. print("What do you want to Send? " .. tostring(os.epoch("utc")))
  163. print("Available commands are:")
  164. PrintStringInColor("Go",colors.green)
  165. PrintStringInColor("Terminate",colors.red)
  166. ResetTerminal()
  167. return io.read()
  168. end
  169.  
  170.  
  171.  
  172. function EvaluateUserInput(input)
  173. local command = string.upper(input)
  174. if (command == "GO")
  175. then
  176. if (COMMANDS[1] == true)
  177. then
  178. PrintSysMessage("The Go signal has already been send")
  179. return
  180. else
  181. MessageAllSlaves(SLAVES,"Go")
  182. COMMANDS[1] = true
  183. term.setTextColor(colors.green)
  184. PrintSysMessage("All Slaves have been notified to Go " .. tostring(os.epoch("utc")))
  185. return
  186. end
  187. end
  188.  
  189. if (command == "TERMINATE")
  190. then
  191. if (COMMANDS[2] == true)
  192. then
  193. PrintSysMessage("The Terminate signal has already been send")
  194. return
  195. else
  196. MessageAllSlaves(SLAVES,"Terminate")
  197. COMMANDS[2] = true
  198. term.setTextColor(colors.red)
  199. PrintSysMessage("All Slaves have been notified to Terminate " .. tostring(os.epoch("utc")))
  200. ACTIVE = false
  201. return
  202. end
  203. end
  204. end
  205.  
  206.  
  207.  
  208. function PrintStringInColor(string,color)
  209. term.setTextColor(color)
  210. print(string)
  211. ResetTerminal()
  212. end
  213.  
  214. function PrintSysMessage(string)
  215. PrintStringInColor(string,colors.magenta)
  216. end
  217.  
  218. function ResetTerminal()
  219. term.setTextColor(colors.white)
  220. end
  221.  
  222. Main()
Add Comment
Please, Sign In to add comment