Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [ --------------------------------------------------------------------- ] --
- -- [ RedNetChat ] --
- -- [ A public chat client for ComputerCraft ] --
- -- [ Created by GPG Incorporated. ] --
- -- [ --------------------------------------------------------------------- ] --
- -- [ --------------------------------------------------------------------- ] --
- local VERSION = "0.3 beta"
- local MODEM = nil
- local NICKNAME = nil
- local ACTIVE = false
- local BUFFER = {}
- local POINTER = 0
- local ONLINE = {}
- local ISONLINE = false
- -- [ --------------------------------------------------------------------- ] --
- -- Application entry
- function main()
- term.clear()
- term.setCursorPos(1, 1)
- print("Starting RedNetChat " .. VERSION)
- --sleep(0.5)
- if not setPeripherals() then
- return false
- end
- print("")
- print("")
- print("Peripherals are setup.")
- print("")
- term.write("Starting application")
- --simulateLoading(8)
- welcome()
- end
- -- Simulate a loading effect
- function simulateLoading(amount)
- local i
- sleep(0.2)
- term.write(" ")
- for i = 0, amount do
- term.write(".")
- sleep(0.2)
- end
- term.write " Done"
- end
- -- Set the attached peripherals. Opens rednet modem and warps monitor
- function setPeripherals()
- local i, side
- for i, side in pairs(redstone.getSides()) do
- if peripheral.isPresent(side) then
- if peripheral.getType(side) == "modem" then
- MODEM = side
- print("")
- term.write("Opening modem on " .. side)
- rednet.open(MODEM)
- --simulateLoading(6)
- end
- end
- end
- -- Exit with a fatal error when modem not found
- if MODEM == nil then
- print("[FATAL ERROR] No modem was detected. Plase attach a modem on any side.")
- return false
- end
- return true
- end
- -- Start the welcome screen
- function welcome()
- local x, y
- term.clear()
- writeHeader()
- print("")
- print("")
- print("Enter a nickname and press [enter].")
- print("")
- term.write("Nickname: ")
- x, y = term.getCursorPos()
- while NICKNAME == nil or NICKNAME == "" do
- term.setCursorPos(x, y)
- NICKNAME = read()
- execute("/online")
- appendBuffer("[RedNet Chat]: Type /help for a list of commands")
- end
- start()
- end
- -- Writes the screen header
- function writeHeader()
- term.setCursorPos(1, 1)
- term.write("RedNet Chat " .. VERSION .. " by GPG Incorporated")
- term.setCursorPos(1, 2)
- term.write("--------------------------------------------------")
- end
- -- Writes the list of online users
- function writeOnlineList()
- local i, v, count, x, y
- count = 0
- x, y = term.getCursorPos()
- term.setCursorPos(1, 17)
- term.write("--------------------------------------------------")
- term.setCursorPos(1, 18)
- term.clearLine()
- term.write("Online: ")
- for i, v in ipairs(ONLINE) do
- if i == 1 then
- term.write(ONLINE[i])
- else
- term.write(", " .. ONLINE[i])
- end
- count = count + 1
- end
- if count == 0 then
- term.write("There is nobody online at this moment")
- end
- term.setCursorPos(x, y)
- end
- -- Start the chat
- function start()
- term.clear()
- writeHeader()
- writeOnlineList()
- ACTIVE = true
- showBuffer()
- parallel.waitForAll(input, watchEvents, receive)
- end
- -- Stop the application
- function stop()
- ACTIVE = false
- end
- -- Reset the application
- function reset()
- execute("/offline")
- rednet.close(MODEM)
- sleep(2)
- os.reboot()
- end
- -- Watch all input to provide possible shortcuts (for example usernames)
- function watchEvents()
- local type, param, param2, param3, i, v
- while ACTIVE do
- type, param, param2, param3 = os.pullEvent()
- end
- end
- -- Handle input from the prompt
- function input()
- local message
- term.setCursorPos(1, 4)
- term.write("--------------------------------------------------")
- while ACTIVE do
- term.setCursorPos(1, 3)
- term.clearLine()
- term.write("Send > ")
- message = read()
- if message ~= nil and message ~= "" then
- execute(message, "local")
- end
- end
- end
- -- Send a message
- function send(message)
- local request = {protocol = "rnc", nickname = NICKNAME, message = message}
- rednet.broadcast(textutils.serialize(request))
- end
- -- Recieve a message
- function receive()
- local sender, message, distance, request
- while ACTIVE do
- sender, message, distance = rednet.receive(1)
- if message ~= nil and message ~= "" then
- request = textutils.unserialize(message)
- if request.protocol == "rnc" then
- if request.nickname ~= nil and request.nickname ~= "" then
- execute(request, "remote")
- end
- end
- end
- end
- end
- -- Execute a command or add a chat message
- function execute(message, source)
- local command
- if message.nickname ~= nil then
- executeRemote(message)
- return
- end
- if message:sub(0, 1) == "/" then
- command = message:sub(2)
- if command == "quit"
- or command == "reset"
- or command == "restart"
- or command == "reboot"
- or command == "stop"
- then
- appendBuffer("[RedNet Chat]: Restarting application")
- reset()
- elseif command == "online" then
- if not ISONLINE then
- send("/online")
- putOnline()
- appendBuffer("[RedNet Chat]: You are now online")
- ISONLINE = true
- else
- appendBuffer("[RedNet Chat]: You are already online")
- end
- elseif command == "offline" then
- if ISONLINE then
- send("/offline")
- takeOffline()
- appendBuffer("[RedNet Chat]: You are now offline")
- ISONLINE = false
- else
- appendBuffer("[RedNet Chat]: You are already offline")
- end
- elseif command:sub(0, 5) == "nick " then
- takeOffline()
- NICKNAME = command:sub(6)
- putOnline()
- appendBuffer("[RedNet Chat]: Your nickname has been changed")
- elseif command:sub(0, 5) == "slap " then
- appendBuffer(command:sub(6) .. " was slapped by " .. NICKNAME)
- elseif command == "help" then
- appendBuffer("[RedNet Chat] Commands:")
- appendBuffer("/quit : Exit the chat")
- end
- return
- end
- appendBuffer(NICKNAME .. ": " .. message)
- send(message)
- end
- --
- function putOnline(nickname)
- local i, v, exists
- if nickname == nil then
- nickname = NICKNAME
- end
- for i, v in ipairs(ONLINE) do
- if ONLINE[i] == nickname then
- exists = true
- end
- end
- if not exists then
- table.insert(ONLINE, nickname)
- end
- writeOnlineList()
- end
- --
- function takeOffline(nickname)
- local i, v
- if nickname == nil then
- nickname = NICKNAME
- end
- for i, v in ipairs(ONLINE) do
- if ONLINE[i] == nickname then
- table.remove(ONLINE, i)
- end
- end
- writeOnlineList()
- end
- --
- function executeRemote(request)
- local command
- if request.message:sub(0, 1) == "/" then
- command = request.message:sub(2)
- if command == "online" then
- putOnline(request.nickname)
- appendBuffer("[RedNet Chat]: " .. request.nickname .. " is now online")
- send("/metoo")
- elseif command == "offline" then
- takeOffline(request.nickname)
- appendBuffer("[RedNet Chat]: " .. request.nickname .. " is now offline")
- elseif command == "metoo" then
- putOnline(request.nickname)
- end
- return
- end
- appendBuffer(request.nickname .. ": " .. request.message)
- end
- --
- function appendBuffer(message)
- local length
- length = message:len()
- if length > 50 then
- table.insert(BUFFER, message:sub(1, 50))
- POINTER = POINTER + 1
- appendBuffer(message:sub(51))
- else
- table.insert(BUFFER, message)
- POINTER = POINTER + 1
- end
- showBuffer()
- end
- --
- function showBuffer()
- local i, line, bufferPointer, x, y
- if POINTER == 0 then
- return
- end
- x, y = term.getCursorPos()
- line = 5
- bufferPointer = -(11 - POINTER)
- for i = bufferPointer, bufferPointer + 11 do
- term.setCursorPos(1, line)
- term.clearLine()
- if BUFFER[i] ~= nil then
- term.write(tostring(BUFFER[i]))
- end
- line = line + 1
- end
- term.setCursorPos(x, y)
- end
- -- [ --------------------------------------------------------------------- ] --
- -- Fire up the application
- --welcome()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement