Advertisement
pulchroxloom

Turtle Server Code

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