private static function getStreaks($client_id, $userDays)
 {
     if (count($userDays) == 0) {
         return array("length" => 0, "startDate" => "");
     }
     $longestStreak = 1;
     $longestStartDate = new DateTime($userDays[0]['date']);
     $lastDate = $longestStartDate;
     $streak = 1;
     $startDate = $longestStartDate;
     foreach ($userDays as $row) {
         $date = new DateTime($row['date']);
         $diff = intval($lastDate->diff($date)->format('%a'));
         if ($diff == 1) {
             $streak++;
             if ($streak > $longestStreak) {
                 $longestStreak = $streak;
                 $longestStartDate = $startDate;
             }
         } else {
             $startDate = $date;
             $streak = 1;
         }
         $lastDate = $date;
     }
     $today = $lastDate->format('Y-m-d') == (new DateTime())->format('Y-m-d');
     $online = isset(OnlineRange::getOnlineRanges()[$client_id]);
     $yesterday = $lastDate >= (new DateTime())->modify("-1 day")->setTime(0, 0, 0);
     if ($yesterday && !$today && $online) {
         $streak++;
     }
     if (!$yesterday && ($today || $online)) {
         $streak = 1;
         $startDate = new DateTime();
     }
     if (!$yesterday && !$today && !$online) {
         $streak = 0;
         $startDate = new DateTime();
     }
     if ($startDate == $longestStartDate) {
         $longestStreak = $streak;
     }
     return array("longest" => $longestStreak, "startLongest" => $longestStartDate, "current" => $streak, "startCurrent" => $startDate);
 }
 public static function getUptimeScoreboard($limit = 50, $offset = 0)
 {
     $sql = "SELECT user_collapser_results.client_id2 as client_id, probable_username.username, SUM(uptime) as total_uptime\n                FROM uptime_results\n                JOIN user_collapser_results ON uptime_results.client_id = user_collapser_results.client_id1\n                JOIN probable_username ON user_collapser_results.client_id2 = probable_username.client_id\n                GROUP BY user_collapser_results.client_id2\n                ORDER BY total_uptime DESC\n                LIMIT :limit OFFSET :offset";
     $online = OnlineRange::getOnlineRanges();
     $query = DB::$DB->prepare($sql);
     $query->bindParam('limit', $limit);
     $query->bindParam('offset', $offset);
     $query->execute();
     $scores = $query->fetchAll(PDO::FETCH_ASSOC);
     $now = (new DateTime())->getTimestamp();
     foreach ($scores as $i => $score) {
         $client_id = $score['client_id'];
         $scores[$i]['online'] = isset($online[$client_id]);
         if ($scores[$i]['online']) {
             $scores[$i]['onlineSince'] = $online[$client_id]->start;
             $scores[$i]['onlineFor'] = $now - $online[$client_id]->start->getTimestamp();
         } else {
             $scores[$i]['onlineSince'] = "";
             $scores[$i]['onlineFor'] = 0;
         }
         $scores[$i]['uptime'] = $score['total_uptime'] + $scores[$i]['onlineFor'];
     }
     return $scores;
 }
Example #3
0
<?php

require_once __DIR__ . '/../API.php';
API::init();
$client_id = API::getClientId();
$username = ProbableUsernameVisualizer::getProbableUsername($client_id);
$online = OnlineRange::getOnlineRanges();
$is_online = isset($online[$client_id]);
if ($is_online) {
    $online_since = $online[$client_id]->start;
    $online_for = (new DateTime())->getTimestamp() - $online_since->getTimestamp();
} else {
    $online_since = null;
    $online_for = 0;
}
$info = ["client_id" => $client_id, "username" => $username, "online" => $is_online, "online_since" => $online_since, "online_for" => $online_for];
API::printJSON($info);