Elrol

server

Apr 10th, 2020 (edited)
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.17 KB | None | 0 0
  1. local serverName = ""
  2. local modem = nil
  3. local port = 0
  4. local cableSide = "bottom"
  5. local color = colors.white
  6. local w, h = nil
  7. local names = {}
  8.  
  9. local function newLine()
  10.     local _, y = term.getCursorPos()
  11.     term.setCursorPos(1, y+1)
  12. end
  13.  
  14. local function info(text, isCenter)
  15.     term.clearLine()
  16.     if text == nil then
  17.         term.setCursorPos(1, y+2)
  18.     elseif isCenter then
  19.         x, y = term.getCursorPos()
  20.         x = (w - #text) / 2
  21.         term.setCursorPos(x, y)
  22.         term.write(text)
  23.         term.setCursorPos(1, y+1)
  24.     else
  25.         term.write(text)
  26.     end
  27. end
  28.  
  29. local function longInfo(text, isCenter)
  30.     line = ""
  31.     for k,s in pairs(split(text, " ")) do
  32.         length = string.len(line) + string.len(s) + 1
  33.         if w >= length then
  34.             a = line .. " " .. s
  35.             line = a
  36.         else
  37.             info(line, isCenter)
  38.             line = " "..s
  39.         end
  40.     end
  41.     info(line, isCenter)
  42. end
  43.  
  44. local function hr()
  45.     x, y = term.getCursorPos()
  46.     for i = 1, w, 1 do
  47.         term.write("-")
  48.     end
  49.     term.setCursorPos(1, y+1)
  50. end
  51.  
  52. local function gui()
  53.     term.setTextColor(color)
  54.     term.setBackgroundColor(colors.black)
  55.     term.setCursorPos(1,1)
  56.     w, h = term.getSize()
  57.     info("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", true)
  58.     info("COPYRIGHT 2019-2020 ROBCO INDUSTRIES", true)
  59.     info("-Server "..port.."-", true)
  60.     info(nil, false)
  61.     info(serverName, false)
  62.     hr()
  63.     term.setCursorPos(1, h)
  64. end
  65.  
  66. local function msg(message)
  67.     term.scroll(1)
  68.     term.write(message)
  69.     gui()
  70. end
  71.  
  72. local function name(id)
  73.     local n = names[id]
  74.     if n then return n
  75.     else return id end
  76. end
  77.  
  78. local function setBundledColor(color, state)
  79.     local test = colors.test(rs.getBundledOutput(cableSide), color);
  80.     if state then
  81.         if not test then
  82.             rs.setBundledOutput(cableSide, colors.combine(rs.getBundledOutput(cableSide), color))
  83.         end
  84.     else
  85.         if test then
  86.             rs.setBundledOutput(cableSide, colors.subtract(rs.getBundledOutput(cableSide), color))
  87.         end
  88.     end
  89. end
  90.  
  91. local function toggleBundledColor(color)
  92.     local output = rs.getBundledOutput(cableSide);
  93.     local turnedOn = false
  94.    
  95.     if colors.test(rs.getBundledOutput(cableSide), color) then
  96.         output = colors.subtract(output, color)
  97.     else
  98.         output = colors.combine(output, color)
  99.         turnedOn = true
  100.     end
  101.    
  102.     rs.setBundledOutput(cableSide, output)
  103.     return turnedOn
  104. end
  105.  
  106. local function split(inputstr, sep)
  107.     if sep == nil then
  108.         sep = "%s"
  109.     end
  110.    
  111.     local input = tostring(inputstr)
  112.    
  113.     local t={}
  114.     for str in string.gmatch(input, "([^"..tostring(sep).."]+)") do
  115.         table.insert(t, tostring(str))
  116.     end
  117.     return t
  118. end
  119.  
  120. function string.starts(String,Start)
  121.    return string.sub(tostring(String),1,string.len(Start))==tostring(Start)
  122. end
  123.  
  124. local function isEmpty(s)
  125.     return s == nil or s == ''
  126. end
  127.  
  128. local function listen()
  129.     local loop = true
  130.     while loop do
  131.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  132.         if isEmpty(message) then return end
  133.         local splitCMD = split(message, " ")
  134.         local cmd = splitCMD[1]
  135.        
  136.         if string.starts(message, "name") then
  137.             modem.transmit(replyChannel, port, serverName)
  138.             local n = split(message, ":")[2]
  139.             names[replyChannel] = n
  140.             msg("["..name(replyChannel).."] connected "..tostring(replyChannel))
  141.            
  142.         elseif cmd == "toggle" then
  143.             local color = tonumber(splitCMD[3])
  144.             local turnedOn = toggleBundledColor(color)
  145.             msg("["..name(replyChannel).."] toggled "..splitCMD[3])
  146.             modem.transmit(replyChannel, port, "toggled "..splitCMD[3].." "..tostring(turnedOn))
  147.            
  148.         elseif cmd == "trigger" then
  149.             local color = tonumber(splitCMD[3])
  150.             toggleBundledColor(color)
  151.             sleep(1)
  152.             toggleBundledColor(color)
  153.             modem.transmit(replyChannel, port, "triggered")
  154.             msg("["..name(replyChannel).."] triggered "..color)
  155.            
  156.         elseif cmd == "check" then
  157.             local splitMsg = split(message, " ")
  158.             local color = tonumber(splitMsg[3])
  159.             local bundledColors = rs.getBundledOutput(cableSide)
  160.             local f = colors.test(bundledColors, color)
  161.             msg("Terminal "..replyChannel.." has checked "..color.." "..tostring(port))
  162.             modem.transmit(replyChannel, port, tostring(f))
  163.            
  164.         elseif cmd == "stop" then
  165.             loop = false
  166.             msg("["..name(replyChannel).."] stopped the server")
  167.             modem.transmit(replyChannel, port, "stopped")
  168.            
  169.         elseif cmd == "ping" then
  170.             modem.transmit(replyChannel, port, "pong")
  171.             msg("["..name(replyChannel).."] pinged the server")
  172.         end
  173.     end
  174. end
  175.  
  176. function init(n, sp, side, cs, c)
  177.     serverName = n
  178.     port = sp
  179.     modem = peripheral.wrap(side)
  180.     cableSide = cs
  181.     color = c
  182.    
  183.     if modem then
  184.         modem.open(port)
  185.     else
  186.         print("Error: Modem was nil: " .. side .. ":" .. port)
  187.         return
  188.     end
  189.    
  190.     gui()
  191.     listen()
  192. end
  193.  
  194. return {
  195.     init = init
  196. }
Advertisement
Add Comment
Please, Sign In to add comment