Advertisement
Chaos_Cash

Connection main backup: 10.02.2024

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