Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CHAT CLIENT
- -- by mcprog
- version = "1.0.1"
- SERVER_CHAN = 5
- CLIENT_CHAN = 1
- SERVER_MSG = "serverMsg"
- SUPER_CHAT = "/superChat"
- WHISPER = "/whisper"
- CMD = "cmd"
- SRC_SERVER = "server"
- op = "mc1030"
- name = nil
- local modem = peripheral.wrap("left")
- function split(s, delimiter)
- result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- function join(array, delimiter, indexStart)
- result = ""
- for i,v in ipairs(array) do
- if i >= indexStart then
- result = result.." "..v
- end
- end
- return result
- end
- function printColor(msg,textColor)
- term.setTextColor(textColor)
- --resetLine()
- print(msg)
- end
- function resetLine()
- term.clearLine()
- x,y = term.getCursorPos()
- term.setCursorPos(1, y)
- end
- function chat(input)
- message = {
- id = name,
- type = "chat",
- value = input
- }
- send(message)
- end
- function leave()
- printColor("Leaving chat room..", colors.blue)
- leftMsg = {
- id = name,
- type = "left"
- }
- send(leftMsg)
- end
- function parseInput(input)
- if input == "quit" or input == "exit" then
- leave()
- error()
- end
- if name == nil then
- printColor("Error: cannot have nil name.", colors.red)
- return
- end
- local s,e = string.find(input, "/")
- if s == 1 then
- splitArr = split(input, " ")
- rest = join(splitArr, "", 3)
- --printColor("rest="..rest, colors.yellow)
- local cmdMsg = {
- type = CMD,
- id = name,
- src = splitArr[1],
- value = splitArr[2],
- extra = rest
- }
- --printColor(textutils.serialise(cmdMsg), colors.yellow)
- send(cmdMsg)
- else
- chat(input)
- end
- end
- function send(message)
- modem.transmit(SERVER_CHAN, CLIENT_CHAN, message)
- end
- function readInput()
- term.setTextColor(colors.green)
- resetLine()
- write("You:")
- input = read()
- parseInput(input)
- end
- function listen()
- local event, side, senderChan, replyChan, message, dist = os.pullEvent("modem_message")
- if message then
- if message.type == "chat" and message.id ~= name then
- resetLine()
- printColor(message.id..": "..message.value, colors.white)
- elseif message.type == "join" and message.id ~= name then
- resetLine()
- printColor(message.id.." joined.", colors.lightBlue)
- elseif message.type == "left" and message.id ~= name then
- resetLine()
- printColor(message.id.." left.", colors.blue)
- elseif message.type == WHISPER and message.id == name then
- resetLine()
- printColor(message.src.." whispers:"..message.value, colors.lightGray)
- elseif message.type == SERVER_MSG then
- if message.value == "/kicked" and message.id == name then
- resetLine()
- printColor(message.id..", you were kicked by "..message.src.."!", colors.orange)
- leave()
- error()
- elseif message.value == "/shutdown" then
- resetLine()
- printColor(name..", the chat_server has been shut down.", colors.blue)
- error()
- end
- else
- --resetLine()
- --printColor(textutils.serialise(message), colors.yellow)
- end
- end
- end
- function start()
- modem.open(CLIENT_CHAN)
- term.clear()
- term.setCursorPos(1, 1)
- printColor("Welcome to the chat room.", colors.white)
- printColor("Version: "..version, colors.white)
- write("Name:")
- name = read()
- printColor("Joining chat room..", colors.white)
- joinMsg = {
- id = name,
- type = "join"
- }
- send(joinMsg)
- while true do
- parallel.waitForAny(readInput, listen)
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement