Advertisement
Chaos_Cash

Connection main backup: 21.03.2024

Mar 21st, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.55 KB | None | 0 0
  1. function startup()
  2.  
  3. dropdown = {}
  4. dropdown["loaded"] = {}
  5. dropdown["loaded"]["len"] = 0
  6.  
  7. settings = {}
  8. settings["names"] = {}
  9. settings["len"] = 0
  10.  
  11. old_label={}
  12. label_save={}
  13.  
  14. got_response=0
  15.  
  16. connected_pcs={}
  17. connected_pcs["len"]=0
  18. connected_pcs["got_response"]={}
  19.  
  20. button_positions={}
  21. button_positions["status"]={}
  22.  
  23. connected_pcs["number"]={}
  24. connected_pcs["number_of_id"]={}
  25.  
  26. connected_pcs["labels"]={}
  27. connected_pcs["labels"]["is_nil"]={}
  28. connected_pcs["labels"]["len"]=0
  29. for i=1,3 do
  30. connected_pcs["labels"]["line"..i]={}
  31. end
  32.  
  33.  
  34.  
  35. rednet.open("top")
  36.  
  37. connections=fs.open("connections","a")
  38.  
  39. connections.close()
  40.  
  41.  
  42. if not fs.exists("connection_code") then
  43.  
  44. term.clear()
  45. term.setCursorPos(1,1)
  46. term.setTextColor(colors.green)
  47. print("Please enter the name you want to use to connect")
  48. print("to other PC´s")
  49. print()
  50. term.setTextColor(colors.white)
  51. connection_name=read()
  52.  
  53. connection_code=fs.open("connection_code","w")
  54. connection_code.writeLine(connection_name)
  55. connection_code.flush()
  56. connection_code.close()
  57.  
  58. end
  59.  
  60. if not fs.exists("settings") then
  61.  
  62. settings_save = fs.open("settings","w")
  63.  
  64. settings_save.writeLine("off")
  65. settings_save.writeLine("right")
  66.  
  67. settings_save.flush()
  68. settings_save.close()
  69. end
  70.  
  71.  
  72. create_setting("solo")
  73. create_setting("monitor_side")
  74. assign_settings()
  75.  
  76.  
  77. assign_name()
  78.  
  79. mon=peripheral.wrap(settings["monitor_side"]["status"])
  80. mon.setTextScale(0.5)
  81.  
  82.  
  83. logs={}
  84. logs["len"]=0
  85. logs["entries"]={}
  86. logs["time"]={}
  87.  
  88.  
  89. assign_connections()
  90.  
  91. assign_labels()
  92.  
  93. for i=1,connected_pcs["len"] do
  94. rednet.send(tonumber(connected_pcs["id"][i]),{["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]="get_info"})
  95. end
  96.  
  97.  
  98. if settings["monitor_side"]["status"]=="right" then
  99. create_dropdown_menu("mon_side",{"right","left","top","bottom","front","back"},3,8,11)
  100. elseif settings["monitor_side"]["status"]=="left" then
  101. create_dropdown_menu("mon_side",{"left","right","top","bottom","front","back"},3,8,11)
  102. elseif settings["monitor_side"]["status"]=="top" then
  103. create_dropdown_menu("mon_side",{"top","right","left","bottom","front","back"},3,8,11)
  104. elseif settings["monitor_side"]["status"]=="bottom" then
  105. create_dropdown_menu("mon_side",{"bottom","right","left","top","front","back"},3,8,11)
  106. elseif settings["monitor_side"]["status"]=="front" then
  107. create_dropdown_menu("mon_side",{"front","right","left","top","bottom","back"},3,8,11)
  108. elseif settings["monitor_side"]["status"]=="back" then
  109. create_dropdown_menu("mon_side",{"back","right","left","top","bottom","front"},3,8,11)
  110. end
  111.  
  112. end
  113.  
  114.  
  115.  
  116.  
  117. function screen()
  118.  
  119. cur_x1=1
  120. cur_y1=1
  121. cur_x2=7
  122. cur_y2=5
  123. cur_connection=1
  124. button_positions["len"]=0
  125.  
  126. mon.clear()
  127.  
  128. for i=1,math.ceil(connected_pcs["len"]^0.5) do
  129.  
  130. for i1=1,math.ceil(connected_pcs["len"]^0.5) do
  131. if connected_pcs["name"][cur_connection]~=nil then
  132. if button_positions["status"][cur_connection]=="on" then
  133. mon_button(connected_pcs["name"][cur_connection],cur_x1,cur_y1,cur_x2,cur_y2)
  134. end
  135. if button_positions["status"][cur_connection]=="off" then
  136. mon_button(connected_pcs["name"][cur_connection],cur_x1,cur_y1,cur_x2,cur_y2,colors.red)
  137. end
  138. if button_positions["status"][cur_connection]==nil then
  139. mon_button(connected_pcs["name"][cur_connection],cur_x1,cur_y1,cur_x2,cur_y2,colors.blue)
  140. end
  141. button_positions[cur_connection]={}
  142. button_positions[cur_connection]={["x1"]=cur_x1,["y1"]=cur_y1,["x2"]=cur_x2,["y2"]=cur_y2,["id"]=connected_pcs["id"][cur_connection],["status"]="on"}
  143. button_positions["len"]=button_positions["len"]+1
  144. end
  145. cur_x1=cur_x1+8
  146. cur_x2=cur_x2+8
  147. cur_connection=cur_connection+1
  148. end
  149.  
  150. cur_y1=cur_y1+5
  151. cur_y2=cur_y2+5
  152.  
  153. cur_x1=1
  154. cur_x2=7
  155.  
  156. end
  157. end
  158.  
  159.  
  160.  
  161. function menu()
  162.  
  163. cur_env="menu"
  164.  
  165. term.setBackgroundColor(colors.black)
  166. term.clear()
  167.  
  168. headline("Menu")
  169. button("Connections",12,7,25,9)
  170. button("Change name",27,7,40,9)
  171. button("Settings",27,11,40,13)
  172. button("Log",12,11,25,13)
  173.  
  174. end
  175.  
  176.  
  177.  
  178. function pull(loop_time)
  179.  
  180. if tonumber(loop_time)~=nil then
  181. timer_id=os.startTimer(loop_time)
  182. end
  183.  
  184. while info_1~="timer" or timer_id~=info_2 do
  185.  
  186. last_info_1 = info_1
  187. last_info_2 = info_2
  188. last_info_3 = info_3
  189. last_info_4 = info_4
  190.  
  191. info_1=0
  192. info_2=0
  193. info_3=0
  194. info_4=0
  195.  
  196. info_1, info_2, info_3, info_4 = os.pullEvent()
  197.  
  198. overwrite_dropdown()
  199.  
  200.  
  201.  
  202. if info_1=="rednet_message" and info_3["sending_to"]==connection_name then
  203.  
  204. if info_3["sending_status"] then
  205. if button_positions["status"][connected_pcs["number"][info_3["name"]]]~=info_3["status"] then
  206. screen()
  207. button_positions["status"][connected_pcs["number"][info_3["name"]]]=info_3["status"]
  208. screen()
  209. end
  210. end
  211.  
  212. if tonumber(answer_id)==tonumber(info_2) then
  213. answered=true
  214. end
  215.  
  216. if info_3["status"]=="disconnected" then
  217. disconnect(connected_pcs["number_of_id"][tonumber(info_2)])
  218. end
  219.  
  220. end
  221.  
  222.  
  223. if info_1=="rednet_message" and info_3["connecting_to"]==connection_name then
  224.  
  225.  
  226. if info_3["is_main"]==true then
  227.  
  228. rednet.send(info_2,{["connecting_to"]=info_3["name"],["is_main"]=true,["name"]=connection_name})
  229.  
  230. for i=1,connected_pcs["len"] do
  231. rednet.send(connected_pcs["id"][i],{["name"]=info_3["name"],["connecting_to"]=connected_pcs["name"][i],["is_main"]=true,["id"]=info_2,["send_response"]=true})
  232. end
  233.  
  234. else
  235.  
  236.  
  237. already_connected=false
  238. same_id=false
  239. same_name=false
  240.  
  241. for i=1,connected_pcs["len"] do
  242.  
  243. if tonumber(connected_pcs["id"][i])==tonumber(info_2) then
  244. same_id=true
  245. end
  246.  
  247. if info_3["name"] == connected_pcs["name"][i] then
  248. same_name=true
  249. end
  250.  
  251. end
  252.  
  253. if not same_id and not same_name then
  254.  
  255. connections = fs.open("connections","a")
  256. connections.writeLine(info_3["name"])
  257. connections.writeLine(info_2)
  258. connections.flush()
  259. connections.close()
  260. assign_connections()
  261. assign_labels()
  262.  
  263. create_log_entry("Connected to the PC named: "..info_3["name"])
  264. change_connection_menu=true
  265. button_positions["status"][connected_pcs["len"]]=info_3["status"]
  266. screen()
  267.  
  268. if not info_3["dont_respond"] then
  269. rednet.send(info_2,{["name"]=connection_name,["connecting_to"]=info_3["name"]})
  270. end
  271.  
  272. end
  273.  
  274. end
  275.  
  276. end
  277.  
  278.  
  279. for i=1,button_positions["len"] do
  280.  
  281. if mon_button_check(button_positions[i]["x1"],button_positions[i]["y1"],button_positions[i]["x2"],button_positions[i]["y2"]) then
  282.  
  283. if settings["solo"]["status"]~="on" then
  284. rednet.send(tonumber(connected_pcs["id"][i]),{["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]="change_status"})
  285. answer_id=connected_pcs["id"][i]
  286. answered=false
  287. pull(0.05,true)
  288.  
  289. if not answered then
  290. screen()
  291. x,y=mon.getSize()
  292. mon.setCursorPos(math.floor(x/2-string.len("ERROR,open log for more info")/2),math.floor(y/2))
  293. mon.setTextColor(colors.red)
  294. mon.write("ERROR,open log for more info")
  295.  
  296. create_log_entry("The PC named, "..connected_pcs["name"][i]..", did not respond, its probably not loaded or the programm is not running")
  297.  
  298. os.startTimer(0.05)
  299. pull(3)
  300. screen()
  301. end
  302.  
  303. else
  304.  
  305. answered_pcs=0
  306. pcs_that_are_on=0
  307. for i1=1,connected_pcs["len"] do
  308. if button_positions["status"][i1] == "on" or i1==i then
  309. pcs_that_are_on=pcs_that_are_on+1
  310. rednet.send(tonumber(connected_pcs["id"][i1]),{["sending_to"]=connected_pcs["name"][i1],["name"]=connection_name,["status"]="get_info"})
  311. answer_id=connected_pcs["id"][i1]
  312. answered=false
  313. pull(0.01,true)
  314. if answered then
  315. answered_pcs=answered_pcs+1
  316. else
  317. create_log_entry("The PC named, "..connected_pcs["name"][i1]..", did not respond, its probably not loaded or the programm is not running")
  318. end
  319. end
  320. end
  321.  
  322.  
  323. if answered_pcs==pcs_that_are_on then
  324.  
  325. for i1=1,connected_pcs["len"] do
  326. if button_positions["status"][i1] == "on" or i1==i then
  327.  
  328. rednet.send(tonumber(connected_pcs["id"][i1]),{["sending_to"]=connected_pcs["name"][i1],["name"]=connection_name,["status"]="change_status"})
  329.  
  330. end
  331. end
  332.  
  333. else
  334.  
  335. screen()
  336. x,y=mon.getSize()
  337. mon.setCursorPos(math.floor(x/2-string.len("ERROR,open log for more info")/2),math.floor(y/2))
  338. mon.setTextColor(colors.red)
  339. mon.write("ERROR,open log for more info")
  340.  
  341. pull(3)
  342. screen()
  343.  
  344. end
  345. end
  346. end
  347.  
  348. end
  349.  
  350.  
  351. if cur_env=="menu" then
  352.  
  353. if button_check(27,7,40,9) then
  354. change_name()
  355. end
  356.  
  357. if button_check(12,11,25,13) then
  358. log()
  359. end
  360.  
  361. if button_check(12,7,25,9) then
  362. connection_menu()
  363. end
  364.  
  365. if button_check(27,11,40,13) then
  366. settings_menu()
  367. end
  368.  
  369. end
  370.  
  371.  
  372. if cur_env=="log" then
  373.  
  374. if change_log then
  375. log()
  376. end
  377.  
  378. if button_check(44,17,51,19) then
  379. menu()
  380. end
  381.  
  382. end
  383.  
  384.  
  385. if cur_env=="connection_menu" then
  386.  
  387. if button_check(44,17,51,19) then
  388. menu()
  389. end
  390.  
  391. if button_check(34,17,42,19) then
  392.  
  393. term.clear()
  394. term.setCursorPos(1,1)
  395. term.setTextColor(colors.green)
  396. print("Please enter the name of the PC you want to")
  397. print("connect to")
  398. print()
  399. term.setTextColor(colors.white)
  400. connecting_to=read()
  401.  
  402. for i=1,1000 do
  403. rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name,["status"]=status,["is_main"]=true})
  404. end
  405.  
  406.  
  407. connected=false
  408. timer=os.startTimer(5)
  409. while info_1~="timer" and not connected do
  410. info_1 ,info_2, info_3, info_4 = os.pullEvent()
  411.  
  412. if info_1=="rednet_message" and info_3["connecting_to"]==connection_name and info_3["is_main"] then
  413. connected=true
  414. end
  415.  
  416.  
  417. if info_1=="rednet_message" and info_3["connecting_to"]==connection_name and not info_3["is_main"] then
  418.  
  419. if info_3["status"]=="disconnected" then
  420. fs.delete("connections")
  421.  
  422. term.clear()
  423. term.setCursorPos(1,1)
  424. term.setTextColor(colors.green)
  425.  
  426. if info_3["same_name"] then
  427. print("A PC with this name was already connected,")
  428. print("please use a different name.")
  429. fs.delete("connection_code")
  430. sleep(3)
  431. change_connection_menu=true
  432. end
  433.  
  434. if info_3["same_id"] then
  435. print("A PC with this id was already connected,")
  436. print("please use a different PC.")
  437. sleep(3)
  438. change_connection_menu=true
  439. end
  440. end
  441.  
  442.  
  443. if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
  444. connections=fs.open("connections","a")
  445. connections.writeLine(connecting_to)
  446. connections.writeLine(info_2)
  447. connections.flush()
  448. connections.close()
  449. assign_connections()
  450. connected=true
  451. button_positions["status"][connected_pcs["number"][info_3["name"]]]=info_3["cur_status"]
  452. create_log_entry("Connected to the PC named: "..info_3["name"])
  453. end
  454.  
  455. end
  456. os.cancelTimer(timer)
  457. end
  458. info_1=0
  459.  
  460. change_connection_menu=true
  461. assign_labels()
  462. screen()
  463.  
  464. end
  465.  
  466.  
  467.  
  468. for i=1,connected_pcs["len"] do
  469.  
  470.  
  471. if info_1=="mouse_click" and info_4==i+3 then
  472. connection_menu()
  473. term.setBackgroundColor(colors.red)
  474. term.setCursorPos(1,i+3)
  475. term.setTextColor(colors.green)
  476. term.write("[Id-"..tonumber(connected_pcs["id"][i]).."]")
  477.  
  478. for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][i]).."]") do
  479. write(" ")
  480. end
  481.  
  482. term.setTextColor(colors.white)
  483. term.write(connected_pcs["name"][i])
  484.  
  485. for i1=1,51-string.len(connected_pcs["name"][i])-9 do
  486. term.write(" ")
  487. end
  488.  
  489. button("Delete",1,17,10,19,colors.red)
  490. button("Settings",12,17,22,19)
  491.  
  492. if i~=clicked_connection then
  493. clicked_connection=i
  494. else
  495. clicked_connection=nil
  496. connection_menu()
  497. end
  498. end
  499. end
  500.  
  501. if clicked_connection~=nil then
  502. if button_check(1,17,10,19) then
  503. disconnect(clicked_connection)
  504. clicked_connection=nil
  505. connection_menu()
  506. end
  507.  
  508. if button_check(12,17,22,19) then
  509. connection_settings(clicked_connection)
  510. end
  511. end
  512.  
  513. if change_connection_menu then
  514. connection_menu()
  515.  
  516. if clicked_connection~=nil then
  517.  
  518. term.setBackgroundColor(colors.red)
  519. term.setCursorPos(1,clicked_connection+3)
  520. term.setTextColor(colors.green)
  521. term.write("[Id-"..tonumber(connected_pcs["id"][clicked_connection]).."]")
  522.  
  523. for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][clicked_connection]).."]") do
  524. write(" ")
  525. end
  526.  
  527. term.setTextColor(colors.white)
  528. term.write(connected_pcs["name"][clicked_connection])
  529.  
  530. for i1=1,51-string.len(connected_pcs["name"][clicked_connection])-9 do
  531. term.write(" ")
  532. end
  533.  
  534. button("Delete",1,17,10,19,colors.red)
  535. button("Settings",12,17,22,19)
  536.  
  537. end
  538.  
  539. change_connection_menu=false
  540. end
  541.  
  542. if clicked_connection~=nil then
  543. 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
  544. if info_4-3 < connected_pcs["len"]+1 and info_4-3>0 then
  545. change_connection_order(clicked_connection,info_4-3)
  546. end
  547. end
  548. end
  549.  
  550. end
  551.  
  552.  
  553. if cur_env=="change_name" then
  554.  
  555. if info_1=="rednet_message" and info_3["sending_to"]==connection_name then
  556. if info_3["sending_status"] then
  557. button_positions["status"][connected_pcs["number"][info_3["name"]]]=info_3["status"]
  558. screen()
  559. got_response=got_response+1
  560. connected_pcs["got_response"][connected_pcs["number"][info_3["name"]]]=true
  561. end
  562. end
  563.  
  564. end
  565.  
  566.  
  567. if cur_env=="connection_settings" then
  568.  
  569. if button_check(44,17,51,19) then
  570. connection_menu()
  571. end
  572.  
  573. if button_check(3,3,16,5) then
  574. label_settings(clicked_connection)
  575. end
  576. end
  577.  
  578.  
  579. if cur_env=="label_settings" then
  580.  
  581. if button_check(44,17,51,19) then
  582. connection_settings(clicked_connection)
  583. end
  584.  
  585. if info_1=="mouse_click" and info_4 < 6 and info_4 > 2 then
  586. term.setTextColor(colors.green)
  587. term.setCursorPos(1,info_4)
  588. term.write("Line "..tostring(info_4-2)..": ")
  589. term.setTextColor(colors.white)
  590. for i=1,42 do
  591. write(" ")
  592. end
  593. term.setCursorPos(9,info_4)
  594. new_text=read()
  595. edit_label(clicked_connection,info_4-2,new_text)
  596. end
  597.  
  598. end
  599.  
  600.  
  601. if cur_env=="settings" then
  602.  
  603. if button_check(44,17,51,19) then
  604. close_dropdown_menus()
  605. menu()
  606. end
  607.  
  608. if button_check(3,3,12,5) then
  609. if settings["solo"]["status"]=="on" then
  610. change_setting("solo","off")
  611. settings_menu()
  612. else
  613. change_setting("solo","on")
  614. settings_menu()
  615. end
  616. settings_menu()
  617. end
  618.  
  619. if dropdown_menu_check("mon_side")=="changed" then
  620. change_setting("monitor_side",dropdown["mon_side"]["option"..1])
  621. mon.clear()
  622.  
  623. mon=peripheral.wrap(settings["monitor_side"]["status"])
  624. mon.setTextScale(0.5)
  625. screen()
  626. end
  627.  
  628. end
  629.  
  630. end
  631. info_1=nil
  632. end
  633.  
  634.  
  635.  
  636. function settings_menu()
  637. cur_env="settings"
  638. info_1=nil
  639.  
  640. term.clear()
  641. headline("Settings")
  642. button("Back",44,17,51,19)
  643. if settings["solo"]["status"] == "on" then
  644. button("Solo",3,3,12,5)
  645. else
  646. button("Solo",3,3,12,5,colors.red)
  647. end
  648.  
  649. term.setCursorPos(3,7)
  650. term.setTextColor(colors.green)
  651. term.write("Monitor:")
  652. load_dropdown_menu("mon_side")
  653.  
  654. end
  655.  
  656.  
  657.  
  658. function create_setting(name)
  659.  
  660. settings[name] = {}
  661.  
  662. settings["len"]=settings["len"]+1
  663.  
  664. settings["names"][settings["len"]] = name
  665.  
  666. settings[name]["number"] = settings["len"]
  667.  
  668. end
  669.  
  670.  
  671.  
  672. function create_dropdown_menu(name,options,pos_x1,pos_y,pos_x2)
  673.  
  674. dropdown[name] = {}
  675. dropdown[name]["pos_x1"] = pos_x1
  676. dropdown[name]["pos_x2"] = pos_x2
  677. dropdown[name]["pos_y"] = pos_y
  678. dropdown[name]["is_open"] = false
  679. dropdown[name]["len"] = 0
  680.  
  681. i=1
  682. while options[i]~= nil do
  683. dropdown[name]["len"] = dropdown[name]["len"]+1
  684. dropdown[name]["option"..i] = options[i]
  685. i=i+1
  686. end
  687.  
  688. end
  689.  
  690.  
  691.  
  692. function print_dropdown_menu(name)
  693.  
  694. for i=1,dropdown[name]["len"] do
  695. term.setCursorPos(dropdown[name]["pos_x1"],dropdown[name]["pos_y"]+i-1)
  696.  
  697. if not (i%2==0) then
  698. term.setBackgroundColor(colors.gray)
  699. term.setTextColor(colors.white)
  700. else
  701. term.setBackgroundColor(colors.lightGray)
  702. term.setTextColor(colors.black)
  703. end
  704.  
  705. if dropdown[name]["is_open"] or i==1 then
  706. term.write(dropdown[name]["option"..i])
  707. for i1=1,(dropdown[name]["pos_x2"]-dropdown[name]["pos_x1"])-string.len(dropdown[name]["option"..i]) do
  708. term.write(" ")
  709. end
  710. end
  711. end
  712.  
  713. term.setBackgroundColor(colors.black)
  714. term.setTextColor(colors.white)
  715. end
  716.  
  717.  
  718.  
  719. function dropdown_menu_check(name)
  720.  
  721. if dropdown[name]["is_open"] then
  722.  
  723. if info_1=="dropdown_menu" and info_3 < dropdown[name]["pos_x2"]+1 and info_3 > dropdown[name]["pos_x1"]-1 and info_4==dropdown[name]["pos_y"] then
  724. dropdown[name]["is_open"]=false
  725. print_menu(cur_env)
  726. return "closed"
  727. end
  728.  
  729. if info_1=="dropdown_menu" and info_3 < dropdown[name]["pos_x2"]+1 and info_3 > dropdown[name]["pos_x1"]-1 and info_4 < dropdown[name]["pos_y"]+dropdown[name]["len"]+1 and info_4 > dropdown[name]["pos_y"] then
  730. option_save = dropdown[name]["option"..1]
  731. dropdown[name]["option"..1] = dropdown[name]["option"..info_4-dropdown[name]["pos_y"]+1]
  732. dropdown[name]["option"..info_4-dropdown[name]["pos_y"]+1] = option_save
  733. dropdown[name]["is_open"]=false
  734. print_menu(cur_env)
  735. return "changed"
  736. end
  737.  
  738. else
  739.  
  740. if info_1=="dropdown_menu" and info_3 < dropdown[name]["pos_x2"]+1 and info_3 > dropdown[name]["pos_x1"]-1 and info_4==dropdown[name]["pos_y"] then
  741. dropdown[name]["is_open"]=true
  742. print_dropdown_menu(name)
  743. return "opened"
  744. end
  745.  
  746. end
  747.  
  748. end
  749.  
  750.  
  751.  
  752. function load_dropdown_menu(name)
  753. dropdown["loaded"]["len"] = dropdown["loaded"]["len"]+1
  754. dropdown["loaded"][dropdown["loaded"]["len"]] = name
  755. print_dropdown_menu(name)
  756. end
  757.  
  758.  
  759.  
  760. function close_dropdown_menus()
  761. dropdown["loaded"]["len"]=0
  762. end
  763.  
  764.  
  765.  
  766. function overwrite_dropdown()
  767.  
  768. for i=1,dropdown["loaded"]["len"] do
  769.  
  770. if not dropdown[dropdown["loaded"][i]]["is_open"] then
  771.  
  772. if info_1=="mouse_click" and info_3 < dropdown[dropdown["loaded"][i]]["pos_x2"]+1 and info_3 > dropdown[dropdown["loaded"][i]]["pos_x1"]-1 and info_4==dropdown[dropdown["loaded"][i]]["pos_y"] then
  773. info_1 = "dropdown_menu"
  774. end
  775.  
  776. else
  777.  
  778. if info_1=="mouse_click" and info_3 < dropdown[dropdown["loaded"][i]]["pos_x2"]+1 and info_3 > dropdown[dropdown["loaded"][i]]["pos_x1"]-1 and info_4 < dropdown[dropdown["loaded"][i]]["pos_y"]+dropdown[dropdown["loaded"][i]]["len"] and info_4 > dropdown[dropdown["loaded"][i]]["pos_y"]-1 then
  779. info_1 = "dropdown_menu"
  780. end
  781.  
  782. end
  783.  
  784. end
  785. end
  786.  
  787.  
  788.  
  789. function print_menu(env)
  790.  
  791. if env=="menu" then
  792. menu()
  793. end
  794.  
  795. if env=="log" then
  796. log()
  797. end
  798.  
  799. if env=="connection_menu" then
  800. connection_menu()
  801. end
  802.  
  803. if env=="change_name" then
  804. change_name()
  805. end
  806.  
  807. if env=="connection_settings" then
  808. connection_settings(clicked_connection)
  809. end
  810.  
  811. if env=="label_settings" then
  812. label_settings(clicked_connection)
  813. end
  814.  
  815. if env=="settings" then
  816. settings_menu()
  817. end
  818.  
  819. end
  820.  
  821.  
  822.  
  823. function assign_settings()
  824.  
  825. settings_save = fs.open("settings","r")
  826.  
  827. for i=1,settings["len"] do
  828. settings[i] = {}
  829. settings[settings["names"][i]]["status"] = settings_save.readLine()
  830. settings[i]["status"] = settings[settings["names"][i]]["status"]
  831. end
  832. settings_save.close()
  833. end
  834.  
  835.  
  836.  
  837. function change_setting(name,new_status)
  838.  
  839. settings_save = fs.open("settings","w")
  840.  
  841. for i=1,settings[name]["number"]-1 do
  842. settings_save.writeLine(settings[i]["status"])
  843. end
  844.  
  845. settings_save.writeLine(new_status)
  846.  
  847. for i=1,settings["len"]-settings[name]["number"] do
  848. settings_save.writeLine(settings[i+settings[name]["number"]]["status"])
  849. end
  850.  
  851. settings_save.flush()
  852. settings_save.close()
  853.  
  854. assign_settings()
  855. end
  856.  
  857.  
  858.  
  859. function change_connection_order(changing,change_to)
  860.  
  861. connection_save_name = connected_pcs["name"][changing]
  862. connection_save_id = connected_pcs["id"][changing]
  863. status_save = button_positions["status"][changing]
  864. for i=1,3 do
  865. label_save[i] = connected_pcs["labels"]["line"..i][changing]
  866. end
  867.  
  868. connected_pcs["name"][changing] = connected_pcs["name"][change_to]
  869. connected_pcs["id"][changing] = connected_pcs["id"][change_to]
  870. button_positions["status"][changing] = button_positions["status"][change_to]
  871. for i=1,3 do
  872. connected_pcs["labels"]["line"..i][changing] = connected_pcs["labels"]["line"..i][change_to]
  873. end
  874.  
  875. connected_pcs["name"][change_to] = connection_save_name
  876. connected_pcs["id"][change_to] = connection_save_id
  877. button_positions["status"][change_to] = status_save
  878. for i=1,3 do
  879. connected_pcs["labels"]["line"..i][change_to] = label_save[i]
  880. end
  881.  
  882.  
  883. connections = fs.open("connections","w")
  884.  
  885. for i=1,connected_pcs["len"] do
  886. connections.writeLine(connected_pcs["name"][i])
  887. connections.writeLine(connected_pcs["id"][i])
  888. end
  889. connections.flush()
  890. connections.close()
  891.  
  892. if clicked_connection==changing then
  893. clicked_connection=change_to
  894. end
  895.  
  896. assign_connections()
  897. change_connection_menu=true
  898. screen()
  899. end
  900.  
  901.  
  902.  
  903. function connection_menu()
  904.  
  905. if cur_env~="connection_menu" then
  906. info_1=nil
  907. end
  908.  
  909. cur_env="connection_menu"
  910.  
  911. term.setBackgroundColor(colors.black)
  912. term.clear()
  913. headline("Connections")
  914. button("Back",44,17,51,19)
  915. button("Connect",34,17,42,19)
  916. term.setCursorPos(1,4)
  917.  
  918. for i=1,connected_pcs["len"] do
  919.  
  920. term.setTextColor(colors.green)
  921. term.write("[Id-"..tonumber(connected_pcs["id"][i]).."]")
  922.  
  923. for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][i]).."]") do
  924. write(" ")
  925. end
  926.  
  927. term.setTextColor(colors.white)
  928. print(connected_pcs["name"][i])
  929.  
  930. end
  931.  
  932. end
  933.  
  934.  
  935.  
  936. function assign_name()
  937.  
  938. connection_code = fs.open("connection_code","r")
  939. connection_name = connection_code.readLine()
  940. connection_code.close()
  941.  
  942. end
  943.  
  944.  
  945.  
  946. function change_name()
  947.  
  948. cur_env="change_name"
  949.  
  950. term.clear()
  951. term.setCursorPos(1,1)
  952. term.setTextColor(colors.green)
  953. print("Please enter the name you want to use to connect")
  954. print("to other PC´s")
  955. print()
  956. term.write("Old name: ")
  957. term.setBackgroundColor(colors.white)
  958. term.setTextColor(colors.blue)
  959. print(" "..connection_name.." ")
  960. print()
  961. term.setTextColor(colors.white)
  962. term.setBackgroundColor(colors.black)
  963. new_name=read()
  964.  
  965. term.clear()
  966. term.setCursorPos(1,1)
  967. term.setTextColor(colors.green)
  968. write("Changing name...")
  969.  
  970.  
  971. for i=1,connected_pcs["len"] do
  972. rednet.send(tonumber(connected_pcs["id"][i]),{["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]="get_info"})
  973. end
  974.  
  975. got_response=0
  976.  
  977. for i=1,connected_pcs["len"] do
  978. connected_pcs["got_response"][i]=false
  979. end
  980.  
  981. pull(1)
  982.  
  983.  
  984. if not (got_response < connected_pcs["len"]) then
  985.  
  986. for i=1,connected_pcs["len"] do
  987. rednet.send(tonumber(connected_pcs["id"][i]),{["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]="changing_name",["new_name"]=new_name})
  988. end
  989.  
  990. connection_name = new_name
  991. connection_code=fs.open("connection_code","w")
  992. connection_code.writeLine(connection_name)
  993. connection_code.flush()
  994. connection_code.close()
  995.  
  996. else
  997.  
  998. for i=1,connected_pcs["len"] do
  999. if not connected_pcs["got_response"][i] then
  1000. create_log_entry("The name could not be changed, because the PC named:, "..connected_pcs["name"][i]..", did not respond.")
  1001. end
  1002. end
  1003.  
  1004. term.clear()
  1005. term.setTextColor(colors.red)
  1006. term.setCursorPos(1,1)
  1007. term.write("ERROR,open log for more info")
  1008. pull(3)
  1009. menu()
  1010. end
  1011.  
  1012. menu()
  1013. end
  1014.  
  1015.  
  1016.  
  1017. function log()
  1018.  
  1019. cur_env="log"
  1020.  
  1021. term.setBackgroundColor(colors.black)
  1022. term.clear()
  1023. button("Back",44,17,51,19)
  1024. headline("Log")
  1025.  
  1026. term.setCursorPos(1,4)
  1027. term.setTextColor(colors.white)
  1028. term.setBackgroundColor(colors.black)
  1029.  
  1030. if logs["len"] < 13 then
  1031.  
  1032. for i=1,12 do
  1033.  
  1034. if logs["time"][i]~=nil then
  1035.  
  1036. term.setTextColor(colors.green)
  1037. write("[")
  1038. write(math.floor(logs["time"][i]))
  1039. write(":")
  1040. write(math.floor( math.floor( (logs["time"][i]-math.floor(logs["time"][i])) *100 ) /1.6666666 ) )
  1041. write("]")
  1042.  
  1043. 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
  1044. write(" ")
  1045. end
  1046.  
  1047. term.setTextColor(colors.white)
  1048. print(logs["entries"][i])
  1049.  
  1050. end
  1051.  
  1052. end
  1053.  
  1054. else
  1055.  
  1056. for i=1,12 do
  1057.  
  1058. if logs["time"][logs["len"]-12+i]~=nil then
  1059.  
  1060. term.setTextColor(colors.green)
  1061. write("[")
  1062. write(math.floor(logs["time"][logs["len"]-12+i]))
  1063. write(":")
  1064. write(math.floor( math.floor( (logs["time"][logs["len"]-12+i]-math.floor(logs["time"][logs["len"]-12+i])) *100 ) /1.6666666 ) )
  1065. write("]")
  1066.  
  1067. 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
  1068. write(" ")
  1069. end
  1070.  
  1071. term.setTextColor(colors.white)
  1072. print(logs["entries"][logs["len"]-12+i])
  1073.  
  1074. end
  1075.  
  1076. end
  1077.  
  1078. end
  1079.  
  1080. change_log=false
  1081. end
  1082.  
  1083.  
  1084.  
  1085. function assign_connections()
  1086.  
  1087. connections=fs.open("connections","r")
  1088.  
  1089. connected_pcs["name"]={}
  1090. connected_pcs["len"]=0
  1091. connected_pcs["id"]={}
  1092. connected_pcs["id"][0]="0"
  1093. i=0
  1094. while tonumber(connected_pcs["id"][i])~=nil do
  1095.  
  1096. i=i+1
  1097.  
  1098. connected_pcs["name"][i] = connections.readLine()
  1099. connected_pcs["id"][i] = connections.readLine()
  1100.  
  1101.  
  1102. if tonumber(connected_pcs["id"][i])~=nil then
  1103. connected_pcs["len"]=connected_pcs["len"]+1
  1104. connected_pcs["number"][connected_pcs["name"][i]]=i
  1105. connected_pcs["number_of_id"][tonumber(connected_pcs["id"][i])]=i
  1106. connected_pcs["id"][i]=tonumber(connected_pcs["id"][i])
  1107. end
  1108.  
  1109. end
  1110. connections.close()
  1111. assign_labels()
  1112. end
  1113.  
  1114.  
  1115.  
  1116. function disconnect(disconnect_number)
  1117. sleep(0.05)
  1118.  
  1119. if tonumber(connected_pcs["id"][disconnect_number])~=nil and connected_pcs["name"][disconnect_number]~=nil then
  1120.  
  1121. rednet.send(connected_pcs["id"][disconnect_number],{["name"]=connection_name,["sending_to"]=connected_pcs["name"][disconnect_number],["status"]="disconnected"})
  1122.  
  1123.  
  1124. connections = fs.open("connections","w")
  1125.  
  1126. for i=1,connected_pcs["len"]-1 do
  1127.  
  1128. if i < disconnect_number then
  1129. connections.writeLine(connected_pcs["name"][i])
  1130. connections.writeLine(connected_pcs["id"][i])
  1131. end
  1132.  
  1133. if i > disconnect_number-1 then
  1134. connections.writeLine(connected_pcs["name"][i+1])
  1135. connections.writeLine(connected_pcs["id"][i+1])
  1136. end
  1137.  
  1138. fs.delete("button label "..connected_pcs["name"][disconnect_number])
  1139.  
  1140. end
  1141. connections.flush()
  1142. connections.close()
  1143.  
  1144. create_log_entry("The PC named: "..connected_pcs["name"][disconnect_number].." was disconnect")
  1145.  
  1146. change_connection_menu=true
  1147.  
  1148. assign_connections()
  1149. assign_labels()
  1150. screen()
  1151. end
  1152. end
  1153.  
  1154.  
  1155.  
  1156. function create_log_entry(entry)
  1157.  
  1158. logs["len"]=logs["len"]+math.ceil((string.len(entry)+8)/51)
  1159. logs["entries"][logs["len"]]=entry
  1160. logs["time"][logs["len"]]=os.time()
  1161. change_log=true
  1162.  
  1163. end
  1164.  
  1165.  
  1166.  
  1167. function connection_settings(connection)
  1168. cur_env="connection_settings"
  1169.  
  1170. term.clear()
  1171. headline("Settings for: "..connected_pcs["name"][connection])
  1172. button("Button label",3,3,16,5)
  1173. button("Back",44,17,51,19)
  1174. end
  1175.  
  1176.  
  1177.  
  1178. function label_settings(connection)
  1179. cur_env="label_settings"
  1180. info_4=0
  1181.  
  1182. term.clear()
  1183. headline("Label settings for: "..connected_pcs["name"][connection])
  1184. button("Back",44,17,51,19)
  1185.  
  1186. for i=1,3 do
  1187. term.setCursorPos(1,2+i)
  1188. term.setTextColor(colors.green)
  1189. term.write("Line "..i..": ")
  1190. term.setTextColor(colors.white)
  1191. print(connected_pcs["labels"]["line"..i][connection])
  1192. end
  1193. end
  1194.  
  1195.  
  1196.  
  1197. function assign_labels()
  1198. connected_pcs["labels"]["len"]=0
  1199.  
  1200. for i=1,connected_pcs["len"] do
  1201.  
  1202. if fs.exists("button label "..connected_pcs["name"][i]) then
  1203. connected_pcs["labels"]["len"]=connected_pcs["labels"]["len"]+1
  1204.  
  1205. read_label = fs.open("button label "..connected_pcs["name"][i],"r")
  1206.  
  1207. connected_pcs["labels"]["line"..1][i] = read_label.readLine()
  1208. connected_pcs["labels"]["line"..2][i] = read_label.readLine()
  1209. connected_pcs["labels"]["line"..3][i] = read_label.readLine()
  1210.  
  1211. if connected_pcs["labels"]["line"..1][i]==nil and connected_pcs["labels"]["line"..2][i]==nil and connected_pcs["labels"]["line"..3][i]==nil then
  1212. connected_pcs["labels"]["is_nil"][i]=true
  1213. else
  1214. connected_pcs["labels"]["is_nil"][i]=false
  1215. end
  1216.  
  1217. read_label.close()
  1218.  
  1219. else
  1220. open_label = fs.open("button label "..connected_pcs["name"][i],"w")
  1221. open_label.flush()
  1222. open_label.close()
  1223.  
  1224. connected_pcs["labels"]["len"]=connected_pcs["labels"]["len"]+1
  1225.  
  1226. read_label = fs.open("button label "..connected_pcs["name"][i],"r")
  1227.  
  1228. connected_pcs["labels"]["line"..1][i] = read_label.readLine()
  1229. connected_pcs["labels"]["line"..2][i] = read_label.readLine()
  1230. connected_pcs["labels"]["line"..3][i] = read_label.readLine()
  1231.  
  1232. read_label.close()
  1233.  
  1234. end
  1235.  
  1236. end
  1237.  
  1238. end
  1239.  
  1240.  
  1241.  
  1242. function edit_label(connection,line,new_label)
  1243.  
  1244. read_label = fs.open("button label "..connected_pcs["name"][connection],"r")
  1245.  
  1246. for i=1,3 do
  1247. old_label[i] = read_label.readLine()
  1248. end
  1249.  
  1250. read_label.close()
  1251. write_label = fs.open("button label "..connected_pcs["name"][connection],"w")
  1252.  
  1253.  
  1254. for i=1,3 do
  1255.  
  1256. if i==line then
  1257. write_label.writeLine(new_label)
  1258. else
  1259. write_label.writeLine(old_label[i])
  1260. end
  1261. end
  1262. write_label.flush()
  1263. write_label.close()
  1264.  
  1265. assign_labels()
  1266. screen()
  1267. end
  1268.  
  1269.  
  1270.  
  1271. function headline(headline)
  1272. headline_lenght=string.len(" "..headline.." ")
  1273. headline_pos=27-headline_lenght/2
  1274. term.setCursorPos(headline_pos,1)
  1275. term.setBackgroundColor(colors.yellow)
  1276. term.setTextColor(colors.blue)
  1277. term.write(" "..headline.." ")
  1278. term.setBackgroundColor(colors.black)
  1279. term.setTextColor(colors.white)
  1280. end
  1281.  
  1282.  
  1283.  
  1284. function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
  1285. term.setTextColor(colors.green)
  1286.  
  1287. if button_color~=nil then
  1288. term.setTextColor(button_color)
  1289. end
  1290.  
  1291. square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  1292. 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)
  1293. term.write(button_text)
  1294. end
  1295.  
  1296.  
  1297.  
  1298. function mon_button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
  1299. mon.setTextColor(colors.green)
  1300.  
  1301. if button_color~=nil then
  1302. mon.setTextColor(button_color)
  1303. end
  1304.  
  1305. mon_square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  1306.  
  1307. if connected_pcs["labels"]["is_nil"][connected_pcs["number"][button_text]] then
  1308. 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)
  1309. mon.write(button_text)
  1310. else
  1311.  
  1312. for i=1,button_pos_y2-button_pos_y1-1 do
  1313. mon.setCursorPos(button_pos_x1+1,button_pos_y1+i)
  1314. mon.write(connected_pcs["labels"]["line"..i][connected_pcs["number"][button_text]])
  1315. end
  1316. end
  1317. end
  1318.  
  1319.  
  1320.  
  1321.  
  1322. function button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  1323. 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
  1324. return true
  1325. else
  1326. return false
  1327. end
  1328. end
  1329.  
  1330.  
  1331.  
  1332. function mon_button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  1333. 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
  1334. return true
  1335. else
  1336. return false
  1337. end
  1338. end
  1339.  
  1340.  
  1341.  
  1342.  
  1343. function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  1344. x_1=x1
  1345. y_1=y1
  1346. x_2=x2
  1347. y_2=y2
  1348. while x_1~=x2+1 do
  1349. term.setBackgroundColor(back_color_square)
  1350. term.setCursorPos(x_1,y_1)
  1351. if text_color~=nil then
  1352. term.setTextColor(text_color)
  1353. end
  1354. term.write(symbol_square_x)
  1355. x_1=x_1+1
  1356. end
  1357.  
  1358. while y_1~=y2-1 do
  1359. term.setCursorPos(x_1-1,y_1+1)
  1360. term.write(symbol_square_y)
  1361. y_1=y_1+1
  1362. end
  1363. x2=x2-1
  1364. while x_2~=x1-1 do
  1365. term.setCursorPos(x_2,y_2)
  1366. term.write(symbol_square_x)
  1367. x_2=x_2-1
  1368. end
  1369.  
  1370. while y_2~=y1+1 do
  1371. term.setCursorPos(x_2+1,y_2-1)
  1372. term.write(symbol_square_y)
  1373. y_2=y_2-1
  1374. end
  1375. term.setBackgroundColor(colors.black)
  1376. end
  1377.  
  1378.  
  1379.  
  1380.  
  1381. function mon_square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  1382. x_1=x1
  1383. y_1=y1
  1384. x_2=x2
  1385. y_2=y2
  1386. while x_1~=x2+1 do
  1387. mon.setBackgroundColor(back_color_square)
  1388. mon.setCursorPos(x_1,y_1)
  1389. if text_color~=nil then
  1390. term.setTextColor(text_color)
  1391. end
  1392. mon.write(symbol_square_x)
  1393. x_1=x_1+1
  1394. end
  1395.  
  1396. while y_1~=y2-1 do
  1397. mon.setCursorPos(x_1-1,y_1+1)
  1398. mon.write(symbol_square_y)
  1399. y_1=y_1+1
  1400. end
  1401. x2=x2-1
  1402. while x_2~=x1-1 do
  1403. mon.setCursorPos(x_2,y_2)
  1404. mon.write(symbol_square_x)
  1405. x_2=x_2-1
  1406. end
  1407.  
  1408. while y_2~=y1+1 do
  1409. mon.setCursorPos(x_2+1,y_2-1)
  1410. mon.write(symbol_square_y)
  1411. y_2=y_2-1
  1412. end
  1413. mon.setBackgroundColor(colors.black)
  1414. end
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421. startup()
  1422. menu()
  1423. screen()
  1424. pull()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement