Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modems = {peripheral.find("modem")}
- local modem
- for k,v in pairs(modems) do
- if v.isWireless() then
- modem = v
- end
- end
- if not modem then
- error("no modem!",0)
- end
- local reactorTimeout = 5
- colors.rainbow = colors.pink --temporary
- if not modem then
- error("no modem!",0)
- end
- if not fs.exists("button") then
- shell.run("pastebin get LTDZZZEJ buttton")
- end
- local b = require("button").terminal
- rednet.open(peripheral.getName(modem))
- local click = {"timeout",0,-1000,-1000}
- local xs,ys = term.getSize()
- local clicks = function()
- click = b.timetouch(0.15)
- end
- local centerWrite = function(text, y)
- local width, height = xs, ys
- term.setCursorPos(math.ceil((width / 2) - (text:len() / 2)), y)
- term.write(text)
- end
- local sizeText = function(strings,len)
- local ost = strings
- if #strings > len then
- local el = #strings - len
- if el < 0 then el = 0 end
- strings = strings:sub(1,-el)
- return strings:match("(.*)..$")..".."
- else
- return strings
- end
- end
- local data = {}
- local quicksort
- local timers = {}
- local tcount = 0
- quicksort = function(t)
- if #t < 2 then return t end
- local pivot = t[1]
- local a, b, c={}, {}, {}
- for _, v in ipairs(t) do
- if v < pivot then a[#a + 1] = v
- elseif v > pivot then c[#c + 1] = v
- else b[#b + 1] = v
- end
- end
- a = quicksort(a)
- c = quicksort(c)
- for _, v in ipairs(b) do a[#a + 1] = v end
- for _, v in ipairs(c) do a[#a + 1] = v end
- return a
- end
- local indexIdentifiers = function(tbl,index)
- local indexes = {}
- for k,v in pairs(tbl) do
- table.insert(indexes,k)
- end
- return quicksort(indexes)[index]
- end
- local getData = function()
- local _,rec = rednet.receive("POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E",2)
- if not rec then data = {} end
- if type(rec) == "table" and type(rec.metadata) == "table" and rec.metadata.stamp then
- if rec.metadata.stamp == "POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E" then
- if timers[rec.metadata.identifier] then
- os.cancelTimer(timers[rec.metadata.identifier].timer)
- end
- data[rec.metadata.identifier] = rec
- timers[rec.metadata.identifier] = {id=rec.metadata.identifier,timer=os.startTimer(reactorTimeout)}
- end
- end
- end
- local timeout = function()
- local _, time = os.pullEvent("timer")
- for k,v in pairs(timers) do
- if time == v.timer then
- data[v.id] = nil
- end
- end
- end
- local countData = function(data)
- local count = 0
- for _ in pairs(data) do
- count = count + 1
- end
- return count
- end
- local running = 1
- local runningIndex = 0
- local selector = function()
- if click then
- local dlen = countData(data)
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.write((" "):rep(xs))
- term.setCursorPos(1,2)
- term.write((" "):rep(xs))
- term.setCursorPos(1,2)
- term.setTextColor(colors.lightGray)
- term.write("connections: "..dlen)
- if running > 1 then
- term.setTextColor(colors.red)
- if b.button(1,click,1,1,"<") then
- running = running - 1
- end
- end
- if running < dlen then
- term.setTextColor(colors.green)
- if b.button(1,click,xs,1,">") then
- running = running + 1
- end
- end
- if running > dlen and dlen ~= 0 then
- term.setBackgroundColor(colors.lightGray)
- term.clear()
- term.setTextColor(colors.red)
- centerWrite("connection closed",10)
- sleep(1)
- running = dlen
- end
- term.setTextColor(colors.white)
- runningIndex = indexIdentifiers(data,running)
- if runningIndex then
- centerWrite(sizeText(data[runningIndex].metadata.name,xs-4),1)
- end
- end
- end
- local GUI = function()
- if data and next(data) then
- term.setBackgroundColor(colors.lightGray)
- term.clear()
- for k,v in pairs(data) do
- local doDraw = false
- if v.metadata.identifier == runningIndex then doDraw = true end
- if doDraw then
- v.reactor.state = b.switch(1,v.metadata.identifier,click,8,6,"red","green","black","reactor",1,1)
- end
- rednet.send(v.metadata.reply,v,"POCKETn@P#C3Y5XnNTj79gyj__GD=Zg92&Mn,e#2wE,Nyuc2sS/D>V>Hg_)3C*Q_%b*N5E")
- end
- else
- term.setBackgroundColor(colors.lightGray)
- term.clear()
- term.setTextColor(colors.red)
- centerWrite("404: no reactor",10)
- end
- end
- while true do
- parallel.waitForAll(getData,timeout,clicks,GUI,selector)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement