Advertisement
9551

Untitled

Sep 4th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.85 KB | None | 0 0
  1. local modems = {peripheral.find("modem")}
  2. local modem
  3. for k,v in pairs(modems) do
  4.     if v.isWireless() then
  5.         modem = v
  6.     end
  7. end
  8. if not modem then
  9.     error("no modem!",0)
  10. end
  11. local reactorTimeout = 5
  12. colors.rainbow = colors.pink --temporary
  13. if not modem then
  14.     error("no modem!",0)
  15. end
  16. if not fs.exists("button") then
  17.     shell.run("pastebin get LTDZZZEJ buttton")
  18. end
  19. local b = require("button").terminal
  20. rednet.open(peripheral.getName(modem))
  21. local click = {"timeout",0,-1000,-1000}
  22. local xs,ys = term.getSize()
  23. local clicks = function()
  24.     click = b.timetouch(0.15)
  25. end
  26. local centerWrite = function(text, y)
  27.     local width, height = xs, ys
  28.     term.setCursorPos(math.ceil((width / 2) - (text:len() / 2)), y)
  29.     term.write(text)
  30. end
  31. local sizeText = function(strings,len)
  32.     local ost = strings
  33.     if #strings > len then
  34.         local el = #strings - len
  35.         if el < 0 then el = 0 end
  36.         strings = strings:sub(1,-el)
  37.         return strings:match("(.*)..$")..".."
  38.     else
  39.         return strings
  40.     end
  41. end
  42. local data = {}
  43. local quicksort
  44. local timers = {}
  45. local tcount = 0
  46. quicksort = function(t)
  47.     if #t < 2 then return t end
  48.     local pivot = t[1]
  49.     local a, b, c={}, {}, {}
  50.     for _, v in ipairs(t) do
  51.         if v < pivot then a[#a + 1] = v
  52.         elseif v > pivot then c[#c + 1] = v
  53.         else b[#b + 1] = v
  54.         end
  55.     end
  56.     a = quicksort(a)
  57.     c = quicksort(c)
  58.     for _, v in ipairs(b) do a[#a + 1] = v end
  59.     for _, v in ipairs(c) do a[#a + 1] = v end
  60.     return a
  61. end
  62. local indexIdentifiers = function(tbl,index)
  63.     local indexes = {}
  64.     for k,v in pairs(tbl) do
  65.         table.insert(indexes,k)
  66.     end
  67.     return quicksort(indexes)[index]
  68. end
  69. local getData = function()
  70.     local _,rec = rednet.receive("POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E",2)
  71.     if not rec then data = {} end
  72.     if type(rec) == "table" and type(rec.metadata) == "table" and rec.metadata.stamp then
  73.         if rec.metadata.stamp == "POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E" then
  74.             if timers[rec.metadata.identifier] then
  75.                 os.cancelTimer(timers[rec.metadata.identifier].timer)
  76.             end
  77.             data[rec.metadata.identifier] = rec
  78.             timers[rec.metadata.identifier] = {id=rec.metadata.identifier,timer=os.startTimer(reactorTimeout)}
  79.         end
  80.     end
  81. end
  82. local timeout = function()
  83.     local _, time = os.pullEvent("timer")
  84.     for k,v in pairs(timers) do
  85.         if time == v.timer then
  86.             data[v.id] = nil
  87.         end
  88.     end
  89. end
  90. local countData = function(data)    
  91.     local count = 0
  92.     for _ in pairs(data) do
  93.         count = count + 1
  94.     end
  95.     return count
  96. end
  97. local running = 1
  98. local runningIndex = 0
  99. local selector = function()
  100.     if click then
  101.         local dlen = countData(data)
  102.         term.setCursorPos(1,1)
  103.         term.setBackgroundColor(colors.gray)
  104.         term.write((" "):rep(xs))
  105.         term.setCursorPos(1,2)
  106.         term.write((" "):rep(xs))
  107.         term.setCursorPos(1,2)
  108.         term.setTextColor(colors.lightGray)
  109.         term.write("connections: "..dlen)
  110.         if running > 1 then
  111.             term.setTextColor(colors.red)
  112.             if b.button(1,click,1,1,"<") then
  113.                 running = running - 1
  114.             end
  115.         end
  116.         if running < dlen then
  117.             term.setTextColor(colors.green)
  118.             if b.button(1,click,xs,1,">") then
  119.                 running = running + 1
  120.             end
  121.         end
  122.         if running > dlen and dlen ~= 0 then
  123.             term.setBackgroundColor(colors.lightGray)
  124.             term.clear()
  125.             term.setTextColor(colors.red)
  126.             centerWrite("connection closed",10)
  127.             sleep(1)
  128.             running = dlen
  129.         end
  130.         term.setTextColor(colors.white)
  131.         runningIndex = indexIdentifiers(data,running)
  132.         if runningIndex then
  133.             centerWrite(sizeText(data[runningIndex].metadata.name,xs-4),1)
  134.         end
  135.     end
  136. end
  137. local GUI = function()
  138.     if data and next(data) then
  139.         term.setBackgroundColor(colors.lightGray)
  140.         term.clear()
  141.         for k,v in pairs(data) do
  142.             local doDraw = false
  143.             if v.metadata.identifier == runningIndex then doDraw = true end
  144.             if doDraw then
  145.                 v.reactor.state = b.switch(1,v.metadata.identifier,click,8,6,"red","green","black","reactor",1,1)
  146.             end
  147.             rednet.send(v.metadata.reply,v,"POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E")
  148.         end
  149.     else
  150.         term.setBackgroundColor(colors.lightGray)
  151.         term.clear()
  152.         term.setTextColor(colors.red)
  153.         centerWrite("404: no reactor",10)
  154.     end
  155. end
  156. while true do
  157.     parallel.waitForAll(getData,timeout,clicks,GUI,selector)
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement