Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Settings Vars
- MOD.Scheduler = MOD.Scheduler or true; --Set to false to turn off the script
- MOD.LiveCheck = MOD.LiveCheck or 60; --Amount of seconds that happens between live ticks
- MOD.PointsPerTick = MOD.PointsPerTick or 1; --Points/coints awarded per minute
- MOD.CoinName = MOD.CoinName or "Coins";
- --Track Vars
- local nexttick = os.time();
- local userparse = {};
- local lasttime = 0;
- local loopy = 1;
- local TimestampToSec = MOD.TimestampToSec;
- local online = function()
- local ok,bad = MOD.GetStream(Channel():sub(2):lower());
- local remembered = tonumber(MOD.GetVar("WentOnline"));
- local LastTick = tonumber(MOD.GetVar("LastTick"));
- MOD.DelVar("WentOnline");
- MOD.DelVar("LastTick");
- if ok then
- local started = TimestampToSec(ok.started);
- if remembered == nil or remembered == started then
- remembered = started;
- elseif (os.time()-LastTick) > 1800 then
- local moretime = LastTick-remembered;
- if moretime>0 then
- MOD.SetVar("session_"..tostring(remembered),tostring(moretime),"sessions");
- end
- remembered = started;
- end
- MOD.SetVar("WentOnline",tostring(remembered));
- MOD.SetVar("LastTick",tostring(remembered));
- return remembered;
- elseif remembered ~= nil then
- local moretime = LastTick-remembered;
- if moretime>0 then
- MOD.SetVar("session_"..tostring(LastTick),tostring(moretime),"sessions");
- end
- end
- return 0;
- end;
- local time_track = {};
- local time_prev = {};
- online = online();
- local function GetUserData(usr)
- local data = MOD.GetVar(usr:lower());
- if data == nil then
- return nil;
- else
- return JSONDecode(data);
- end
- end
- local function ParseUser(usr,secs)
- if usr == nil then
- return;
- end
- usr = usr:lower();
- local data = MOD.GetVar(usr);
- local tbl;
- if data == nil then
- tbl = {};
- tbl.Coins = 0;
- tbl.Joined = secs;
- tbl.Seconds = 0;
- tbl.LastSeen = secs;
- else
- tbl = JSONDecode(data);
- local lsttime = time_track[usr];
- time_prev[usr]=secs;
- if lsttime ~= nil then
- local delta = secs-lsttime;
- local earned = MOD.PointsPerTick;
- tbl.Seconds = tbl.Seconds + delta;
- tbl.Coins = tbl.Coins + earned;
- end
- tbl.LastSeen = secs;
- end
- MOD.SetVar(usr,JSONEncode(tbl),"USERS");
- end
- MOD.AddCom("uptime",function()
- if online == nil or online == 0 then
- print("Stream not online!");
- return;
- end
- local moretime = os.time()-online;
- print("Stream has been online for " .. MOD.SecondToSpan(moretime));
- end);
- local downtimefunc = function()
- local l = MOD.GetArray("sessions",Channel():lower(),true,1);
- local start = "";
- local duration = 0;
- for k,v in pairs(l) do
- if k and v then
- start = k:match("%d+");
- duration = tonumber(v);
- break;
- end
- end
- if start == nil or start == "" then
- print("No previous sessions was found");
- return;
- else
- start = tonumber(start);
- end
- local ended = start+duration;
- local str_start = Date("%Y-%m-%d %X", start);
- local str_end = Date("%Y-%m-%d %X", ended);
- print("Last session was: " .. str_start.." until " .. str_end .. " ("..MOD.SecondToSpan(duration)..") which was " .. MOD.SecondToSpan(os.time()-ended).." ago");
- end
- MOD.AddCom("downtime",downtimefunc);
- local cmd = function(msg,usr,chan)
- local target = usr;
- local data = GetUserData(target);
- local delta = 0;
- if online ~= nil and online > 0 then
- time_track[target:lower()] = time_track[target:lower()] or os.time();
- delta = os.time() - time_track[target:lower()];
- else
- print("(Not tracking)");
- end
- if data == nil then
- print(target.." has no recorded data!");
- else
- print(target.." has " .. tostring(data.Coins) .. " " .. MOD.CoinName .. " and has a combined time of " .. MOD.SecondToSpan(data.Seconds+delta));
- end
- end
- local function Scheduler()
- MOD.Timer("pts_scheduler",0,Scheduler);
- if MOD.LiveCheck < 60 then MOD.LiveCheck = 60; end
- if lasttime > 0 and #userparse > 0 then
- local cnt = 0;
- for n=loopy,#userparse do
- ParseUser(userparse[n],lasttime);
- cnt = cnt + 1;
- if cnt >= 25 then
- loopy = n+1;
- return;
- end
- end
- loopy = 1;
- lasttime = 0;
- userparse = {};
- lasttimeusr = {};
- time_track = time_prev;
- time_prev = {};
- return;
- end
- if MOD.Scheduler and nexttick > os.time() then return; end
- nexttick = os.time()+MOD.LiveCheck;
- local channel = Channel():sub(2):lower();
- local ok,bad = MOD.GetStream(channel);
- if ok==nil then
- if online==0 then
- return;
- else
- local moretime = os.time()-online;
- MOD.SetVar("session_"..tostring(online),tostring(moretime),"sessions");
- online=0;
- print(channel.." flagged as offline");
- MOD.DelVar("WentOnline");
- time_track = {};
- end
- elseif online == 0 then
- online = TimestampToSec(ok.started);
- MOD.SetVar("WentOnline",tostring(online));
- print(channel.." flagged as online");
- end
- lasttime = os.time();
- MOD.SetVar("LastTick",tostring(lasttime));
- userparse = GetUsers();
- end
- Scheduler();
- MOD.Timer("reconnectedmsg",0,function()print("/me has reconnected!");end);
- return cmd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement