Advertisement
Chaos_Cash

Connection sub backup: 01.03.2024

Mar 1st, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 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. term.setTextColor(colors.green)
  66. print("connecting...")
  67. term.setTextColor(colors.white)
  68. print("If the PC is not connecting then this could be because the main PC is not loaded or the main programm is not running, please always name the sub and main programms ´startup´")
  69.  
  70. while not connected do
  71. for i=1,250 do
  72.  
  73. rednet.send(i,{["connecting_to"]=connected_pcs["name"][1],["name"]=connection_name})
  74.  
  75. end
  76.  
  77. while info_1~="timer" do
  78. timer=os.startTimer(5)
  79. info_1 ,info_2, info_3, info_4 = os.pullEvent()
  80. if info_1=="rednet_message" then
  81. if info_3["name"]==connected_pcs["name"][1] and info_3["connecting_to"]==connection_name then
  82. connections=fs.open("connections","w")
  83. connections.writeLine(connected_pcs["name"][1])
  84. connections.writeLine(info_2)
  85. connections.flush()
  86. connections.close()
  87. connected=true
  88. end
  89.  
  90. if info_3["status"]=="disconnected" then
  91. fs.delete("connections")
  92.  
  93. term.clear()
  94. term.setCursorPos(1,1)
  95. term.setTextColor(colors.green)
  96.  
  97. if info_3["same_name"] then
  98. print("A PC with this name was already connected,")
  99. print("please use a diffrent name.")
  100. fs.delete("connection_code")
  101. sleep(3)
  102. startup()
  103. end
  104.  
  105. if info_3["same_id"] then
  106. print("A PC with this id was already connected,")
  107. print("please use a diffrent name.")
  108. sleep(3)
  109. startup()
  110. end
  111.  
  112.  
  113. for i=1,250 do
  114. rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name})
  115. end
  116.  
  117. connected=false
  118.  
  119. while not connected do
  120. info_1 ,info_2, info_3, info_4 = os.pullEvent()
  121. if info_1=="rednet_message" then
  122. if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
  123. connections=fs.open("connections","w")
  124. connections.writeLine(connecting_to)
  125. connections.writeLine(info_2)
  126. connections.flush()
  127. connections.close()
  128. connected=true
  129. end
  130. end
  131. end
  132.  
  133. end
  134.  
  135. end
  136. os.cancelTimer(timer)
  137. end
  138. info_1=0
  139. end
  140.  
  141. assign_connections()
  142.  
  143. term.clear()
  144.  
  145. for i=1,connected_pcs["len"] do
  146. rednet.send(connected_pcs["id"][i],{["sending_status"]=true,["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]=status})
  147. end
  148.  
  149. if status=="on" then
  150. redstone.setOutput("bottom",true)
  151. redstone.setOutput("top",true)
  152. redstone.setOutput("back",true)
  153. redstone.setOutput("front",true)
  154. redstone.setOutput("right",true)
  155. redstone.setOutput("left",true)
  156. end
  157.  
  158. if status=="off" then
  159. redstone.setOutput("bottom",false)
  160. redstone.setOutput("top",false)
  161. redstone.setOutput("back",false)
  162. redstone.setOutput("front",false)
  163. redstone.setOutput("right",false)
  164. redstone.setOutput("left",false)
  165. end
  166.  
  167. end
  168.  
  169.  
  170.  
  171.  
  172.  
  173. function pull()
  174.  
  175. while true do
  176.  
  177. info_1, info_2, info_3, info_4 = os.pullEvent()
  178.  
  179. if info_1=="rednet_message" and info_3["sending_to"]==connection_name and info_3["name"]==connected_pcs["name"][1] then
  180.  
  181. if info_3["status"]=="changing_name" then
  182. connection_name = info_3["new_name"]
  183. connections = fs.open("connections","w")
  184. connections.writeLine(info_3["new_name"])
  185. connections.writeLine(info_2)
  186. connections.flush()
  187. connections.close()
  188. startup()
  189. end
  190.  
  191. if info_3["status"]=="get_info" then
  192. rednet.send(info_2,{["sending_status"]=true,["sending_to"]=connected_pcs["name"][1],["name"]=connection_name,["status"]=status})
  193. end
  194.  
  195.  
  196. if info_3["status"]=="change_status" then
  197.  
  198. if status=="on" then
  199. status="off"
  200. change_status()
  201. else
  202. status="on"
  203. change_status()
  204. end
  205.  
  206. rednet.send(info_2,{["sending_status"]=true,["sending_to"]=connected_pcs["name"][1],["name"]=connection_name,["status"]=status})
  207. end
  208.  
  209.  
  210. if status=="on" then
  211. redstone.setOutput("bottom",true)
  212. redstone.setOutput("top",true)
  213. redstone.setOutput("back",true)
  214. redstone.setOutput("front",true)
  215. redstone.setOutput("right",true)
  216. redstone.setOutput("left",true)
  217. end
  218.  
  219. if status=="off" then
  220. redstone.setOutput("bottom",false)
  221. redstone.setOutput("top",false)
  222. redstone.setOutput("back",false)
  223. redstone.setOutput("front",false)
  224. redstone.setOutput("right",false)
  225. redstone.setOutput("left",false)
  226. end
  227.  
  228.  
  229. if info_3["status"]=="disconnected" then
  230. fs.delete("connections")
  231.  
  232. term.clear()
  233. term.setCursorPos(1,1)
  234. term.setTextColor(colors.green)
  235. print("Please enter the name of the PC you want to")
  236. print("connect to")
  237. print()
  238. term.setTextColor(colors.white)
  239. connecting_to=read()
  240.  
  241. connections=fs.open("connections","w")
  242. connections.writeLine(connecting_to)
  243. connections.flush()
  244. connections.close()
  245.  
  246. for i=1,250 do
  247. rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name})
  248. end
  249.  
  250. connected=false
  251.  
  252. while not connected do
  253. info_1 ,info_2, info_3, info_4 = os.pullEvent()
  254. if info_1=="rednet_message" then
  255. if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
  256. connections=fs.open("connections","w")
  257. connections.writeLine(connecting_to)
  258. connections.writeLine(info_2)
  259. connections.flush()
  260. connections.close()
  261. connected=true
  262. end
  263. end
  264. end
  265.  
  266. end
  267.  
  268. end
  269.  
  270. end
  271. end
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292. function change_status()
  293.  
  294. status_save = fs.open("status","w")
  295.  
  296. status_save.write(status)
  297. status_save.flush()
  298. status_save.close()
  299. end
  300.  
  301.  
  302.  
  303. function assign_status()
  304.  
  305. status_save = fs.open("status","r")
  306.  
  307. status = status_save.readLine()
  308. status_save.close()
  309. end
  310.  
  311.  
  312.  
  313. function assign_connections()
  314.  
  315. connections=fs.open("connections","r")
  316.  
  317. connected_pcs["name"]={}
  318. connected_pcs["len"]=0
  319. connected_pcs["id"]={}
  320. connected_pcs["id"][0]="0"
  321. i=0
  322. while tonumber(connected_pcs["id"][i])~=nil do
  323.  
  324. i=i+1
  325.  
  326. connected_pcs["name"][i] = connections.readLine()
  327. connected_pcs["id"][i] = connections.readLine()
  328.  
  329.  
  330. if tonumber(connected_pcs["id"][i])~=nil then
  331. connected_pcs["len"]=connected_pcs["len"]+1
  332. connected_pcs["id"][i]=tonumber(connected_pcs["id"][i])
  333. connected=true
  334. end
  335.  
  336. end
  337. connections.close()
  338. end
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345. function assign_name()
  346.  
  347. connection_code = fs.open("connection_code","r")
  348. connection_name = connection_code.readLine()
  349. connection_code.close()
  350.  
  351. end
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365. startup()
  366. pull()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement