Advertisement
Wyvern67

AEC: Pocket

Aug 10th, 2015
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.63 KB | None | 0 0
  1. shell.run("pastebin", "get", "jzpwtcvf", "dl")
  2. shell.run("dl", "ini")
  3. assert(os.loadAPI("ini"), "Couldn't download the INI api")
  4.  
  5. settings = ini.open("settings")
  6.  
  7. api = nil
  8. while api == nil do
  9.     api = settings.read("API", "Name", "craftacloud")
  10.     if api == "cx4" then
  11.         cx4ID = settings.read("CloudChest", "ID")
  12.         if cx4ID == false then
  13.             print("A CloudChest ID is required !")
  14.             print("If you don't have one, simply go to https://boomtree.com/cx4 and get one.")
  15.             print("(You'll only have to do this once)")
  16.             term.write(">CloudChest ID: ")
  17.             cx4ID = io.read()
  18.             settings.write("CloudChest", "ID", cx4ID)
  19.         end
  20.         shell.run("dl", "cx4")
  21.         assert(os.loadAPI("cx4"), "Couldn't download the CloudChest api")
  22.         cx4.Chest(cx4ID)
  23.     elseif api == "craftacloud" then
  24.         packetMaxSize = settings.read("Craftacloud", "packetMaxSize", 4000)
  25.         salt = settings.read("Craftacloud", "ID")
  26.         if not salt then
  27.             print("A unique Craftacloud ID is required !")
  28.             print("What is your Minecraft name ?")
  29.             salt = tostring(io.read())
  30.             assert(salt, "Non-valid Minecraft name")
  31.             settings.write("Craftacloud", "ID", salt)
  32.         end
  33.  
  34.         cx4 = {}
  35.         function stringSplitInParts(txt, count)
  36.             length = string.len(txt)
  37.             parts = math.floor(length/count)
  38.             lastPartLength = length%count
  39.            
  40.             composition = {}
  41.             for i=0,parts-1 do
  42.                 table.insert(composition, string.sub(txt, (count*i)+1, count*(i+1)))
  43.             end
  44.             if lastPartLength ~= 0 then
  45.                 table.insert(composition, string.sub(txt, count*parts+1, length+1))
  46.             end
  47.             return composition
  48.         end
  49.        
  50.         function sSend(channel, data, doVerifications)
  51.             if doVerifications == nil then
  52.                 doVerifications = true
  53.             end
  54.             encoded = textutils.urlEncode(data)
  55.             url = "http://craft-a-cloud.mooo.com/upload.php?name="..textutils.urlEncode(salt..channel).."&data="..encoded
  56.             -- print(salt..channel..":"..string.len(encoded))
  57.             http.request(url)
  58.             local i=0
  59.             if doVerifications then
  60.                 while sGet(channel) ~= data do
  61.                         if i >= 20 then
  62.                             print("couldn't update "..salt..channel)
  63.                             return false
  64.                         end
  65.                         sleep(0.25)
  66.                         i = i+1
  67.                 end
  68.             end
  69.             return true
  70.         end
  71.        
  72.         function Set(channel, data)
  73.             data = tostring(data)
  74.             if not channel then
  75.                 return false
  76.             end
  77.             composite = stringSplitInParts(data, packetMaxSize)
  78.             if #composite == 1 then
  79.                 return sSend(channel, data)
  80.             else
  81.                 if not sSend(channel, "composite "..#composite) then
  82.                     return false
  83.                 end
  84.                
  85.                 for i=1, #composite do
  86.                     print("composite: "..string.len(composite[i]))
  87.                     ret = sSend(channel+i, composite[i], false)
  88.                     if not ret then
  89.                         return false
  90.                     end
  91.                 end
  92.             end
  93.             return true
  94.         end
  95.         cx4.Set = Set
  96.        
  97.         function sGet(channel)
  98.             url = "http://craft-a-cloud.mooo.com/download.php?name="..textutils.urlEncode(salt..channel)
  99.             response = ""
  100.             http.request(url)
  101.             event, returnurl, response = os.pullEvent("http_success")
  102.             response = response.readAll()
  103.             return response
  104.         end
  105.            
  106.         function Get(channel)
  107.             response = sGet(channel)
  108.             if string.sub(response, 1, 10) == "composite " then
  109.                 parts = tonumber(string.sub(response, 11))
  110.                 if not parts then
  111.                     return false
  112.                 end
  113.                
  114.                 composite = {}
  115.                 for i=1, parts do
  116.                     composite[i] = sGet(channel+i)
  117.                 end
  118.                 result, response = pcall(table.concat, composite)
  119.                 if result == true then
  120.                     return response
  121.                 else
  122.                     return false
  123.                 end
  124.             else
  125.                 return response
  126.             end
  127.         end
  128.         cx4.Get = Get
  129.     else
  130.         error("No default API ?")
  131.     end
  132.     settings.close("settings")
  133. end
  134.  
  135. termSize = term.getSize()
  136. sleep(1)
  137.  
  138. function stringRepeat(txt, times)
  139.     s = ""
  140.     for i=1,times do
  141.         s = s..txt
  142.     end
  143.     return s
  144. end
  145.  
  146. function cycleThrough(a,b)
  147.     if not b then
  148.         b = a
  149.         a = 1
  150.     end
  151.    
  152.     if not last then
  153.         last = a-1
  154.     end
  155.    
  156.     if last < b then
  157.         last = last+1
  158.     else
  159.         last = a
  160.     end
  161.     return last
  162. end
  163.  
  164. function split(str,splitter)
  165.     if not splitter then return end
  166.     if not str then return end
  167.     words = {}
  168.     i=0
  169.     for part in string.gmatch(str, "[^%"..splitter.."]+") do -- get each part
  170.         i=i+1
  171.         words[i] = part
  172.     end
  173.     return words
  174. end
  175. string.split = split
  176.  
  177. function waitingScreen()
  178.     _,y = term.getCursorPos()
  179.     term.setCursorPos(1,y)
  180.     term.write(stringRepeat(".", cycleThrough(1,termSize)).."                                                                                     ")
  181. end
  182.  
  183. function search(txt)
  184.     txt = tostring(txt)
  185.     matches = {}
  186.     j = 0
  187.     for i=1,#inv do
  188.         size = inv[i].size
  189.         id = inv[i].id
  190.         dmg = inv[i].dmg
  191.         assert(size)
  192.         assert(id)
  193.         if string.find(string.lower(id), string.lower(txt)) then
  194.             matches[#matches+1] = {}
  195.             matches[#matches].id = id
  196.             matches[#matches].size = size
  197.             matches[#matches].dmg = dmg
  198.         end
  199.     end
  200.     if #matches > 0 then
  201.         return matches
  202.     else
  203.         return false
  204.     end
  205. end
  206.  
  207. function undone()
  208.     local g = cx4.Get(0)
  209.    
  210.     if string.find(g, "done") or string.find(g, "fail") then
  211.         return true
  212.     else
  213.         return false
  214.     end
  215. end
  216.  
  217. function temppost(var)
  218.     f = fs.open("f", "w")
  219.     f.write(var)
  220.     f.close()
  221.     shell.run("temppost", "f")
  222.     fs.delete("f")
  223. end
  224.  
  225. function refresh()
  226.     print("Refreshing inventory...")
  227.     while not inv do
  228.         result, inv = pcall(cx4.Get, 1)
  229.         if result == false then
  230.             return false, "Corrupted inventory data"
  231.         end
  232.         save = fs.open("last_inventory", "w")
  233.         save.write(inv)
  234.         save.close()
  235.        
  236.         result, inv = pcall(textutils.unserialize, inv)
  237.         if result == false then
  238.             return false
  239.         end
  240.     end
  241.     if type(inv) == "table" then
  242.         print("Done !")
  243.         return true
  244.     else
  245.         return false
  246.     end
  247. end
  248.  
  249. function loadLastInventory()
  250.     if fs.exists("last_inventory") then
  251.         save = fs.open("last_inventory", "r")
  252.         inv = save.readAll()
  253.         save.close()
  254.         inv = textutils.unserialize(inv)
  255.         if type(inv) == "table" then
  256.             return true
  257.         end
  258.     end
  259.     return refresh()
  260. end
  261.  
  262. print("Initializing inventory...")
  263. if not loadLastInventory() then
  264.     inv = {}
  265. end
  266.  
  267. term.clear()
  268. term.setCursorPos(1,1)
  269. if type(inv) == "table" then
  270.     if #inv > 0 then
  271.         print("Inventory initialized")
  272.     else
  273.         print("Unable to initalize the inventory.")
  274.     end
  275. else
  276.     error("Inventory couldn't be initialized")
  277. end
  278.  
  279. while true do
  280.     print("Commands: search, refresh ")
  281.     term.write(">")
  282.     commands = io.read()
  283.     if not commands then break end
  284.     commands = string.split(commands, " ")
  285.     if string.lower(commands[1]) == "search" and commands[2] then
  286.         matches = search(commands[2])
  287.         if type(matches) == "table" then
  288.             -- print(tostring(#matches).." matches.")
  289.             for i=1, #matches do
  290.                 textutils.pagedPrint("   "..i..":["..matches[i].size.."]"..matches[i].id..":"..matches[i].dmg)
  291.             end
  292.         end
  293.         print("End of list !")
  294.         if matches then
  295.             while true do
  296.                 print("request <ID> <Amount>")
  297.                 term.write(">request ")
  298.                 request = io.read()
  299.                 if not request then break end
  300.                 request = "request "..request
  301.                 words = string.split(request, " ")
  302.                 words[2] = tonumber(words[2])
  303.                 words[3] = tonumber(words[3])
  304.                 if type(words[2]) == "number" and type(words[3]) == "number" then
  305.                     if words[2] <= #matches and words[2] > 0 then
  306.                         cx4.Set(0, "request "..matches[words[2]].id.." "..words[3].." "..matches[words[2]].dmg, false)
  307.                         while undone() do
  308.                             waitingScreen()
  309.                             sleep()
  310.                         end
  311.                         term.clear()
  312.                         term.setCursorPos(1,1)
  313.                         print("Done !")
  314.                         break
  315.                     else
  316.                         print("Wrong input")
  317.                     end
  318.                 end
  319.             end
  320.         else
  321.             print("No items found for that query !")
  322.         end
  323.     elseif string.lower(commands[1]) == "refresh" then
  324.         cx4.Set(0, "refresh")
  325.         while undone() do
  326.             sleep()
  327.         end
  328.         term.clear()
  329.         term.setCursorPos(1,1)
  330.         print("Done !")
  331.         refresh()
  332.     end
  333. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement