Advertisement
Terrah

race

Nov 7th, 2014
1,802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. --Upvalue storing the league array
  2. local Leagues = JSONDecode(MOD.HTTPGet("http://api.pathofexile.com/leagues?type=event"));
  3.  
  4. --Convert a "yyyy-mm-ddThh:mm:ssZ" timestamp to unix
  5. local TimestampToSec = MOD.TimestampToSec;
  6. --Turn seconds into a span IE "hh:mm:ss"
  7. local SecondToSpan = MOD.SecondToSpan;
  8.  
  9. --returns true if the league is a hardcore league
  10. local function GetLeagueIsHardcore(league)
  11.  
  12.     if league.rules == nil then
  13.         return false;
  14.     end
  15.  
  16.     for k,v in pairs(league.rules) do
  17.         if type(v) == "table" and v.id == 4 then
  18.             return true;
  19.         end
  20.     end
  21.  
  22.     return false;
  23. end
  24.  
  25. --Get the current or next race
  26. --Returns a table with the members:
  27. --starts: unix timestamp when it starts
  28. --ends: unix timestamp when it ends
  29. --id: the id of the race
  30. --url: the link to the race
  31. local function GetLeague()
  32.  
  33.     local league = Leagues[1];
  34.     local ts = os.time();
  35.     local tbl = {};
  36.  
  37.     if league == nil then
  38.         return nil;
  39.     end
  40.  
  41.     tbl.ends = TimestampToSec(league.endAt);
  42.  
  43.     --Race has ended, grab a new set and update the upvalue
  44.     if (tbl.ends-ts) <= 0 then
  45.  
  46.         Leagues = JSONDecode(MOD.HTTPGet("http://api.pathofexile.com/leagues?type=event"));
  47.         league = Leagues[1];
  48.  
  49.         if league == nil then
  50.             return nil;
  51.         else
  52.             tbl.ends = TimestampToSec(league.endAt);
  53.  
  54.             local count = 1;
  55.             while (tbl.ends-ts) <= 0 do
  56.                 count = count + 1;
  57.                 league = Leagues[count];
  58.                 if league == nil then
  59.                     return nil;
  60.                 else
  61.                     tbl.ends = TimestampToSec(league.endAt);
  62.                 end
  63.             end
  64.         end
  65.     end
  66.  
  67.     if not GetLeagueIsHardcore(league) and not MOD.SCLeague then
  68.         local count = 1;
  69.         local prev = league;
  70.         while (tbl.ends-ts) <= 0 or not GetLeagueIsHardcore(league) do
  71.  
  72.             count = count + 1;
  73.             league = Leagues[count];
  74.             if league == nil then
  75.                 if prev == nil then
  76.                     return nil;
  77.                 else
  78.                     league = prev;
  79.                     tbl.ends = TimestampToSec(league.endAt);
  80.                     break;
  81.                 end
  82.             else
  83.                 tbl.ends = TimestampToSec(league.endAt);
  84.             end
  85.         end
  86.     end
  87.  
  88.     tbl.starts = TimestampToSec(league.startAt);
  89.     tbl.id = league.id;
  90.     tbl.url = league.url;
  91.  
  92.     return tbl;
  93. end
  94.  
  95. --The actual CMD function
  96. local cmd = function(msg,usr,cha)
  97.  
  98.     local l = GetLeague();
  99.  
  100.     --Get current unixtimestamp
  101.     local ts = os.time();
  102.  
  103.     --If its nil it didnt find any races
  104.     if type(l) ~= "table" then
  105.         print("No races registered in the POE API");
  106.         return;
  107.     end
  108.  
  109.     if not l.url then
  110.         l.url = "(no thread exists)";
  111.     elseif type(l.url)~="string" or l.url == "" then
  112.         l.url = type(l.url) .. " (no url found)";
  113.     end
  114.  
  115.     --current timestamp is higher then the start one, it has started
  116.     if ts > l.starts then
  117.         print(l.id .. " " .. l.url .. " ends in " .. SecondToSpan(l.ends-ts));
  118.  
  119.     --The race hasn't started yet
  120.     else
  121.         print(l.id .. " " .. l.url .. " begins in " .. SecondToSpan(l.starts-ts));
  122.     end
  123. end
  124.  
  125. --Publish useful functions in the mod table
  126. MOD.GetLeague = GetLeague;
  127.  
  128. --Return the function
  129. return cmd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement