Advertisement
CCHacker132

stargate manager

Jan 5th, 2021
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.61 KB | None | 0 0
  1.  
  2.  
  3. --CONFIG
  4. local monitor = peripheral.wrap('top')
  5. local monitor2 = peripheral.wrap('monitor_4')
  6. --
  7.  
  8. monitor2.setTextScale(0.5)
  9. monitor2.setBackgroundColor(colors.black)
  10. monitor2.clear()
  11. monitor2.setCursorPos(1,1)
  12. monitor2.setTextColor(colors.green)
  13. monitor2.write('Passed arguments: ')
  14. local stargate = peripheral.find('stargate')
  15. stargate.disconnect()
  16. stargate.openIris()
  17.  
  18. monitor.clear()
  19. monitor.setTextScale(0.5)
  20. rednet.open('modem_6')
  21.  
  22. local keyboard = {
  23.   '1234567890_',
  24.   'QWERTYUIOP',
  25.   'ASDFGHJKL',
  26.   'ZXCVBNM',
  27.   }
  28.  
  29. local file = fs.open('destinations.cfg','r')
  30. local destinations = textutils.unserialise(file.readAll())
  31. file.close()
  32.  
  33. local selected = 1
  34. local hasQueuedClose = false
  35. local manualSelection = 1
  36. local manualAddress = {'_','_','_','_','_','_','_','_','_'}
  37.  
  38. function getKey(x,y)
  39.   local success = true
  40.   local yIndex = y-13
  41.   local xIndex = nil
  42.   if yIndex and yIndex > 0 and keyboard[yIndex] then
  43.       xIndex = (x/2)-(18-(#keyboard[yIndex]/2))+10
  44.   end
  45.   if xIndex and xIndex%1~=0 and xIndex > 0 then
  46.     success = false
  47.   end
  48.   if xIndex then
  49.       xIndex = math.floor(xIndex)
  50.   end
  51.   local letterToGet = string.sub(keyboard[yIndex],xIndex,xIndex)
  52.   if not success then
  53.     letterToGet = nil
  54.   end
  55.   return letterToGet
  56. end
  57.  
  58. function redrawScreen(drawKeyboard)
  59.   monitor.setBackgroundColor(colors.black)
  60.   monitor.clear()
  61.   if selected~=1 then
  62.     manualSelection = 1
  63.   end
  64.   for i,v in pairs(destinations) do
  65.       if selected == 1 and i == 1 then
  66.         monitor.setTextColor(colors.white)
  67.         monitor.setCursorPos(1,i)
  68.         monitor.setBackgroundColor(colors.gray)
  69.         monitor.clearLine()
  70.         monitor.write(v[1])
  71.         monitor.setTextColor(colors.green)
  72.         monitor.write(' - ')
  73.         for pos = 1,#v[2] do
  74.           if pos == manualSelection then
  75.             monitor.setBackgroundColor(colors.lightGray)
  76.             monitor.setTextColor(colors.red)
  77.           else
  78.             monitor.setBackgroundColor(colors.gray)
  79.             monitor.setTextColor(colors.cyan)
  80.           end
  81.           monitor.write(string.sub(v[2],pos,pos))
  82.         end
  83.       else
  84.         monitor.setTextColor(colors.white)
  85.         monitor.setCursorPos(1,i)
  86.         if selected == i then
  87.             monitor.setBackgroundColor(colors.gray)
  88.             monitor.clearLine()
  89.         else
  90.             monitor.setBackgroundColor(colors.black)
  91.         end
  92.         monitor.write(v[1])
  93.         monitor.setTextColor(colors.green)
  94.         monitor.write(' - ')
  95.         monitor.setTextColor(colors.cyan)
  96.         monitor.write(v[2])
  97.       end
  98.   end
  99.   monitor.setTextColor(colors.white)
  100.   if drawKeyboard then
  101.     for i,v in pairs(keyboard) do
  102.       monitor.setCursorPos(18-(#v),i+13)
  103.       for pos = 1,#v do
  104.         monitor.setBackgroundColor(colors.gray)
  105.         monitor.write(string.sub(v,pos,pos))
  106.         monitor.setBackgroundColor(colors.black)
  107.         monitor.write(' ')
  108.       end
  109.     end
  110.   end
  111.   monitor.setBackgroundColor(colors.green)
  112.   monitor.setCursorPos(16,18)
  113.   monitor.write('Dial')
  114.   monitor.setTextColor(colors.gray)
  115.   monitor.setCursorPos(2,23)
  116.   monitor.setBackgroundColor(colors.black)
  117.   monitor.write('Stargate ID: '..stargate.localAddress())
  118. end
  119.  
  120. function queueClose(dialLength)
  121.   hasQueuedClose = true
  122.   redrawScreen(false)
  123.   local oldTerm = term.current()
  124.   if dialLength then
  125.     repeat
  126.         sleep()
  127.         local state = {stargate.stargateState()}
  128.         term.redirect(monitor)
  129.         term.setBackgroundColor(colors.lightGray)
  130.         paintutils.drawLine(2,20,35,20)
  131.         term.setBackgroundColor(colors.green)
  132.         paintutils.drawLine(2,20,2+math.min(33,(state[2]/dialLength)*34),20)
  133.       until state[1] == 'Connected' or state[1] == 'Closed'
  134.     end
  135.     term.redirect(oldTerm)
  136.     monitor.setCursorPos(2,24)
  137.     monitor.setTextColor(colors.green)
  138.     monitor.setBackgroundColor(colors.black)
  139.     monitor.write('Connected to: '..stargate.remoteAddress())
  140.     monitor.setCursorPos(1,20)
  141.     monitor.setBackgroundColor(colors.green)
  142.     monitor.clearLine()
  143.     monitor.setBackgroundColor(colors.black)
  144.     monitor.setCursorPos(1,20)
  145.     monitor.write(' ')
  146.     monitor.setCursorPos(36,20)
  147.     monitor.write(' ')
  148.     monitor.setBackgroundColor(colors.red)
  149.     for progress = 1,68 do
  150.       monitor.setCursorPos((math.floor((progress/68)*33))+2,20)
  151.       monitor.write(' ')
  152.       stargate.sendMessage('Time Remaining:',tostring(68-progress))
  153.       sleep(1)
  154.     end
  155.     stargate.disconnect()
  156.     hasQueuedClose = false
  157.     monitor2.setBackgroundColor(colors.black)
  158.     monitor2.clear()
  159.     monitor2.setCursorPos(1,1)
  160.     monitor2.setTextColor(colors.green)
  161.     monitor2.write('Passed arguments: ')
  162.     redrawScreen(selected==1)
  163. end
  164.  
  165. function mainThread()
  166. while true do
  167.     local state = stargate.stargateState()
  168.     if not hasQueuedClose then
  169.       redrawScreen(selected==1)
  170.     end
  171.     repeat
  172.       sleep()
  173.     until not hasQueuedClose
  174.     local event = {os.pullEvent('monitor_touch')}
  175.     state = stargate.stargateState()
  176.     if event[4] < 13 and not hasQueuedClose then
  177.         selected = math.min(event[4],#destinations)
  178.     elseif event[4] >= 13 and event[4] < 18 and selected == 1 then
  179.         local returnKey = getKey(event[3],event[4])
  180.         if returnKey then
  181.           manualAddress[manualSelection] = returnKey
  182.           destinations[1][2] = ''
  183.           for i,v in pairs(manualAddress) do
  184.             destinations[1][2] = destinations[1][2]..v
  185.           end
  186.           manualSelection = manualSelection+1
  187.           if manualSelection > 9 then
  188.             manualSelection = 1    
  189.           end
  190.         end
  191.     elseif event[4] == 18 and not hasQueuedClose then
  192.         if event[3] >= 16 and event[3] <= 20 then
  193.             local dialAddress = destinations[selected][2]
  194.             local newAddress = ''
  195.             for num = 1,#dialAddress do
  196.               if string.sub(dialAddress,num,num) ~= '_' then
  197.                 newAddress = newAddress..string.sub(dialAddress,num,num)
  198.               end
  199.             end
  200.             local success = stargate.dial(newAddress)
  201.             if not success then
  202.                 redrawScreen(false)
  203.                 monitor.setCursorPos(12,16)
  204.                 monitor.setBackgroundColor(colors.black)
  205.                 monitor.setTextColor(colors.red)
  206.                 monitor.write('Invalid Code')
  207.                 os.pullEvent('monitor_touch')
  208.             else
  209.                 queueClose(#destinations[selected][2])
  210.             end
  211.         end
  212.     end
  213. end
  214. end
  215.  
  216. function irisManagement()
  217.     while true do
  218.         sleep()
  219.         local state = {stargate.stargateState()}
  220.         if state[1] ~= 'Idle' and state[1] ~= 'Connected' then
  221.             stargate.closeIris()
  222.         else
  223.             stargate.openIris()
  224.         end
  225.     end
  226. end
  227.  
  228. function manageIncomingConnections()
  229.   while true do
  230.       sleep()
  231.       local state = {stargate.stargateState()}
  232.       if state[1] == 'Connected' and state[3] == 'Incoming' then
  233.         queueClose()
  234.       end
  235.   end
  236. end
  237.  
  238. function drawPassedArgs()
  239.   while true do
  240.     sleep()
  241.     local event = {os.pullEvent('sgMessageReceived')}
  242.     monitor2.clear()
  243.     monitor2.setCursorPos(1,1)
  244.     monitor2.setTextColor(colors.green)
  245.     monitor2.write('Passed arguments: ')
  246.     monitor2.setTextColor(colors.white)
  247.     for i,v in pairs(event) do
  248.       monitor2.setCursorPos(1,i+2)
  249.       monitor2.setTextColor(colors.cyan)
  250.       monitor2.write(i..'- ')
  251.       monitor2.setTextColor(colors.white)
  252.       monitor2.write(tostring(v))
  253.     end
  254.   end
  255. end
  256.  
  257. parallel.waitForAll(irisManagement,mainThread,manageIncomingConnections,drawPassedArgs)
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement