Advertisement
pulchroxloom

Error Server

Dec 30th, 2020 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1. -- == Data ==
  2. -- Constants
  3. local SERVER_CHANNEL = 65534
  4. local ERROR_SERVER_CHANNEL = 65535
  5. local DEFAULT_BACKGROUND = colors.black
  6. local DEFAULT_TEXT = colors.white
  7. -- == Peripherals ==
  8. local modem = nil
  9. local peripherals = peripheral.getNames()
  10. for name = 1, #peripherals, 1 do
  11.     if(peripheral.getType(peripherals[name]) == "modem") then
  12.         modem = peripheral.wrap(peripherals[name])
  13.         break
  14.     end
  15. end
  16.  
  17. function resetColor()
  18.     term.setTextColor(DEFAULT_TEXT)
  19.     term.setBackgroundColor(DEFAULT_BACKGROUND)
  20. end
  21.  
  22. function getName(channel_id)
  23.     if(channel_id == 0) then
  24.         return "White"
  25.     elseif(channel_id == 1) then
  26.         return "Orange"
  27.     elseif(channel_id == 2) then
  28.         return "Magenta"
  29.     elseif(channel_id == 3) then
  30.         return "Light Blue"
  31.     elseif(channel_id == 4) then
  32.         return "Yellow"
  33.     elseif(channel_id == 5) then
  34.         return "Lime"
  35.     elseif(channel_id == 6) then
  36.         return "Pink"
  37.     elseif(channel_id == 7) then
  38.         return "Gray"
  39.     elseif(channel_id == 8) then
  40.         return "Light Gray"
  41.     elseif(channel_id == 9) then
  42.         return "Cyan"
  43.     elseif(channel_id == 10) then
  44.         return "Purple"
  45.     elseif(channel_id == 11) then
  46.         return "Blue"
  47.     elseif(channel_id == 12) then
  48.         return "Brown"
  49.     elseif(channel_id == 13) then
  50.         return "Green"
  51.     elseif(channel_id == 14) then
  52.         return "Red"
  53.     elseif(channel_id == 15) then
  54.         return "Black"
  55.     end
  56. end
  57.  
  58. function setColor(channel_id)
  59.     if(channel_id == 0) then
  60.         term.setTextColor(colors.black)
  61.         term.setBackgroundColor(colors.white)
  62.     elseif(channel_id == 1) then
  63.         term.setTextColor(colors.black)
  64.         term.setBackgroundColor(colors.orange)
  65.     elseif(channel_id == 2) then
  66.         term.setTextColor(colors.black)
  67.         term.setBackgroundColor(colors.magenta)
  68.     elseif(channel_id == 3) then
  69.         term.setTextColor(colors.black)
  70.         term.setBackgroundColor(colors.lightBlue)
  71.     elseif(channel_id == 4) then
  72.         term.setTextColor(colors.black)
  73.         term.setBackgroundColor(colors.yellow)
  74.     elseif(channel_id == 5) then
  75.         term.setTextColor(colors.black)
  76.         term.setBackgroundColor(colors.lime)
  77.     elseif(channel_id == 6) then
  78.         term.setTextColor(colors.black)
  79.         term.setBackgroundColor(colors.pink)
  80.     elseif(channel_id == 7) then
  81.         term.setTextColor(colors.black)
  82.         term.setBackgroundColor(colors.gray)
  83.     elseif(channel_id == 8) then
  84.         term.setTextColor(colors.black)
  85.         term.setBackgroundColor(colors.lightGray)
  86.     elseif(channel_id == 9) then
  87.         term.setTextColor(colors.black)
  88.         term.setBackgroundColor(colors.cyan)
  89.     elseif(channel_id == 10) then
  90.         term.setTextColor(colors.black)
  91.         term.setBackgroundColor(colors.purple)
  92.     elseif(channel_id == 11) then
  93.         term.setTextColor(colors.black)
  94.         term.setBackgroundColor(colors.blue)
  95.     elseif(channel_id == 12) then
  96.         term.setTextColor(colors.black)
  97.         term.setBackgroundColor(colors.brown)
  98.     elseif(channel_id == 13) then
  99.         term.setTextColor(colors.black)
  100.         term.setBackgroundColor(colors.green)
  101.     elseif(channel_id == 14) then
  102.         term.setTextColor(colors.black)
  103.         term.setBackgroundColor(colors.red)
  104.     elseif(channel_id == 15) then
  105.         term.setTextColor(colors.white)
  106.         term.setBackgroundColor(colors.black)
  107.     end
  108. end
  109.  
  110. function handleRequests()
  111.     while(true) do
  112.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  113.         setColor(replyChannel)
  114.         io.write(message)
  115.         resetColor()
  116.         io.write("\n")
  117.     end
  118. end
  119.  
  120. function start()
  121.     if(modem == nil) then
  122.         error("Error, this program requires a Modem!")
  123.     end
  124.     modem.open(ERROR_SERVER_CHANNEL)
  125.     term.setTextColor(colors.green)
  126.     print("Awake and Listening on Port " .. ERROR_SERVER_CHANNEL)
  127.     handleRequests()
  128. end
  129.  
  130. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement