Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function startup()
- connected_pcs={}
- connected_pcs["len"]=0
- button_positions={}
- button_positions["status"]={}
- connected_pcs["number"]={}
- connected_pcs["number_of_id"]={}
- rednet.open("top")
- connections=fs.open("connections","a")
- connections.close()
- if not fs.exists("connection_code") then
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.green)
- print("Please enter the name you want to use to connect")
- print("to other PC´s")
- print()
- term.setTextColor(colors.white)
- connection_name=read()
- connection_code=fs.open("connection_code","w")
- connection_code.writeLine(connection_name)
- connection_code.flush()
- connection_code.close()
- end
- assign_name()
- mon=peripheral.wrap("right")
- mon.setTextScale(0.5)
- logs={}
- logs["len"]=0
- logs["entries"]={}
- logs["time"]={}
- assign_connections()
- for i=1,connected_pcs["len"] do
- rednet.send(tonumber(connected_pcs["id"][i]),{["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]="get_info"})
- end
- end
- function screen()
- cur_x1=1
- cur_y1=1
- cur_x2=9
- cur_y2=3
- cur_connection=1
- button_positions["len"]=0
- mon.clear()
- for i=1,math.ceil(connected_pcs["len"]^0.5) do
- for i1=1,math.ceil(connected_pcs["len"]^0.5) do
- if connected_pcs["name"][cur_connection]~=nil then
- if button_positions["status"][cur_connection]=="on" then
- mon_button(connected_pcs["name"][cur_connection],cur_x1,cur_y1,cur_x2,cur_y2)
- end
- if button_positions["status"][cur_connection]=="off" then
- mon_button(connected_pcs["name"][cur_connection],cur_x1,cur_y1,cur_x2,cur_y2,colors.red)
- end
- if button_positions["status"][cur_connection]==nil then
- mon_button(connected_pcs["name"][cur_connection],cur_x1,cur_y1,cur_x2,cur_y2,colors.blue)
- end
- button_positions[cur_connection]={}
- button_positions[cur_connection]={["x1"]=cur_x1,["y1"]=cur_y1,["x2"]=cur_x2,["y2"]=cur_y2,["id"]=connected_pcs["id"][cur_connection],["status"]="on"}
- button_positions["len"]=button_positions["len"]+1
- end
- cur_x1=cur_x1+10
- cur_x2=cur_x2+10
- cur_connection=cur_connection+1
- end
- cur_y1=cur_y1+3
- cur_y2=cur_y2+3
- cur_x1=1
- cur_x2=9
- end
- end
- function menu()
- cur_env="menu"
- term.setBackgroundColor(colors.black)
- term.clear()
- headline("Menu")
- button("Connections",12,7,25,9)
- button("Change name",27,7,40,9)
- button("Settings",27,11,40,13)
- button("Log",12,11,25,13)
- end
- function pull()
- while true do
- last_info_1 = info_1
- last_info_2 = info_2
- last_info_3 = info_3
- last_info_4 = info_4
- info_1, info_2, info_3, info_4 = os.pullEvent()
- if info_1=="rednet_message" and info_3["sending_to"]==connection_name then
- if info_3["sending_status"] then
- button_positions["status"][connected_pcs["number"][info_3["name"]]]=info_3["status"]
- screen()
- end
- end
- if info_1=="rednet_message" and info_3["connecting_to"]==connection_name then
- already_connected=false
- for i=1,connected_pcs["len"] do
- if tonumber(connected_pcs["id"][i])==tonumber(info_2) then
- already_connected=true
- end
- end
- if not already_connected then
- connections = fs.open("connections","a")
- connections.writeLine(info_3["name"])
- connections.writeLine(info_2)
- connections.close()
- assign_connections()
- logs["len"]=logs["len"]+math.ceil((string.len("Connected to the PC named: "..info_3["name"])+8)/51)
- logs["entries"][logs["len"]]="Connected to the PC named: "..info_3["name"]
- logs["time"][logs["len"]]=os.time()
- change_log=true
- change_connection_menu=true
- screen()
- rednet.send(info_2,{["name"]=connection_name,["connecting_to"]=info_3["name"]})
- else
- logs["len"]=logs["len"]+math.ceil((string.len("rejected PC because was already connected, name: "..info_3["name"])+8)/51)
- logs["entries"][logs["len"]]="rejected PC because was already connected, name: "..info_3["name"]
- logs["time"][logs["len"]]=os.time()
- change_log=true
- end
- end
- for i=1,button_positions["len"] do
- if mon_button_check(button_positions[i]["x1"],button_positions[i]["y1"],button_positions[i]["x2"],button_positions[i]["y2"]) then
- rednet.send(tonumber(connected_pcs["id"][i]),{["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]="change_status"})
- end
- end
- if cur_env=="menu" then
- if button_check(27,7,40,9) then
- change_name()
- end
- if button_check(12,11,25,13) then
- log()
- end
- if button_check(12,7,25,9) then
- connection_menu()
- end
- end
- if cur_env=="log" then
- if change_log then
- log()
- end
- if button_check(44,17,51,19) then
- menu()
- end
- end
- if cur_env=="connection_menu" then
- if button_check(44,17,51,19) then
- menu()
- end
- for i=1,connected_pcs["len"] do
- if info_1=="mouse_click" and info_4==i+3 then
- connection_menu()
- term.setBackgroundColor(colors.red)
- term.setCursorPos(1,i+3)
- term.setTextColor(colors.green)
- term.write("[Id-"..tonumber(connected_pcs["id"][i]).."]")
- for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][i]).."]") do
- write(" ")
- end
- term.setTextColor(colors.white)
- term.write(connected_pcs["name"][i])
- for i1=1,51-string.len(connected_pcs["name"][i])-9 do
- term.write(" ")
- end
- button("Delete",1,17,10,19,colors.red)
- if i~=clicked_connection then
- clicked_connection=i
- else
- clicked_connection=nil
- connection_menu()
- end
- end
- end
- if clicked_connection~=nil then
- if button_check(1,17,10,19) then
- rednet.send(tonumber(connected_pcs["id"][clicked_connection]),{["sending_to"]=connected_pcs["name"][clicked_connection],["status"]="disconnected"})
- disconnect(clicked_connection)
- clicked_connection=nil
- connection_menu()
- end
- end
- if change_connection_menu then
- connection_menu()
- if clicked_connection~=nil then
- term.setBackgroundColor(colors.red)
- term.setCursorPos(1,clicked_connection+3)
- term.setTextColor(colors.green)
- term.write("[Id-"..tonumber(connected_pcs["id"][clicked_connection]).."]")
- for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][clicked_connection]).."]") do
- write(" ")
- end
- term.setTextColor(colors.white)
- term.write(connected_pcs["name"][clicked_connection])
- for i1=1,51-string.len(connected_pcs["name"][clicked_connection])-9 do
- term.write(" ")
- end
- button("Delete",1,17,10,19,colors.red)
- end
- change_connection_menu=false
- end
- if clicked_connection~=nil then
- 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
- if info_4-3 < connected_pcs["len"]+1 and info_4-3>0 then
- change_connection_order(clicked_connection,info_4-3)
- end
- end
- end
- end
- end
- end
- function change_connection_order(changing,change_to)
- connection_save_name=connected_pcs["name"][changing]
- connection_save_id=connected_pcs["id"][changing]
- connected_pcs["name"][changing]=connected_pcs["name"][change_to]
- connected_pcs["id"][changing]=connected_pcs["id"][change_to]
- connected_pcs["name"][change_to]=connection_save_name
- connected_pcs["id"][change_to]=connection_save_id
- connections = fs.open("connections","w")
- for i=1,connected_pcs["len"] do
- connections.writeLine(connected_pcs["name"][i])
- connections.writeLine(connected_pcs["id"][i])
- end
- connections.flush()
- connections.close()
- if clicked_connection==changing then
- clicked_connection=change_to
- end
- screen()
- assign_connections()
- change_connection_menu=true
- end
- function connection_menu()
- cur_env="connection_menu"
- term.setBackgroundColor(colors.black)
- term.clear()
- headline("Connections")
- button("Back",44,17,51,19)
- term.setCursorPos(1,4)
- for i=1,connected_pcs["len"] do
- term.setTextColor(colors.green)
- term.write("[Id-"..tonumber(connected_pcs["id"][i]).."]")
- for i1=1,9-string.len("[Id-"..tonumber(connected_pcs["id"][i]).."]") do
- write(" ")
- end
- term.setTextColor(colors.white)
- print(connected_pcs["name"][i])
- end
- end
- function assign_name()
- connection_code = fs.open("connection_code","r")
- connection_name = connection_code.readLine()
- connection_code.close()
- end
- function change_name()
- cur_env="change_name"
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.green)
- print("Please enter the name you want to use to connect")
- print("to other PC´s")
- print()
- term.write("Old name: ")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.blue)
- print(" "..connection_name.." ")
- print()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- connection_name=read()
- connection_code=fs.open("connection_code","w")
- connection_code.writeLine(connection_name)
- connection_code.flush()
- connection_code.close()
- menu()
- end
- function log()
- cur_env="log"
- term.setBackgroundColor(colors.black)
- term.clear()
- button("Back",44,17,51,19)
- headline("Log")
- term.setCursorPos(1,4)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- if logs["len"] < 13 then
- for i=1,12 do
- if logs["time"][i]~=nil then
- term.setTextColor(colors.green)
- write("[")
- write(math.floor(logs["time"][i]))
- write(":")
- write(math.floor( math.floor( (logs["time"][i]-math.floor(logs["time"][i])) *100 ) /1.6666666 ) )
- write("]")
- 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
- write(" ")
- end
- term.setTextColor(colors.white)
- print(logs["entries"][i])
- end
- end
- else
- for i=1,12 do
- if logs["time"][logs["len"]-12+i]~=nil then
- term.setTextColor(colors.green)
- write("[")
- write(math.floor(logs["time"][logs["len"]-12+i]))
- write(":")
- write(math.floor( math.floor( (logs["time"][logs["len"]-12+i]-math.floor(logs["time"][logs["len"]-12+i])) *100 ) /1.6666666 ) )
- write("]")
- 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
- write(" ")
- end
- term.setTextColor(colors.white)
- print(logs["entries"][logs["len"]-12+i])
- end
- end
- end
- change_log=false
- end
- function assign_connections()
- connections=fs.open("connections","r")
- connected_pcs["name"]={}
- connected_pcs["len"]=0
- connected_pcs["id"]={}
- connected_pcs["id"][0]="0"
- i=0
- while tonumber(connected_pcs["id"][i])~=nil do
- i=i+1
- connected_pcs["name"][i] = connections.readLine()
- connected_pcs["id"][i] = connections.readLine()
- if tonumber(connected_pcs["id"][i])~=nil then
- connected_pcs["len"]=connected_pcs["len"]+1
- connected_pcs["number"][connected_pcs["name"][i]]=i
- connected_pcs["number_of_id"][connected_pcs["id"][i]]=i
- connected_pcs["id"][i]=tonumber(connected_pcs["id"][i])
- end
- end
- connections.close()
- end
- function disconnect(disconnect_number)
- connections = fs.open("connections","w")
- for i=1,connected_pcs["len"]-1 do
- if i < disconnect_number then
- connections.writeLine(connected_pcs["name"][i])
- connections.writeLine(connected_pcs["id"][i])
- end
- if i > disconnect_number-1 then
- connections.writeLine(connected_pcs["name"][i+1])
- connections.writeLine(connected_pcs["id"][i+1])
- end
- end
- connections.flush()
- connections.close()
- assign_connections()
- screen()
- end
- function headline(headline)
- headline_lenght=string.len(" "..headline.." ")
- headline_pos=27-headline_lenght/2
- term.setCursorPos(headline_pos,1)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.blue)
- term.write(" "..headline.." ")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
- term.setTextColor(colors.green)
- if button_color~=nil then
- term.setTextColor(button_color)
- end
- square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
- 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)
- term.write(button_text)
- end
- function mon_button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
- mon.setTextColor(colors.green)
- if button_color~=nil then
- mon.setTextColor(button_color)
- end
- mon_square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
- 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)
- mon.write(button_text)
- end
- function button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
- 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
- return true
- else
- return false
- end
- end
- function mon_button_check(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
- 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
- return true
- else
- return false
- end
- end
- function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
- x_1=x1
- y_1=y1
- x_2=x2
- y_2=y2
- while x_1~=x2+1 do
- term.setBackgroundColor(back_color_square)
- term.setCursorPos(x_1,y_1)
- if text_color~=nil then
- term.setTextColor(text_color)
- end
- term.write(symbol_square_x)
- x_1=x_1+1
- end
- while y_1~=y2-1 do
- term.setCursorPos(x_1-1,y_1+1)
- term.write(symbol_square_y)
- y_1=y_1+1
- end
- x2=x2-1
- while x_2~=x1-1 do
- term.setCursorPos(x_2,y_2)
- term.write(symbol_square_x)
- x_2=x_2-1
- end
- while y_2~=y1+1 do
- term.setCursorPos(x_2+1,y_2-1)
- term.write(symbol_square_y)
- y_2=y_2-1
- end
- term.setBackgroundColor(colors.black)
- end
- function mon_square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
- x_1=x1
- y_1=y1
- x_2=x2
- y_2=y2
- while x_1~=x2+1 do
- mon.setBackgroundColor(back_color_square)
- mon.setCursorPos(x_1,y_1)
- if text_color~=nil then
- term.setTextColor(text_color)
- end
- mon.write(symbol_square_x)
- x_1=x_1+1
- end
- while y_1~=y2-1 do
- mon.setCursorPos(x_1-1,y_1+1)
- mon.write(symbol_square_y)
- y_1=y_1+1
- end
- x2=x2-1
- while x_2~=x1-1 do
- mon.setCursorPos(x_2,y_2)
- mon.write(symbol_square_x)
- x_2=x_2-1
- end
- while y_2~=y1+1 do
- mon.setCursorPos(x_2+1,y_2-1)
- mon.write(symbol_square_y)
- y_2=y_2-1
- end
- mon.setBackgroundColor(colors.black)
- end
- startup()
- menu()
- screen()
- pull()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement