Advertisement
monsticraft

ComputerCraft Global Chat

Jul 18th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 KB | None | 0 0
  1. local session, nick
  2.  
  3. local term_w, term_h = term.getSize()
  4.  
  5. local function urlencode(msg)
  6.     local ok = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
  7.     local enc = ""
  8.     local fmt = "%%%02X"
  9.     for k = 1, msg:len() do
  10.         local ch = msg:sub(k, k)
  11.         if ok:find(ch, 1, true) then
  12.             enc = enc .. ch
  13.         else
  14.             enc = enc .. fmt:format(string.byte(ch))
  15.         end
  16.     end
  17.     return enc
  18. end
  19.  
  20. local function getLastIndex()
  21.     local f = http.get("http://immibis.awardspace.biz/cc/iim/get.php")
  22.     local last = tonumber(f.readLine())
  23.     f.close()
  24.     return last
  25. end
  26.  
  27. local function CheckUserName()
  28.   http.request("http://login.minecraft.net/?user=" ..nick.. "&password=" ..nickpass.. "&version=12")
  29.   local event, url, response = os.pullEvent()
  30.     if event == "http_success" then
  31.    _sResponse = { response.readAll() }
  32.    if string.find(tostring(_sResponse[1]), "deprecated") then
  33.         return true
  34.    elseif string.find(tostring(_sResponse[1]), "premium") then
  35.         return true
  36.     else
  37.         return false, "Senha incorreta. Reiniciando PC...."
  38.     end
  39.   response.close()
  40. end
  41. end
  42.  
  43.  
  44. local function startSession()
  45.     local f = http.get("http://immibis.awardspace.biz/cc/iim/start.php?nick="..urlencode(nick))
  46.     local response = f.readLine()
  47.     f.close()
  48.     if response:sub(1,2) == "OK" then
  49.         session = response:sub(3)
  50.     else
  51.         print(response)
  52.     end
  53. end
  54.  
  55. local function sendMessage(msg)
  56.     http.request("http://immibis.awardspace.biz/cc/iim/send.php?s="..urlencode(session).."&msg="..urlencode(msg))
  57. end
  58.  
  59. local buffer = {}
  60. local typing = ""
  61. local typing_pos = 0
  62. local typing_scroll = 0
  63. local history_pos = 0
  64. local history = {}
  65. local lastChatIndex = getLastIndex()
  66.  
  67. local function addLine(line)
  68.     if #buffer >= term_h - 1 then
  69.         table.remove(buffer, 1)
  70.     end
  71.     table.insert(buffer, line)
  72. end
  73.  
  74. local function displayChat(msg)
  75.     while msg:len() > term_w do
  76.         addLine(msg:sub(1,term_w))
  77.         msg = msg:sub(term_w + 1)
  78.     end
  79.     addLine(msg)
  80. end
  81.  
  82. local function updateCursor()
  83.     term.setCursorPos(typing_pos - typing_scroll + 3, term_h)
  84. end
  85.  
  86. local function updateInputLine()
  87.     term.setCursorPos(1, term_h)
  88.     term.clearLine()
  89.     term.write("> " .. typing:sub(typing_scroll + 1))
  90.     updateCursor()
  91. end
  92.  
  93. local function updateScreen()
  94.     for k = 1, term_h - 1 do
  95.         term.setCursorPos(1, k)
  96.         term.clearLine()
  97.         term.write(buffer[k] or "")
  98.     end
  99.     updateCursor()
  100. end
  101.  
  102. local pollURL
  103.  
  104. local function startPoll()
  105.     pollURL = "http://immibis.awardspace.biz/cc/iim/get.php?last=" .. lastChatIndex .. "&ping=" .. session
  106.     http.request(pollURL)
  107. end
  108.  
  109. local function handlePollResponse(f)
  110.     local any = false
  111.     while true do
  112.         local line = f.readLine()
  113.         if line == nil then break end
  114.         if line == "EMPTY" then break end
  115.         if line == "END" then break end
  116.         local index = line:find(" ")
  117.         if index ~= nil then
  118.             local id = tonumber(line:sub(1, index-1))
  119.             local msg = line:sub(index+1)
  120.             lastChatIndex = id
  121.             displayChat(msg)
  122.             any = true
  123.         end
  124.     end
  125.     f.close()
  126.     if any then updateScreen() end
  127. end
  128.  
  129. repeat
  130.     term.clear()
  131.     print(" Bem Vindo ao Chat Global MinesCraft!")
  132.     print ("")
  133.     print ("     _/      _/    _/_/_/    _/_/_/")
  134.     print ("    _/_/  _/_/  _/        _/")
  135.     print ("   _/  _/  _/    _/_/    _/")
  136.     print ("  _/      _/        _/  _/")
  137.     print (" _/      _/  _/_/_/      _/_/_/")
  138.     print ("")
  139.     print ("   MinesCraft Tekkit Survival")
  140.     print ("  http://www.mineScraft.com.br")
  141.     print ("")
  142.     print("  ########## AVISOS ##########")
  143.     print("  # Nenhum aviso no momento  #")
  144.     print("  ############################")
  145.     print("")
  146.     print("Login Minecraft.net: ")
  147.     nick = read()
  148.     print("Senha: ")
  149.     nickpass = read("*")
  150.     local bValid, err = CheckUserName()
  151.     if not bValid then
  152.         print(err)
  153.         sleep(2)
  154.         os.reboot()
  155.     end
  156.     startSession()
  157. until session ~= nil
  158.  
  159. local function updateHScroll()
  160.     if typing_pos < typing_scroll + 4 and typing_pos > 4 then
  161.         typing_scroll = math.max(0, typing_pos - 4)
  162.         return true
  163.     elseif typing_pos > typing_scroll + term_w - 7 then
  164.         typing_scroll = typing_pos - term_w + 7
  165.         return true
  166.     else
  167.         return false
  168.     end
  169. end
  170.  
  171. local function onInputLine(msg)
  172.     sendMessage(msg)
  173. end
  174.  
  175. lastChatIndex = 0
  176. startPoll()
  177. term.setCursorBlink(true)
  178. updateScreen()
  179. updateInputLine()
  180. while true do
  181.     local evt, p1, p2 = os.pullEvent()
  182.     if evt == "http_failure" and p1 == pollURL then
  183.         displayChat("Network error")
  184.     elseif evt == "http_success" and p1 == pollURL then
  185.         handlePollResponse(p2)
  186.         startPoll()
  187.     elseif evt == "http_success" then
  188.         p2.close()
  189.     elseif evt == "char" then
  190.         typing = typing:sub(1, typing_pos)..p1..typing:sub(typing_pos + 1)
  191.         typing_pos = typing_pos + 1
  192.         updateHScroll()
  193.         updateInputLine()
  194.     elseif evt == "key" then
  195.         if p1 == 28 then
  196.             -- Enter
  197.             onInputLine(typing)
  198.             table.insert(history, typing)
  199.             typing = ""
  200.             typing_pos = 0
  201.             typing_scroll = 0
  202.             history_pos = #history
  203.             updateInputLine()
  204.         elseif p1 == 203 then
  205.             -- Left
  206.             if typing_pos > 0 then
  207.                 typing_pos = typing_pos - 1
  208.                 if updateHScroll() then
  209.                     updateInputLine()
  210.                 else
  211.                     updateCursor()
  212.                 end
  213.             end
  214.         elseif p1 == 205 then
  215.             -- Right
  216.             if typing_pos < typing:len() then
  217.                 typing_pos = typing_pos + 1
  218.                 if updateHScroll() then
  219.                     updateInputLine()
  220.                 else
  221.                     updateCursor()
  222.                 end
  223.             end
  224.         elseif p1 == 14 then
  225.             -- Backspace
  226.             if typing_pos > 0 then
  227.                 typing = typing:sub(1, typing_pos-1) .. typing:sub(typing_pos+1)
  228.                 typing_pos = typing_pos - 1
  229.                 updateHScroll()
  230.                 updateInputLine()
  231.             end
  232.         elseif p1 == 200 then
  233.             -- Up
  234.             if history_pos > 1 then
  235.                 history_pos = history_pos - 1
  236.                 typing = history[history_pos]
  237.                 typing_pos = typing:len()
  238.                 updateHScroll()
  239.                 updateInputLine()
  240.             end
  241.         elseif p1 == 208 then
  242.             -- Down
  243.             if history_pos < #history then
  244.                 history_pos = history_pos + 1
  245.                 typing = history[history_pos]
  246.                 typing_pos = typing:len()
  247.                 updateHScroll()
  248.                 updateInputLine()
  249.             end
  250.         end
  251.     end
  252. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement