Advertisement
CelticCoder

clientMessage

May 19th, 2024 (edited)
157
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.01 KB | None | 0 0
  1. -- Set up modem
  2. rednet.open("back") -- Replace "right" with the side your modem is attached to
  3.  
  4. -- Function to send a message to a chat
  5. local function sendMessage(serverID, command, chatID, username, text)
  6.     rednet.send(serverID, command .. " " .. chatID .. " " .. username .. " " .. text)
  7.     local id, reply = rednet.receive()
  8.     if reply == "true" then
  9.         print("Message sent successfully!")
  10.     else
  11.         print("Failed to send message.")
  12.     end
  13. end
  14.  
  15. -- Function to retrieve messages from a chat
  16. local function retrieveMessages(serverID, chatID, username)
  17.     rednet.send(serverID, "retrieve " .. chatID .. " " .. username)
  18.     local _, reply = rednet.receive(nil, 2)
  19.     return reply
  20. end
  21.  
  22.  
  23. local function updateChat()
  24.     while true do
  25.         local _, reply = rednet.receive()
  26.         if reply == "update" then
  27.             start = true
  28.         end
  29.     end
  30. end
  31.  
  32. local function chatControls(serverID, chatID, username)
  33.     local event, key = os.pullEvent("key")
  34.     if key == keys.up then
  35.         scrollPosition = math.max(1, scrollPosition - 1)
  36.     elseif key == keys.down then
  37.         scrollPosition = math.min(#dmessages - maxLines + 1, scrollPosition + 1)
  38.     elseif key == keys.backspace then
  39.         exitloop = true
  40.     end
  41.     if scrollPosition >= #dmessages - maxLines + 1 then
  42.         bottom = true
  43.         if key == keys.enter then
  44.             local text = read()
  45.             sendMessage(serverID, "send", chatID, username, text)
  46.         end
  47.     else
  48.         bottom = false
  49.     end
  50. end
  51.  
  52. local function displayNewChat(serverID, chatID, username)
  53.     while true do
  54.         if start then
  55.             dmessages = retrieveMessages(serverID, chatID, username)
  56.             if dmessages == nil then
  57.                 start = false
  58.                 print("Error")
  59.                 os.sleep(2)
  60.                 return nil
  61.             elseif dmessages == "false" then
  62.                 start = false
  63.                 print("Access Denied")
  64.                 os.sleep(2)
  65.                 return nil
  66.             else
  67.                 dmessages[1] = "Users in Chat: " .. dmessages[1] .. "\n"
  68.                 table.insert(dmessages, "----------------------")
  69.                 table.insert(dmessages, " ")
  70.                 if bottom then
  71.                     scrollPosition = math.min(#dmessages - maxLines + 1, scrollPosition + 1)
  72.                 end
  73.                 startIndex = math.max(1, scrollPosition)
  74.                 endIndex = math.min(#dmessages, scrollPosition + maxLines - 1)
  75.                 term.clear()
  76.                 term.setCursorPos(1, 1)
  77.                 for i = startIndex, endIndex do
  78.                     print(dmessages[i])
  79.                 end
  80.                 start = false
  81.             end
  82.         end
  83.         os.sleep(0.001)
  84.     end
  85. end
  86.  
  87. local function displayMessages(serverID, chatID, username)
  88.     start = false  
  89.     exitloop = false
  90.     scrollPosition = 1
  91.     dmessages = retrieveMessages(serverID, chatID, username)
  92.     dmessages[1] = "Users in Chat: " .. dmessages[1] .. "\n"
  93.     table.insert(dmessages, "----------------------")
  94.     table.insert(dmessages, " ")
  95.     maxLines = 15
  96.     local startIndex = math.max(1, scrollPosition)
  97.     local endIndex = math.min(#dmessages, scrollPosition + maxLines - 1)
  98.     while true do
  99.         term.clear()
  100.         term.setCursorPos(1, 1)
  101.         -- Display the database content after the instruction
  102.         for i = startIndex, endIndex do
  103.             print(dmessages[i])
  104.         end
  105.  
  106.         parallel.waitForAny(
  107.         function() chatControls(serverID, chatID, username) end,
  108.         function() displayNewChat(serverID, chatID, username) end
  109.         )
  110.         startIndex = math.max(1, scrollPosition)
  111.         endIndex = math.min(#dmessages, scrollPosition + maxLines - 1)
  112.         if exitloop then
  113.             break
  114.         end
  115.     end
  116. end      
  117.  
  118.  
  119. -- Function to create a chat
  120. local function createChat(serverID, chatID, username)
  121.     rednet.send(serverID, "create " .. chatID .. " " .. username)
  122.     local _, reply = rednet.receive(nil, 3)
  123.     print(reply)
  124.     os.sleep(1)
  125.     if reply == "true" then
  126.         print("Chat " .. chatID .. " created successfully!")
  127.     else
  128.         print("Failed to create chat " .. chatID .. ".")
  129.     end
  130. end
  131.  
  132. -- Function to delete a chat
  133. local function deleteChat(serverID, chatID, username)
  134.     rednet.send(serverID, "delete " .. chatID .. " " .. username)
  135.     local _, reply = rednet.receive(nil, 3)
  136.     if reply == nil then
  137.         print("Failed to delete chat " .. chatID .. ".")
  138.     elseif reply == "denied" then
  139.         print("Failed to delete chat " .. chatID .. ". Not Owner of Chat")
  140.     else
  141.         print("Chat " .. chatID .. " deleted successfully!")
  142.     end
  143. end
  144.  
  145. local function printChats(serverID, username)
  146.     rednet.send(serverID, "chats " .. " " .. username)
  147.     local _, reply = rednet.receive(nil, 3)
  148.     if type(reply) == "String" then
  149.         if reply == "no" then
  150.             print("Failed to connect to Server")
  151.         elseif reply == "none" then
  152.             print("No Chats Found")
  153.         end
  154.     else
  155.         for _, chatname in pairs(reply) do
  156.             print(chatname)
  157.         end
  158.     end
  159. end
  160.  
  161.  
  162. function text(sender, serverID)
  163.     while true do
  164.         print("Select an action: ")
  165.         print("1. Display Chats")
  166.         print("2. Enter a Chat")
  167.         print("3. Create a chat")
  168.         print("4. Delete a chat")
  169.         print("5. Add User to Chat")
  170.         print("6. Remove User from Chat")
  171.         print("7. Exit")
  172.    
  173.         local choice = tonumber(read())
  174.  
  175.         if choice == 1 then
  176.             print("Chats Available: ")
  177.             printChats(serverID, sender)
  178.             print("Type Anything to Continue")
  179.             read()
  180.             term.clear()
  181.             term.setCursorPos(1, 1)
  182.         elseif choice == 2 then
  183.             print("Enter chat ID to retrieve messages from:")
  184.             local chatID = read()
  185.             reply = retrieveMessages(serverID, chatID, sender)
  186.             if reply == nil then
  187.                 print("Chat does not Exist")
  188.                 os.sleep(3)
  189.             elseif reply == "false" then
  190.                 print("Access to Chat Denied")
  191.                 os.sleep(3)
  192.                 term.clear()
  193.                 term.setCursorPos(1, 1)
  194.             else
  195.                 term.clear()
  196.                 term.setCursorPos(1, 1)
  197.                 print("Text Controls are: \n")
  198.                 print("Up and Down to Scroll \n")
  199.                 print("Enter at Bottom of Chat to type \n")
  200.                 print("Backspace to Leave Chat \n")
  201.                 print("Type Anything to Continue")
  202.                 read()
  203.                 term.clear()
  204.                 term.setCursorPos(1, 1)
  205.                 parallel.waitForAny(
  206.                     function() displayMessages(serverID, chatID, sender) end,
  207.                     function() updateChat() end
  208.                 )
  209.             end
  210.         elseif choice == 3 then
  211.             print("Enter chat ID to create:")
  212.             local chatID = read()
  213.             createChat(serverID, chatID, sender)
  214.         elseif choice == 4 then
  215.             print("Enter chat ID to delete:")
  216.             local chatID = read()
  217.             deleteChat(serverID, chatID, sender)
  218.         elseif choice == 5 then
  219.             print("Enter chat ID:")
  220.             local chatID = read()
  221.             print("Enter User to Add:")
  222.             local auser = read()
  223.             sendMessage(serverID, "add", chatID, sender, auser)
  224.         elseif choice == 6 then
  225.             print("Enter chat ID:")
  226.             local chatID = read()
  227.             print("Enter User to Remove:")
  228.             local ruser = read()
  229.             sendMessage(serverID, "remove", chatID, sender, ruser)
  230.  
  231.         elseif choice == 7 then
  232.             break
  233.         else
  234.             print("Invalid choice. Please try again.")
  235.         end
  236.     end
  237. end
  238.  
Advertisement
Comments
  • CelticCoder
    187 days
    # text 0.21 KB | 0 0
    1. 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