Advertisement
Terrah

tracker

Mar 13th, 2015
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. --Settings Vars
  2. MOD.Scheduler = MOD.Scheduler or true; --Set to false to turn off the script
  3. MOD.LiveCheck = MOD.LiveCheck or 60; --Amount of seconds that happens between live ticks
  4. MOD.PointsPerTick = MOD.PointsPerTick or 1; --Points/coints awarded per minute
  5. MOD.CoinName = MOD.CoinName or "Coins";
  6.  
  7. --Track Vars
  8. local nexttick = os.time();
  9. local userparse = {};
  10. local lasttime = 0;
  11. local loopy = 1;
  12.  
  13. local TimestampToSec = MOD.TimestampToSec;
  14.  
  15. local online = function()
  16.  
  17.     local ok,bad = MOD.GetStream(Channel():sub(2):lower());
  18.     local remembered = tonumber(MOD.GetVar("WentOnline"));
  19.     local LastTick = tonumber(MOD.GetVar("LastTick"));
  20.  
  21.     MOD.DelVar("WentOnline");
  22.     MOD.DelVar("LastTick");
  23.  
  24.     if ok then
  25.        
  26.         local started = TimestampToSec(ok.started);
  27.  
  28.         if remembered == nil or remembered == started then
  29.             remembered = started;
  30.         elseif (os.time()-LastTick) > 1800 then
  31.             local moretime = LastTick-remembered;
  32.             if moretime>0 then
  33.                 MOD.SetVar("session_"..tostring(remembered),tostring(moretime),"sessions");
  34.             end
  35.             remembered = started;
  36.         end
  37.  
  38.         MOD.SetVar("WentOnline",tostring(remembered));
  39.         MOD.SetVar("LastTick",tostring(remembered));
  40.  
  41.         return remembered;
  42.  
  43.     elseif remembered ~= nil then
  44.         local moretime = LastTick-remembered;
  45.         if moretime>0 then
  46.             MOD.SetVar("session_"..tostring(LastTick),tostring(moretime),"sessions");  
  47.         end
  48.     end
  49.  
  50.     return 0;
  51. end;
  52.  
  53.  
  54. local time_track = {};
  55. local time_prev = {};
  56.  
  57. online = online();
  58.  
  59. local function GetUserData(usr)
  60.  
  61.     local data = MOD.GetVar(usr:lower());
  62.  
  63.     if data == nil then
  64.  
  65.         return nil;
  66.     else
  67.         return JSONDecode(data);
  68.     end
  69. end
  70.  
  71. local function ParseUser(usr,secs)
  72.  
  73.     if usr == nil then
  74.         return;
  75.     end
  76.  
  77.     usr = usr:lower();
  78.  
  79.     local data = MOD.GetVar(usr);
  80.     local tbl;
  81.  
  82.  
  83.     if data == nil then
  84.  
  85.         tbl = {};
  86.  
  87.         tbl.Coins = 0;
  88.         tbl.Joined = secs;
  89.         tbl.Seconds = 0;
  90.         tbl.LastSeen = secs;
  91.     else
  92.  
  93.         tbl = JSONDecode(data);
  94.  
  95.         local lsttime = time_track[usr];
  96.         time_prev[usr]=secs;
  97.  
  98.         if lsttime ~= nil then
  99.  
  100.             local delta = secs-lsttime;
  101.             local earned = MOD.PointsPerTick;
  102.  
  103.             tbl.Seconds = tbl.Seconds + delta;
  104.             tbl.Coins = tbl.Coins + earned;
  105.         end
  106.  
  107.         tbl.LastSeen = secs;
  108.     end
  109.  
  110.     MOD.SetVar(usr,JSONEncode(tbl),"USERS");
  111.  
  112. end
  113.  
  114. MOD.AddCom("uptime",function()
  115.  
  116.     if online == nil or online == 0 then
  117.         print("Stream not online!");
  118.         return;
  119.     end
  120.  
  121.     local moretime = os.time()-online;
  122.     print("Stream has been online for " .. MOD.SecondToSpan(moretime));
  123. end);
  124.  
  125. local downtimefunc = function()
  126.    
  127.     local l = MOD.GetArray("sessions",Channel():lower(),true,1);
  128.     local start = "";
  129.     local duration = 0;
  130.     for k,v in pairs(l) do
  131.         if k and v then
  132.             start = k:match("%d+");
  133.             duration = tonumber(v);
  134.             break;
  135.         end
  136.     end
  137.  
  138.     if start == nil or start == "" then
  139.         print("No previous sessions was found");
  140.         return;
  141.     else
  142.         start = tonumber(start);
  143.     end
  144.  
  145.     local ended = start+duration;
  146.     local str_start = Date("%Y-%m-%d %X", start);
  147.     local str_end = Date("%Y-%m-%d %X", ended);
  148.  
  149.     print("Last session was: " .. str_start.." until " .. str_end .. " ("..MOD.SecondToSpan(duration)..") which was " .. MOD.SecondToSpan(os.time()-ended).." ago");
  150. end
  151.  
  152. MOD.AddCom("downtime",downtimefunc);
  153.  
  154. local cmd = function(msg,usr,chan)
  155.  
  156.     local target = usr;
  157.  
  158.     local data = GetUserData(target);
  159.  
  160.     local delta = 0;
  161.  
  162.     if online ~= nil and online > 0 then
  163.         time_track[target:lower()] = time_track[target:lower()] or os.time();
  164.         delta = os.time() - time_track[target:lower()];
  165.     else
  166.         print("(Not tracking)");
  167.     end
  168.  
  169.     if data == nil then
  170.         print(target.." has no recorded data!");
  171.     else
  172.  
  173.         print(target.." has " .. tostring(data.Coins) .. " " .. MOD.CoinName .. " and has a combined time of " .. MOD.SecondToSpan(data.Seconds+delta));
  174.     end
  175. end
  176.  
  177. local function Scheduler()
  178.  
  179.     MOD.Timer("pts_scheduler",0,Scheduler);
  180.  
  181.     if MOD.LiveCheck < 60 then MOD.LiveCheck = 60; end
  182.  
  183.     if lasttime > 0 and #userparse > 0 then
  184.  
  185.         local cnt = 0;
  186.  
  187.         for n=loopy,#userparse do
  188.  
  189.             ParseUser(userparse[n],lasttime);
  190.  
  191.             cnt = cnt + 1;
  192.  
  193.             if cnt >= 25 then
  194.                 loopy = n+1;
  195.                 return;
  196.             end
  197.         end
  198.  
  199.         loopy = 1;
  200.         lasttime = 0;
  201.         userparse = {};
  202.         lasttimeusr = {};
  203.  
  204.         time_track = time_prev;
  205.         time_prev = {};
  206.  
  207.         return;
  208.     end
  209.  
  210.     if MOD.Scheduler and nexttick > os.time() then return; end
  211.  
  212.     nexttick = os.time()+MOD.LiveCheck;
  213.  
  214.     local channel = Channel():sub(2):lower();
  215.  
  216.     local ok,bad = MOD.GetStream(channel);
  217.  
  218.     if ok==nil then
  219.  
  220.         if online==0 then
  221.             return;
  222.         else
  223.             local moretime = os.time()-online;
  224.             MOD.SetVar("session_"..tostring(online),tostring(moretime),"sessions");
  225.             online=0;
  226.             print(channel.." flagged as offline");
  227.             MOD.DelVar("WentOnline");
  228.             time_track = {};
  229.         end
  230.  
  231.     elseif online == 0 then
  232.         online = TimestampToSec(ok.started);
  233.         MOD.SetVar("WentOnline",tostring(online));
  234.         print(channel.." flagged as online");
  235.     end
  236.  
  237.     lasttime = os.time();
  238.     MOD.SetVar("LastTick",tostring(lasttime));
  239.     userparse = GetUsers();
  240.  
  241. end
  242.  
  243. Scheduler();
  244. MOD.Timer("reconnectedmsg",0,function()print("/me has reconnected!");end);
  245. return cmd;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement