Advertisement
mc1030

chat_client

Nov 22nd, 2020 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. -- CHAT CLIENT
  2. -- by mcprog
  3. version = "1.0.1"
  4.  
  5. SERVER_CHAN = 5
  6. CLIENT_CHAN = 1
  7.  
  8. SERVER_MSG = "serverMsg"
  9. SUPER_CHAT = "/superChat"
  10. WHISPER = "/whisper"
  11. CMD = "cmd"
  12.  
  13. SRC_SERVER = "server"
  14.  
  15. op = "mc1030"
  16.  
  17. name = nil
  18. local modem = peripheral.wrap("left")
  19.  
  20. function split(s, delimiter)
  21.     result = {};
  22.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  23.         table.insert(result, match);
  24.     end
  25.     return result;
  26. end
  27.  
  28. function join(array, delimiter, indexStart)
  29.     result = ""
  30.     for i,v in ipairs(array) do
  31.         if i >= indexStart then
  32.             result = result.." "..v
  33.         end
  34.     end
  35.     return result
  36. end
  37.  
  38. function printColor(msg,textColor)
  39.     term.setTextColor(textColor)
  40.     --resetLine()
  41.     print(msg)
  42. end
  43.  
  44. function resetLine()
  45.     term.clearLine()
  46.     x,y = term.getCursorPos()
  47.     term.setCursorPos(1, y)
  48. end
  49.  
  50. function chat(input)
  51.     message = {
  52.         id = name,
  53.         type = "chat",
  54.         value = input
  55.     }
  56.     send(message)
  57.    
  58. end
  59.  
  60. function leave()
  61.     printColor("Leaving chat room..", colors.blue)
  62.     leftMsg = {
  63.         id = name,
  64.         type = "left"
  65.     }
  66.     send(leftMsg)
  67. end
  68.  
  69. function parseInput(input)
  70.     if input == "quit" or input == "exit" then
  71.         leave()
  72.         error()
  73.     end
  74.     if name == nil then
  75.         printColor("Error: cannot have nil name.", colors.red)
  76.         return
  77.     end
  78.  
  79.     local s,e = string.find(input, "/")
  80.     if s == 1 then
  81.         splitArr = split(input, " ")
  82.         rest = join(splitArr, "", 3)
  83.         --printColor("rest="..rest, colors.yellow)
  84.         local cmdMsg = {
  85.             type = CMD,
  86.             id = name,
  87.             src = splitArr[1],
  88.             value = splitArr[2],
  89.             extra = rest
  90.         }
  91.         --printColor(textutils.serialise(cmdMsg), colors.yellow)
  92.         send(cmdMsg)
  93.     else
  94.         chat(input)
  95.     end
  96. end
  97.  
  98. function send(message)
  99.     modem.transmit(SERVER_CHAN, CLIENT_CHAN, message)
  100. end
  101.    
  102. function readInput()
  103.     term.setTextColor(colors.green)
  104.     resetLine()
  105.     write("You:")
  106.     input = read()
  107.     parseInput(input)
  108. end
  109.  
  110. function listen()
  111.     local event, side, senderChan, replyChan, message, dist = os.pullEvent("modem_message")
  112.     if message then
  113.         if message.type == "chat" and message.id ~= name then
  114.             resetLine()
  115.             printColor(message.id..": "..message.value, colors.white)
  116.         elseif message.type == "join" and message.id ~= name then
  117.             resetLine()
  118.             printColor(message.id.." joined.", colors.lightBlue)
  119.         elseif message.type == "left" and message.id ~= name then
  120.             resetLine()
  121.             printColor(message.id.." left.", colors.blue)
  122.         elseif message.type == WHISPER and message.id == name then
  123.             resetLine()
  124.             printColor(message.src.." whispers:"..message.value, colors.lightGray)
  125.         elseif message.type == SERVER_MSG then
  126.             if message.value == "/kicked" and message.id == name then
  127.                 resetLine()
  128.                 printColor(message.id..", you were kicked by "..message.src.."!", colors.orange)
  129.                 leave()
  130.                 error()
  131.             elseif message.value == "/shutdown" then
  132.                 resetLine()
  133.                 printColor(name..", the chat_server has been shut down.", colors.blue)
  134.                 error()
  135.             end
  136.         else
  137.             --resetLine()
  138.             --printColor(textutils.serialise(message), colors.yellow)
  139.         end
  140.     end
  141. end
  142.  
  143. function start()
  144.     modem.open(CLIENT_CHAN)
  145.     term.clear()
  146.     term.setCursorPos(1, 1)
  147.     printColor("Welcome to the chat room.", colors.white)
  148.     printColor("Version: "..version, colors.white)
  149.    
  150.     write("Name:")
  151.     name = read()
  152.  
  153.     printColor("Joining chat room..", colors.white)
  154.     joinMsg = {
  155.         id = name,
  156.         type = "join"
  157.     }
  158.     send(joinMsg)
  159.  
  160.     while true do
  161.         parallel.waitForAny(readInput, listen)
  162.     end
  163. end
  164.  
  165. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement