Advertisement
bryceio

Computercraft Chatroom V1

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