Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function startup()
- connected_pcs={}
- 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
- if not fs.exists("status") then
- status_save=fs.open("status","w")
- status_save.write("off")
- status_save.flush()
- status_save.close()
- end
- assign_status()
- assign_name()
- assign_connections()
- term.clear()
- for i=1,connected_pcs["len"] do
- rednet.send(connected_pcs["id"][i],{["sending_status"]=true,["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]=status})
- end
- if status=="on" then
- redstone.setOutput("bottom",true)
- redstone.setOutput("top",true)
- redstone.setOutput("back",true)
- redstone.setOutput("front",true)
- redstone.setOutput("right",true)
- redstone.setOutput("left",true)
- end
- if status=="off" then
- redstone.setOutput("bottom",false)
- redstone.setOutput("top",false)
- redstone.setOutput("back",false)
- redstone.setOutput("front",false)
- redstone.setOutput("right",false)
- redstone.setOutput("left",false)
- end
- end
- function pull()
- while true do
- info_1=0
- info_2=0
- info_3=0
- info_4=0
- info_1, info_2, info_3, info_4 = os.pullEvent()
- if info_1=="rednet_message" and info_3["sending_to"]==connection_name then
- connected=false
- for i=1,connected_pcs["len"] do
- if info_3["name"]==connected_pcs["name"][i] then
- connected=true
- end
- end
- if connected then
- if info_3["status"]=="changing_name" then
- connection_name = info_3["new_name"]
- connections = fs.open("connections","w")
- connections.writeLine(info_3["new_name"])
- connections.writeLine(info_2)
- connections.flush()
- connections.close()
- startup()
- end
- if info_3["status"]=="get_info" then
- rednet.send(info_2,{["sending_status"]=true,["sending_to"]=info_3["name"],["name"]=connection_name,["status"]=status})
- end
- if info_3["status"]=="change_status" then
- if status=="on" then
- status="off"
- change_status()
- else
- status="on"
- change_status()
- end
- for i=1,connected_pcs["len"] do
- rednet.send(connected_pcs["id"][i],{["sending_status"]=true,["sending_to"]=connected_pcs["name"][i],["name"]=connection_name,["status"]=status})
- end
- end
- if status=="on" then
- redstone.setOutput("bottom",true)
- redstone.setOutput("top",true)
- redstone.setOutput("back",true)
- redstone.setOutput("front",true)
- redstone.setOutput("right",true)
- redstone.setOutput("left",true)
- end
- if status=="off" then
- redstone.setOutput("bottom",false)
- redstone.setOutput("top",false)
- redstone.setOutput("back",false)
- redstone.setOutput("front",false)
- redstone.setOutput("right",false)
- redstone.setOutput("left",false)
- end
- if info_3["status"]=="disconnected" then
- connections=fs.open("connections","w")
- for i=1,connected_pcs["len"] do
- if connected_pcs["name"][i]~=info_3["name"] and tonumber(connected_pcs["id"][i])~=tonumber(info_2) then
- connections.writeLine(connected_pcs["name"][i])
- connections.writeLine(connected_pcs["id"][i])
- end
- end
- connections.flush()
- connections.close()
- assign_connections()
- if cur_env=="connection_menu" then
- change_connection_menu=true
- end
- end
- end
- end
- if info_1=="rednet_message" and info_3["connecting_to"]==connection_name then
- already_connected=false
- same_id=false
- same_name=false
- if info_3["id"]==nil then
- for i=1,connected_pcs["len"] do
- if tonumber(connected_pcs["id"][i])==tonumber(info_2) then
- same_id=true
- end
- if info_3["name"] == connected_pcs["name"][i] then
- same_name=true
- end
- end
- else
- for i=1,connected_pcs["len"] do
- if tonumber(connected_pcs["id"][i])==tonumber(info_3["id"]) then
- same_id=true
- end
- if info_3["name"] == connected_pcs["name"][i] then
- same_name=true
- end
- end
- end
- if not same_id and not same_name then
- connections = fs.open("connections","a")
- connections.writeLine(info_3["name"])
- if info_3["id"]==nil then
- connections.writeLine(info_2)
- else
- connections.writeLine(info_3["id"])
- end
- connections.close()
- assign_connections()
- change_connection_menu=true
- if info_3["id"]==nil then
- rednet.send(info_2,{["name"]=connection_name,["connecting_to"]=info_3["name"],["cur_status"]=status,["status"]=status})
- else
- rednet.send(info_3["id"],{["name"]=connection_name,["connecting_to"]=info_3["name"],["cur_status"]=status,["status"]=status})
- end
- -- elseif same_id then
- -- if info_3["id"]==nil then
- -- rednet.send(info_2,{["name"]=connection_name,["sending_to"]=info_3["name"],["status"]="disconnected",["same_id"]=true})
- -- else
- -- rednet.send(info_3["id"],{["name"]=connection_name,["sending_to"]=info_3["name"],["status"]="disconnected",["same_id"]=true})
- -- end
- -- elseif same_name then
- -- if info_3["id"]==nil then
- -- rednet.send(info_2,{["name"]=connection_name,["sending_to"]=info_3["name"],["status"]="disconnected",["same_name"]=true})
- -- else
- -- rednet.send(info_3["id"],{["name"]=connection_name,["sending_to"]=info_3["name"],["status"]="disconnected",["same_name"]=true})
- -- end
- end
- end
- if cur_env=="menu" then
- if button_check(12,7,25,9) then
- connection_menu()
- end
- end
- if cur_env=="connection_menu" then
- if button_check(44,17,51,19) then
- menu()
- end
- if button_check(34,17,42,19) then
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.green)
- print("Please enter the name of the PC you want to")
- print("connect to")
- print()
- term.setTextColor(colors.white)
- connecting_to=read()
- for i=1,1000 do
- rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name,["status"]=status})
- end
- connected=false
- while info_1~="timer" and not connected do
- timer=os.startTimer(5)
- info_1 ,info_2, info_3, info_4 = os.pullEvent()
- if info_1=="rednet_message" then
- if info_3["status"]=="disconnected" then
- fs.delete("connections")
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.green)
- if info_3["same_name"] then
- print("A PC with this name was already connected,")
- print("please use a different name.")
- fs.delete("connection_code")
- sleep(3)
- change_connection_menu=true
- end
- if info_3["same_id"] then
- print("A PC with this id was already connected,")
- print("please use a different PC.")
- sleep(3)
- change_connection_menu=true
- end
- end
- if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
- connections=fs.open("connections","a")
- connections.writeLine(connecting_to)
- connections.writeLine(info_2)
- connections.flush()
- connections.close()
- assign_connections()
- connected=true
- end
- end
- os.cancelTimer(timer)
- end
- info_1=0
- change_connection_menu=true
- 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",["name"]=connection_name})
- 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 menu()
- cur_env="menu"
- term.setBackgroundColor(colors.black)
- term.clear()
- headline("Menu")
- button("Connections",12,7,25,9)
- end
- function connection_menu()
- if cur_env~="connection_menu" then
- info_1=nil
- end
- cur_env="connection_menu"
- term.setBackgroundColor(colors.black)
- term.clear()
- headline("Connections")
- button("Back",44,17,51,19)
- button("Connect",34,17,42,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 change_status()
- status_save = fs.open("status","w")
- status_save.write(status)
- status_save.flush()
- status_save.close()
- end
- function assign_status()
- status_save = fs.open("status","r")
- status = status_save.readLine()
- status_save.close()
- 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["id"][i]=tonumber(connected_pcs["id"][i])
- connected=true
- end
- end
- connections.close()
- end
- function assign_name()
- connection_code = fs.open("connection_code","r")
- connection_name = connection_code.readLine()
- connection_code.close()
- end
- function disconnect(disconnect_number)
- rednet.send(connected_pcs["id"][disconnect_number],{["name"]=connection_name,["sending_to"]=connected_pcs["name"][disconnect_number],["status"]="disconnected"})
- 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()
- 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 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 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
- startup()
- menu()
- pull()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement