Advertisement
DOGGYWOOF

Doggy OS network chat

Sep 24th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. -- Function to list all connected modems
  2. local function listModems()
  3. local modems = {}
  4. for _, side in ipairs(peripheral.getNames()) do
  5. if peripheral.getType(side) == "modem" then
  6. table.insert(modems, side)
  7. end
  8. end
  9. return modems
  10. end
  11.  
  12. -- Function to setup the modem
  13. local function setupModem()
  14. local modems = listModems()
  15. if #modems == 0 then
  16. print("No modems found! Please connect a modem and restart.")
  17. return nil
  18. end
  19.  
  20. print("Connected modems:")
  21. for i, side in ipairs(modems) do
  22. print(i .. ": " .. side)
  23. end
  24.  
  25. -- Open the first modem found
  26. rednet.open(modems[1])
  27. print("Using modem on " .. modems[1])
  28. return modems[1]
  29. end
  30.  
  31. -- Function to display a message in the chat
  32. local function displayMessage(senderId, message, isOlderClient)
  33. term.setTextColor(colors.yellow) -- Set text color
  34. if isOlderClient then
  35. print("[Unknown User (" .. senderId .. ")]: " .. message) -- Show ID for older clients
  36. else
  37. print("[" .. senderId .. "]: " .. message)
  38. end
  39. term.setTextColor(colors.white) -- Reset text color
  40. end
  41.  
  42. -- Function to handle incoming messages
  43. local function handleIncomingMessages()
  44. while true do
  45. local senderId, message, protocol = rednet.receive()
  46. local username, chatMessage = message:match("^(.-): (.+)$")
  47. if username and chatMessage then
  48. displayMessage(username, chatMessage, false)
  49. else
  50. displayMessage(senderId, message, true) -- Handle messages from older clients
  51. end
  52. end
  53. end
  54.  
  55. -- Function to send a message to all connected Rednet users
  56. local function sendMessage(username)
  57. while true do
  58. print("Type your message (or 'exit' to quit): ")
  59. local userInput = read() -- Get the user's input
  60. if userInput == "exit" then
  61. print("Exiting chat...")
  62. shell.run("/disk/os/gui") -- Run the GUI program on exit
  63. break
  64. end
  65. -- Format the message to include the username
  66. local formattedMessage = username .. ": " .. userInput
  67. -- Broadcast the message to all users
  68. rednet.broadcast(formattedMessage)
  69. displayMessage(username, userInput, false)
  70. end
  71. end
  72.  
  73. -- Main program
  74. term.clear() -- Clear the terminal
  75. term.setCursorPos(1, 1) -- Set cursor position
  76.  
  77. print("Welcome to the Doggy OS Network Chatroom!")
  78.  
  79. -- Prompt for the username
  80. print("Enter your username:")
  81. local username = read()
  82.  
  83. local modemSide = setupModem()
  84. if modemSide == nil then return end -- Exit if no modem was found
  85.  
  86. print("Type 'exit' to leave the chat.")
  87.  
  88. -- Start monitoring messages in a separate thread
  89. parallel.waitForAny(handleIncomingMessages, function()
  90. sendMessage(username)
  91. end)
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement