Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server-side functions
- server = function()
- -- Get network if one already exists
- if fs.exists("network.txt") then
- file = fs.open("network.txt","r")
- clients = file.readAll()
- file.close()
- clients = textutils.unserialise(clients)
- else
- clients = {}
- end
- -- Check if message is from member client
- isMember = function(id)
- exists = false
- for i = 1,#clients do
- if clients[i] == id then
- exists = true
- end
- end
- return exists
- end
- -- Provided client isn't a member, add
- addMember = function(id)
- if not isMember(id) then
- rednet.send(id, nil, "hive.add")
- id,conf,cmd = rednet.receive("hive.add",1)
- if conf == true then
- table.insert(clients, id)
- print(id.." Added")
- else
- print(id.." "..conf)
- end
- end
- end
- -- Provided client is a member, remove
- rmvMember = function(id)
- if isMember(id) then
- for i = 1,#clients do
- if clients[i] == id then
- rednet.send(id,nil,"hive.rmv")
- id,conf,cmd = rednet.receive("hive.rmv",1)
- if conf == true then
- table.remove(clients,i)
- print(id.." Removed")
- else print("[Failed]")
- end
- end
- end
- end
- end
- remoteControl = function(id)
- if isMember(id) then
- rednet.send(id,true,"hive.rc")
- cid,ctype,cprot = rednet.receive("hive.rc",1)
- if not cid then
- print("[Error] Connection failure")
- elseif ctype == false then
- print("[Error] Device not compatible")
- else
- rcQueue = nil
- while true do
- evt,key = os.pullEvent("key")
- if key == keys.tab then
- rednet.send(id,false,"hive.rc")
- break
- elseif key == keys.up then
- rcQueue = "forward"
- elseif key == keys.down then
- rcQueue = "back"
- elseif key == keys.left then
- rcQueue = "left"
- elseif key == keys.right then
- rcQueue = "right"
- elseif key == keys.insert then
- rcQueue = "place"
- elseif key == keys.delete then
- rcQueue = "dig"
- elseif key == keys.pageUp then
- rcQueue = "up"
- elseif key == keys.pageDown then
- rcQueue = "down"
- end
- if rcQueue ~= nil then
- rednet.send(id,rcQueue,"hive.rc")
- rcQueue = nil
- end
- end
- end
- end
- end
- -- Display information on first line
- serverInfo = function()
- x,y = term.getCursorPos()
- term.setCursorPos(1,1)
- term.clearLine()
- write("Members: "..#clients)
- if y == 1 then y = 2 end
- term.setCursorPos(x,y)
- end
- -- Wait for user to give command
- waitForCmd = function()
- if cmd == nil then
- cmd = read()
- end
- if cmd == "clear" then
- shell.run("clear")
- else
- if cmd:sub(1,3) == "add" then
- id = cmd:gsub("add ","")
- id = tonumber(id)
- addMember(id)
- elseif cmd:sub(1,6) == "remove" then
- id = cmd:gsub("remove ","")
- id = tonumber(id)
- rmvMember(id)
- elseif cmd:sub(1,5) == "ping " then
- cmd = cmd:gsub("ping ","")
- if cmd:sub(1,3) == "all" then
- if #cmd > 4 then
- ping = cmd:sub(5,#cmd)
- else
- ping = "ping"
- end
- for i = 1,#clients do
- rednet.send(tonumber(clients[i]),ping,"hive.msg")
- end
- else
- id = cmd:sub(1,cmd:find(" ")-1)
- id = tonumber(id)
- ping = cmd:sub(cmd:find(" ")+1,#cmd)
- if isMember(id) then
- if not ping then
- ping = "ping"
- end
- rednet.send(id,ping,"hive.msg")
- end
- end
- elseif cmd:sub(1,3) == "rc " then
- id = cmd:gsub("rc ","")
- id = tonumber(id)
- remoteControl(id)
- end
- end
- cmd = nil
- end
- -- Main server process
- while true do
- memberCount = #clients
- serverInfo()
- waitForCmd()
- if #clients == 0 and fs.exists("network.txt") then
- fs.delete("network.txt")
- elseif #clients ~= memberCount then
- file = fs.open("network.txt","w")
- file.write(textutils.serialise(clients))
- file.close()
- end
- end
- end
- -- Client-side functions
- client = function()
- -- Get server ID if one exists
- if fs.exists("server.txt") then
- file = fs.open("server.txt","r")
- sID = file.readLine()
- file.close()
- sID = tonumber(sID)
- end
- -- Display information on first line
- clientInfo = function()
- x,y = term.getCursorPos()
- term.setCursorPos(1,1)
- term.clearLine()
- write("Client#"..os.getComputerID())
- if sID then
- print(" [Connected]")
- else
- print(" [Disconnected]")
- end
- if y < 2 then y = 2 end
- term.setCursorPos(x,y)
- end
- -- Wait for message from server
- local listen = function()
- id,data,cmd = rednet.receive()
- if cmd:sub(1,5) == "hive." then
- if cmd == "hive.add" then
- if not sID then
- sID = id
- rednet.send(id,true,cmd)
- if sID then
- file = fs.open("server.txt","w")
- file.write(tostring(sID))
- file.close()
- end
- else
- rednet.send(id,"Already Has Network",cmd)
- end
- elseif id == sID then
- if cmd == "hive.rmv" then
- rednet.send(id,true,cmd)
- if fs.exists("server.txt") then
- fs.delete("server.txt")
- sID = nil
- shell.run("clear")
- end
- elseif cmd == "hive.msg" then
- print("Server:"..data)
- elseif cmd == "hive.rc" and data == true then
- if not turtle and not peripheral.getType("back") == "neuralInteface" then
- rednet.send(sID,false,"hive.rc")
- elseif not fs.exists("//hivemind/movement.lua") then
- rednet.send(sID,false,"hive.rc")
- else
- rednet.send(sID,true,"hive.rc")
- while true do
- id,data,cmd = rednet.receive("hive.rc")
- if id == sID then
- if data == false then
- break
- end
- end
- end
- end
- end
- end
- end
- end
- -- Main client process
- while true do
- clientInfo()
- listen()
- end
- end
- -- Main process
- shell.run("clear")
- modem = peripheral.find("modem")
- args = {...}
- if args[1] == "server" then type = "server" else type = "client" end
- if not modem then
- print("[Error] No Modem Found")
- else
- rednet.open(peripheral.getName(modem))
- if type == "server" then
- if args[2] == "rc" then
- cmd = "rc "..args[3]
- end
- server()
- elseif type == "client" then
- client()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement