Advertisement
Heracles421

CCiri_beta

Jan 9th, 2013
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.97 KB | None | 0 0
  1. --[[ Variables ]]--
  2. version = "BETA 2.5.6"
  3. screenX, screenY = term.getSize()
  4. loadingBar = false
  5. running = true
  6. progress = false
  7. pastebin_url = "vi275s3k"
  8. isBeta = true
  9. path = shell.dir()
  10. programPath = shell.getRunningProgram()
  11.  
  12. -- Tables
  13. local t_mic = {
  14. ".::.",
  15. "///\\\\\\",
  16. "||||||",
  17. "HH  ||||||  HH",
  18. "HH==========HH",
  19. "HH==========HH",
  20. "HH  ######  HH",
  21. "HH   ####   HH",
  22. "HH    ##    HH",
  23. "HH          HH",
  24. "\\\\      //",
  25. "\\\\  //",
  26. "___\\/___ ",
  27. }
  28.  
  29. local logo = {
  30. " 9999  9999 7    7 8                   ",
  31. "9     9            8          8        ",
  32. "9     9     7 77 7 8888 8888 888  88 ",
  33. "9     9     7 7  7 8  8 8  8  8  8  8 ",
  34. "9     9     7 7  7 8  8 8888  8  8  8 ",
  35. "9     9     7 7  7 8  8 8     8  8  8 ",
  36. " 9999  9999 7 7  7 8888 8888  8   888",
  37. }
  38.  
  39. local appkeys = {"GU7GAL-HLW2TGXR4J","P99XV4-6Y359T8584","5KGYY5-YE2JLLG74H"}
  40. --[[ End of Variables ]]--
  41.  
  42. --[[ Functions ]]--
  43.  
  44. -- Draw a line at _yPos
  45. function line(_yPos)
  46.   term.setCursorPos(1, _yPos)
  47.   if term.isColor and term.isColor() then
  48.     for i = 1, screenX do
  49.       paintutils.drawPixel(i, _yPos, colors.white)
  50.       term.setTextColor(colors.black)
  51.       term.setBackgroundColor(colors.white)
  52.     end
  53.   else
  54.     for i = 1, screenX do
  55.       write("-")
  56.     end
  57.   end
  58. end
  59.  
  60. -- Print text centered at line _yPos
  61. function printC(text, _yPos)
  62.   term.setCursorPos((screenX - #text)/2, _yPos)
  63.   write(text)
  64. end
  65.  
  66. -- Clear screen and set cursor pos
  67. function cScreen(swidth, sheight)
  68.   if term.isColor and term.isColor() then
  69.     term.setBackgroundColor(colors.white)
  70.   end
  71.   term.clear()
  72.   term.setCursorPos(swidth, sheight)
  73. end
  74.  
  75. -- Wait for key pressing
  76. function waitForExitKey()
  77.   t = "Press x to go back"
  78.   term.setCursorPos(screenX - #t, 4)
  79.   write(t)
  80.   while true do
  81.     _, key = os.pullEvent("key")
  82.     if key == keys.x then break end
  83.   end
  84. end
  85.  
  86. -- Print a progress bar
  87. function progressBar()
  88.   local function doWrite(char)
  89.     term.setCursorPos(3, 6)
  90.     write("Loading "..char)
  91.     sleep(0.1)
  92.   end
  93.   while true do
  94.     doWrite("|")
  95.     doWrite("/")
  96.     doWrite("-")
  97.     doWrite("\\")
  98.   end
  99. end
  100.  
  101. -- Split a paragraph into single lines
  102. function splitLine( text )
  103.   local swidth = term.getSize()
  104.   local function split( str, regex )
  105.     local t = { }
  106.     local fpat = "(.-)"..regex
  107.     local last_end = 1
  108.     local s, e, cap = str:find(fpat, 1)
  109.     while s do
  110.       if s ~= 1 or cap ~= "" then
  111.         table.insert(t,cap)
  112.       end
  113.       last_end = e+1
  114.       s, e, cap = str:find(fpat, last_end)
  115.     end
  116.     if last_end <= #str then
  117.       cap = str:sub(last_end)
  118.       table.insert(t, cap)
  119.     end
  120.     return t
  121.   end
  122.   local currLine = ""
  123.   local words = split( text, " " )
  124.   local t = {}
  125.   for i = 1, #words do
  126.     if #currLine + #words[i] + 1 > swidth then
  127.       table.insert( t, currLine )
  128.       currLine = words[i].." "
  129.     else
  130.       currLine = currLine..words[i].." "
  131.     end
  132.     if i == #words then
  133.       table.insert( t, currLine )
  134.     end
  135.   end
  136. return t
  137. end
  138.  
  139. -- Get the answer from Wolfram Alpha
  140. function getMessage(_input)
  141.   _input = textutils.urlEncode(_input)
  142.   http.request("http://api.wolframalpha.com/v2/query?appid="..appkeys[math.random(1,#appkeys)].."&input=".._input.."&format=plaintext")
  143.   parallel.waitForAny(progressBar,
  144.   function()
  145.     repeat sEvent, url, sourceText = os.pullEvent() until sEvent == "http_success" or sEvent == "http_failure"
  146.   end)
  147.   if sEvent == "http_success" then
  148.     local respondedText = sourceText.readAll()
  149.     text = {}
  150.     mainTable = {}
  151.     index = 1
  152.     for content in respondedText:gmatch "<plaintext>(.-)</plaintext>" do
  153.       table.insert(text, content)
  154.       text[index] = string.gsub(text[index],"&apos;","'")
  155.       text[index] = string.gsub(text[index],"&quot;","\"")
  156.       text[index] = string.gsub(text[index],"&amp;","&")
  157.       text[index] = string.gsub(text[index],"&lt;","<")
  158.       text[index] = string.gsub(text[index],"&gt;",">")
  159.       index = index + 1
  160.     end
  161.     cScreen(1,1)
  162.     line(1)  printC("CCiri V "..version, 2) line(3)
  163.     if text[1] ~= nil then
  164.       screenx = term.getSize()
  165.       for i = 1, #text do
  166.         if #text[i] >= screenx-5 then
  167.           k = splitLine(text[i])
  168.           for l = 1, #k do
  169.             table.insert(mainTable, k[l])
  170.           end
  171.         else
  172.           table.insert(mainTable, text[i])
  173.         end
  174.       end
  175.       return true, mainTable
  176.     else
  177.       printC("Output: No answer found", 7)
  178.       waitForExitKey()
  179.       return false, nil
  180.     end
  181.   elseif sEvent == "http_failure" then
  182.     cScreen(1,1)
  183.     line(1)  printC("CCiri V "..version, 2) line(3)
  184.     write("Couldn't contact server")
  185.     waitForExitKey()
  186.     return false, nil
  187.   end
  188. end
  189.  
  190. -- Print the answer
  191. function printMessage(_table)
  192.   yPos = 1
  193.   while true do
  194.     cScreen(1,1)
  195.     line(1)  printC("CCiri V "..version, 2) line(3)
  196.     write("Input: "..input, 5)
  197.     t = "Press x to go back"
  198.     term.setCursorPos(screenX - #t, 4)
  199.     write(t)
  200.     for i = 6, screenY do
  201.       term.setCursorPos(1, i)
  202.       term.clearLine()
  203.       if not _table[i + yPos - 6] then
  204.         break
  205.       else
  206.         if string.find(_table[i + yPos - 6], "\n") then
  207.           _table[i + yPos - 6] = string.sub(_table[i + yPos - 6], 1, string.find(_table[i + yPos - 6], "\n") - 1)
  208.           .. " " ..
  209.           string.sub(_table[i + yPos - 6], string.find(_table[i + yPos - 6], "\n") + 1)
  210.         end
  211.         write(_table[i + yPos - 6])
  212.       end
  213.     end
  214.     e, button = os.pullEvent()
  215.     if e == "mouse_scroll" and term.isColor() then
  216.       if button == 1 and yPos < #_table - (screenY - 6) then
  217.       -- Down
  218.       yPos = yPos + 1
  219.     elseif button == -1 and yPos > 1 then
  220.       yPos = yPos - 1
  221.       end
  222.     elseif e == "key" then
  223.       if button == keys.down and yPos < #_table - (screenY - 6) then
  224.         -- Down
  225.         yPos = yPos + 1
  226.       elseif button == keys.up and yPos > 1 then
  227.         --Up
  228.         yPos = yPos - 1
  229.       elseif button == keys.x then
  230.         break
  231.       end
  232.     end
  233.   end
  234.   sleep(0.1)
  235. end  
  236.  
  237. -- Check for Updates
  238. function checkForUpdate()
  239.   intBar:setMessage( "Starting up..." )
  240.   sleep(1)
  241.   intBar:triggerUpdate( "1/4 Checking for updates..." )
  242.   sleep(0.5)
  243.   if http then
  244.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode( pastebin_url ))
  245.     if response then
  246.       v = response.readLine()
  247.       v = response.readLine()
  248.       content = "--[[ Variables ]]--\n"..v.."\n"..response.readAll()
  249.       v = string.sub(v, 12, -2)
  250.       response.close()
  251.       if v == version then
  252.         intBar:triggerUpdate( "2/4 No update required" )
  253.         sleep(0.75)
  254.         intBar:triggerUpdate()
  255.         sleep(0.5)
  256.         intBar:triggerUpdate( "4/4 Done." )
  257.       else
  258.         intBar:triggerUpdate( "2/4 Update found ("..v..")" )
  259.         t = "Click Y to update, N to continue"
  260.         term.setCursorPos((screenX-#t)/2, screenY-2)
  261.         write(t)
  262.         repeat _, k = os.pullEvent("key") until k == keys.y or k == keys.n
  263.         term.setCursorPos(1, screenY-2) term.clearLine()
  264.         if k == keys.y then
  265.           version = v
  266.           intBar:triggerUpdate( "3/4 Updating... Please wait" )
  267.           fileName = fs.getName(shell.getRunningProgram())
  268.           file = fs.open(path.."/"..fileName, "w")
  269.           file.write(content)
  270.           file.close()
  271.           sleep(1)
  272.           intBar:triggerUpdate( "4/4 Done." )
  273.           sleep(1)
  274.           return true        
  275.         elseif k == keys.n then
  276.           intBar:triggerUpdate( "3/4 Update canceled.")
  277.           sleep(0.75)
  278.           intBar:triggerUpdate( "4/4 Done." )
  279.           sleep(1)
  280.           return false
  281.         end
  282.       end
  283.     else
  284.       write("Failed to connect to pastebin.")
  285.       return false
  286.     end
  287.   else
  288.     intBar:triggerUpdate()
  289.     intBar:triggerUpdate( "3/4 HTTP api is disabled" )
  290.     sleep(1)
  291.     intBar:triggerUpdate( "4/4 Done." )
  292.   end
  293.   sleep(2)
  294. end
  295.  
  296. os.unloadAPI(path.."/CCiri_Files/load")
  297. os.loadAPI(path.."/CCiri_Files/load")
  298.  
  299. -- Load bar API by TheOriginalBit
  300. if term.isColor and term.isColor() then -- this allows it to work on CC1.3 AND CC1.4
  301.   intBar = load.init( load.LOGO_IS_LOAD, logo, 4, nil, screenY - 5, colors.red, "Starting...", " ", "CCiri " .. version )
  302. else
  303.   intBar = load.init( load.ASCII, logo, 4, nil, screenY - 5, "Starting...", " ", "CCiri".. version )
  304. end
  305. local function loadBar()
  306.   intBar:run( true )
  307. end
  308. parallel.waitForAll( loadBar, checkForUpdate )
  309. term.setBackgroundColour(colours.black)
  310. term.setTextColour(colours.white)
  311. term.clear()
  312.  
  313. -- Startup
  314. while running do
  315.   cScreen(1, 1)
  316.   line(1) printC("CCiri V "..version, 2) line(3) line(17) line(19)
  317.   for i = 1, #t_mic do
  318.     term.setCursorPos((screenX - #t_mic[i])/2, i+3)
  319.     write(t_mic[i])
  320.   end
  321.   term.setCursorPos(1,18)
  322.   write("> ")
  323.   input = read()
  324.   cScreen(1,1)
  325.   line(1)  printC("CCiri V "..version, 2) line(3)
  326.   write("Input: "..input, 5)
  327.   progress, tab = getMessage(input)
  328.   if progress then printMessage(tab) end
  329. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement