XileHorizon

LumiaStream

Feb 22nd, 2025
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.77 KB | None | 0 0
  1. -- color_control.lua
  2.  
  3. -- 1. Define color-to-relay mapping
  4. local colorToRelay = {
  5.   red =    { relayName = "redstone_relay_0", side = "front" },
  6.   blue =   { relayName = "redstone_relay_1", side = "front" },
  7.   green =  { relayName = "redstone_relay_2", side = "front" },
  8.   purple = { relayName = "redstone_relay_3", side = "front" },
  9.   pink = { relayName = "redstone_relay_4", side = "front" },
  10.   yellow = { relayName = "redstone_relay_5", side = "front" },
  11. }
  12.  
  13. -- Optional: If you only have fewer relays, remove unused colors,
  14. -- or if you have more relays, add more color mappings.
  15.  
  16. -- 2. Helper: turn off all relays
  17. local function turnOffAll()
  18.   for _, info in pairs(colorToRelay) do
  19.     local relay = peripheral.wrap(info.relayName)
  20.     if relay then
  21.       relay.setOutput(info.side, false)
  22.     end
  23.   end
  24. end
  25.  
  26. turnOffAll()  -- ensure everything is off at start
  27.  
  28. -- Keep track of what's currently active
  29. local currentColor = nil
  30.  
  31. local function activateColor(newColor)
  32.   -- If the incoming argument isn't a string, log and return
  33.   if type(newColor) ~= "string" then
  34.     print("activateColor received a non-string value:", tostring(newColor))
  35.     return
  36.   end
  37.  
  38.   -- 3A. Turn off the old color's relay
  39.   if currentColor and colorToRelay[currentColor] then
  40.     local oldInfo = colorToRelay[currentColor]
  41.     local oldRelay = peripheral.wrap(oldInfo.relayName)
  42.     if oldRelay then
  43.       oldRelay.setOutput(oldInfo.side, false)
  44.     end
  45.   end
  46.  
  47.   -- 3B. Turn on the new color's relay
  48.   if colorToRelay[newColor] then
  49.     local info = colorToRelay[newColor]
  50.     local relay = peripheral.wrap(info.relayName)
  51.     if relay then
  52.       relay.setOutput(info.side, true)
  53.       currentColor = newColor
  54.       print("Activated color: "..newColor)
  55.     else
  56.       print("Relay peripheral not found for color: "..newColor)
  57.     end
  58.   else
  59.     print("Unknown color: "..newColor)
  60.   end
  61. end
  62.  
  63. -- 4. Connect to your Node.js WebSocket server
  64. local url = "ws://kev.unoriginal.studio:8080"
  65. print("Connecting to "..url)
  66. http.websocketAsync(url)
  67.  
  68. -- 5. Event loop to handle WebSocket messages
  69. while true do
  70.   local event, url, message = os.pullEvent()
  71.   if event == "websocket_success" then
  72.     print("WebSocket connected!")
  73.     -- Optionally send a greeting
  74.     message.send("Hello from CC: Tweaked")
  75.  
  76.   elseif event == "websocket_failure" then
  77.     print("Failed to connect:", message)
  78.     break  -- exit the program, or handle reconnection logic
  79.  
  80.   elseif event == "websocket_message" then
  81.     -- p2 = the websocket handle, p3 = the messag content (color)
  82.     local colorMsg = message
  83.     print("Received color:", colorMsg)
  84.     activateColor(colorMsg)
  85.  
  86.   elseif event == "websocket_closed" then
  87.     print("WebSocket closed.")
  88.     break
  89.   end
  90. end
  91.  
  92. print("Exiting program.")
  93.  
Add Comment
Please, Sign In to add comment