Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Set up modem
- rednet.open("back") -- Replace "right" with the side your modem is attached to
- -- Function to send a message to a chat
- local function sendMessage(serverID, command, chatID, username, text)
- rednet.send(serverID, command .. " " .. chatID .. " " .. username .. " " .. text)
- local id, reply = rednet.receive()
- if reply == "true" then
- print("Message sent successfully!")
- else
- print("Failed to send message.")
- end
- end
- -- Function to retrieve messages from a chat
- local function retrieveMessages(serverID, chatID, username)
- rednet.send(serverID, "retrieve " .. chatID .. " " .. username)
- local _, reply = rednet.receive(nil, 2)
- return reply
- end
- local function updateChat()
- while true do
- local _, reply = rednet.receive()
- if reply == "update" then
- start = true
- end
- end
- end
- local function chatControls(serverID, chatID, username)
- local event, key = os.pullEvent("key")
- if key == keys.up then
- scrollPosition = math.max(1, scrollPosition - 1)
- elseif key == keys.down then
- scrollPosition = math.min(#dmessages - maxLines + 1, scrollPosition + 1)
- elseif key == keys.backspace then
- exitloop = true
- end
- if scrollPosition >= #dmessages - maxLines + 1 then
- bottom = true
- if key == keys.enter then
- local text = read()
- sendMessage(serverID, "send", chatID, username, text)
- end
- else
- bottom = false
- end
- end
- local function displayNewChat(serverID, chatID, username)
- while true do
- if start then
- dmessages = retrieveMessages(serverID, chatID, username)
- if dmessages == nil then
- start = false
- print("Error")
- os.sleep(2)
- return nil
- elseif dmessages == "false" then
- start = false
- print("Access Denied")
- os.sleep(2)
- return nil
- else
- dmessages[1] = "Users in Chat: " .. dmessages[1] .. "\n"
- table.insert(dmessages, "----------------------")
- table.insert(dmessages, " ")
- if bottom then
- scrollPosition = math.min(#dmessages - maxLines + 1, scrollPosition + 1)
- end
- startIndex = math.max(1, scrollPosition)
- endIndex = math.min(#dmessages, scrollPosition + maxLines - 1)
- term.clear()
- term.setCursorPos(1, 1)
- for i = startIndex, endIndex do
- print(dmessages[i])
- end
- start = false
- end
- end
- os.sleep(0.001)
- end
- end
- local function displayMessages(serverID, chatID, username)
- start = false
- exitloop = false
- scrollPosition = 1
- dmessages = retrieveMessages(serverID, chatID, username)
- dmessages[1] = "Users in Chat: " .. dmessages[1] .. "\n"
- table.insert(dmessages, "----------------------")
- table.insert(dmessages, " ")
- maxLines = 15
- local startIndex = math.max(1, scrollPosition)
- local endIndex = math.min(#dmessages, scrollPosition + maxLines - 1)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- -- Display the database content after the instruction
- for i = startIndex, endIndex do
- print(dmessages[i])
- end
- parallel.waitForAny(
- function() chatControls(serverID, chatID, username) end,
- function() displayNewChat(serverID, chatID, username) end
- )
- startIndex = math.max(1, scrollPosition)
- endIndex = math.min(#dmessages, scrollPosition + maxLines - 1)
- if exitloop then
- break
- end
- end
- end
- -- Function to create a chat
- local function createChat(serverID, chatID, username)
- rednet.send(serverID, "create " .. chatID .. " " .. username)
- local _, reply = rednet.receive(nil, 3)
- print(reply)
- os.sleep(1)
- if reply == "true" then
- print("Chat " .. chatID .. " created successfully!")
- else
- print("Failed to create chat " .. chatID .. ".")
- end
- end
- -- Function to delete a chat
- local function deleteChat(serverID, chatID, username)
- rednet.send(serverID, "delete " .. chatID .. " " .. username)
- local _, reply = rednet.receive(nil, 3)
- if reply == nil then
- print("Failed to delete chat " .. chatID .. ".")
- elseif reply == "denied" then
- print("Failed to delete chat " .. chatID .. ". Not Owner of Chat")
- else
- print("Chat " .. chatID .. " deleted successfully!")
- end
- end
- local function printChats(serverID, username)
- rednet.send(serverID, "chats " .. " " .. username)
- local _, reply = rednet.receive(nil, 3)
- if type(reply) == "String" then
- if reply == "no" then
- print("Failed to connect to Server")
- elseif reply == "none" then
- print("No Chats Found")
- end
- else
- for _, chatname in pairs(reply) do
- print(chatname)
- end
- end
- end
- function text(sender, serverID)
- while true do
- print("Select an action: ")
- print("1. Display Chats")
- print("2. Enter a Chat")
- print("3. Create a chat")
- print("4. Delete a chat")
- print("5. Add User to Chat")
- print("6. Remove User from Chat")
- print("7. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- print("Chats Available: ")
- printChats(serverID, sender)
- print("Type Anything to Continue")
- read()
- term.clear()
- term.setCursorPos(1, 1)
- elseif choice == 2 then
- print("Enter chat ID to retrieve messages from:")
- local chatID = read()
- reply = retrieveMessages(serverID, chatID, sender)
- if reply == nil then
- print("Chat does not Exist")
- os.sleep(3)
- elseif reply == "false" then
- print("Access to Chat Denied")
- os.sleep(3)
- term.clear()
- term.setCursorPos(1, 1)
- else
- term.clear()
- term.setCursorPos(1, 1)
- print("Text Controls are: \n")
- print("Up and Down to Scroll \n")
- print("Enter at Bottom of Chat to type \n")
- print("Backspace to Leave Chat \n")
- print("Type Anything to Continue")
- read()
- term.clear()
- term.setCursorPos(1, 1)
- parallel.waitForAny(
- function() displayMessages(serverID, chatID, sender) end,
- function() updateChat() end
- )
- end
- elseif choice == 3 then
- print("Enter chat ID to create:")
- local chatID = read()
- createChat(serverID, chatID, sender)
- elseif choice == 4 then
- print("Enter chat ID to delete:")
- local chatID = read()
- deleteChat(serverID, chatID, sender)
- elseif choice == 5 then
- print("Enter chat ID:")
- local chatID = read()
- print("Enter User to Add:")
- local auser = read()
- sendMessage(serverID, "add", chatID, sender, auser)
- elseif choice == 6 then
- print("Enter chat ID:")
- local chatID = read()
- print("Enter User to Remove:")
- local ruser = read()
- sendMessage(serverID, "remove", chatID, sender, ruser)
- elseif choice == 7 then
- break
- else
- print("Invalid choice. Please try again.")
- end
- end
- end
Advertisement
Comments
-
- Add a parallel between chatControls and a new function called displayChat() [parameters might be startindex and endindex, but might not need to as this function is within the function where those variables are changed[
Add Comment
Please, Sign In to add comment
Advertisement