Advertisement
Chaos_Cash

Connection sub backup: 10.02.2024

Feb 10th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. function startup()
  2.  
  3. connected_pcs={}
  4.  
  5. rednet.open("top")
  6. connections=fs.open("connections","a")
  7. connections.close()
  8.  
  9.  
  10. if not fs.exists("connection_code") then
  11.  
  12. term.clear()
  13. term.setCursorPos(1,1)
  14. term.setTextColor(colors.green)
  15. print("Please enter the name you want to use to connect")
  16. print("to other PC´s")
  17. print()
  18. term.setTextColor(colors.white)
  19. connection_name=read()
  20.  
  21. connection_code=fs.open("connection_code","w")
  22. connection_code.writeLine(connection_name)
  23. connection_code.flush()
  24. connection_code.close()
  25.  
  26. end
  27.  
  28. if not fs.exists("status") then
  29.  
  30. status_save=fs.open("status","w")
  31. status_save.write("off")
  32. status_save.flush()
  33. status_save.close()
  34.  
  35. end
  36.  
  37. assign_status()
  38.  
  39. assign_name()
  40.  
  41. assign_connections()
  42.  
  43.  
  44. if connected_pcs["name"][1]==nil then
  45.  
  46. term.clear()
  47. term.setCursorPos(1,1)
  48. term.setTextColor(colors.green)
  49. print("Please enter the name of the PC you want to")
  50. print("connect to")
  51. print()
  52. term.setTextColor(colors.white)
  53. connecting_to=read()
  54.  
  55. connections=fs.open("connections","w")
  56. connections.writeLine(connecting_to)
  57. connections.flush()
  58. connections.close()
  59.  
  60. end
  61.  
  62. connected=false
  63. assign_connections()
  64.  
  65. if not connected then
  66. for i=1,250 do
  67.  
  68. rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name})
  69.  
  70. end
  71. end
  72.  
  73. while not connected do
  74. info_1 ,info_2, info_3, info_4 = os.pullEvent()
  75. if info_1=="rednet_message" then
  76. if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
  77. connections=fs.open("connections","w")
  78. connections.writeLine(connecting_to)
  79. connections.writeLine(info_2)
  80. connections.flush()
  81. connections.close()
  82. connected=true
  83. end
  84. end
  85. end
  86.  
  87. assign_connections()
  88.  
  89. for i=1,connected_pcs["len"] do
  90. rednet.send(connected_pcs["id"][i],{["sending_status"]=true,["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]=status})
  91. end
  92.  
  93. if status=="on" then
  94. redstone.setOutput("bottom",true)
  95. redstone.setOutput("top",true)
  96. redstone.setOutput("back",true)
  97. redstone.setOutput("front",true)
  98. redstone.setOutput("right",true)
  99. redstone.setOutput("left",true)
  100. end
  101.  
  102. if status=="off" then
  103. redstone.setOutput("bottom",false)
  104. redstone.setOutput("top",false)
  105. redstone.setOutput("back",false)
  106. redstone.setOutput("front",false)
  107. redstone.setOutput("right",false)
  108. redstone.setOutput("left",false)
  109. end
  110.  
  111. end
  112.  
  113.  
  114.  
  115.  
  116.  
  117. function pull()
  118.  
  119. while true do
  120.  
  121. info_1, info_2, info_3, info_4 = os.pullEvent()
  122.  
  123. if info_1=="rednet_message" and info_3["sending_to"]==connection_name then
  124.  
  125.  
  126. if info_3["status"]=="get_info" then
  127. rednet.send(info_2,{["sending_status"]=true,["sending_to"]=connected_pcs["name"][1],["name"]=connection_name,["status"]=status})
  128. end
  129.  
  130.  
  131. if info_3["status"]=="change_status" then
  132.  
  133. if status=="on" then
  134. status="off"
  135. change_status()
  136. else
  137. status="on"
  138. change_status()
  139. end
  140.  
  141. rednet.send(info_2,{["sending_status"]=true,["sending_to"]=connected_pcs["name"][1],["name"]=connection_name,["status"]=status})
  142. end
  143.  
  144.  
  145. if status=="on" then
  146. redstone.setOutput("bottom",true)
  147. redstone.setOutput("top",true)
  148. redstone.setOutput("back",true)
  149. redstone.setOutput("front",true)
  150. redstone.setOutput("right",true)
  151. redstone.setOutput("left",true)
  152. end
  153.  
  154. if status=="off" then
  155. redstone.setOutput("bottom",false)
  156. redstone.setOutput("top",false)
  157. redstone.setOutput("back",false)
  158. redstone.setOutput("front",false)
  159. redstone.setOutput("right",false)
  160. redstone.setOutput("left",false)
  161. end
  162.  
  163.  
  164. if info_3["status"]=="disconnected" then
  165. fs.delete("connections")
  166.  
  167. term.clear()
  168. term.setCursorPos(1,1)
  169. term.setTextColor(colors.green)
  170. print("Please enter the name of the PC you want to")
  171. print("connect to")
  172. print()
  173. term.setTextColor(colors.white)
  174. connecting_to=read()
  175.  
  176. connections=fs.open("connections","w")
  177. connections.writeLine(connecting_to)
  178. connections.flush()
  179. connections.close()
  180.  
  181. for i=1,250 do
  182. rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name})
  183. end
  184.  
  185. connected=false
  186.  
  187. while not connected do
  188. info_1 ,info_2, info_3, info_4 = os.pullEvent()
  189. if info_1=="rednet_message" then
  190. if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
  191. connections=fs.open("connections","w")
  192. connections.writeLine(connecting_to)
  193. connections.writeLine(info_2)
  194. connections.flush()
  195. connections.close()
  196. connected=true
  197. end
  198. end
  199. end
  200.  
  201. end
  202.  
  203. end
  204.  
  205. end
  206. end
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. function change_status()
  228.  
  229. status_save = fs.open("status","w")
  230.  
  231. status_save.write(status)
  232. status_save.flush()
  233. status_save.close()
  234. end
  235.  
  236.  
  237.  
  238. function assign_status()
  239.  
  240. status_save = fs.open("status","r")
  241.  
  242. status = status_save.readLine()
  243. status_save.close()
  244. end
  245.  
  246.  
  247.  
  248. function assign_connections()
  249.  
  250. connections=fs.open("connections","r")
  251.  
  252. connected_pcs["name"]={}
  253. connected_pcs["len"]=0
  254. connected_pcs["id"]={}
  255. connected_pcs["id"][0]="0"
  256. i=0
  257. while tonumber(connected_pcs["id"][i])~=nil do
  258.  
  259. i=i+1
  260.  
  261. connected_pcs["name"][i] = connections.readLine()
  262. connected_pcs["id"][i] = connections.readLine()
  263.  
  264.  
  265. if tonumber(connected_pcs["id"][i])~=nil then
  266. connected_pcs["len"]=connected_pcs["len"]+1
  267. connected_pcs["id"][i]=tonumber(connected_pcs["id"][i])
  268. connected=true
  269. end
  270.  
  271. end
  272. connections.close()
  273. end
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280. function assign_name()
  281.  
  282. connection_code = fs.open("connection_code","r")
  283. connection_name = connection_code.readLine()
  284. connection_code.close()
  285.  
  286. end
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300. startup()
  301. pull()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement