Advertisement
Guest User

startup

a guest
Jun 11th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. for i,v in ipairs(peripheral.getNames()) do
  2.   if peripheral.getType(v) == "modem" then
  3.     rednet.open(v)
  4.   end
  5. end
  6.  
  7. function clear()
  8.   term.clear()
  9.   term.setCursorPos(1,1)
  10. end
  11.  
  12. function choose(a,s)
  13.   selection = 1
  14.   min = 1
  15.   max = #a
  16.  
  17.   while true do
  18.     clear()
  19.     if s ~= nil then
  20.       print(s)
  21.       print()
  22.     end
  23.    
  24.     for i,v in ipairs(a) do
  25.       if selection == i then
  26.         print(">"..v)
  27.       else
  28.         print(" "..v)
  29.       end
  30.     end
  31.    
  32.     e,p1 = os.pullEvent()
  33.     if e == "key" then
  34.       if p1 == 200 then
  35.         if selection == min then
  36.           selection = max
  37.         else
  38.           selection = selection - 1
  39.         end
  40.       elseif p1 == 208 then
  41.         if selection == max then
  42.           selection = min
  43.         else
  44.           selection = selection + 1
  45.         end
  46.       elseif p1 == 28 then
  47.         break
  48.       end
  49.     end
  50.   end
  51.   return a[selection]
  52. end
  53.  
  54.  
  55. if not fs.exists("data") then
  56.   clear()
  57.   print("SETTING UP RS SWITCH")
  58.   print()
  59.   sleep(1)
  60.   side = choose(rs.getSides(),"Side out")
  61.   clear()
  62.   stateString = choose({"ACTIVE","DEACTIVE"},"Default state")
  63.   if stateString == "ACTIVE" then
  64.     state = true
  65.   else
  66.     state = false
  67.   end
  68.   clear()
  69.   term.write("Name of RS Switch: ")
  70.   name = read()
  71.   clear()
  72.   term.write("Cearance needed to change state: ")
  73.   clearance = tonumber(read())
  74.   clear()
  75.   term.write("Server ID? ")
  76.   serverID = tonumber(read())
  77.   clear()
  78.   print("LOG IN")
  79.   print()
  80.   term.write("Username: ")
  81.   username = read()
  82.   term.write("Password: ")
  83.   password = read("*")
  84.   clear()
  85.   print("AUTHORIZING RS SWITCH")
  86.   rednet.send(serverID,{ {username, " ", 0, password},"LOG_IN" })
  87.   id,msg,d = rednet.receive(.3)
  88.   cID = os.getComputerID()
  89.   if id == serverID then
  90.     print("AUTHORIZED... REGISTERING RS SWITCH")
  91.     rednet.send(serverID, {msg,"REGISTER_RS_SWITCH",cID,stateString,clearance,name})
  92.     id,msg,d = rednet.receive(.3)
  93.     if id == serverID then
  94.       data = {side,stateString,state,serverID}
  95.      
  96.       file = fs.open("data","w")
  97.       file.write(textutils.serialise(data))
  98.       file.close()
  99.     else
  100.       print("CLEARANCE TOO LOW... EXITING PROGRAM")
  101.       sleep(1)
  102.       return exit
  103.     end
  104.      
  105.   else
  106.     print("UNAUTHORIZED... EXITING PROGRAM")
  107.     sleep(1)
  108.     return exit
  109.   end
  110.  
  111. else
  112.   file = fs.open("data", "r")
  113.   data = file.readAll()
  114.   file.close()
  115.  
  116.   stats = textutils.unserialise(data)
  117.   side = stats[1]
  118.   stateString = stats[2]
  119.   state = stats[3]
  120.   serverID = stats[4]
  121. end
  122.  
  123. rs.setOutput(side,state)
  124.  
  125. while true do
  126.   id,msg,d = rednet.receive()
  127.  
  128.   if id == serverID then
  129.     if msg == "CHANGE" then
  130.       state = not state
  131.       if state then
  132.         stringState = "ACTIVE"
  133.       else
  134.         stringState = "DEACTIVE"
  135.       end
  136.       rs.setOutput(side, state)
  137.       rednet.send(serverID, stringState)
  138.     elseif msg == "GETSTATE" then
  139.       rednet.send(serverID, stringState)
  140.     end
  141.   end
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement