Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Upvalue storing the league array
- local Leagues = JSONDecode(MOD.HTTPGet("http://api.pathofexile.com/leagues?type=event"));
- --Convert a "yyyy-mm-ddThh:mm:ssZ" timestamp to unix
- local TimestampToSec = MOD.TimestampToSec;
- --Turn seconds into a span IE "hh:mm:ss"
- local SecondToSpan = MOD.SecondToSpan;
- --returns true if the league is a hardcore league
- local function GetLeagueIsHardcore(league)
- if league.rules == nil then
- return false;
- end
- for k,v in pairs(league.rules) do
- if type(v) == "table" and v.id == 4 then
- return true;
- end
- end
- return false;
- end
- --Get the current or next race
- --Returns a table with the members:
- --starts: unix timestamp when it starts
- --ends: unix timestamp when it ends
- --id: the id of the race
- --url: the link to the race
- local function GetLeague()
- local league = Leagues[1];
- local ts = os.time();
- local tbl = {};
- if league == nil then
- return nil;
- end
- tbl.ends = TimestampToSec(league.endAt);
- --Race has ended, grab a new set and update the upvalue
- if (tbl.ends-ts) <= 0 then
- Leagues = JSONDecode(MOD.HTTPGet("http://api.pathofexile.com/leagues?type=event"));
- league = Leagues[1];
- if league == nil then
- return nil;
- else
- tbl.ends = TimestampToSec(league.endAt);
- local count = 1;
- while (tbl.ends-ts) <= 0 do
- count = count + 1;
- league = Leagues[count];
- if league == nil then
- return nil;
- else
- tbl.ends = TimestampToSec(league.endAt);
- end
- end
- end
- end
- if not GetLeagueIsHardcore(league) and not MOD.SCLeague then
- local count = 1;
- local prev = league;
- while (tbl.ends-ts) <= 0 or not GetLeagueIsHardcore(league) do
- count = count + 1;
- league = Leagues[count];
- if league == nil then
- if prev == nil then
- return nil;
- else
- league = prev;
- tbl.ends = TimestampToSec(league.endAt);
- break;
- end
- else
- tbl.ends = TimestampToSec(league.endAt);
- end
- end
- end
- tbl.starts = TimestampToSec(league.startAt);
- tbl.id = league.id;
- tbl.url = league.url;
- return tbl;
- end
- --The actual CMD function
- local cmd = function(msg,usr,cha)
- local l = GetLeague();
- --Get current unixtimestamp
- local ts = os.time();
- --If its nil it didnt find any races
- if type(l) ~= "table" then
- print("No races registered in the POE API");
- return;
- end
- if not l.url then
- l.url = "(no thread exists)";
- elseif type(l.url)~="string" or l.url == "" then
- l.url = type(l.url) .. " (no url found)";
- end
- --current timestamp is higher then the start one, it has started
- if ts > l.starts then
- print(l.id .. " " .. l.url .. " ends in " .. SecondToSpan(l.ends-ts));
- --The race hasn't started yet
- else
- print(l.id .. " " .. l.url .. " begins in " .. SecondToSpan(l.starts-ts));
- end
- end
- --Publish useful functions in the mod table
- MOD.GetLeague = GetLeague;
- --Return the function
- return cmd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement