Elrol

server

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