private void UserLeave(UserTimestampMessage message)
        {
            if (!loggedInUsers.Contains(message.Username))
            {
                //User logged out that wasn't logged in
                return;
            }

            loggedInUsers.Remove(message.Username);

            var userDictionary = UserDailyLoginTimeDictionary[message.Username];
            var loginDateTime  = lastLogin[message.Username];
            var logoutDateTime = message.ZonedTimestamp.Value;

            var loginDate  = new LocalDate(loginDateTime.Year, loginDateTime.Month, loginDateTime.Day);
            var logoutDate = new LocalDate(logoutDateTime.Year, logoutDateTime.Month, logoutDateTime.Day);

            //Case 1: User logged in and out on the same day
            if (loginDate == logoutDate)
            {
                var date = new LocalDate(logoutDateTime.Year, logoutDateTime.Month, logoutDateTime.Day);

                var duration    = AssignAndReturnDurationForDate(message.Username, date);
                var delta       = logoutDateTime.Minus(loginDateTime);
                var newDuration = duration.Plus(delta);

                if (newDuration > OneDay)
                {
                    newDuration = OneDay;
                }

                userDictionary[date] = newDuration;
                return;
            }

            //Case 2: User logged in on one date and out on another
            var dates = GetDurationForEachDate(loginDateTime, logoutDateTime).ToList();

            foreach (var dateTimespanTuple in dates)
            {
                var currentTimespan = AssignAndReturnDurationForDate(message.Username, dateTimespanTuple.Item1);
                userDictionary[dateTimespanTuple.Item1] = currentTimespan.Plus(dateTimespanTuple.Item2);

                if (userDictionary[dateTimespanTuple.Item1] > OneDay)
                {
                    userDictionary[dateTimespanTuple.Item1] = OneDay;
                }
            }
        }
        private void UserJoin(UserTimestampMessage message)
        {
            if (loggedInUsers.Contains(message.Username))
            {
                //User logged in, they are already logged in - ERROR
                return;
            }

            if (!UserDailyLoginTimeDictionary.ContainsKey(message.Username))
            {
                UserDailyLoginTimeDictionary[message.Username] = new Dictionary <LocalDate, Duration>();
            }

            loggedInUsers.Add(message.Username);
            lastLogin[message.Username] = message.ZonedTimestamp.Value;
        }
        private void UserJoin(UserTimestampMessage message)
        {
            if (loggedInUsers.Contains(message.Username))
            {
                //User logged in, they are already logged in - ERROR
                return;
            }

            if (!UserTimeSpans.ContainsKey(message.Username))
            {
                UserTimeSpans[message.Username] = Duration.Zero;
            }

            loggedInUsers.Add(message.Username);
            lastLogin[message.Username] = message.Timestamp;
        }
        private void UserLeave(UserTimestampMessage message)
        {
            if (!loggedInUsers.Contains(message.Username))
            {
                //User logged out that wasn't logged in
                return;
            }

            loggedInUsers.Remove(message.Username);

            var loginDateTime   = lastLogin[message.Username];
            var logoutDateTime  = message.Timestamp;
            var currentDuration = UserTimeSpans[message.Username];

            var newDuration = currentDuration.Plus(logoutDateTime.Minus(loginDateTime));

            if (newDuration.TotalTicks < currentDuration.TotalTicks)
            {
                return;
            }

            UserTimeSpans[message.Username] = newDuration;
        }
Exemple #5
0
 public UserTimestampMessage(UserTimestampMessage message) : base(message)
 {
     this.Username = message.Username;
     this.Type     = message.Type;
 }