bryceio

Computercraft Chatroom V2

Apr 24th, 2021 (edited)
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.43 KB | None | 0 0
  1. --Code by bryceio
  2.  
  3. --Set up login with username.
  4. term.clear()
  5. term.setCursorPos(1, 1)
  6. print("Welcome to the chatroom. Please enter a username.")
  7. user = read()
  8. print("Welcome to the chatroom")
  9. sleep(2)
  10. rednet.broadcast(user, "chatroomlogin")
  11.  mes = {}
  12. messages = 0
  13. userUpdate = false
  14. newUserEntry = false
  15.  
  16. --Function to color usernames
  17. function usercolor()
  18.     if term.isColor() then
  19.         term.setTextColor(8192)
  20.     end
  21. end
  22.  
  23. --Function to end color
  24. function endcolor()
  25.     if term.isColor() then
  26.         term.setTextColor(1)
  27.     end
  28. end
  29.  
  30. --Adds a new message to the terminal
  31. function newMessage(senderName, messageText)
  32.     if userUpdate == true then
  33.         messageText = ("<----Updated name to: "..messageText.."---->")
  34.         userUpdate = false
  35.     elseif newUserEntry == true then
  36.         messageText = ("<----User joined the chat---->")
  37.         newUserEntry = false
  38.     end
  39.     if messages < 7 then
  40.         messages = messages + 1
  41.         newmessage = fs.open("chatroommessages/"..messages, "w")
  42.         newmessage.writeLine(senderName)
  43.         newmessage.writeLine(messageText)
  44.         newmessage.close()
  45.         term.setCursorPos(1, messages*2-1)
  46.         usercolor()
  47.         print("<"..senderName..">                                            ")
  48.         endcolor()
  49.         term.setCursorPos(1, messages*2)
  50.         print(messageText)
  51.     elseif messages >= 7 then
  52.         for i = 1, 6 do
  53.             fs.delete("chatroommessages/"..i)
  54.             fs.copy("chatroommessages/"..i+1, "chatroommessages/"..i)
  55.         end
  56.         newmessage = fs.open("chatroommessages/7", "w")
  57.         newmessage.writeLine(senderName)
  58.         newmessage.writeLine(messageText)
  59.         newmessage.close()
  60.         for i = 1, 7 do
  61.             oldmessage = fs.open("chatroommessages/"..i, "r")
  62.             term.setCursorPos(1, i*2-1)
  63.             usercolor()
  64.             print("<"..oldmessage.readLine()..">                                ")
  65.             endcolor()
  66.             term.setCursorPos(1, i*2)
  67.             print(oldmessage.readLine())
  68.             oldmessage.close()
  69.         end
  70.     end
  71. end
  72.  
  73. --Set up blank chatroom
  74. bestnumber = 8675309
  75. term.clear()
  76. enter = 257    --The enter key's number code
  77. backspace = 259    --The backspace key's number code
  78. relog = 342         --The relog command key, by default it is left alt
  79. exit = 346          --The exit command key, by default it is right alt
  80. rednet.open("back")   --Change accordingly
  81. cpos = 1     --Cursor x position
  82. chat = 0
  83. --Set up screen
  84. term.setCursorPos(1, 15)
  85. print("---------------------------------------------------")
  86. term.setCursorPos(1, 16)
  87. usercolor()
  88. print("<"..user..">")
  89. endcolor()
  90. fs.delete("chatroomusers")
  91. setup = fs.open("chatroomfiles/outgoing", "w")
  92. setup.write("test")
  93. setup.close()
  94. rednet.broadcast(user, "userQuery")
  95. while true do
  96.         event, x, y, z = os.pullEvent()   --Waits for something to happen
  97.         if event == "char" then        --If a character is typed
  98.            if cpos <= 50 then           --Makes sure the cursor is on screen
  99.             term.setCursorPos(cpos, 17)
  100.             print(x)                     --Displays the character typed in message box
  101.             mes[cpos] = x                --Adds the typed character to a table that holds the message
  102.             cpos = cpos + 1              --Moves cursor forward
  103.            end
  104.         elseif event == "key_up" then        --Checks for other keys pressed
  105.             if x == enter then
  106.                 cpos = 1                   --Resets cursor
  107.                 for i = 1, 50 do           --Clears message box
  108.                     term.setCursorPos(i, 17)
  109.                     print(" ")
  110.                 end
  111.                 mess = fs.open("chatroomfiles/outgoing", "w")    --Writes the contents of the table to a file to be read later
  112.                 for i = 1,#mes do
  113.                     mess.write(mes[i])
  114.                     mes[i] = " "
  115.                 end
  116.                 mess.close()
  117.                 send = fs.open("chatroomfiles/outgoing", "r")        
  118.                 finalmes = send.readAll()                           --Puts the message into a variable
  119.                 send.close()
  120.                 rednet.broadcast(finalmes, "chatroomsend")    --Sends message to other computers if not a command
  121.                 newMessage(user, finalmes)
  122.             elseif x == backspace then            --Removes the last character when backspace is pressed
  123.                 if cpos > 1 then
  124.                     cpos = cpos-1
  125.                     if cpos <= 50 then
  126.                         term.setCursorPos(cpos, 17)
  127.                         print(" ")
  128.                         mes[cpos] = " "
  129.                     end
  130.                 end    
  131.             elseif x == relog then              --Allows you to change your username or update it to other chat members
  132.                 mes = {}
  133.                 term.setCursorPos(1, 16)
  134.                 print("Enter Username:         ")
  135.                 user = read()
  136.                 rednet.broadcast(user, "chatroomlogin")
  137.                 sleep(0.5)
  138.                 term.setCursorPos(1, 16)
  139.                 usercolor()
  140.                 print("<"..user..">                                     ")
  141.                 endcolor()
  142.                 term.setCursorPos(1, 17)
  143.                 print("                                                      ")
  144.             elseif x == exit then         --Exits the program
  145.                 term.clear()
  146.                 term.setCursorPos(1, 1)
  147.                 error()
  148.             end
  149.         elseif event == "rednet_message" and z == "chatroomsend" then          --Receives messages
  150.             if fs.exists("chatroomusers/"..x) then
  151.                 sentuser = fs.open("chatroomusers/"..x, "r")
  152.                 usersent = sentuser.readLine()
  153.                 sentuser.close()
  154.             else
  155.                 usersent = ("ID: "..x)              --Uses the computer's id if there is no registered username
  156.             end
  157.             newMessage(usersent, y)
  158.         elseif event == "rednet_message" and z == "chatroomlogin" then    --Registers new usernames and changes old ones.
  159.             if fs.exists("chatroomusers/"..x) then
  160.                 olduser = fs.open("chatroomusers/"..x, "r")
  161.                 userUpdate = true
  162.                 newMessage(olduser.readLine(), y)
  163.                 olduser.close()
  164.             else
  165.                 newUserEntry = true
  166.                 newMessage(y, "potatoes")
  167.             end
  168.             newuser = fs.open("chatroomusers/"..x, "w")
  169.             newuser.write(y)
  170.             newuser.close()
  171.         elseif event == "rednet_message" and z == "userQuery" then
  172.             rednet.broadcast(user, "userQueryResponse")
  173.         elseif event == "rednet_message" and z == "userQueryResponse" then
  174.             newuser = fs.open("chatroomusers/"..x, "w")
  175.             newuser.write(y)
  176.             newuser.close()
  177.         end
  178. end
Add Comment
Please, Sign In to add comment