Advertisement
fyrkantis

MessengerClient.lua

Aug 3rd, 2024 (edited)
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  1. write("Initializing messenger...")
  2. -- https://stackoverflow.com/a/7615129/13347795
  3. function split(input, maxSplits, sep)
  4.     if sep == nil then
  5.         sep = ";"
  6.     end
  7.     splits = {}
  8.     splitCount = 0
  9.     for str in string.gmatch(input, "[^"..sep.."]+") do
  10.         if maxSplits ~= nil and splitCount > maxSplits then
  11.             splits[splitCount] = splits[splitCount]..sep..str
  12.         else
  13.             table.insert(splits, str)
  14.             splitCount = splitCount + 1
  15.         end
  16.     end
  17.     return splits
  18. end
  19. function readLoop()
  20.     while true do
  21.         message = read()
  22.         rednet.broadcast(message, "broadcast-messenger-message")
  23.    end
  24. end
  25. function rednetLoop()
  26.     while true do
  27.         id, message = rednet.receive("broadcast-messenger-message")
  28.         print(id..": "..message)
  29.    end
  30. end
  31. print(" Done!")
  32.  
  33. print("Loading messages...")
  34.  
  35. peripheral.find("modem", rednet.open)
  36.  
  37. rednet.broadcast(nil, "request-messenger-backlog")
  38. _, response = rednet.receive("response-messenger-backlog")
  39. term.clear()
  40. term.setCursorPos(1, 1)
  41.  
  42. for _, row in ipairs(split(response, nil, "\n")) do
  43.     date, time, id, message = table.unpack(split(row, 3))
  44.     if message ~= nil then
  45.         if id == tostring(os.getComputerID()) then
  46.             print(message)
  47.         else
  48.             print(id..": "..message)
  49.         end
  50.     end
  51. end
  52.  
  53. parallel.waitForAll(readLoop, rednetLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement