Advertisement
asianviking

oChat

Nov 21st, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.19 KB | None | 0 0
  1. local username = "Not set"
  2. local channel = "Not set"
  3. local modem = peripheral.wrap("top")
  4. function topBar()
  5.     term.setCursorPos(1,1)
  6.     term.clearLine()
  7.     print("oChat v 1.0")
  8.     term.clearLine()
  9.     print("User: "..username..", Channel: "..channel)
  10. end
  11. function clearScreen()
  12.     term.clear()
  13.     term.setCursorPos(1,1)
  14.     print("oChat v 1.0")
  15.     print("User: "..username..", Channel: "..channel)
  16. end
  17. function setChannel()
  18.     clearScreen()
  19.     write("Channel number (1-100): ")
  20.     local channelInput = tonumber(read())
  21.     if (channelInput < 1) or (channelInput > 100) then
  22.         print("Invalid channel...")
  23.         sleep(1)
  24.         setChannel()
  25.     else
  26.         channel = channelInput
  27.     end
  28. end
  29. clearScreen()
  30. write("Enter a username: ")
  31. username = read()
  32. setChannel()
  33. modem.open(channel)
  34. function receive()
  35.     local event,modemSide,senderChannel,replyChannel,message,distance = os.pullEvent("modem_message")
  36.     term.clearLine()
  37.     if not (string.match(message, username)) then
  38.         print(message)
  39.         topBar()
  40.     end
  41. end
  42. function send()
  43.     term.setCursorPos(1,19)
  44.     write("> ")
  45.     local message = read()
  46.     modem.transmit(channel,1,"<"..username.."> "..message)
  47.     topBar()
  48. end
  49. clearScreen()
  50. while true do
  51.     parallel.waitForAny(receive, send)
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement