Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.setCursorPos(1, 10)
- center("Firewolf chat demo")
- term.setCursorPos(3, 12)
- write("Enter a username: ")
- local username = read()
- local inputColor = colors.orange
- local chatColor = colors.white
- local rawChats = firewolf.query("chat", {message = username .. " has joined the room"})
- local updateChat = function()
- local previousX, previousY = term.getCursorPos()
- if rawChats then
- term.setTextColor(chatColor)
- local chat = textutils.unserialize(rawChats)
- if chat then
- local previousPosition = term.getCursorPos(1, 2)
- for k,v in pairs(chat) do
- term.setCursorPos(1, k)
- term.clearLine()
- term.write(v)
- end
- else
- center("Invalid response from server! Retrying...")
- end
- else
- term.setCursorPos(1, 1)
- term.setTextColor(colors.red)
- center("Failed to connect to server! Retrying...")
- end
- term.setTextColor(inputColor)
- term.setCursorPos(previousX, previousY)
- end
- local chatUpdateLoop = function()
- while true do
- rawChats = firewolf.query("chat")
- updateChat()
- sleep(2)
- end
- end
- local readInput = function()
- while true do
- term.setTextColor(inputColor)
- term.setCursorPos(1, 18)
- term.write(string.rep("-", 51))
- term.setCursorPos(1, 19)
- term.clearLine()
- term.write("> ")
- local message = read()
- term.setCursorPos(1, 18)
- term.write(string.rep("-", 51))
- term.setCursorPos(1, 19)
- term.clearLine()
- updateChat()
- term.write("Sending...")
- if #message > 0 then
- updateChat()
- rawChats = firewolf.query("chat", {message = "<"..username.."> "..message})
- updateChat()
- end
- end
- end
- term.clear()
- parallel.waitForAny(chatUpdateLoop, readInput)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement