Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Code by bryceio
- --Set up login with username.
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome to the chatroom. Please enter a username.")
- user = read()
- print("Welcome to the chatroom")
- sleep(2)
- rednet.broadcast(user, "chatroomlogin")
- --Function to color usernames
- function usercolor()
- if term.isColor() then
- term.setTextColor(8192)
- end
- end
- --Function to end color
- function endcolor()
- if term.isColor() then
- term.setTextColor(1)
- end
- end
- --Set up blank chatroom
- bestnumber = 8675309
- term.clear()
- enter = 257 --The enter key's number code
- backspace = 259 --The backspace key's number code
- relog = 342 --The relog command key, by default it is left alt
- exit = 346 --The exit command key, by default it is right alt
- rednet.open("back") --Change accordingly
- cpos = 1 --Cursor x position
- chat = 0
- mes = {}
- messages = 0
- --Set up screen
- term.setCursorPos(1, 15)
- print("---------------------------------------------------")
- term.setCursorPos(1, 16)
- usercolor()
- print("<"..user..">")
- endcolor()
- setup = fs.open("chatroomfiles/outgoing", "w")
- setup.write("test")
- setup.close()
- while true do
- event, x, y, z = os.pullEvent() --Waits for something to happen
- if event == "char" then --If a character is typed
- if cpos <= 50 then --Makes sure the cursor is on screen
- term.setCursorPos(cpos, 17)
- print(x) --Displays the character typed in message box
- mes[cpos] = x --Adds the typed character to a table that holds the message
- cpos = cpos + 1 --Moves cursor forward
- end
- elseif event == "key_up" then --Checks for other keys pressed
- if x == enter then
- cpos = 1 --Resets cursor
- for i = 1, 50 do --Clears message box
- term.setCursorPos(i, 17)
- print(" ")
- end
- mess = fs.open("chatroomfiles/outgoing", "w") --Writes the contents of the table to a file to be read later
- for i = 1,#mes do
- mess.write(mes[i])
- mes[i] = " "
- end
- mess.close()
- send = fs.open("chatroomfiles/outgoing", "r")
- finalmes = send.readAll() --Puts the message into a variable
- send.close()
- rednet.broadcast(finalmes, "chatroomsend") --Sends message to other computers if not a command
- if messages < 7 then --Adds message to file and chatroom on screen --code by bryceio
- messages = messages + 1
- newmessage = fs.open("chatroommessages/"..messages, "w")
- newmessage.writeLine(user)
- newmessage.writeLine(finalmes)
- newmessage.close()
- term.setCursorPos(1, messages*2-1)
- usercolor()
- print("<"..user.."> ")
- endcolor()
- term.setCursorPos(1, messages*2)
- print(finalmes)
- elseif messages >= 7 then
- for i = 1, 6 do
- fs.delete("chatroommessages/"..i)
- fs.copy("chatroommessages/"..i+1, "chatroommessages/"..i)
- end
- newmessage = fs.open("chatroommessages/7", "w")
- newmessage.writeLine(user)
- newmessage.writeLine(finalmes)
- newmessage.close()
- for i = 1, 7 do
- oldmessage = fs.open("chatroommessages/"..i, "r")
- term.setCursorPos(1, i*2-1)
- usercolor()
- print("<"..oldmessage.readLine().."> ")
- endcolor()
- term.setCursorPos(1, i*2)
- print(oldmessage.readLine())
- oldmessage.close()
- end
- end
- elseif x == backspace then --Removes the last character when backspace is pressed
- if cpos > 1 then
- cpos = cpos-1
- if cpos <= 50 then
- term.setCursorPos(cpos, 17)
- print(" ")
- mes[cpos] = " "
- end
- end
- elseif x == relog then --Allows you to change your username or update it to other chat members
- mes = {}
- term.setCursorPos(1, 16)
- print("Enter Username: ")
- user = read()
- rednet.broadcast(user, "chatroomlogin")
- sleep(0.5)
- term.setCursorPos(1, 16)
- usercolor()
- print("<"..user.."> ")
- endcolor()
- term.setCursorPos(1, 17)
- print(" ")
- elseif x == exit then --Exits the program
- term.clear()
- term.setCursorPos(1, 1)
- error()
- end
- elseif event == "rednet_message" and z == "chatroomsend" then --Receives messages
- if messages < 7 then
- messages = messages + 1
- if fs.exists("chatroomusers/"..x) then
- sentuser = fs.open("chatroomusers/"..x, "r")
- usersent = sentuser.readLine()
- sentuser.close()
- else
- usersent = ("ID: "..x) --Uses the computer's id if there is no registered username
- end
- newmessage = fs.open("chatroommessages/"..messages, "w")
- newmessage.writeLine(usersent)
- newmessage.writeLine(y)
- newmessage.close()
- term.setCursorPos(1, messages*2-1)
- usercolor()
- print("<"..usersent.."> ")
- endcolor()
- --code by bryceio
- term.setCursorPos(1, (messages*2))
- print(y)
- elseif messages >= 7 then
- for i = 1,6 do
- fs.delete("chatroommessages/"..i)
- fs.copy("chatroommessages/"..i+1, "chatroommessages/"..i)
- end
- if fs.exists("chatroomusers/"..x) then
- sentuser = fs.open("chatroomusers/"..x, "r")
- usersent = sentuser.readLine()
- sentuser.close()
- else
- usersent = ("ID: "..x)
- end
- newmessage = fs.open("chatroommessages/7", "w")
- newmessage.writeLine(usersent)
- newmessage.writeLine(y)
- newmessage.close()
- for i = 1, 7 do
- oldmessage = fs.open("chatroommessages/"..i, "r")
- term.setCursorPos(1, i*2-1)
- usercolor()
- print("<"..oldmessage.readLine().."> ")
- endcolor()
- term.setCursorPos(1, (i*2))
- print(oldmessage.readLine())
- oldmessage.close()
- end
- end
- elseif event == "rednet_message" and z == "chatroomlogin" then --Registers new usernames or updates old ones.
- newuser = fs.open("chatroomusers/"..x, "w")
- newuser.write(y)
- newuser.close()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement