Advertisement
1lann

Firewolf Chat Demo index

Aug 10th, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1. term.setCursorPos(1, 10)
  2. center("Firewolf chat demo")
  3. term.setCursorPos(3, 12)
  4. write("Enter a username: ")
  5. local username = read()
  6. local inputColor = colors.orange
  7. local chatColor = colors.white
  8. local rawChats = firewolf.query("chat", {message = username .. " has joined the room"})
  9.  
  10. local updateChat = function()
  11.     local previousX, previousY = term.getCursorPos()
  12.     if rawChats then
  13.         term.setTextColor(chatColor)
  14.         local chat = textutils.unserialize(rawChats)
  15.         if chat then
  16.             local previousPosition = term.getCursorPos(1, 2)
  17.             for k,v in pairs(chat) do
  18.                 term.setCursorPos(1, k)
  19.                 term.clearLine()
  20.                 term.write(v)
  21.             end
  22.         else
  23.             center("Invalid response from server! Retrying...")
  24.         end
  25.     else
  26.         term.setCursorPos(1, 1)
  27.         term.setTextColor(colors.red)
  28.         center("Failed to connect to server! Retrying...")
  29.     end
  30.     term.setTextColor(inputColor)
  31.     term.setCursorPos(previousX, previousY)
  32. end
  33.  
  34. local chatUpdateLoop = function()
  35.     while true do
  36.         rawChats = firewolf.query("chat")
  37.         updateChat()
  38.         sleep(2)
  39.     end
  40. end
  41.  
  42. local readInput = function()
  43.     while true do
  44.         term.setTextColor(inputColor)
  45.         term.setCursorPos(1, 18)
  46.         term.write(string.rep("-", 51))
  47.         term.setCursorPos(1, 19)
  48.         term.clearLine()
  49.         term.write("> ")
  50.         local message = read()
  51.         term.setCursorPos(1, 18)
  52.         term.write(string.rep("-", 51))
  53.         term.setCursorPos(1, 19)
  54.         term.clearLine()
  55.         updateChat()
  56.         term.write("Sending...")
  57.         if #message > 0  then
  58.             updateChat()
  59.             rawChats = firewolf.query("chat", {message = "<"..username.."> "..message})
  60.             updateChat()
  61.         end
  62.     end
  63. end
  64.  
  65. term.clear()
  66. parallel.waitForAny(chatUpdateLoop, readInput)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement