Advertisement
Terrah

rank

Nov 8th, 2014
1,736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. local function Merge(tbl,tbl2)
  2.  
  3.     for k,v in ipairs(tbl2.entries)do
  4.         table.insert(tbl.entries,v);
  5.     end
  6.  
  7.     return tbl;
  8. end
  9.  
  10. local cache = {};
  11.  
  12. local function RawLadderRequest(leaguename,entries)
  13.  
  14.         if cache.request and cache.request == leaguename and cache.tbl and cache.time and (os.time()-cache.time) < 300 then
  15.             return cache.tbl;
  16.         end
  17.  
  18.         local cnt = 0;
  19.         local request = entries;
  20.         local ladder = false;
  21.         local current = false;
  22.         local actual = entries;
  23.  
  24.         while(entries>=cnt)do
  25.  
  26.                 if actual> 200 then
  27.                     actual= 200;
  28.                 elseif actual<=0 then
  29.                     break;
  30.                 end
  31.  
  32.                 local req = "http://api.pathofexile.com/ladders/"..leaguename:gsub("/","%%2F").."?offset="..tostring(cnt).."&limit="..tostring(actual);
  33.  
  34.                 --print(req);
  35.  
  36.                 cnt = cnt + actual;
  37.                 request = request - actual;
  38.                 actual = request;
  39.  
  40.                 current = JSONDecode(MOD.HTTPGet(req));
  41.  
  42.                 if not current or current.error~=nil then
  43.                     break;
  44.                 end
  45.  
  46.                 if(not ladder)then
  47.                         ladder = current;
  48.                 else
  49.                         ladder = Merge(ladder,current);
  50.                 end
  51.  
  52.                 if(entries>=ladder.total)then
  53.                         break;
  54.                 end
  55.         end
  56.  
  57.         cache.tbl = ladder;
  58.         cache.time = os.time();
  59.         cache.request = leaguename;
  60.  
  61.         return ladder;
  62. end
  63.  
  64. local function GetLadder(laddersize)
  65.  
  66.     local league = MOD.GetLeague()
  67.  
  68.     if not league or league.starts > os.time() then
  69.         return nil;
  70.     end
  71.  
  72.     local ladder = RawLadderRequest(league.id,laddersize);
  73.  
  74.     if ladder == nil then
  75.         print("HTTPGet error");
  76.         return nil;
  77.     elseif ladder.error ~= nil then
  78.         print("ERROR: " .. ladder.error.message);
  79.         return nil;
  80.     end
  81.  
  82.     local entries = ladder.total;
  83.  
  84.     if(entries>laddersize) then
  85.         entries=laddersize;
  86.     end
  87.  
  88.     local formatted = {id=league.id,time=league.ends-os.time()};
  89.  
  90.     for n=1,entries do
  91.  
  92.         if ladder.entries[n] ~= nil then
  93.  
  94.             local entry = {dead=ladder.entries[n].dead};
  95.             entry.class = ladder.entries[n].character.class;
  96.             entry.rank = n;
  97.             entry.level = ladder.entries[n].character.level;
  98.  
  99.             if ladder.entries[n].account.twitch ~= nil then
  100.                 entry.name = ladder.entries[n].account.twitch.name:lower();
  101.                 entry.twitch = true;
  102.             else
  103.                 entry.name = ladder.entries[n].account.name:lower();
  104.                 entry.twitch = false;
  105.             end
  106.  
  107.             table.insert(formatted,entry);
  108.         end
  109.     end
  110.  
  111.     return formatted;
  112. end
  113.  
  114. local classes = {};
  115. table.insert(classes,"witch");
  116. table.insert(classes,"shadow");
  117. table.insert(classes,"ranger");
  118. table.insert(classes,"duelist");
  119. table.insert(classes,"marauder");
  120. table.insert(classes,"templar");
  121. table.insert(classes,"scion");
  122.  
  123. local function isClass(str)
  124.  
  125.     local l = str:lower();
  126.  
  127.     for n=1,#classes do
  128.  
  129.         if classes[n]==l then
  130.             return true;
  131.         end
  132.     end
  133.  
  134.     return false;
  135. end
  136.  
  137. local function isNumber(str)
  138.  
  139.     for n=1,str:len() do
  140.         if str:byte(n) < 48 or str:byte(n) > 57 then
  141.             return false;
  142.         end
  143.     end
  144.  
  145.     return true;
  146. end
  147.  
  148. local cmd = function(msg,usr,chan)
  149.  
  150.     if not msg or msg == "" then
  151.         msg = chan:sub(2);
  152.     end
  153.  
  154.     local laddersize = 400;
  155.     local ladder = GetLadder(laddersize);
  156.  
  157.     if ladder == nil then
  158.         print("No race is currently running or the race has not yet started!");
  159.         return nil;
  160.     end
  161.  
  162.     local deadinfo = false;
  163.     local class = false;
  164.     local rank = nil;
  165.  
  166.     if(isClass(msg))then
  167.         class=true;
  168.     elseif isNumber(msg) then
  169.         rank = math.floor(tonumber(msg));
  170.         if rank > #ladder then
  171.             rank = #ladder;
  172.         elseif rank < 1 then
  173.             rank = 1;
  174.         end
  175.     end
  176.  
  177.     msg = msg:lower();
  178.  
  179.     local data = {};
  180.     local first = {};
  181.  
  182.     for n=1,#ladder do
  183.  
  184.         local entry = ladder[n];
  185.  
  186.         if class and entry.class:lower() == msg and not entry.dead then
  187.             msg = entry.name;
  188.             class = false;
  189.         elseif rank ~= nil and n == rank then
  190.             msg = entry.name;
  191.             rank=nil;
  192.         end
  193.  
  194.         if entry.name == msg and data.rank == nil then
  195.             data.rank = entry.rank;
  196.             data.currentclass = entry.class;
  197.             data.level = entry.level;
  198.  
  199.             if data[entry.class] == nil then
  200.                 data[entry.class] = 0;
  201.             end
  202.  
  203.             data[entry.class] = data[entry.class] + 1;
  204.  
  205.             data.classrank = data[entry.class];
  206.  
  207.             data.dead = entry.dead;
  208.  
  209.             if data.dead then
  210.                
  211.                 if not deadinfo then
  212.                     deadinfo = {};
  213.                     deadinfo.Class = entry.class;
  214.                     deadinfo.ClassRank = data.classrank;
  215.                     deadinfo.Rank = data.rank;
  216.                     deadinfo.Level = data.level;               
  217.                 end
  218.  
  219.                 data.deadrank = data.rank;
  220.                 data.rank=nil;
  221.             end
  222.  
  223.             if first[entry.class] == nil then
  224.                 first[entry.class] = entry.name;
  225.             end
  226.  
  227.         else
  228.  
  229.             if data[entry.class] == nil then
  230.                 data[entry.class] = 0;
  231.             end
  232.  
  233.             if first[entry.class] == nil then
  234.                 first[entry.class] = entry.name;
  235.             end
  236.  
  237.             data[entry.class] = data[entry.class] + 1;
  238.         end
  239.     end
  240.  
  241.     if data.deadrank and not data.rank then
  242.         data.rank = data.deadrank;
  243.     end
  244.  
  245.     if data.currentclass == nil then
  246.  
  247.         print(msg.." is not in the top "..tostring(laddersize));
  248.         print("Time left: " .. MOD.SecondToSpan(ladder.time));
  249.  
  250.     else
  251.  
  252.         print(msg.." is");
  253.         print("#"..tostring(data.classrank));
  254.         print(data.currentclass .. " level " .. tostring(data.level));
  255.         print("#"..tostring(data.rank).." overall");
  256.         if data.dead then
  257.             print("but is however dead raizRip");
  258.         elseif deadinfo then
  259.             print("also has a dead level ".. tostring(deadinfo.Level).. " " ..deadinfo.Class.." at rank #" .. tostring(deadinfo.ClassRank) .. " (#".. tostring(deadinfo.Rank).. " overall) raizRip");  
  260.         end
  261.         print("Time left: " .. MOD.SecondToSpan(ladder.time));
  262.     end
  263. end
  264.  
  265.  
  266. return cmd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement