Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Custom vars
- local varsize = 6 --number of lines in your pcinfo file
- local pcinfo = "clientinfo"
- --program vars, these are handled by the program...Dont touch!
- local info = {}
- local mside=nil
- local id=nil
- local channel = nil
- local channelcheck = nil
- local pastebin = nil
- local sides={"front","back","left","right","top","bottom"}
- local state=nil
- local rside=nil
- local input=nil
- local output=nil
- local mevent={}
- local command=""
- local dependancy=nil
- local serverm = {}
- local function setstr(file,input) --writes to file from a variable
- local f=fs.open(file,"a")
- f.writeLine(input)
- f.close()
- end
- local function mkfile(file) --creates a new file, deletes original first
- shell.run("delete "..file)
- local f=fs.open(file,"w")
- f.close()
- end
- local function sidevalid(str) --checks a string to ensure that a correct side name was used
- for i2 = 1, 6 do
- if str:lower() == sides[i2] then return true end
- end
- return false
- end
- local function sideerror() --self explanatory
- print("side entered incorrect, sides can only be:")
- print("top,bottom,left,right,front or back")
- print("please enter the correct side")
- end
- local function getDeviceSide(deviceType) --function thanks to Sharidan!
- -- List of all sides
- local lstSides = {"left","right","up","down","front","back"};
- -- Now loop through all the sides
- -- "i" holds a table counter
- -- "side" will hold the actual side text
- for i, side in pairs(lstSides) do
- if (peripheral.isPresent(side)) then
- -- Yup, there is something on this side
- -- Check the type against the device
- -- string.lower() just ensures that the passed
- -- device type is in lower case.
- -- peripheral.getType() always returns lower case texts
- if (peripheral.getType(side) == string.lower(deviceType)) then
- -- Yes, this is the device type we need, so return the side
- return side;
- -- Note, that this call to "return" also terminates
- -- further running of this function
- end -- if .getType()
- end -- if .isPresent()
- end -- for-do
- -- If we reach this point, it means that we didnt find
- -- the specified device anywhere, so return nil indicating
- -- that it doesnt exist.
- return nil;
- end -- function()
- local function config ()--first time configuration
- mkfile(pcinfo)
- print("insert name for this client pc")
- id = read()
- setstr(pcinfo,id)
- shell.run("label set "..id)
- print("Detecting Modem side...")
- mside = getDeviceSide("modem")
- if (mside) then
- setstr(pcinfo,mside)
- print("modem detected on "..mside..", saving...")
- else
- print("modem not detected, please place a modem!")
- mevent = {os.pullEvent("peripheral")}
- if mevent[1]=="peripheral" then
- mside = getDeviceSide("modem")
- if (mside) then
- setstr(pcinfo,mside)
- print("modem detected on "..mside..", saving...")
- else repeat
- print("checking...")
- sleep(1)
- print("not a modem, please place a modem!")
- mevent = {os.pullEvent("peripheral")}
- if mevent[1]=="peripheral" then
- mside = getDeviceSide("modem")
- if (mside) then
- setstr(pcinfo,mside)
- print("modem detected on "..mside)
- end
- end
- until (mside) end
- end
- end
- print("what side do you want as the redstone output?")
- rside=read()
- if not sidevalid(rside) then
- repeat
- sideerror()
- rside = read()
- until sidevalid(rside)
- setstr(pcinfo,rside)
- elseif sidevalid(rside) then
- setstr(pcinfo,rside)
- end
- print("What server channel will this client be using?")
- channel = tonumber(read())
- channelfeed = tostring(channel)
- print("channel entered:"..channelfeed)
- print("querying matching server, please be patient")
- modem=peripheral.wrap(mside)
- local state = false
- if not state then
- modem.open(channel)
- local timeout = os.startTimer(3)
- modem.transmit(channel,channel,channelfeed)
- local event = {os.pullEvent()}
- print("transmitting info")
- if event[1] == "modem_message" then
- print("server query success!")
- setstr(pcinfo,channel)
- print("what command will this pc be reacting to? do not include the $$")
- command = read()
- print("command entered: $$"..command)
- print("is this correct? press desired number")
- print("1:yes")
- print("2:no")
- local ckevent = {os.pullEvent("char")}
- if ckevent[2]=="1" then
- print("registering new command with server")
- setstr(pcinfo,command)
- modem.transmit(channel,channel,"New Command")
- modem.transmit(channel,channel,command)
- local c,r,s,f,m =os.pullEvent("modem_message")
- if m =="Command Registered" then
- print("command registered successfully!")
- print("Finally, will this switch require another switch to be on before this one can be?")
- print("press desired number:")
- print("1:Yes")
- print("2:No")
- local kevent = {os.pullEvent("char")}
- if kevent[2]=="1" then
- print("Yes...ok what command is the other pc using?")
- print("type skip if you got here by mistake")
- dependancy=read()
- if dependancy ~= "skip" then
- setstr(pcinfo,dependancy)
- print("registering parent switch with server")
- modem.transmit(channel,channel,"New Parent Switch")
- modem.transmit(channel,channel,dependancy)
- local depmes={os.pullEvent("modem_message")}
- if depmes[5]=="Parent Registered" then
- print("Parent switch registered successfully!")
- else
- repeat
- sleep(0.1)
- until depmes[5]=="Parent Registered"
- end
- else
- setstr(pcinfo,"null")
- print("OK...skipping")
- end
- elseif kevent[2]=="2" then
- print("No pressed")
- setstr(pcinfo,"null")
- end
- end
- elseif ckevent[2]=="2" then repeat
- print("please type in the command you want this pc to use")
- command=read()
- print("command entered: $$"..command)
- print("is this correct? press desired number")
- print("1:yes")
- print("2:no")
- local ckevent2 = {os.pullEvent("char")}
- until ckevent2[2]=="1" end--ensures the use of our second pullevent array which determines the user has validated their command
- print("config complete, rebooting in...")
- state = true
- for i = 3,0,-1 do
- if i == 0 and state then
- shell.run("reboot")
- end
- print(tostring(i).."...")
- sleep(1)
- end
- elseif event[1] == "timer" then
- state=false
- print("no response or wrong channel number entered!")
- print("aborting config and rebooting!")
- sleep(1)
- shell.run("delete "..pcinfo)
- shell.run("reboot")
- end
- end
- end
- local function getvars(file)
- local f = fs.open(file,"r")
- for i = 1, varsize do
- info[i] = f.readLine()
- end
- f.close()
- end
- local function startup ()-- catches info from getvars
- getvars(pcinfo)
- id=info[1]
- mside=info[2]
- rside=info[3]
- channel=tonumber(info[4])
- channelcheck =tonumber(info[4])
- command = info[5]
- dependancy=info[6]
- end
- if not fs.exists(pcinfo) then --this is the pc start-time check to see if the computer has been configured
- config()
- else
- startup()
- end
- modem=peripheral.wrap(mside)
- print("client:"..id..":ACTIVE")
- while true do
- modem.open(channel)--opens a channel on the predefined channel
- local event={os.pullEvent()}
- if event[1]=="redstone" then
- if rs.getInput("right") then
- print("enabling redstone")
- modem.transmit(channel,channel,"rs")
- else
- print("disabling redstone")
- modem.transmit(channel,channel,"rs off")
- end
- end
- if event[5]=="done" then
- print("redstone enabled")
- elseif event[5]=="disabled" then
- print("redstone disabled")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment