AnotherTekkituser

client pc

May 7th, 2014
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.57 KB | None | 0 0
  1. --Custom vars
  2. local varsize = 6 --number of lines in your pcinfo file
  3. local pcinfo = "clientinfo"
  4.  
  5. --program vars, these are handled by the program...Dont touch!
  6. local info = {}
  7. local mside=nil
  8. local id=nil
  9. local channel = nil
  10. local channelcheck = nil
  11. local pastebin = nil
  12. local sides={"front","back","left","right","top","bottom"}
  13. local state=nil
  14. local rside=nil
  15. local input=nil
  16. local output=nil
  17. local mevent={}
  18. local command=""
  19. local dependancy=nil
  20. local serverm = {}
  21.  
  22. local function setstr(file,input) --writes to file from a variable
  23.     local f=fs.open(file,"a")
  24.     f.writeLine(input)
  25.     f.close()
  26. end
  27.  
  28. local function mkfile(file) --creates a new file, deletes original first
  29.     shell.run("delete "..file)
  30.     local f=fs.open(file,"w")
  31.     f.close()
  32. end
  33.  
  34. local function sidevalid(str) --checks a string to ensure that a correct side name was used
  35.         for i2 = 1, 6 do
  36.                 if str:lower() == sides[i2] then return true end
  37.         end
  38.  
  39.         return false
  40. end
  41.  
  42. local function sideerror() --self explanatory
  43.     print("side entered incorrect, sides can only be:")
  44.     print("top,bottom,left,right,front or back")
  45.     print("please enter the correct side")
  46. end
  47.  
  48. local function getDeviceSide(deviceType) --function thanks to Sharidan!
  49.   -- List of all sides
  50.   local lstSides = {"left","right","up","down","front","back"};
  51.   -- Now loop through all the sides
  52.   -- "i" holds a table counter
  53.   -- "side" will hold the actual side text
  54.   for i, side in pairs(lstSides) do
  55.     if (peripheral.isPresent(side)) then
  56.       -- Yup, there is something on this side
  57.       -- Check the type against the device
  58.       -- string.lower() just ensures that the passed
  59.       -- device type is in lower case.
  60.       -- peripheral.getType() always returns lower case texts
  61.       if (peripheral.getType(side) == string.lower(deviceType)) then
  62.         -- Yes, this is the device type we need, so return the side
  63.         return side;
  64.         -- Note, that this call to "return" also terminates
  65.         -- further running of this function
  66.       end -- if .getType()
  67.     end -- if .isPresent()
  68.   end -- for-do
  69.   -- If we reach this point, it means that we didnt find
  70.   -- the specified device anywhere, so return nil indicating
  71.   -- that it doesnt exist.
  72.   return nil;
  73. end -- function()
  74.    
  75. local function config ()--first time configuration
  76.     mkfile(pcinfo)
  77.     print("insert name for this client pc")
  78.     id = read()
  79.     setstr(pcinfo,id)
  80.     shell.run("label set "..id)
  81.     print("Detecting Modem side...")
  82.     mside = getDeviceSide("modem")
  83.     if (mside) then
  84.         setstr(pcinfo,mside)
  85.         print("modem detected on "..mside..", saving...")
  86.     else
  87.         print("modem not detected, please place a modem!")
  88.         mevent = {os.pullEvent("peripheral")}
  89.         if mevent[1]=="peripheral" then
  90.             mside = getDeviceSide("modem")
  91.             if (mside) then
  92.                 setstr(pcinfo,mside)
  93.                 print("modem detected on "..mside..", saving...")
  94.             else repeat
  95.                 print("checking...")
  96.                 sleep(1)
  97.                 print("not a modem, please place a modem!")
  98.                 mevent = {os.pullEvent("peripheral")}
  99.                 if mevent[1]=="peripheral" then
  100.                     mside = getDeviceSide("modem")
  101.                     if (mside) then
  102.                         setstr(pcinfo,mside)
  103.                         print("modem detected on "..mside)
  104.                     end
  105.                 end
  106.             until (mside) end
  107.         end
  108.     end
  109.     print("what side do you want as the redstone output?")
  110.     rside=read()
  111.     if not sidevalid(rside) then
  112.         repeat
  113.             sideerror()
  114.             rside = read()
  115.         until sidevalid(rside)
  116.         setstr(pcinfo,rside)
  117.     elseif sidevalid(rside) then
  118.         setstr(pcinfo,rside)
  119.     end
  120.     print("What server channel will this client be using?")
  121.     channel = tonumber(read())
  122.     channelfeed = tostring(channel)
  123.     print("channel entered:"..channelfeed)
  124.     print("querying matching server, please be patient")
  125.     modem=peripheral.wrap(mside)
  126.     local state = false
  127.     if not state then
  128.         modem.open(channel)
  129.         local timeout = os.startTimer(3)
  130.         modem.transmit(channel,channel,channelfeed)
  131.         local event = {os.pullEvent()}
  132.         print("transmitting info")
  133.         if event[1] == "modem_message" then
  134.             print("server query success!")
  135.             setstr(pcinfo,channel)
  136.             print("what command will this pc be reacting to? do not include the $$")
  137.             command = read()
  138.             print("command entered: $$"..command)
  139.             print("is this correct? press desired number")
  140.             print("1:yes")
  141.             print("2:no")
  142.             local ckevent = {os.pullEvent("char")}
  143.             if ckevent[2]=="1" then
  144.                 print("registering new command with server")
  145.                 setstr(pcinfo,command)
  146.                 modem.transmit(channel,channel,"New Command")
  147.                 modem.transmit(channel,channel,command)
  148.                 local c,r,s,f,m =os.pullEvent("modem_message")
  149.                 if m =="Command Registered" then
  150.                     print("command registered successfully!")
  151.                     print("Finally, will this switch require another switch to be on before this one can be?")
  152.                     print("press desired number:")
  153.                     print("1:Yes")
  154.                     print("2:No")
  155.                     local kevent = {os.pullEvent("char")}
  156.                     if kevent[2]=="1" then
  157.                         print("Yes...ok what command is the other pc using?")
  158.                         print("type skip if you got here by mistake")
  159.                         dependancy=read()
  160.                         if dependancy ~= "skip" then
  161.                             setstr(pcinfo,dependancy)
  162.                             print("registering parent switch with server")
  163.                             modem.transmit(channel,channel,"New Parent Switch")
  164.                             modem.transmit(channel,channel,dependancy)
  165.                             local depmes={os.pullEvent("modem_message")}
  166.                             if depmes[5]=="Parent Registered" then
  167.                                 print("Parent switch registered successfully!")
  168.                             else
  169.                                 repeat
  170.                                     sleep(0.1)
  171.                                 until depmes[5]=="Parent Registered"
  172.                             end
  173.                         else
  174.                             setstr(pcinfo,"null")
  175.                             print("OK...skipping")
  176.                         end
  177.                     elseif kevent[2]=="2" then
  178.                         print("No pressed")
  179.                         setstr(pcinfo,"null")
  180.                     end  
  181.                 end
  182.             elseif ckevent[2]=="2" then repeat
  183.                 print("please type in the command you want this pc to use")
  184.                 command=read()
  185.                 print("command entered: $$"..command)
  186.                 print("is this correct? press desired number")
  187.                 print("1:yes")
  188.                 print("2:no")
  189.                 local ckevent2 = {os.pullEvent("char")}
  190.             until ckevent2[2]=="1" end--ensures the use of our second pullevent array which determines the user has validated their command
  191.             print("config complete, rebooting in...")
  192.             state = true
  193.             for i = 3,0,-1 do
  194.                 if i == 0 and state then
  195.                     shell.run("reboot")
  196.                 end
  197.                 print(tostring(i).."...")
  198.                 sleep(1)
  199.             end
  200.         elseif event[1] == "timer" then
  201.             state=false
  202.             print("no response or wrong channel number entered!")
  203.             print("aborting config and rebooting!")
  204.             sleep(1)
  205.             shell.run("delete "..pcinfo)
  206.             shell.run("reboot")
  207.         end
  208.     end
  209. end
  210.  
  211. local function getvars(file)
  212.     local f = fs.open(file,"r")
  213.     for i = 1, varsize do
  214.         info[i] = f.readLine()
  215.     end
  216.     f.close()
  217. end
  218.  
  219. local function startup ()-- catches info from getvars
  220.     getvars(pcinfo)
  221.     id=info[1]
  222.     mside=info[2]
  223.     rside=info[3]
  224.     channel=tonumber(info[4])
  225.     channelcheck =tonumber(info[4])
  226.     command = info[5]
  227.     dependancy=info[6]
  228. end
  229.  
  230. if not fs.exists(pcinfo) then --this is the pc start-time check to see if the computer has been configured
  231.     config()
  232. else
  233.     startup()
  234. end
  235.  
  236. modem=peripheral.wrap(mside)
  237. print("client:"..id..":ACTIVE")
  238. while true do
  239. modem.open(channel)--opens a channel on the predefined channel
  240. local event={os.pullEvent()}
  241.     if event[1]=="redstone" then
  242.         if rs.getInput("right") then
  243.             print("enabling redstone")
  244.             modem.transmit(channel,channel,"rs")
  245.         else
  246.             print("disabling redstone")
  247.             modem.transmit(channel,channel,"rs off")
  248.         end
  249.     end
  250.     if event[5]=="done" then
  251.         print("redstone enabled")
  252.     elseif event[5]=="disabled" then
  253.         print("redstone disabled")
  254.     end
  255. end
Advertisement
Add Comment
Please, Sign In to add comment