Advertisement
Chaos_Cash

Connection sub backup: 21.03.2024

Mar 21st, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.87 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. assign_name()
  39. assign_connections()
  40.  
  41. term.clear()
  42.  
  43. for i=1,connected_pcs["len"] do
  44. rednet.send(connected_pcs["id"][i],{["sending_status"]=true,["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]=status})
  45. end
  46.  
  47. if status=="on" then
  48. redstone.setOutput("bottom",true)
  49. redstone.setOutput("top",true)
  50. redstone.setOutput("back",true)
  51. redstone.setOutput("front",true)
  52. redstone.setOutput("right",true)
  53. redstone.setOutput("left",true)
  54. end
  55.  
  56. if status=="off" then
  57. redstone.setOutput("bottom",false)
  58. redstone.setOutput("top",false)
  59. redstone.setOutput("back",false)
  60. redstone.setOutput("front",false)
  61. redstone.setOutput("right",false)
  62. redstone.setOutput("left",false)
  63. end
  64.  
  65. end
  66.  
  67.  
  68.  
  69. function pull()
  70.  
  71. while true do
  72.  
  73. info_1=0
  74. info_2=0
  75. info_3=0
  76. info_4=0
  77. info_1, info_2, info_3, info_4 = os.pullEvent()
  78.  
  79. if info_1=="rednet_message" and info_3["sending_to"]==connection_name then
  80. connected=false
  81. for i=1,connected_pcs["len"] do
  82. if info_3["name"]==connected_pcs["name"][i] then
  83. connected=true
  84. end
  85. end
  86.  
  87. if connected then
  88.  
  89.  
  90. if info_3["status"]=="changing_name" then
  91. connection_name = info_3["new_name"]
  92. connections = fs.open("connections","w")
  93. connections.writeLine(info_3["new_name"])
  94. connections.writeLine(info_2)
  95. connections.flush()
  96. connections.close()
  97. startup()
  98. end
  99.  
  100. if info_3["status"]=="get_info" then
  101. rednet.send(info_2,{["sending_status"]=true,["sending_to"]=info_3["name"],["name"]=connection_name,["status"]=status})
  102. end
  103.  
  104.  
  105. if info_3["status"]=="change_status" then
  106.  
  107. if status=="on" then
  108. status="off"
  109. change_status()
  110. else
  111. status="on"
  112. change_status()
  113. end
  114.  
  115. for i=1,connected_pcs["len"] do
  116. rednet.send(connected_pcs["id"][i],{["sending_status"]=true,["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]=status})
  117. end
  118. end
  119.  
  120.  
  121. if status=="on" then
  122. redstone.setOutput("bottom",true)
  123. redstone.setOutput("top",true)
  124. redstone.setOutput("back",true)
  125. redstone.setOutput("front",true)
  126. redstone.setOutput("right",true)
  127. redstone.setOutput("left",true)
  128. end
  129.  
  130. if status=="off" then
  131. redstone.setOutput("bottom",false)
  132. redstone.setOutput("top",false)
  133. redstone.setOutput("back",false)
  134. redstone.setOutput("front",false)
  135. redstone.setOutput("right",false)
  136. redstone.setOutput("left",false)
  137. end
  138.  
  139.  
  140. if info_3["status"]=="disconnected" then
  141.  
  142. connections=fs.open("connections","w")
  143.  
  144. for i=1,connected_pcs["len"] do
  145. if connected_pcs["name"][i]~=info_3["name"] and tonumber(connected_pcs["id"][i])~=tonumber(info_2) then
  146. connections.writeLine(connected_pcs["name"][i])
  147. connections.writeLine(connected_pcs["id"][i])
  148. end
  149. end
  150.  
  151. connections.flush()
  152. connections.close()
  153. assign_connections()
  154.  
  155. if cur_env=="connection_menu" then
  156. change_connection_menu=true
  157. end
  158.  
  159. end
  160. end
  161.  
  162. end
  163.  
  164.  
  165. if info_1=="rednet_message" and info_3["connecting_to"]==connection_name then
  166.  
  167.  
  168. already_connected=false
  169. same_id=false
  170. same_name=false
  171.  
  172.  
  173. if info_3["id"]==nil then
  174.  
  175. for i=1,connected_pcs["len"] do
  176. if tonumber(connected_pcs["id"][i])==tonumber(info_2) then
  177. same_id=true
  178. end
  179.  
  180.  
  181. if info_3["name"] == connected_pcs["name"][i] then
  182. same_name=true
  183. end
  184. end
  185.  
  186. else
  187.  
  188. for i=1,connected_pcs["len"] do
  189. if tonumber(connected_pcs["id"][i])==tonumber(info_3["id"]) then
  190. same_id=true
  191. end
  192.  
  193.  
  194. if info_3["name"] == connected_pcs["name"][i] then
  195. same_name=true
  196. end
  197. end
  198.  
  199. end
  200.  
  201. if not same_id and not same_name then
  202.  
  203. connections = fs.open("connections","a")
  204. connections.writeLine(info_3["name"])
  205. if info_3["id"]==nil then
  206. connections.writeLine(info_2)
  207. else
  208. connections.writeLine(info_3["id"])
  209. end
  210. connections.close()
  211. assign_connections()
  212.  
  213.  
  214. change_connection_menu=true
  215.  
  216. if info_3["id"]==nil then
  217. rednet.send(info_2,{["name"]=connection_name,["connecting_to"]=info_3["name"],["cur_status"]=status,["status"]=status})
  218. else
  219. rednet.send(info_3["id"],{["name"]=connection_name,["connecting_to"]=info_3["name"],["cur_status"]=status,["status"]=status})
  220. end
  221.  
  222. -- elseif same_id then
  223. -- if info_3["id"]==nil then
  224. -- rednet.send(info_2,{["name"]=connection_name,["sending_to"]=info_3["name"],["status"]="disconnected",["same_id"]=true})
  225. -- else
  226. -- rednet.send(info_3["id"],{["name"]=connection_name,["sending_to"]=info_3["name"],["status"]="disconnected",["same_id"]=true})
  227. -- end
  228. -- elseif same_name then
  229. -- if info_3["id"]==nil then
  230. -- rednet.send(info_2,{["name"]=connection_name,["sending_to"]=info_3["name"],["status"]="disconnected",["same_name"]=true})
  231. -- else
  232. -- rednet.send(info_3["id"],{["name"]=connection_name,["sending_to"]=info_3["name"],["status"]="disconnected",["same_name"]=true})
  233. -- end
  234. end
  235.  
  236. end
  237.  
  238.  
  239.  
  240. if cur_env=="menu" then
  241.  
  242. if button_check(12,7,25,9) then
  243. connection_menu()
  244. end
  245.  
  246. end
  247.  
  248.  
  249. if cur_env=="connection_menu" then
  250.  
  251. if button_check(44,17,51,19) then
  252. menu()
  253. end
  254.  
  255. if button_check(34,17,42,19) then
  256.  
  257. term.clear()
  258. term.setCursorPos(1,1)
  259. term.setTextColor(colors.green)
  260. print("Please enter the name of the PC you want to")
  261. print("connect to")
  262. print()
  263. term.setTextColor(colors.white)
  264. connecting_to=read()
  265.  
  266. for i=1,1000 do
  267. rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name,["status"]=status})
  268. end
  269.  
  270. connected=false
  271.  
  272. while info_1~="timer" and not connected do
  273. timer=os.startTimer(5)
  274. info_1 ,info_2, info_3, info_4 = os.pullEvent()
  275. if info_1=="rednet_message" then
  276.  
  277. if info_3["status"]=="disconnected" then
  278. fs.delete("connections")
  279.  
  280. term.clear()
  281. term.setCursorPos(1,1)
  282. term.setTextColor(colors.green)
  283.  
  284. if info_3["same_name"] then
  285. print("A PC with this name was already connected,")
  286. print("please use a different name.")
  287. fs.delete("connection_code")
  288. sleep(3)
  289. change_connection_menu=true
  290. end
  291.  
  292. if info_3["same_id"] then
  293. print("A PC with this id was already connected,")
  294. print("please use a different PC.")
  295. sleep(3)
  296. change_connection_menu=true
  297. end
  298. end
  299.  
  300.  
  301. if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
  302. connections=fs.open("connections","a")
  303. connections.writeLine(connecting_to)
  304. connections.writeLine(info_2)
  305. connections.flush()
  306. connections.close()
  307. assign_connections()
  308. connected=true
  309. end
  310.  
  311. end
  312. os.cancelTimer(timer)
  313. end
  314. info_1=0
  315. change_connection_menu=true
  316.  
  317. end
  318.  
  319.  
  320. for i=1,connected_pcs["len"] do
  321.  
  322.  
  323. if info_1=="mouse_click" and info_4==i+3 then
  324. connection_menu()
  325. term.setBackgroundColor(colors.red)
  326. term.setCursorPos(1,i+3)
  327. term.setTextColor(colors.green)
  328. term.write("[Id-"..tonumber(connected_pcs["id"][i]).."]")
  329.  
  330. for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][i]).."]") do
  331. write(" ")
  332. end
  333.  
  334. term.setTextColor(colors.white)
  335. term.write(connected_pcs["name"][i])
  336.  
  337. for i1=1,51-string.len(connected_pcs["name"][i])-9 do
  338. term.write(" ")
  339. end
  340.  
  341. button("Delete",1,17,10,19,colors.red)
  342.  
  343. if i~=clicked_connection then
  344. clicked_connection=i
  345. else
  346. clicked_connection=nil
  347. connection_menu()
  348. end
  349. end
  350. end
  351.  
  352. if clicked_connection~=nil then
  353. if button_check(1,17,10,19) then
  354. rednet.send(tonumber(connected_pcs["id"][clicked_connection]),{["sending_to"]=connected_pcs["name"][clicked_connection],["status"]="disconnected",["name"]=connection_name})
  355. disconnect(clicked_connection)
  356. clicked_connection=nil
  357. connection_menu()
  358. end
  359. end
  360.  
  361. if change_connection_menu then
  362. connection_menu()
  363.  
  364. if clicked_connection~=nil then
  365.  
  366. term.setBackgroundColor(colors.red)
  367. term.setCursorPos(1,clicked_connection+3)
  368. term.setTextColor(colors.green)
  369. term.write("[Id-"..tonumber(connected_pcs["id"][clicked_connection]).."]")
  370.  
  371. for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][clicked_connection]).."]") do
  372. write(" ")
  373. end
  374.  
  375. term.setTextColor(colors.white)
  376. term.write(connected_pcs["name"][clicked_connection])
  377.  
  378. for i1=1,51-string.len(connected_pcs["name"][clicked_connection])-9 do
  379. term.write(" ")
  380. end
  381.  
  382. button("Delete",1,17,10,19,colors.red)
  383.  
  384. end
  385.  
  386. change_connection_menu=false
  387. end
  388.  
  389. if clicked_connection~=nil then
  390. if ((last_info_1=="mouse_click" or last_info_1=="mouse_drag") and last_info_4==clicked_connection+3) and (info_1=="mouse_drag" and (info_4 < clicked_connection+3 or info_4 > clicked_connection+3)) then
  391. if info_4-3 < connected_pcs["len"]+1 and info_4-3>0 then
  392. change_connection_order(clicked_connection,info_4-3)
  393. end
  394. end
  395. end
  396.  
  397. end
  398.  
  399. end
  400. end
  401.  
  402.  
  403.  
  404. function menu()
  405.  
  406. cur_env="menu"
  407.  
  408. term.setBackgroundColor(colors.black)
  409. term.clear()
  410.  
  411. headline("Menu")
  412. button("Connections",12,7,25,9)
  413.  
  414. end
  415.  
  416.  
  417.  
  418. function connection_menu()
  419.  
  420. if cur_env~="connection_menu" then
  421. info_1=nil
  422. end
  423.  
  424. cur_env="connection_menu"
  425.  
  426. term.setBackgroundColor(colors.black)
  427. term.clear()
  428. headline("Connections")
  429. button("Back",44,17,51,19)
  430. button("Connect",34,17,42,19)
  431.  
  432. term.setCursorPos(1,4)
  433.  
  434. for i=1,connected_pcs["len"] do
  435.  
  436. term.setTextColor(colors.green)
  437. term.write("[Id-"..tonumber(connected_pcs["id"][i]).."]")
  438.  
  439. for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][i]).."]") do
  440. write(" ")
  441. end
  442.  
  443. term.setTextColor(colors.white)
  444. print(connected_pcs["name"][i])
  445.  
  446. end
  447.  
  448. end
  449.  
  450.  
  451.  
  452. function change_status()
  453.  
  454. status_save = fs.open("status","w")
  455.  
  456. status_save.write(status)
  457. status_save.flush()
  458. status_save.close()
  459. end
  460.  
  461.  
  462.  
  463. function assign_status()
  464.  
  465. status_save = fs.open("status","r")
  466.  
  467. status = status_save.readLine()
  468. status_save.close()
  469. end
  470.  
  471.  
  472.  
  473. function assign_connections()
  474.  
  475. connections=fs.open("connections","r")
  476.  
  477. connected_pcs["name"]={}
  478. connected_pcs["len"]=0
  479. connected_pcs["id"]={}
  480. connected_pcs["id"][0]="0"
  481. i=0
  482. while tonumber(connected_pcs["id"][i])~=nil do
  483.  
  484. i=i+1
  485.  
  486. connected_pcs["name"][i] = connections.readLine()
  487. connected_pcs["id"][i] = connections.readLine()
  488.  
  489.  
  490. if tonumber(connected_pcs["id"][i])~=nil then
  491. connected_pcs["len"]=connected_pcs["len"]+1
  492. connected_pcs["id"][i]=tonumber(connected_pcs["id"][i])
  493. connected=true
  494. end
  495.  
  496. end
  497. connections.close()
  498. end
  499.  
  500.  
  501.  
  502. function assign_name()
  503.  
  504. connection_code = fs.open("connection_code","r")
  505. connection_name = connection_code.readLine()
  506. connection_code.close()
  507.  
  508. end
  509.  
  510.  
  511.  
  512. function disconnect(disconnect_number)
  513.  
  514. rednet.send(connected_pcs["id"][disconnect_number],{["name"]=connection_name,["sending_to"]=connected_pcs["name"][disconnect_number],["status"]="disconnected"})
  515.  
  516. connections = fs.open("connections","w")
  517.  
  518. for i=1,connected_pcs["len"]-1 do
  519.  
  520. if i < disconnect_number then
  521. connections.writeLine(connected_pcs["name"][i])
  522. connections.writeLine(connected_pcs["id"][i])
  523. end
  524.  
  525. if i > disconnect_number-1 then
  526. connections.writeLine(connected_pcs["name"][i+1])
  527. connections.writeLine(connected_pcs["id"][i+1])
  528. end
  529.  
  530. end
  531. connections.flush()
  532. connections.close()
  533. assign_connections()
  534. end
  535.  
  536.  
  537.  
  538. function headline(headline)
  539. headline_lenght=string.len(" "..headline.." ")
  540. headline_pos=27-headline_lenght/2
  541. term.setCursorPos(headline_pos,1)
  542. term.setBackgroundColor(colors.yellow)
  543. term.setTextColor(colors.blue)
  544. term.write(" "..headline.." ")
  545. term.setBackgroundColor(colors.black)
  546. term.setTextColor(colors.white)
  547. end
  548.  
  549.  
  550.  
  551. function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
  552. term.setTextColor(colors.green)
  553.  
  554. if button_color~=nil then
  555. term.setTextColor(button_color)
  556. end
  557.  
  558. square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  559. term.setCursorPos(math.ceil(button_pos_x1-string.len(button_text)/2+(button_pos_x2-button_pos_x1)/2),button_pos_y1+(button_pos_y2-button_pos_y1)/2)
  560. term.write(button_text)
  561. end
  562.  
  563.  
  564.  
  565. function button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  566. if info_1=="mouse_click" and info_3 > b_check_x1-1 and info_3 < b_check_x2+1 and info_4 > b_check_y1-1 and info_4 < b_check_y2+1 then
  567. return true
  568. else
  569. return false
  570. end
  571. end
  572.  
  573.  
  574.  
  575. function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  576. x_1=x1
  577. y_1=y1
  578. x_2=x2
  579. y_2=y2
  580. while x_1~=x2+1 do
  581. term.setBackgroundColor(back_color_square)
  582. term.setCursorPos(x_1,y_1)
  583. if text_color~=nil then
  584. term.setTextColor(text_color)
  585. end
  586. term.write(symbol_square_x)
  587. x_1=x_1+1
  588. end
  589.  
  590. while y_1~=y2-1 do
  591. term.setCursorPos(x_1-1,y_1+1)
  592. term.write(symbol_square_y)
  593. y_1=y_1+1
  594. end
  595. x2=x2-1
  596. while x_2~=x1-1 do
  597. term.setCursorPos(x_2,y_2)
  598. term.write(symbol_square_x)
  599. x_2=x_2-1
  600. end
  601.  
  602. while y_2~=y1+1 do
  603. term.setCursorPos(x_2+1,y_2-1)
  604. term.write(symbol_square_y)
  605. y_2=y_2-1
  606. end
  607. term.setBackgroundColor(colors.black)
  608. end
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619. startup()
  620. menu()
  621. pull()
  622.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement