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()
- if connected_pcs["name"][1]==nil 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()
- connections=fs.open("connections","w")
- connections.writeLine(connecting_to)
- connections.flush()
- connections.close()
- end
- connected=false
- assign_connections()
- term.setTextColor(colors.green)
- print("connecting...")
- term.setTextColor(colors.white)
- print("If the PC is not connecting then this could be because the main PC is not loaded or the main programm is not running, please always name the sub and main programms ´startup´")
- while not connected do
- for i=1,250 do
- rednet.send(i,{["connecting_to"]=connected_pcs["name"][1],["name"]=connection_name})
- end
- while info_1~="timer" do
- timer=os.startTimer(5)
- info_1 ,info_2, info_3, info_4 = os.pullEvent()
- if info_1=="rednet_message" then
- if info_3["name"]==connected_pcs["name"][1] and info_3["connecting_to"]==connection_name then
- connections=fs.open("connections","w")
- connections.writeLine(connected_pcs["name"][1])
- connections.writeLine(info_2)
- connections.flush()
- connections.close()
- connected=true
- end
- 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 diffrent name.")
- fs.delete("connection_code")
- sleep(3)
- startup()
- end
- if info_3["same_id"] then
- print("A PC with this id was already connected,")
- print("please use a diffrent name.")
- sleep(3)
- startup()
- end
- for i=1,250 do
- rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name})
- end
- connected=false
- while not connected do
- info_1 ,info_2, info_3, info_4 = os.pullEvent()
- if info_1=="rednet_message" then
- if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
- connections=fs.open("connections","w")
- connections.writeLine(connecting_to)
- connections.writeLine(info_2)
- connections.flush()
- connections.close()
- connected=true
- end
- end
- end
- end
- end
- os.cancelTimer(timer)
- end
- info_1=0
- end
- 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, info_2, info_3, info_4 = os.pullEvent()
- if info_1=="rednet_message" and info_3["sending_to"]==connection_name and info_3["name"]==connected_pcs["name"][1] 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"]=connected_pcs["name"][1],["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
- rednet.send(info_2,{["sending_status"]=true,["sending_to"]=connected_pcs["name"][1],["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
- if info_3["status"]=="disconnected" then
- fs.delete("connections")
- 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()
- connections=fs.open("connections","w")
- connections.writeLine(connecting_to)
- connections.flush()
- connections.close()
- for i=1,250 do
- rednet.send(i,{["connecting_to"]=connecting_to,["name"]=connection_name})
- end
- connected=false
- while not connected do
- info_1 ,info_2, info_3, info_4 = os.pullEvent()
- if info_1=="rednet_message" then
- if info_3["name"]==connecting_to and info_3["connecting_to"]==connection_name then
- connections=fs.open("connections","w")
- connections.writeLine(connecting_to)
- connections.writeLine(info_2)
- connections.flush()
- connections.close()
- connected=true
- end
- end
- end
- end
- end
- 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
- startup()
- pull()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement