Advertisement
Chaos_Cash

Connection main test

Feb 7th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.13 KB | None | 0 0
  1. function startup()
  2.  
  3. rednet.open("top")
  4.  
  5. connections=fs.open("connections","a")
  6.  
  7. connections.close()
  8.  
  9.  
  10.  
  11. if not fs.exists("connection_code") then
  12.  
  13. term.clear()
  14. term.setCursorPos(1,1)
  15. term.setTextColor(colors.green)
  16. print("Please enter the name you want to use to connect")
  17. print("to other PC´s")
  18. print()
  19. term.setTextColor(colors.white)
  20. connection_name=read()
  21.  
  22. connection_code=fs.open("connection_code","w")
  23. connection_code.writeLine(connection_name)
  24. connection_code.flush()
  25. connection_code.close()
  26.  
  27. end
  28.  
  29. assign_name()
  30.  
  31. mon=peripheral.wrap("right")
  32. mon.setTextScale(0.5)
  33.  
  34.  
  35. connected_pcs={}
  36. connected_pcs["len"]=0
  37.  
  38. button_positions={}
  39.  
  40. logs={}
  41. logs["len"]=0
  42. logs["entries"]={}
  43. logs["time"]={}
  44.  
  45.  
  46. assign_connections()
  47.  
  48.  
  49.  
  50. end
  51.  
  52.  
  53.  
  54.  
  55. function screen()
  56.  
  57. cur_x1=2
  58. cur_y1=2
  59. cur_x2=10
  60. cur_y2=4
  61. cur_connection=1
  62. button_positions["len"]=0
  63.  
  64. mon.clear()
  65.  
  66. for i=1,math.ceil(connected_pcs["len"]^0.5) do
  67.  
  68. for i1=1,math.ceil(connected_pcs["len"]^0.5) do
  69. if connected_pcs["name"][cur_connection]~=nil then
  70. mon_button(connected_pcs["name"][cur_connection],cur_x1,cur_y1,cur_x2,cur_y2)
  71. button_positions[cur_connection]={}
  72. button_positions[cur_connection]={["x1"]=cur_x1,["y1"]=cur_y1,["x2"]=cur_x2,["y2"]=cur_y2,["id"]=connected_pcs["id"][cur_connection],["status"]="on"}
  73. button_positions["len"]=button_positions["len"]+1
  74. end
  75. cur_x1=cur_x1+10
  76. cur_x2=cur_x2+10
  77. cur_connection=cur_connection+1
  78. end
  79.  
  80. cur_y1=cur_y1+4
  81. cur_y2=cur_y2+4
  82.  
  83. cur_x1=2
  84. cur_x2=10
  85.  
  86. end
  87. end
  88.  
  89.  
  90.  
  91.  
  92. function menu()
  93.  
  94. cur_env="menu"
  95.  
  96. term.setBackgroundColor(colors.black)
  97. term.clear()
  98.  
  99. headline("Menu")
  100. button("Connections",12,7,25,9)
  101. button("Change name",27,7,40,9)
  102. button("Settings",27,11,40,13)
  103. button("Log",12,11,25,13)
  104.  
  105. pull()
  106.  
  107. end
  108.  
  109.  
  110.  
  111.  
  112. function pull()
  113.  
  114. while true do
  115.  
  116. info_1, info_2, info_3, info_4 = os.pullEvent()
  117.  
  118. if info_1=="rednet_message" and info_3["connecting_to"]==connection_name then
  119.  
  120. already_connected=false
  121.  
  122. for i=1,connected_pcs["len"] do
  123.  
  124. if tonumber(connected_pcs["id"][i])==tonumber(info_2) then
  125. already_connected=true
  126. end
  127. end
  128.  
  129. if not already_connected then
  130.  
  131. connections = fs.open("connections","a")
  132. connections.writeLine(info_3["name"])
  133. connections.writeLine(info_2)
  134. connections.close()
  135. assign_connections()
  136.  
  137. logs["len"]=logs["len"]+math.ceil((string.len("Connected to the PC named: "..info_3["name"])+8)/51)
  138. logs["entries"][logs["len"]]="Connected to the PC named: "..info_3["name"]
  139. logs["time"][logs["len"]]=os.time()
  140. change_log=true
  141. screen()
  142.  
  143. else
  144.  
  145. logs["len"]=logs["len"]+math.ceil((string.len("rejected PC because was already connected, name: "..info_3["name"])+8)/51)
  146. logs["entries"][logs["len"]]="rejected PC because was already connected, name: "..info_3["name"]
  147. logs["time"][logs["len"]]=os.time()
  148. change_log=true
  149.  
  150. end
  151. end
  152.  
  153.  
  154. for i=1,button_positions["len"] do
  155.  
  156. if mon_button_check(button_positions[i]["x1"],button_positions[i]["y1"],button_positions[i]["x2"],button_positions[i]["y2"]) then
  157. if button_positions[i]["status"]=="on" then
  158. mon_button(connected_pcs["name"][i],button_positions[i]["x1"],button_positions[i]["y1"],button_positions[i]["x2"],button_positions[i]["y2"],"red")
  159. button_positions[i]["status"]="off"
  160. rednet.send(tonumber(connected_pcs["id"][i]),{["sending_to"]=connected_pcs["name"][i],["status"]=button_positions[i]["status"]})
  161. else
  162. mon_button(connected_pcs["name"][i],button_positions[i]["x1"],button_positions[i]["y1"],button_positions[i]["x2"],button_positions[i]["y2"])
  163. button_positions[i]["status"]="on"
  164. rednet.send(tonumber(connected_pcs["id"][i]),{["sending_to"]=connected_pcs["name"][i],["status"]=button_positions[i]["status"]})
  165. end
  166. end
  167.  
  168. end
  169.  
  170.  
  171. if cur_env=="menu" then
  172.  
  173. if button_check(27,7,40,9) then
  174. change_name()
  175. end
  176.  
  177. if button_check(12,11,25,13) then
  178. log()
  179. end
  180.  
  181. if button_check(12,7,25,9) then
  182. connection_menu()
  183. end
  184.  
  185. end
  186.  
  187. if cur_env=="log" then
  188.  
  189. if change_log then
  190. log()
  191. end
  192.  
  193. if button_check(44,17,51,19) then
  194. menu()
  195. end
  196.  
  197. end
  198.  
  199. if cur_env=="connection_menu" then
  200.  
  201. if button_check(44,17,51,19) then
  202. menu()
  203. end
  204.  
  205. end
  206.  
  207. end
  208. end
  209.  
  210.  
  211.  
  212. function connection_menu()
  213.  
  214. cur_env="connection_menu"
  215.  
  216. term.clear()
  217. headline("Connections")
  218. button("Back",44,17,51,19)
  219. term.setCursorPos(1,4)
  220.  
  221. for i=1,connected_pcs["len"] do
  222.  
  223. term.setTextColor(colors.green)
  224. term.write("[Id-"..tonumber(connected_pcs["id"][i]).."]")
  225.  
  226. for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][i]).."]") do
  227. write(" ")
  228. end
  229.  
  230. term.setTextColor(colors.white)
  231. print(connected_pcs["name"][i])
  232.  
  233. end
  234.  
  235. end
  236.  
  237.  
  238.  
  239. function assign_name()
  240.  
  241. connection_code = fs.open("connection_code","r")
  242. connection_name = connection_code.readLine()
  243. connection_code.close()
  244.  
  245. end
  246.  
  247.  
  248.  
  249. function change_name()
  250.  
  251. cur_env="change_name"
  252.  
  253. term.clear()
  254. term.setCursorPos(1,1)
  255. term.setTextColor(colors.green)
  256. print("Please enter the name you want to use to connect")
  257. print("to other PC´s")
  258. print()
  259. term.write("Old name: ")
  260. term.setBackgroundColor(colors.white)
  261. term.setTextColor(colors.blue)
  262. print(" "..connection_name.." ")
  263. print()
  264. term.setTextColor(colors.white)
  265. term.setBackgroundColor(colors.black)
  266. connection_name=read()
  267.  
  268. connection_code=fs.open("connection_code","w")
  269. connection_code.writeLine(connection_name)
  270. connection_code.flush()
  271. connection_code.close()
  272.  
  273. menu()
  274. end
  275.  
  276.  
  277.  
  278. function log()
  279.  
  280. cur_env="log"
  281.  
  282. term.setBackgroundColor(colors.black)
  283. term.clear()
  284. button("Back",44,17,51,19)
  285. headline("Log")
  286.  
  287. term.setCursorPos(1,4)
  288. term.setTextColor(colors.white)
  289. term.setBackgroundColor(colors.black)
  290.  
  291. if logs["len"] < 13 then
  292.  
  293. for i=1,12 do
  294.  
  295. if logs["time"][i]~=nil then
  296.  
  297. term.setTextColor(colors.green)
  298. write("[")
  299. write(math.floor(logs["time"][i]))
  300. write(":")
  301. write(math.floor( math.floor( (logs["time"][i]-math.floor(logs["time"][i])) *100 ) /1.6666666 ) )
  302. write("]")
  303.  
  304. 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
  305. write(" ")
  306. end
  307.  
  308. term.setTextColor(colors.white)
  309. print(logs["entries"][i])
  310.  
  311. end
  312.  
  313. end
  314.  
  315. else
  316.  
  317. for i=1,12 do
  318.  
  319. if logs["time"][logs["len"]-12+i]~=nil then
  320.  
  321. term.setTextColor(colors.green)
  322. write("[")
  323. write(math.floor(logs["time"][logs["len"]-12+i]))
  324. write(":")
  325. write(math.floor( math.floor( (logs["time"][logs["len"]-12+i]-math.floor(logs["time"][logs["len"]-12+i])) *100 ) /1.6666666 ) )
  326. write("]")
  327.  
  328. 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
  329. write(" ")
  330. end
  331.  
  332. term.setTextColor(colors.white)
  333. print(logs["entries"][logs["len"]-12+i])
  334.  
  335. end
  336.  
  337. end
  338.  
  339. end
  340.  
  341. change_log=false
  342. end
  343.  
  344.  
  345.  
  346. function assign_connections()
  347.  
  348. connections=fs.open("connections","r")
  349.  
  350. connected_pcs["name"]={}
  351. connected_pcs["len"]=0
  352. connected_pcs["id"]={}
  353. connected_pcs["id"][0]="0"
  354. i=0
  355. while tonumber(connected_pcs["id"][i])~=nil do
  356.  
  357. i=i+1
  358.  
  359. connected_pcs["name"][i] = connections.readLine()
  360. connected_pcs["id"][i] = connections.readLine()
  361.  
  362. if tonumber(connected_pcs["id"][i])~=nil then
  363. connected_pcs["len"]=connected_pcs["len"]+1
  364. end
  365.  
  366. end
  367. connections.close()
  368. end
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375. function headline(headline)
  376. headline_lenght=string.len(" "..headline.." ")
  377. headline_pos=27-headline_lenght/2
  378. term.setCursorPos(headline_pos,1)
  379. term.setBackgroundColor(colors.yellow)
  380. term.setTextColor(colors.blue)
  381. term.write(" "..headline.." ")
  382. term.setBackgroundColor(colors.black)
  383. term.setTextColor(colors.white)
  384. end
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392. function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2)
  393. term.setTextColor(colors.green)
  394. square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  395. 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)
  396. term.write(button_text)
  397. end
  398.  
  399.  
  400.  
  401. function mon_button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
  402. mon.setTextColor(colors.green)
  403.  
  404. if button_color=="red" then
  405. mon.setTextColor(colors.red)
  406. end
  407.  
  408. mon_square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  409. 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)
  410. mon.write(button_text)
  411. end
  412.  
  413.  
  414.  
  415.  
  416. function button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  417. 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
  418. return true
  419. else
  420. return false
  421. end
  422. end
  423.  
  424.  
  425.  
  426. function mon_button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  427. 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
  428. return true
  429. else
  430. return false
  431. end
  432. end
  433.  
  434.  
  435.  
  436.  
  437. function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  438. x_1=x1
  439. y_1=y1
  440. x_2=x2
  441. y_2=y2
  442. while x_1~=x2+1 do
  443. term.setBackgroundColor(back_color_square)
  444. term.setCursorPos(x_1,y_1)
  445. if text_color~=nil then
  446. term.setTextColor(text_color)
  447. end
  448. term.write(symbol_square_x)
  449. x_1=x_1+1
  450. end
  451.  
  452. while y_1~=y2-1 do
  453. term.setCursorPos(x_1-1,y_1+1)
  454. term.write(symbol_square_y)
  455. y_1=y_1+1
  456. end
  457. x2=x2-1
  458. while x_2~=x1-1 do
  459. term.setCursorPos(x_2,y_2)
  460. term.write(symbol_square_x)
  461. x_2=x_2-1
  462. end
  463.  
  464. while y_2~=y1+1 do
  465. term.setCursorPos(x_2+1,y_2-1)
  466. term.write(symbol_square_y)
  467. y_2=y_2-1
  468. end
  469. term.setBackgroundColor(colors.black)
  470. end
  471.  
  472.  
  473.  
  474.  
  475. function mon_square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  476. x_1=x1
  477. y_1=y1
  478. x_2=x2
  479. y_2=y2
  480. while x_1~=x2+1 do
  481. mon.setBackgroundColor(back_color_square)
  482. mon.setCursorPos(x_1,y_1)
  483. if text_color~=nil then
  484. term.setTextColor(text_color)
  485. end
  486. mon.write(symbol_square_x)
  487. x_1=x_1+1
  488. end
  489.  
  490. while y_1~=y2-1 do
  491. mon.setCursorPos(x_1-1,y_1+1)
  492. mon.write(symbol_square_y)
  493. y_1=y_1+1
  494. end
  495. x2=x2-1
  496. while x_2~=x1-1 do
  497. mon.setCursorPos(x_2,y_2)
  498. mon.write(symbol_square_x)
  499. x_2=x_2-1
  500. end
  501.  
  502. while y_2~=y1+1 do
  503. mon.setCursorPos(x_2+1,y_2-1)
  504. mon.write(symbol_square_y)
  505. y_2=y_2-1
  506. end
  507. mon.setBackgroundColor(colors.black)
  508. end
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515. startup()
  516. screen()
  517. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement