Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local serverName = ""
- local modem = nil
- local port = 0
- local cableSide = "bottom"
- local color = colors.white
- local w, h = nil
- local names = {}
- local function newLine()
- local _, y = term.getCursorPos()
- term.setCursorPos(1, y+1)
- end
- local function info(text, isCenter)
- term.clearLine()
- if text == nil then
- term.setCursorPos(1, y+2)
- elseif isCenter then
- x, y = term.getCursorPos()
- x = (w - #text) / 2
- term.setCursorPos(x, y)
- term.write(text)
- term.setCursorPos(1, y+1)
- else
- term.write(text)
- end
- end
- local function longInfo(text, isCenter)
- line = ""
- for k,s in pairs(split(text, " ")) do
- length = string.len(line) + string.len(s) + 1
- if w >= length then
- a = line .. " " .. s
- line = a
- else
- info(line, isCenter)
- line = " "..s
- end
- end
- info(line, isCenter)
- end
- local function hr()
- x, y = term.getCursorPos()
- for i = 1, w, 1 do
- term.write("-")
- end
- term.setCursorPos(1, y+1)
- end
- local function gui()
- term.setTextColor(color)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,1)
- w, h = term.getSize()
- info("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", true)
- info("COPYRIGHT 2019-2020 ROBCO INDUSTRIES", true)
- info("-Server "..port.."-", true)
- info(nil, false)
- info(serverName, false)
- hr()
- term.setCursorPos(1, h)
- end
- local function msg(message)
- term.scroll(1)
- term.write(message)
- gui()
- end
- local function name(id)
- local n = names[id]
- if n then return n
- else return id end
- end
- local function setBundledColor(color, state)
- local test = colors.test(rs.getBundledOutput(cableSide), color);
- if state then
- if not test then
- rs.setBundledOutput(cableSide, colors.combine(rs.getBundledOutput(cableSide), color))
- end
- else
- if test then
- rs.setBundledOutput(cableSide, colors.subtract(rs.getBundledOutput(cableSide), color))
- end
- end
- end
- local function toggleBundledColor(color)
- local output = rs.getBundledOutput(cableSide);
- local turnedOn = false
- if colors.test(rs.getBundledOutput(cableSide), color) then
- output = colors.subtract(output, color)
- else
- output = colors.combine(output, color)
- turnedOn = true
- end
- rs.setBundledOutput(cableSide, output)
- return turnedOn
- end
- local function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local input = tostring(inputstr)
- local t={}
- for str in string.gmatch(input, "([^"..tostring(sep).."]+)") do
- table.insert(t, tostring(str))
- end
- return t
- end
- function string.starts(String,Start)
- return string.sub(tostring(String),1,string.len(Start))==tostring(Start)
- end
- local function isEmpty(s)
- return s == nil or s == ''
- end
- local function listen()
- local loop = true
- while loop do
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- if isEmpty(message) then return end
- local splitCMD = split(message, " ")
- local cmd = splitCMD[1]
- if string.starts(message, "name") then
- modem.transmit(replyChannel, port, serverName)
- local n = split(message, ":")[2]
- names[replyChannel] = n
- msg("["..name(replyChannel).."] connected "..tostring(replyChannel))
- elseif cmd == "toggle" then
- local color = tonumber(splitCMD[3])
- local turnedOn = toggleBundledColor(color)
- msg("["..name(replyChannel).."] toggled "..splitCMD[3])
- modem.transmit(replyChannel, port, "toggled "..splitCMD[3].." "..tostring(turnedOn))
- elseif cmd == "trigger" then
- local color = tonumber(splitCMD[3])
- toggleBundledColor(color)
- sleep(1)
- toggleBundledColor(color)
- modem.transmit(replyChannel, port, "triggered")
- msg("["..name(replyChannel).."] triggered "..color)
- elseif cmd == "check" then
- local splitMsg = split(message, " ")
- local color = tonumber(splitMsg[3])
- local bundledColors = rs.getBundledOutput(cableSide)
- local f = colors.test(bundledColors, color)
- msg("Terminal "..replyChannel.." has checked "..color.." "..tostring(port))
- modem.transmit(replyChannel, port, tostring(f))
- elseif cmd == "stop" then
- loop = false
- msg("["..name(replyChannel).."] stopped the server")
- modem.transmit(replyChannel, port, "stopped")
- elseif cmd == "ping" then
- modem.transmit(replyChannel, port, "pong")
- msg("["..name(replyChannel).."] pinged the server")
- end
- end
- end
- function init(n, sp, side, cs, c)
- serverName = n
- port = sp
- modem = peripheral.wrap(side)
- cableSide = cs
- color = c
- if modem then
- modem.open(port)
- else
- print("Error: Modem was nil: " .. side .. ":" .. port)
- return
- end
- gui()
- listen()
- end
- return {
- init = init
- }
Advertisement
Add Comment
Please, Sign In to add comment