Advertisement
Guest User

index

a guest
Oct 10th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.49 KB | None | 0 0
  1. while true do
  2.     term.setBackgroundColor(colors.white)
  3.     term.setTextColor(colors.black)
  4.     term.clear()
  5.     term.setCursorPos(1, 10)
  6.     center("Chattr - by Chervilpaw")
  7.     term.setCursorPos(3, 12)
  8.     write("Username: ")
  9.     term.setCursorPos(3, 14)
  10.     write("Password: ")
  11.     term.setCursorPos(13, 12)
  12.     local username = read()
  13.     term.setCursorPos(13, 14)
  14.     local password = read("*")
  15.  
  16.     term.setCursorPos(3, 18)
  17.     term.setTextColor(colors.red)
  18.     local t = firewolf.query("login", {user=username, pass=password})
  19.     if t == "valid" then
  20.         term.setTextColor(colors.green)
  21.         write("Success!")
  22.         sleep(1)
  23.         break
  24.     elseif t ==  "invalid" then
  25.         write("Wrong password/invalid username")
  26.         sleep(1)
  27.     elseif t == "noparm" then
  28.         write("Forgot to enter username/password.")
  29.         sleep(1)
  30.  elseif t == false then
  31.   write("Query errored.")
  32.   sleep(2)
  33.     else
  34.         write("Unknown error.")
  35.         sleep(1)
  36.     end
  37. end
  38. local inputColor = colors.gray
  39. local chatColor = colors.black
  40. local rawChats = firewolf.query("chat", {user = username, pass = password, message = "I joined."})
  41.  
  42. local updateChat = function()
  43.     local previousX, previousY = term.getCursorPos()
  44.     if rawChats then
  45.         term.setTextColor(chatColor)
  46.         local chat = textutils.unserialize(rawChats)
  47.         if chat then
  48.             local previousPosition = term.getCursorPos(1, 2)
  49.             for k,v in pairs(chat) do
  50.                 term.setCursorPos(1, k)
  51.                 term.clearLine()
  52.                 if v.user then
  53.                     term.write("<"..tostring(v.user)..">: "..tostring(v))
  54.                 else
  55.                     term.write(v)
  56.                 end
  57.             end
  58.         else
  59.             center("Invalid response from server! Retrying...")
  60.         end
  61.     else
  62.         term.setCursorPos(1, 1)
  63.         term.setTextColor(colors.red)
  64.         center("Failed to connect to server! Retrying...")
  65.     end
  66.     term.setTextColor(inputColor)
  67.     term.setCursorPos(previousX, previousY)
  68. end
  69.  
  70. local chatUpdateLoop = function()
  71.     while true do
  72.         rawChats = firewolf.query("chat")
  73.         updateChat()
  74.         sleep(2)
  75.     end
  76. end
  77.  
  78. local readInput = function()
  79.     while true do
  80.         term.setTextColor(inputColor)
  81.         term.setCursorPos(1, 18)
  82.         term.write(string.rep("-", 51))
  83.         term.setCursorPos(1, 19)
  84.         term.clearLine()
  85.         term.write("> ")
  86.         local message = read()
  87.         term.setCursorPos(1, 18)
  88.         term.write(string.rep("-", 51))
  89.         term.setCursorPos(1, 19)
  90.         term.clearLine()
  91.         updateChat()
  92.         term.write("Sending...")
  93.         if #message > 0  then
  94.             updateChat()
  95.             rawChats = firewolf.query("chat", {message = message, user = username, pass = password})
  96.             updateChat()
  97.         end
  98.     end
  99. end
  100.  
  101. term.clear()
  102. parallel.waitForAny(chatUpdateLoop, readInput)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement