Advertisement
GravityScore

CCleverBot

Mar 2nd, 2013
1,712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.03 KB | None | 0 0
  1.  
  2. --  
  3. --  CCleverBot
  4. --  Made by 1lann and GravityScore
  5. --  
  6.  
  7.  
  8. --  Variables
  9.  
  10. local version = "1.1"
  11.  
  12. local responseURL = "http://firewolfcc.com/ccleverbot/response.php"
  13. local event_updateChat = "ccleverbot_updateChatEvent"
  14. local event_continue = "ccleverbot_continueEvent"
  15. local event_exit = "ccleverbot_exitEvent"
  16.  
  17. local chatHistory = {}
  18. local chatLog = {}
  19. local w, h = term.getSize()
  20.  
  21. --  Drawing
  22.  
  23. local function centerPrint(text, y)
  24.     if type(text) == "table" then for _, v in pairs(text) do centerPrint(v) end
  25.     else
  26.         local x, y = term.getCursorPos()
  27.         term.setCursorPos((w + 2)/2 - text:len()/2, ny or y)
  28.         print(text)
  29.     end
  30. end
  31.  
  32. local function drawChat(chatData, offset, thinking, scrolled)
  33.     local a, b = false, false
  34.     for i = 1, 10 do
  35.         term.setCursorPos(3, 16 - i)
  36.         term.clearLine()
  37.     end
  38.  
  39.     for i = 1, 10 do
  40.         local c = chatData[#chatData + 1 - i + offset]
  41.  
  42.         if #chatData + 1 - i + offset < 1 then break end
  43.         term.setCursorPos(3, 16 - i)
  44.  
  45.         if thinking and i == 1 then
  46.             term.setTextColor(colors.lightGray)
  47.             write("...")
  48.             offset = offset + 1
  49.         else
  50.             if c[2] == "user" then
  51.                 term.setTextColor(colors.black)
  52.                 write(c[1])
  53.             else
  54.                 term.setTextColor(colors.lightBlue)
  55.                 if i == 1 and scrolled ~= true then
  56.                     a = true
  57.                 elseif i == 2 and scrolled ~= true then
  58.                     b = true
  59.                 else
  60.                     write(c[1])
  61.                 end
  62.             end
  63.         end
  64.     end
  65.  
  66.     if a then
  67.         term.setTextColor(colors.lightBlue)
  68.         if b then
  69.             term.setCursorPos(3, 14)
  70.             textutils.slowWrite(chatData[#chatData - 1][1])
  71.         end
  72.  
  73.         term.setCursorPos(3, 15)
  74.         textutils.slowWrite(chatData[#chatData][1])
  75.     end
  76.  
  77.     os.queueEvent(event_continue)
  78. end
  79.  
  80.  
  81. --  Interface
  82.  
  83. local function interface()
  84.     local scrollPos = 0
  85.     local updateChatLog = nil
  86.     while true do
  87.         local e, p1, p2 = os.pullEvent()
  88.         if e == event_updateChat then
  89.             updateChatLog = textutils.unserialize(p1)
  90.             scrollPos = 0
  91.             drawChat(updateChatLog, 0, p2)
  92.         elseif e == event_exit then
  93.             return
  94.         elseif e == "mouse_scroll" then
  95.             if scrollPos + p1 <= 0 and updateChatLog then
  96.                 if #updateChatLog > 10 and #updateChatLog >= (scrollPos+p1)*-1+10 then
  97.                     scrollPos = scrollPos+p1
  98.                     local scrollChat = {}
  99.                     for i = 10, 1, -1 do
  100.                         scrollChat[i] = updateChatLog[#updateChatLog-(10-i)+scrollPos]
  101.                     end
  102.                     local x, y = term.getCursorPos()
  103.                     drawChat(scrollChat, 0, nil, true)
  104.                     term.setCursorPos(x, y)
  105.                     term.setTextColor(colors.gray)
  106.                 end
  107.             end
  108.         end
  109.     end
  110. end
  111.  
  112.  
  113. --  Input
  114.  
  115. local function input()
  116.     while true do
  117.         -- Read
  118.         term.setCursorPos(3, 17)
  119.         term.setTextColor(colors.gray)
  120.         term.clearLine()
  121.         write("> ")
  122.         local inputData = read(nil, chatHistory):gsub("^%s*(.-)%s*$", "%1")
  123.         table.insert(chatHistory, inputData)
  124.         if inputData == "/exit" then
  125.             os.queueEvent(event_exit)
  126.             return
  127.         end
  128.  
  129.         -- Parse input
  130.         if inputData == "" then inputData = "Hello." end
  131.         inputData = inputData:sub(1, 1):upper() .. inputData:sub(2, -1)
  132.         if not inputData:sub(-1, -1):find("[\.\?!,]") then inputData = inputData .. "." end
  133.         if inputData:len() > 45 and not inputData:sub(1, 45):find(" ") then
  134.             inputData = inputData:sub(1, 45) .. " " .. inputData:sub(46, -1)
  135.         end
  136.  
  137.         -- Clear
  138.         term.setCursorPos(3, 17)
  139.         term.clearLine()
  140.         write("> ")
  141.  
  142.         if inputData:len() > 46 then
  143.             local spaceData = {}
  144.             local loopNum = 0
  145.             while true do
  146.                 loopNum = inputData:find(" ", loopNum + 1, 1, true)
  147.                 table.insert(spaceData, loopNum)
  148.                 if type(loopNum) ~= "number" then
  149.                     table.insert(spaceData, 47)
  150.                     break
  151.                 end
  152.             end
  153.  
  154.             for k, v in ipairs(spaceData) do
  155.                 if v > 46 then
  156.                     chatLog[#chatLog + 1] = {inputData:sub(1, spaceData[k - 1] - 1), "user"}
  157.                     chatLog[#chatLog + 1] = {inputData:sub(spaceData[k - 1] + 1, -1), "user"}
  158.                     break
  159.                 end
  160.             end
  161.         else
  162.             chatLog[#chatLog + 1] = {inputData, "user"}
  163.         end
  164.  
  165.         os.queueEvent(event_updateChat, textutils.serialize(chatLog), true)
  166.  
  167.         -- Get response
  168.         local response = http.post(responseURL, "input=" .. textutils.urlEncode(inputData))
  169.         if response then
  170.             local responseData = response.readAll()
  171.             if responseData:len() > 46 then
  172.                 local spaceData = {}
  173.                 local loopNum = 0
  174.                 while true do
  175.                     loopNum = responseData:find(" ", loopNum + 1, 1, true)
  176.                     table.insert(spaceData, loopNum)
  177.                     if type(loopNum) ~= "number" then
  178.                         table.insert(spaceData, 47)
  179.                         break
  180.                     end
  181.                 end
  182.  
  183.                 for k, v in ipairs(spaceData) do
  184.                     if v > 46 then
  185.                         chatLog[#chatLog + 1] = {responseData:sub(1, spaceData[k - 1] - 1), "bot"}
  186.                         chatLog[#chatLog + 1] = {responseData:sub(spaceData[k - 1]+1, -1), "bot"}
  187.                         break
  188.                     end
  189.                 end
  190.             else
  191.                 chatLog[#chatLog + 1] = {responseData, "bot"}
  192.             end
  193.         else
  194.             chatLog[#chatLog + 1] = {"CCBot: An error has ocurred!", "bot"}
  195.         end
  196.  
  197.         os.queueEvent(event_updateChat, textutils.serialize(chatLog))
  198.         os.pullEvent(event_continue)
  199.     end
  200. end
  201.  
  202.  
  203. --  Main
  204.  
  205. local function main()
  206.     -- Top logo
  207.     term.setBackgroundColor(colors.white)
  208.     term.clear()
  209.     term.setCursorPos(19, 2)
  210.  
  211.     term.setTextColor(colors.lightBlue)
  212.     write("CClever")
  213.     term.setTextColor(colors.lime)
  214.     write("B")
  215.     term.setTextColor(colors.yellow)
  216.     write("o")
  217.     term.setTextColor(colors.red)
  218.     write("t  ")
  219.     term.setTextColor(colors.lightGray)
  220.     write(version)
  221.     term.setTextColor(colors.gray)
  222.     term.setCursorPos(1, 3)
  223.     centerPrint("- www.cleverbot.com -")
  224.  
  225.     -- Run
  226.     parallel.waitForAll(input, interface)
  227. end
  228.  
  229. -- Check
  230. if not http then
  231.     print("HTTP API Needs to be Enabled!")
  232.     print("CCleverBot heavily orties on it!")
  233.     error()
  234. end
  235.  
  236. -- Run
  237. local _, err = pcall(main)
  238. if err then
  239.     term.setTextColor(colors.white)
  240.     term.setBackgroundColor(colors.black)
  241.     term.clear()
  242.     term.setCursorPos(1, 1)
  243.     print("Error!")
  244.     print(err)
  245.     print("\nPress any key to exit...")
  246.     os.pullEvent("key")
  247. end
  248.  
  249. -- Exit
  250. term.setTextColor(colors.white)
  251. term.setBackgroundColor(colors.black)
  252. term.clear()
  253. term.setCursorPos(1, 1)
  254. centerPrint("Thank You for Using CCleverBot " .. version)
  255. centerPrint("Made by 1lann and GravityScore")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement