jaypar

RingProgAdv

Feb 14th, 2018 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ring = peripheral.find("stargate")
  2. mon = peripheral.find("monitor")
  3. ringList = {{"Close"}, {"Open"}}
  4.  
  5. if fs.exists("ringList") then
  6.  ringFile = fs.open("ringList", "r")
  7.  ringList = textutils.unserialize(ringFile.readAll())
  8. end
  9.  
  10. if ring then else
  11.  print("Stargate not found!")
  12.  error()
  13. end
  14.  
  15. if mon then else
  16.  monP = false
  17. end
  18.  
  19. function checkForUpdates()
  20.  while true do
  21.   webFile = http.get("https://pastebin.com/raw/0ZQxxTxU")
  22.   webFile = webFile.readAll()
  23.   stF = fs.open("startup", "r").readAll()
  24.   if stF ~= webFile then
  25.    stF = fs.open("startup", "w")
  26.    stF.write(webFile)
  27.    stF.close()
  28.    os.reboot()
  29.   end
  30.   sleep(0.001)
  31.  end
  32. end
  33.  
  34. function waitForIris(state)
  35.  while true do
  36.   if ring.irisState() == state then
  37.    break
  38.   end
  39.   sleep(0.001)
  40.  end
  41. end
  42.  
  43. function waitForRing(state)
  44.  while true do
  45.   if ring.stargateState() == state then
  46.    break
  47.   end
  48.   sleep(0.001)
  49.  end
  50. end
  51.  
  52. function dial(adrs)
  53.  dialed = true
  54.  ring.closeIris()
  55.  waitForIris("Closed")
  56.  ring.disconnect()
  57.  waitForRing("Idle")
  58.  ring.dial(adrs)
  59.  waitForRing("Connected")
  60.  ring.openIris()
  61. end
  62.  
  63. function printToMon(t, x, y)
  64.  if monP ~= false then
  65.   mon.setCursorPos(x, y)
  66.   mon.write(tostring(t))
  67.  end
  68. end
  69. randAdrs = ""
  70. function randDial()
  71.  while true do
  72.   for i = string.len(randAdrs), 8 do
  73.    if math.random(1, 2) == 1 then
  74.     randAdrs = randAdrs .. string.char(math.random(48, 57))
  75.    else
  76.     randAdrs = randAdrs .. string.char(math.random(65, 90))
  77.    end
  78.   end
  79.   if string.len(randAdrs) < 9 then
  80.    print("Failed")
  81.   else
  82.    print("Trying "..randAdrs)
  83.    ring.dial(randAdrs)
  84.    randAdrs = ""
  85.   end
  86.   sleep(0.001)
  87.  end
  88. end
  89.  
  90. function monitor()
  91.  if monP ~= false then
  92.   while true do
  93.    x, y = 3, 3
  94.    length = 0
  95.    for i,v in pairs(ringList) do
  96.     printToMon(v[1], x + length, y)
  97.     x = x + 3 + length
  98.     length = string.len(v[1])
  99.     z, t = mon.getSize()
  100.     if x >= z then
  101.      y = y + 2
  102.      x = 3
  103.      length = 0
  104.     end
  105.    end
  106.    sleep(0.001)
  107.    mon.clear()
  108.   end
  109.  end
  110. end
  111.  
  112. dialed = false
  113.  
  114. function main()
  115.  while true do
  116.   e, v, k, n = os.pullEvent("monitor_touch")
  117.   l, m = 3, 3
  118.   for i,v in pairs(ringList) do
  119.    if l <= k and string.len(v[1]) + l - 1 >= k and m == n then
  120.     if v[1] == "Close" then
  121.      ring.closeIris()
  122.      ring.disconnect()
  123.      dialed = false
  124.     elseif v[1] == "Open" then
  125.      ring.openIris()
  126.      dialed = false
  127.     else
  128.  
  129.      dial(v[2])
  130.     end
  131.    end
  132.    len = string.len(v[1])
  133.    l = l + 3 + len
  134.   end
  135.  end
  136. end
  137.  
  138. function saveRingList(file, list)
  139.  ringFile = fs.open(file, "w")
  140.  ringFile.write(textutils.serialize(list))
  141.  ringFile.close()
  142. end
  143.  
  144. function getId()
  145.  while true do
  146.   exists = true
  147.  
  148.   if dialed ~= true then
  149.    ring.closeIris()
  150.    waitForIris("Closed")
  151.   end
  152.  
  153.   if ring.stargateState() == "Connected" then
  154.    for i,v in pairs(ringList) do
  155.     if v[2] == ring.remoteAddress() then
  156.      exists = true
  157.     end
  158.    end
  159.  
  160.    if exists ~= true then
  161.     ringInfo = {math.random(255), ring.remoteAddress()}
  162.     table.insert(ringList, table.getn(ringList) - 1, ringInfo)
  163.     saveRingList("ringList", ringList)
  164.    end
  165.   end
  166.   sleep(0.001)
  167.  end
  168. end
  169.  
  170. parallel.waitForAll(getId, main, monitor, checkForUpdates)--, randDial)
Advertisement
Add Comment
Please, Sign In to add comment